~hikiko/nux/arb-srgba-shader

« back to all changes in this revision

Viewing changes to NuxCore/Zip/zipstream/zip_stream_test.cpp

  • Committer: Neil Jagdish Patel
  • Date: 2010-09-02 03:28:11 UTC
  • Revision ID: neil.patel@canonical.com-20100902032811-i2m18tfb6pkasnvt
Remove Win EOL chars

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 */
21
21
 
22
22
 
23
 
/*
24
 
zipstream Library License:
25
 
--------------------------
26
 
 
27
 
The zlib/libpng License Copyright (c) 2003 Jonathan de Halleux.
28
 
 
29
 
This software is provided 'as-is', without any express or implied warranty.
30
 
In no event will the authors be held liable for any damages arising from the use of this software.
31
 
 
32
 
Permission is granted to anyone to use this software for any purpose, including commercial applications,
33
 
and to alter it and redistribute it freely, subject to the following restrictions:
34
 
 
35
 
1. The origin of this software must not be misrepresented; you must not claim that you wrote the
36
 
original software. If you use this software in a product, an acknowledgment in the product
37
 
documentation would be appreciated but is not required.
38
 
 
39
 
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
40
 
 
41
 
3. This notice may not be removed or altered from any source distribution
42
 
 
43
 
Author: Jonathan de Halleux, dehalleux@pelikhan.com, 2003
44
 
*/
45
 
 
46
 
#include "zipstream.h"
47
 
#include <iostream>
48
 
#include <string>
49
 
#include <sstream>
50
 
#include <fstream>
51
 
#include <cmath>
52
 
#include <cstdlib>
53
 
 
54
 
#include "zip_stream_test.h"
55
 
 
56
 
using namespace std;
57
 
