| 1 | #include <iostream> | 
| 2 | #include <string> | 
| 3 |  | 
| 4 | #include "io/basic_ifstrstream.hpp" | 
| 5 | #include "ZipstreamTestCase.hpp" | 
| 6 |  | 
| 7 | // Registers the fixture into the 'registry' | 
| 8 |  | 
| 9 | using namespace std; | 
| 10 | using namespace OpenMD; | 
| 11 | CPPUNIT_TEST_SUITE_REGISTRATION( ZipstreamTeseCase ); | 
| 12 |  | 
| 13 | #include "zipstream.hpp" | 
| 14 | #include <iostream> | 
| 15 | #include <string> | 
| 16 | #include <sstream> | 
| 17 | #include <fstream> | 
| 18 | #include <cmath> | 
| 19 | #include <cstdlib> | 
| 20 |  | 
| 21 | using namespace std; | 
| 22 | using namespace zlib_stream; | 
| 23 |  | 
| 24 | // a dummy class | 
| 25 | class dummy | 
| 26 | { | 
| 27 | public: | 
| 28 | dummy(float f_ = 3.14f, int i_= 1) : f(f_), i(i_) | 
| 29 | {}; | 
| 30 | void reset(){f=0.0;i=0;}; | 
| 31 |  | 
| 32 | friend ostream& operator << ( ostream& out, dummy const& d) | 
| 33 | { | 
| 34 | out<<" "<<d.f<<" "<<d.i; | 
| 35 | return out; | 
| 36 | } | 
| 37 | friend istream& operator >> ( istream& in, dummy& d) | 
| 38 | { | 
| 39 | in>>d.f>>d.i; | 
| 40 | return in; | 
| 41 | } | 
| 42 |  | 
| 43 | friend wostream& operator << ( wostream& out, dummy const& d) | 
| 44 | { | 
| 45 | out<<L" "<<d.f<<L" "<<d.i; | 
| 46 | return out; | 
| 47 | } | 
| 48 | friend wistream& operator >> ( wistream& in, dummy& d) | 
| 49 | { | 
| 50 | in>>d.f>>d.i; | 
| 51 | return in; | 
| 52 | } | 
| 53 | protected: | 
| 54 | float f; | 
| 55 | int i; | 
| 56 | }; | 
| 57 |  | 
| 58 | void ZipstreamTeseCase::test_buffer_to_buffer() | 
| 59 | { | 
| 60 | const size_t n= 1024; | 
| 61 | char in_buffer[n]={'\0'}; | 
| 62 | char zip_buffer[n]={'\0'}; | 
| 63 |  | 
| 64 | for (size_t i=0;i<n-1;++i) | 
| 65 | in_buffer[i]=static_cast<char>(48+i%48); | 
| 66 |  | 
| 67 |  | 
| 68 | ostringstream out; | 
| 69 | zip_ostream zipper(out); | 
| 70 |  | 
| 71 | zipper.write(in_buffer, n); | 
| 72 | if (zipper.fail()) | 
| 73 | cerr<<"failed to write stream"<<endl; | 
| 74 |  | 
| 75 | zipper.zflush(); | 
| 76 |  | 
| 77 |  | 
| 78 | istringstream in( out.str() ); | 
| 79 | zip_istream unzipper(in); | 
| 80 | unzipper.read(zip_buffer, n); | 
| 81 |  | 
| 82 | cerr<<"buffers equals: "<<endl | 
| 83 | <<"-----------------"<<endl | 
| 84 | <<"\t crc source:"<<crc32(0L,(unsigned char*)in_buffer,n)<<endl | 
| 85 | <<"\t crc result:"<<crc32(0L,(unsigned char*)zip_buffer,n)<<endl; | 
| 86 |  | 
| 87 | } | 
| 88 |  | 
| 89 | void ZipstreamTeseCase::test_wbuffer_to_wbuffer() | 
| 90 | { | 
| 91 | const size_t n= 1024; | 
| 92 | wchar_t in_buffer[n]={'\0'}; | 
| 93 | wchar_t zip_buffer[n]={'\0'}; | 
| 94 |  | 
| 95 | for (size_t i=0;i<n-1;++i) | 
| 96 | in_buffer[i]=static_cast<wchar_t>(48+i%48); | 
| 97 |  | 
| 98 |  | 
| 99 | wostringstream out; | 
| 100 | zip_wostream zipper(out); | 
| 101 |  | 
| 102 | zipper.write(in_buffer, n); | 
| 103 | if (zipper.fail()) | 
| 104 | cerr<<"failed to write stream"<<endl; | 
| 105 |  | 
| 106 | zipper.zflush(); | 
| 107 |  | 
| 108 |  | 
| 109 | wistringstream in( out.str() ); | 
| 110 | zip_wistream unzipper(in); | 
| 111 | unzipper.read(zip_buffer, n); | 
| 112 |  | 
| 113 | wcerr<<L"buffers equals: "<<endl | 
| 114 | <<L"-----------------"<<endl | 
| 115 | <<L"\t crc source:"<<crc32(0L,(unsigned char*)in_buffer,n*sizeof(wchar_t))<<endl | 
| 116 | <<L"\t crc result:"<<crc32(0L,(unsigned char*)zip_buffer,n*sizeof(wchar_t))<<endl; | 
| 117 | } | 
| 118 |  | 
| 119 | void ZipstreamTeseCase::test_string_string() | 
| 120 | { | 
| 121 | // create some test values | 
| 122 | char c_r='-',c= 'a'; | 
| 123 | string s_r="",s = "std::string"; | 
| 124 | double d_r=0,d = asin( static_cast<double>(1.0) ) *2.0; | 
| 125 | float f_r=0, f = static_cast<float>(asin( 1.0 ) *2.0); | 
| 126 | unsigned int ui_r=0,ui = rand(); | 
| 127 | unsigned long ul_r=0,ul = rand(); | 
| 128 | unsigned short us_r=0, us = rand(); | 
| 129 | dummy dum,dum_r(0,0); | 
| 130 |  | 
| 131 | /*---------------------------------------------------------------------- | 
| 132 |  | 
| 133 | Testing string to string zipping/unzipping | 
| 134 |  | 
| 135 | -------------------------------------------------------------------------*/ | 
| 136 | // creating the target zip string, could be a fstream | 
| 137 | ostringstream ostringstream_(ios::out ); | 
| 138 | // creating the zip layer | 
| 139 | zip_ostream zipper(ostringstream_); | 
| 140 |  | 
| 141 | // writing data | 
| 142 | zipper<<f<<" "<<d<<" "<<ui<<" "<<ul<<" "<<us<<" "<<c<<" "<<dum; | 
| 143 | // zip ostream needs special flushing... | 
| 144 | zipper.zflush(); | 
| 145 |  | 
| 146 | // create a stream on zip string | 
| 147 | istringstream istringstream_( ostringstream_.str(), ios::in); | 
| 148 | // create unzipper istream | 
| 149 | zip_istream unzipper( istringstream_); | 
| 150 |  | 
| 151 | // unzipping | 
| 152 | unzipper>>f_r>>d_r>>ui_r>>ul_r>>us_r>>c_r>>dum_r; | 
| 153 |  | 
| 154 | // ouputing results | 
| 155 | cerr<<"tests string-string,  char:"<<endl | 
| 156 | <<"----------------------------"<<endl | 
| 157 | <<"double : "<<d<<" "<<d_r<<endl | 
| 158 | <<"char : "<<c<<" "<<c_r<<endl | 
| 159 | <<"float : "<<f<<" "<<f_r<<endl | 
| 160 | <<"unsigned int : "<<ui<<" "<<ui_r<<endl | 
| 161 | <<"unsigned long : "<<ul<<" "<<ul_r<<endl | 
| 162 | <<"unsigned short : "<<us<<" "<<us_r<<endl | 
| 163 | <<"dummy class: "<<dum<<" "<<dum_r<<endl | 
| 164 | <<endl | 
| 165 | ; | 
| 166 | } | 
| 167 |  | 
| 168 | void ZipstreamTeseCase::test_wstring_wstring() | 
| 169 | { | 
| 170 | // create some test values | 
| 171 | char c_r='-',c= 'a'; | 
| 172 | double d_r=0,d = asin( 1.0 ) *2.0; | 
| 173 | float f_r=0, f = static_cast<float>(asin( 1.0 ) *2.0); | 
| 174 | unsigned int ui_r=0,ui = rand(); | 
| 175 | unsigned long ul_r=0,ul = rand(); | 
| 176 | unsigned short us_r=0, us = rand(); | 
| 177 | dummy dum,dum_r(0,0); | 
| 178 |  | 
| 179 | /* | 
| 180 |  | 
| 181 | Testing wide characters | 
| 182 |  | 
| 183 | */ | 
| 184 | f_r=0.0f; | 
| 185 | d_r=0.0; | 
| 186 | ui_r=ul_r=us_r=0; | 
| 187 | dum_r.reset(); | 
| 188 | // creating the target zip string, could be a fstream | 
| 189 | wostringstream wostringstream_; | 
| 190 | // creating the zip layer | 
| 191 | zip_wostream wzipper(wostringstream_); | 
| 192 |  | 
| 193 | // writing data | 
| 194 | wzipper<<f<<L" "<<d<<L" "<<ui<<L" "<<ul<<L" "<<us<<L" "<<dum; | 
| 195 | // zip ostream needs special flushing... | 
| 196 | wzipper.zflush(); | 
| 197 |  | 
| 198 |  | 
| 199 | // create a stream on zip string | 
| 200 | wistringstream wistringstream_( wostringstream_.str()); | 
| 201 | // create unzipper istream | 
| 202 | zip_wistream wunzipper( wistringstream_ ); | 
| 203 |  | 
| 204 | // unzipping | 
| 205 | wunzipper>>f_r>>d_r>>ui_r>>ul_r>>us_r>>dum_r; | 
| 206 |  | 
| 207 | // ouputing results | 
| 208 | cerr<<"tests string-string (wchar_t):"<<endl | 
| 209 | <<"------------------------------"<<endl | 
| 210 | <<"double : "<<d<<" "<<d_r<<endl | 
| 211 | <<"float : "<<f<<" "<<f_r<<endl | 
| 212 | <<"unsigned int : "<<ui<<" "<<ui_r<<endl | 
| 213 | <<"unsigned long : "<<ul<<" "<<ul_r<<endl | 
| 214 | <<"unsigned short : "<<us<<" "<<us_r<<endl | 
| 215 | <<"dummy class: "<<dum<<" "<<dum_r<<endl | 
| 216 | <<endl | 
| 217 | ; | 
| 218 | } | 
| 219 |  | 
| 220 | void ZipstreamTeseCase::test_file_file() | 
| 221 | { | 
| 222 | bool add_gzip_header = true; | 
| 223 | // create some test values | 
| 224 | char c_r='-',c= 'a'; | 
| 225 | const char* sz = "const char*"; | 
| 226 | string s_r="",s = "std::string"; | 
| 227 | double d_r=0,d = asin( 1.0 ) *2.0; | 
| 228 | float f_r=0, f = static_cast<float>(asin( 1.0 ) *2.0); | 
| 229 | unsigned int ui_r=0,ui = rand(); | 
| 230 | unsigned long ul_r=0,ul = rand(); | 
| 231 | unsigned short us_r=0, us = rand(); | 
| 232 | dummy dum,dum_r(0,0); | 
| 233 | char sbuf[256]={'-'}; | 
| 234 | long crc_z(0), crc_uz(0); | 
| 235 | long in_size_z(0), in_size_uz(0), out_size_z(0), out_size_uz(0); | 
| 236 |  | 
| 237 | /*---------------------------------------------------------------------------- | 
| 238 |  | 
| 239 | Testing file to file unzipping | 
| 240 |  | 
| 241 | ------------------------------------------------------------------------------*/ | 
| 242 | f_r=0.0f; | 
| 243 | d_r=0.0; | 
| 244 | ui_r=ul_r=us_r=0; dum_r.reset(); | 
| 245 |  | 
| 246 | { | 
| 247 | // creating the target zip string, could be a fstream | 
| 248 | ofstream ofstream_("test.zip",ios::out | ios::binary ); | 
| 249 | // creating the zip layer | 
| 250 | zip_ostream fzipper(ofstream_, add_gzip_header); | 
| 251 | // writing data | 
| 252 | fzipper<<f<<" "<<d<<" "<<ui<<" "<<ul<<" "<<us<<" "<<c<<" "<<dum; | 
| 253 | // zip ostream needs special flushing... | 
| 254 | fzipper.zflush(); | 
| 255 | crc_z = fzipper.rdbuf()->get_crc(); | 
| 256 | in_size_z = fzipper.rdbuf()->get_in_size(); | 
| 257 | out_size_z = fzipper.rdbuf()->get_out_size(); | 
| 258 | } | 
| 259 |  | 
| 260 | // create a stream on zip string | 
| 261 | ifstream ifstream_; | 
| 262 | ifstream_.open("test.zip", ios::in | ios::binary); | 
| 263 | if (!ifstream_.is_open()) | 
| 264 | { | 
| 265 | cerr<<"Could not open file test.zip"<<endl; | 
| 266 | } | 
| 267 | // create unzipper istream | 
| 268 | zip_istream funzipper( ifstream_); | 
| 269 |  | 
| 270 | // unzipping | 
| 271 | funzipper>>f_r>>d_r>>ui_r>>ul_r>>us_r>>c_r>>dum_r; | 
| 272 | funzipper.read_footer(); | 
| 273 |  | 
| 274 | crc_uz = funzipper.rdbuf()->get_crc(); | 
| 275 | in_size_uz = funzipper.rdbuf()->get_in_size(); | 
| 276 | out_size_uz = funzipper.rdbuf()->get_out_size(); | 
| 277 |  | 
| 278 |  | 
| 279 | // ouputing results | 
| 280 | cerr<<"tests file-to-file (char, "<<(funzipper.is_gzip() ? "gzip" : "no gzip")<<"):"<<endl | 
| 281 | <<"------------------------------"<<endl | 
| 282 | <<"double : "<<d<<" "<<d_r<<endl | 
| 283 | <<"char : "<<c<<" "<<c_r<<endl | 
| 284 | <<"float : "<<f<<" "<<f_r<<endl | 
| 285 | <<"unsigned int : "<<ui<<" "<<ui_r<<endl | 
| 286 | <<"unsigned long : "<<ul<<" "<<ul_r<<endl | 
| 287 | <<"unsigned short : "<<us<<" "<<us_r<<endl | 
| 288 | <<"dummy class: "<<dum<<" "<<dum_r<<endl | 
| 289 | <<"crc (zipped, unzipped, from file): "<<crc_z<<" "<<crc_uz<<" | 
| 290 | "<<funzipper.get_gzip_crc()<<endl | 
| 291 | <<"uncompressed data size: "<<in_size_z<<" "<<out_size_uz<<" | 
| 292 | "<<funzipper.get_gzip_data_size()<<endl | 
| 293 | <<"compressed data size: "<<out_size_z<<" "<<in_size_uz<<endl | 
| 294 | <<"check_crc: "<<( funzipper.check_crc() ? "ok" : "failed")<<endl | 
| 295 | <<"check_data_size: "<<( funzipper.check_data_size() ? "ok" : "failed")<<endl | 
| 296 | ; | 
| 297 | } | 
| 298 |  | 
| 299 | void ZipstreamTeseCase::test_crc() | 
| 300 | { | 
| 301 | int i; | 
| 302 |  | 
| 303 | { | 
| 304 | ofstream outFile("test.gz", ios::out | ios::binary); | 
| 305 | zip_ostream zipOut(outFile,true); | 
| 306 | char buff[102400]; | 
| 307 | for (i = 0; i < 102400; i++) | 
| 308 | { | 
| 309 | buff[i] = (char)48+i%48; | 
| 310 | } | 
| 311 | zipOut.write(buff, sizeof(buff)); | 
| 312 | } | 
| 313 |  | 
| 314 | ifstream inFile("test.gz", ios::in | ios::binary); | 
| 315 |  | 
| 316 | zip_istream unzipper(inFile); | 
| 317 |  | 
| 318 | char c; | 
| 319 |  | 
| 320 | for (i = 0; i < 102400; i++) | 
| 321 | { | 
| 322 | unzipper >> c; | 
| 323 | } | 
| 324 |  | 
| 325 | unzipper.read_footer(); | 
| 326 |  | 
| 327 | std::cout << "DoMyTest() " | 
| 328 | << ", outsize " << unzipper.get_out_size() | 
| 329 | << ", insize " << unzipper.get_in_size() | 
| 330 | << ", z_err " << unzipper.get_zerr() | 
| 331 | << ", crc " << unzipper.get_crc() | 
| 332 | << ", gzip data size " << unzipper.get_gzip_data_size() << std::endl; | 
| 333 | } |