namespace zlib_stream{
58
 
 
59
 
// a dummy class
60
 
class dummy
61
 
{
62
 
public:
63
 
        dummy(float f_ = 3.14f, int i_= 1) : f(f_), i(i_)
64
 
        {};
65
 
        void reset(){f=0.0;i=0;};
66
 
 
67
 
        friend ostream& operator << ( ostream& out, dummy const& d)
68
 
        {
69
 
                out<<" "<<d.f<<" "<<d.i;
70
 
                return out;
71
 
        }
72
 
        friend istream& operator >> ( istream& in, dummy& d)
73
 
        {
74
 
                in>>d.f>>d.i;
75
 
                return in;
76
 
        }
77
 
 
78
 
        friend wostream& operator << ( wostream& out, dummy const& d)
79
 
        {
80
 
                out<<L" "<<d.f<<L" "<<d.i;
81
 
                return out;
82
 
        }
83
 
        friend wistream& operator >> ( wistream& in, dummy& d)
84
 
        {
85
 
                in>>d.f>>d.i;
86
 
                return in;
87
 
        }
88
 
protected:
89
 
        float f;
90
 
        int i;
91
 
};
92
 
 
93
 
void test_buffer_to_buffer()
94
 
{
95
 
        const size_t n= 1024;
96
 
        char in_buffer[n]={'\0'};
97
 
        char zip_buffer[n]={'\0'};
98
 
 
99
 
        for (size_t i=0;i<n-1;++i)
100
 
                in_buffer[i]=static_cast<char>(48+i%48);
101
 
 
102
 
 
103
 
        ostringstream out;
104
 
        zip_ostream zipper(out);
105
 
 
106
 
        zipper.write(in_buffer, n);
107
 
        if (zipper.fail())
108
 
                cerr<<"failed to write stream"<<endl;
109
 
 
110
 
        zipper.zflush();
111
 
 
112
 
 
113
 
        istringstream in( out.str() );
114
 
        zip_istream unzipper(in);
115
 
        unzipper.read(zip_buffer, n);
116
 
 
117
 
        cerr<<"buffers equals: "<<endl
118
 
                <<"-----------------"<<endl
119
 
                 <<"\t crc source:"<<crc32(0L,(unsigned char*)in_buffer,n)<<endl
120
 
                 <<"\t crc result:"<<crc32(0L,(unsigned char*)zip_buffer,n)<<endl;
121
 
 
122
 
}
123
 
 
124
 
void test_wbuffer_to_wbuffer()
125
 
{
126
 
        const size_t n= 1024;
127
 
        wchar_t in_buffer[n]={'\0'};
128
 
        wchar_t zip_buffer[n]={'\0'};
129
 
 
130
 
        for (size_t i=0;i<n-1;++i)
131
 
                in_buffer[i]=static_cast<wchar_t>(48+i%48);
132
 
 
133
 
 
134
 
        wostringstream out;
135
 
        zip_wostream zipper(out);
136
 
 
137
 
        zipper.write(in_buffer, n);
138
 
        if (zipper.fail())
139
 
                cerr<<"failed to write stream"<<endl;
140
 
 
141
 
        zipper.zflush();
142
 
 
143
 
 
144
 
        wistringstream in( out.str() );
145
 
        zip_wistream unzipper(in);
146
 
        unzipper.read(zip_buffer, n);
147
 
 
148
 
        wcerr<<L"buffers equals: "<<endl
149
 
                 <<L"-----------------"<<endl
150
 
                 <<L"\t crc source:"<<crc32(0L,(unsigned char*)in_buffer,n*sizeof(wchar_t))<<endl
151
 
                 <<L"\t crc result:"<<crc32(0L,(unsigned char*)zip_buffer,n*sizeof(wchar_t))<<endl;
152
 
}
153
 
 
154
 
void test_string_string()
155
 
{
156
 
        // create some test values
157
 
        char c_r='-',c= 'a';
158
 
        string s_r="",s = "std::string";
159
 
        double d_r=0,d = asin( static_cast<double>(1.0) ) *2.0;
160
 
        float f_r=0, f = static_cast<float>(asin( 1.0 ) *2.0);
161
 
        unsigned int ui_r=0,ui = rand();
162
 
        unsigned long ul_r=0,ul = rand();
163
 
        unsigned short us_r=0, us = rand();
164
 
        dummy dum,dum_r(0,0);
165
 
 
166
 
        /*----------------------------------------------------------------------
167
 
        
168
 
        Testing string to string zipping/unzipping
169
 
        
170
 
        -------------------------------------------------------------------------*/
171
 
        // creating the target zip string, could be a fstream
172
 
        ostringstream ostringstream_(ios::out );
173
 
        // creating the zip layer
174
 
        zip_ostream zipper(ostringstream_);
175
 
 
176
 
        // writing data
177
 
        zipper<<f<<" "<<d<<" "<<ui<<" "<<ul<<" "<<us<<" "<<c<<" "<<dum;
178
 
        // zip ostream needs special flushing...
179
 
        zipper.zflush();
180
 
 
181
 
        // create a stream on zip string
182
 
        istringstream istringstream_( ostringstream_.str(), ios::in);
183
 
        // create unzipper istream
184
 
        zip_istream unzipper( istringstream_);
185
 
 
186
 
        // unzipping
187
 
        unzipper>>f_r>>d_r>>ui_r>>ul_r>>us_r>>c_r>>dum_r;
188
 
 
189
 
        // ouputing results
190
 
        cerr<<"tests string-string,  char:"<<endl
191
 
                <<"----------------------------"<<endl
192
 
                <<"double : "<<d<<" "<<d_r<<endl
193
 
                <<"char : "<<c<<" "<<c_r<<endl
194
 
                <<"float : "<<f<<" "<<f_r<<endl
195
 
                <<"unsigned int : "<<ui<<" "<<ui_r<<endl
196
 
                <<"unsigned long : "<<ul<<" "<<ul_r<<endl
197
 
                <<"unsigned short : "<<us<<" "<<us_r<<endl
198
 
                <<"dummy class: "<<dum<<" "<<dum_r<<endl
199
 
                <<endl
200
 
                ;
201
 
}
202
 
 
203
 
void test_wstring_wstring()
204
 
{
205
 
        // create some test values
206
 
        char c_r='-',c= 'a';
207
 
        double d_r=0,d = asin( 1.0 ) *2.0;
208
 
        float f_r=0, f = static_cast<float>(asin( 1.0 ) *2.0);
209
 
        unsigned int ui_r=0,ui = rand();
210
 
        unsigned long ul_r=0,ul = rand();
211
 
        unsigned short us_r=0, us = rand();
212
 
        dummy dum,dum_r(0,0);
213
 
 
214
 
        /*
215
 
 
216
 
        Testing wide characters
217
 
 
218
 
        */
219
 
        f_r=0.0f;
220
 
        d_r=0.0;
221
 
        ui_r=ul_r=us_r=0;
222
 
        dum_r.reset();
223
 
        // creating the target zip string, could be a fstream
224
 
        wostringstream wostringstream_;
225
 
        // creating the zip layer
226
 
        zip_wostream wzipper(wostringstream_);
227
 
 
228
 
        // writing data
229
 
        wzipper<<f<<L" "<<d<<L" "<<ui<<L" "<<ul<<L" "<<us<<L" "<<dum;
230
 
        // zip ostream needs special flushing...
231
 
        wzipper.zflush();
232
 
 
233
 
 
234
 
        // create a stream on zip string
235
 
        wistringstream wistringstream_( wostringstream_.str());
236
 
        // create unzipper istream
237
 
        zip_wistream wunzipper( wistringstream_ );
238
 
 
239
 
        // unzipping
240
 
        wunzipper>>f_r>>d_r>>ui_r>>ul_r>>us_r>>dum_r;
241
 
 
242
 
        // ouputing results
243
 
        cerr<<"tests string-string (wchar_t):"<<endl
244
 
                <<"------------------------------"<<endl
245
 
                <<"double : "<<d<<" "<<d_r<<endl
246
 
                <<"float : "<<f<<" "<<f_r<<endl
247
 
                <<"unsigned int : "<<ui<<" "<<ui_r<<endl
248
 
                <<"unsigned long : "<<ul<<" "<<ul_r<<endl
249
 
                <<"unsigned short : "<<us<<" "<<us_r<<endl
250
 
                <<"dummy class: "<<dum<<" "<<dum_r<<endl
251
 
                <<endl
252
 
                ;
253
 
}
254
 
 
255
 
void test_file_file(bool add_gzip_header)
256
 
{
257
 
        // create some test values
258
 
        char c_r='-',c= 'a';
259
 
        const char* sz = "const char*";
260
 
        string s_r="",s = "std::string";
261
 
        double d_r=0,d = asin( 1.0 ) *2.0;
262
 
        float f_r=0, f = static_cast<float>(asin( 1.0 ) *2.0);
263
 
        unsigned int ui_r=0,ui = rand();
264
 
        unsigned long ul_r=0,ul = rand();
265
 
        unsigned short us_r=0, us = rand();
266
 
        dummy dum,dum_r(0,0);
267
 
        char sbuf[256]={'-'};
268
 
        long crc_z(0), crc_uz(0);
269
 
        long in_size_z(0), in_size_uz(0), out_size_z(0), out_size_uz(0);
270
 
 
271
 
        /*----------------------------------------------------------------------------
272
 
        
273
 
        Testing file to file unzipping
274
 
 
275
 
        ------------------------------------------------------------------------------*/
276
 
        f_r=0.0f;
277
 
        d_r=0.0;
278
 
        ui_r=ul_r=us_r=0; dum_r.reset();
279
 
 
280
 
        {
281
 
                // creating the target zip string, could be a fstream
282
 
        ofstream ofstream_("test.zip",ios::out | ios::binary );
283
 
                // creating the zip layer
284
 
        zip_ostream fzipper(ofstream_, add_gzip_header);
285
 
                // writing data
286
 
                fzipper<<f<<" "<<d<<" "<<ui<<" "<<ul<<" "<<us<<" "<<c<<" "<<dum;
287
 
                // zip ostream needs special flushing...
288
 
                fzipper.zflush();
289
 
                crc_z = fzipper.rdbuf()->get_crc();
290
 
                in_size_z = fzipper.rdbuf()->get_in_size();
291
 
                out_size_z = fzipper.rdbuf()->get_out_size();
292
 
        }
293
 
 
294
 
        // create a stream on zip string
295
 
        ifstream ifstream_;
296
 
        ifstream_.open("test.zip", ios::in | ios::binary);
297
 
        if (!ifstream_.is_open())
298
 
        {
299
 
                cerr<<"Could not open file test.zip"<<endl;
300
 
        }
301
 
        // create unzipper istream
302
 
        zip_istream funzipper( ifstream_);
303
 
 
304
 
        // unzipping
305
 
        funzipper>>f_r>>d_r>>ui_r>>ul_r>>us_r>>c_r>>dum_r;
306
 
        funzipper.read_footer();
307
 
 
308
 
        crc_uz = funzipper.rdbuf()->get_crc();
309
 
        in_size_uz = funzipper.rdbuf()->get_in_size();
310
 
        out_size_uz = funzipper.rdbuf()->get_out_size();
311
 
 
312
 
 
313
 
        // ouputing results
314
 
        cerr<<"tests file-to-file (char, "<<(funzipper.is_gzip() ? "gzip" : "no gzip")<<"):"<<endl
315
 
                <<"------------------------------"<<endl
316
 
                <<"double : "<<d<<" "<<d_r<<endl
317
 
                <<"char : "<<c<<" "<<c_r<<endl
318
 
                <<"float : "<<f<<" "<<f_r<<endl
319
 
                <<"unsigned int : "<<ui<<" "<<ui_r<<endl
320
 
                <<"unsigned long : "<<ul<<" "<<ul_r<<endl
321
 
                <<"unsigned short : "<<us<<" "<<us_r<<endl
322
 
                <<"dummy class: "<<dum<<" "<<dum_r<<endl
323
 
                <<"crc (zipped, unzipped, from file): "<<crc_z<<" "<<crc_uz<<" "<<funzipper.get_gzip_crc()<<endl
324
 
                <<"uncompressed data size: "<<in_size_z<<" "<<out_size_uz<<" "<<funzipper.get_gzip_data_size()<<endl
325
 
                <<"compressed data size: "<<out_size_z<<" "<<in_size_uz<<endl
326
 
                <<"check_crc: "<<( funzipper.check_crc() ? "ok" : "failed")<<endl
327
 
                <<"check_data_size: "<<( funzipper.check_data_size() ? "ok" : "failed")<<endl
328
 
                ;
329
 
}
330
 
 
331
 
void test_crc()
332
 
{
333
 
int i;
334
 
 
335
 
{
336
 
    ofstream outFile("test.gz", ios::out | ios::binary);
337
 
    zip_ostream zipOut(outFile,true);
338
 
    char buff[102400];
339
 
    for (i = 0; i < 102400; i++)
340
 
    {
341
 
        buff[i] = (char)48+i%48;
342
 
    }
343
 
    zipOut.write(buff, sizeof(buff));
344
 
}
345
 
 
346
 
ifstream inFile("test.gz", ios::in | ios::binary);
347
 
 
348
 
zip_istream unzipper(inFile);
349
 
 
350
 
char c;
351
 
 
352
 
for (i = 0; i < 102400; i++)
353
 
{
354
 
    unzipper >> c;
355
 
}
356
 
 
357
 
unzipper.read_footer();
358
 
 
359
 
std::cout << "DoMyTest() " 
360
 
<< ", outsize " << unzipper.get_out_size()
361
 
<< ", insize " << unzipper.get_in_size() 
362
 
<< ", z_err " << unzipper.get_zerr() 
363
 
<< ", crc " << unzipper.get_crc() 
364
 
<< ", gzip data size " << unzipper.get_gzip_data_size() << std::endl;
365
 
}
366
 
 
367
 
};
 
23
/*
 
24
zipstream Library License:
 
25
--------------------------
 
26
 
 
27
The zlib/libpng License Copyright (c) 2003 Jonathan de Halleux.
 
28
 
 
29
This software is provided 'as-is', without any express or implied warranty.
 
30
In no event will the authors be held liable for any damages arising from the use of this software.
 
31
 
 
32
Permission is granted to anyone to use this software for any purpose, including commercial applications,
 
33
and to alter it and redistribute it freely, subject to the following restrictions:
 
34
 
 
35
1. The origin of this software must not be misrepresented; you must not claim that you wrote the
 
36
original software. If you use this software in a product, an acknowledgment in the product
 
37
documentation would be appreciated but is not required.
 
38
 
 
39
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
 
40
 
 
41
3. This notice may not be removed or altered from any source distribution
 
42
 
 
43
Author: Jonathan de Halleux, dehalleux@pelikhan.com, 2003
 
44
*/
 
45
 
 
46
#include "zipstream.h"
 
47
#include <iostream>
 
48
#include <string>
 
49
#include <sstream>
 
50
#include <fstream>
 
51
#include <cmath>
 
52
#include <cstdlib>
 
53
 
 
54
#include "zip_stream_test.h"
 
55
 
 
56
using namespace std;
 
57
namespace zlib_stream{
 
58
 
 
59
// a dummy class
 
60
class dummy
 
61
{
 
62
public:
 
63
        dummy(float f_ = 3.14f, int i_= 1) : f(f_), i(i_)
 
64
        {};
 
65
        void reset(){f=0.0;i=0;};
 
66
 
 
67
        friend ostream& operator << ( ostream& out, dummy const& d)
 
68
        {
 
69
                out<<" "<<d.f<<" "<<d.i;
 
70
                return out;
 
71
        }
 
72
        friend istream& operator >> ( istream& in, dummy& d)
 
73
        {
 
74
                in>>d.f>>d.i;
 
75
                return in;
 
76
        }
 
77
 
 
78
        friend wostream& operator << ( wostream& out, dummy const& d)
 
79
        {
 
80
                out<<L" "<<d.f<<L" "<<d.i;
 
81
                return out;
 
82
        }
 
83
        friend wistream& operator >> ( wistream& in, dummy& d)
 
84
        {
 
85
                in>>d.f>>d.i;
 
86
                return in;
 
87
        }
 
88
protected:
 
89
        float f;
 
90
        int i;
 
91
};
 
92
 
 
93
void test_buffer_to_buffer()
 
94
{
 
95
        const size_t n= 1024;
 
96
        char in_buffer[n]={'\0'};
 
97
        char zip_buffer[n]={'\0'};
 
98
 
 
99
        for (size_t i=0;i<n-1;++i)
 
100
                in_buffer[i]=static_cast<char>(48+i%48);
 
101
 
 
102
 
 
103
        ostringstream out;
 
104
        zip_ostream zipper(out);
 
105
 
 
106
        zipper.write(in_buffer, n);
 
107
        if (zipper.fail())
 
108
                cerr<<"failed to write stream"<<endl;
 
109
 
 
110
        zipper.zflush();
 
111
 
 
112
 
 
113
        istringstream in( out.str() );
 
114
        zip_istream unzipper(in);
 
115
        unzipper.read(zip_buffer, n);
 
116
 
 
117
        cerr<<"buffers equals: "<<endl
 
118
                <<"-----------------"<<endl
 
119
                 <<"\t crc source:"<<crc32(0L,(unsigned char*)in_buffer,n)<<endl
 
120
                 <<"\t crc result:"<<crc32(0L,(unsigned char*)zip_buffer,n)<<endl;
 
121
 
 
122
}
 
123
 
 
124
void test_wbuffer_to_wbuffer()
 
125
{
 
126
        const size_t n= 1024;
 
127
        wchar_t in_buffer[n]={'\0'};
 
128
        wchar_t zip_buffer[n]={'\0'};
 
129
 
 
130
        for (size_t i=0;i<n-1;++i)
 
131
                in_buffer[i]=static_cast<wchar_t>(48+i%48);
 
132
 
 
133
 
 
134
        wostringstream out;
 
135
        zip_wostream zipper(out);
 
136
 
 
137
        zipper.write(in_buffer, n);
 
138
        if (zipper.fail())
 
139
                cerr<<"failed to write stream"<<endl;
 
140
 
 
141
        zipper.zflush();
 
142
 
 
143
 
 
144
        wistringstream in( out.str() );
 
145
        zip_wistream unzipper(in);
 
146
        unzipper.read(zip_buffer, n);
 
147
 
 
148
        wcerr<<L"buffers equals: "<<endl
 
149
                 <<L"-----------------"<<endl
 
150
                 <<L"\t crc source:"<<crc32(0L,(unsigned char*)in_buffer,n*sizeof(wchar_t))<<endl
 
151
                 <<L"\t crc result:"<<crc32(0L,(unsigned char*)zip_buffer,n*sizeof(wchar_t))<<endl;
 
152
}
 
153
 
 
154
void test_string_string()
 
155
{
 
156
        // create some test values
 
157
        char c_r='-',c= 'a';
 
158
        string s_r="",s = "std::string";
 
159
        double d_r=0,d = asin( static_cast<double>(1.0) ) *2.0;
 
160
        float f_r=0, f = static_cast<float>(asin( 1.0 ) *2.0);
 
161
        unsigned int ui_r=0,ui = rand();
 
162
        unsigned long ul_r=0,ul = rand();
 
163
        unsigned short us_r=0, us = rand();
 
164
        dummy dum,dum_r(0,0);
 
165
 
 
166
        /*----------------------------------------------------------------------
 
167
        
 
168
        Testing string to string zipping/unzipping
 
169
        
 
170
        -------------------------------------------------------------------------*/
 
171
        // creating the target zip string, could be a fstream
 
172
        ostringstream ostringstream_(ios::out );
 
173
        // creating the zip layer
 
174
        zip_ostream zipper(ostringstream_);
 
175
 
 
176
        // writing data
 
177
        zipper<<f<<" "<<d<<" "<<ui<<" "<<ul<<" "<<us<<" "<<c<<" "<<dum;
 
178
        // zip ostream needs special flushing...
 
179
        zipper.zflush();
 
180
 
 
181
        // create a stream on zip string
 
182
        istringstream istringstream_( ostringstream_.str(), ios::in);
 
183
        // create unzipper istream
 
184
        zip_istream unzipper( istringstream_);
 
185
 
 
186
        // unzipping
 
187
        unzipper>>f_r>>d_r>>ui_r>>ul_r>>us_r>>c_r>>dum_r;
 
188
 
 
189
        // ouputing results
 
190
        cerr<<"tests string-string,  char:"<<endl
 
191
                <<"----------------------------"<<endl
 
192
                <<"double : "<<d<<" "<<d_r<<endl
 
193
                <<"char : "<<c<<" "<<c_r<<endl
 
194
                <<"float : "<<f<<" "<<f_r<<endl
 
195
                <<"unsigned int : "<<ui<<" "<<ui_r<<endl
 
196
                <<"unsigned long : "<<ul<<" "<<ul_r<<endl
 
197
                <<"unsigned short : "<<us<<" "<<us_r<<endl
 
198
                <<"dummy class: "<<dum<<" "<<dum_r<<endl
 
199
                <<endl
 
200
                ;
 
201
}
 
202
 
 
203
void test_wstring_wstring()
 
204
{
 
205
        // create some test values
 
206
        char c_r='-',c= 'a';
 
207
        double d_r=0,d = asin( 1.0 ) *2.0;
 
208
        float f_r=0, f = static_cast<float>(asin( 1.0 ) *2.0);
 
209
        unsigned int ui_r=0,ui = rand();
 
210
        unsigned long ul_r=0,ul = rand();
 
211
        unsigned short us_r=0, us = rand();
 
212
        dummy dum,dum_r(0,0);
 
213
 
 
214
        /*
 
215
 
 
216
        Testing wide characters
 
217
 
 
218
        */
 
219
        f_r=0.0f;
 
220
        d_r=0.0;
 
221
        ui_r=ul_r=us_r=0;
 
222
        dum_r.reset();
 
223
        // creating the target zip string, could be a fstream
 
224
        wostringstream wostringstream_;
 
225
        // creating the zip layer
 
226
        zip_wostream wzipper(wostringstream_);
 
227
 
 
228
        // writing data
 
229
        wzipper<<f<<L" "<<d<<L" "<<ui<<L" "<<ul<<L" "<<us<<L" "<<dum;
 
230
        // zip ostream needs special flushing...
 
231
        wzipper.zflush();
 
232
 
 
233
 
 
234
        // create a stream on zip string
 
235
        wistringstream wistringstream_( wostringstream_.str());
 
236
        // create unzipper istream
 
237
        zip_wistream wunzipper( wistringstream_ );
 
238
 
 
239
        // unzipping
 
240
        wunzipper>>f_r>>d_r>>ui_r>>ul_r>>us_r>>dum_r;
 
241
 
 
242
        // ouputing results
 
243
        cerr<<"tests string-string (wchar_t):"<<endl
 
244
                <<"------------------------------"<<endl
 
245
                <<"double : "<<d<<" "<<d_r<<endl
 
246
                <<"float : "<<f<<" "<<f_r<<endl
 
247
                <<"unsigned int : "<<ui<<" "<<ui_r<<endl
 
248
                <<"unsigned long : "<<ul<<" "<<ul_r<<endl
 
249
                <<"unsigned short : "<<us<<" "<<us_r<<endl
 
250
                <<"dummy class: "<<dum<<" "<<dum_r<<endl
 
251
                <<endl
 
252
                ;
 
253
}
 
254
 
 
255
void test_file_file(bool add_gzip_header)
 
256
{
 
257
        // create some test values
 
258
        char c_r='-',c= 'a';
 
259
        const char* sz = "const char*";
 
260
        string s_r="",s = "std::string";
 
261
        double d_r=0,d = asin( 1.0 ) *2.0;
 
262
        float f_r=0, f = static_cast<float>(asin( 1.0 ) *2.0);
 
263
        unsigned int ui_r=0,ui = rand();
 
264
        unsigned long ul_r=0,ul = rand();
 
265
        unsigned short us_r=0, us = rand();
 
266
        dummy dum,dum_r(0,0);
 
267
        char sbuf[256]={'-'};
 
268
        long crc_z(0), crc_uz(0);
 
269
        long in_size_z(0), in_size_uz(0), out_size_z(0), out_size_uz(0);
 
270
 
 
271
        /*----------------------------------------------------------------------------
 
272
        
 
273
        Testing file to file unzipping
 
274
 
 
275
        ------------------------------------------------------------------------------*/
 
276
        f_r=0.0f;
 
277
        d_r=0.0;
 
278
        ui_r=ul_r=us_r=0; dum_r.reset();
 
279
 
 
280
        {
 
281
                // creating the target zip string, could be a fstream
 
282
        ofstream ofstream_("test.zip",ios::out | ios::binary );
 
283
                // creating the zip layer
 
284
        zip_ostream fzipper(ofstream_, add_gzip_header);
 
285
                // writing data
 
286
                fzipper<<f<<" "<<d<<" "<<ui<<" "<<ul<<" "<<us<<" "<<c<<" "<<dum;
 
287
                // zip ostream needs special flushing...
 
288
                fzipper.zflush();
 
289
                crc_z = fzipper.rdbuf()->get_crc();
 
290
                in_size_z = fzipper.rdbuf()->get_in_size();
 
291
                out_size_z = fzipper.rdbuf()->get_out_size();
 
292
        }
 
293
 
 
294
        // create a stream on zip string
 
295
        ifstream ifstream_;
 
296
        ifstream_.open("test.zip", ios::in | ios::binary);
 
297
        if (!ifstream_.is_open())
 
298
        {
 
299
                cerr<<"Could not open file test.zip"<<endl;
 
300
        }
 
301
        // create unzipper istream
 
302
        zip_istream funzipper( ifstream_);
 
303
 
 
304
        // unzipping
 
305
        funzipper>>f_r>>d_r>>ui_r>>ul_r>>us_r>>c_r>>dum_r;
 
306
        funzipper.read_footer();
 
307
 
 
308
        crc_uz = funzipper.rdbuf()->get_crc();
 
309
        in_size_uz = funzipper.rdbuf()->get_in_size();
 
310
        out_size_uz = funzipper.rdbuf()->get_out_size();
 
311
 
 
312
 
 
313
        // ouputing results
 
314
        cerr<<"tests file-to-file (char, "<<(funzipper.is_gzip() ? "gzip" : "no gzip")<<"):"<<endl
 
315
                <<"------------------------------"<<endl
 
316
                <<"double : "<<d<<" "<<d_r<<endl
 
317
                <<"char : "<<c<<" "<<c_r<<endl
 
318
                <<"float : "<<f<<" "<<f_r<<endl
 
319
                <<"unsigned int : "<<ui<<" "<<ui_r<<endl
 
320
                <<"unsigned long : "<<ul<<" "<<ul_r<<endl
 
321
                <<"unsigned short : "<<us<<" "<<us_r<<endl
 
322
                <<"dummy class: "<<dum<<" "<<dum_r<<endl
 
323
                <<"crc (zipped, unzipped, from file): "<<crc_z<<" "<<crc_uz<<" "<<funzipper.get_gzip_crc()<<endl
 
324
                <<"uncompressed data size: "<<in_size_z<<" "<<out_size_uz<<" "<<funzipper.get_gzip_data_size()<<endl
 
325
                <<"compressed data size: "<<out_size_z<<" "<<in_size_uz<<endl
 
326
                <<"check_crc: "<<( funzipper.check_crc() ? "ok" : "failed")<<endl
 
327
                <<"check_data_size: "<<( funzipper.check_data_size() ? "ok" : "failed")<<endl
 
328
                ;
 
329
}
 
330
 
 
331
void test_crc()
 
332
{
 
333
int i;
 
334
 
 
335
{
 
336
    ofstream outFile("test.gz", ios::out | ios::binary);
 
337
    zip_ostream zipOut(outFile,true);
 
338
    char buff[102400];
 
339
    for (i = 0; i < 102400; i++)
 
340
    {
 
341
        buff[i] = (char)48+i%48;
 
342
    }
 
343
    zipOut.write(buff, sizeof(buff));
 
344
}
 
345
 
 
346
ifstream inFile("test.gz", ios::in | ios::binary);
 
347
 
 
348
zip_istream unzipper(inFile);
 
349
 
 
350
char c;
 
351
 
 
352
for (i = 0; i < 102400; i++)
 
353
{
 
354
    unzipper >> c;
 
355
}
 
356
 
 
357
unzipper.read_footer();
 
358
 
 
359
std::cout << "DoMyTest() " 
 
360
<< ", outsize " << unzipper.get_out_size()
 
361
<< ", insize " << unzipper.get_in_size() 
 
362
<< ", z_err " << unzipper.get_zerr() 
 
363
<< ", crc " << unzipper.get_crc() 
 
364
<< ", gzip data size " << unzipper.get_gzip_data_size() << std::endl;
 
365
}
 
366
 
 
367
};