Pyrogenesis  trunk
test_codec_zlib.h
Go to the documentation of this file.
1 /* Copyright (c) 2010 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "lib/self_test.h"
24 
25 #include "lib/self_test.h"
26 #include "lib/res/file/archive/codec_zlib.h"
27 
28 class TestCodecZLib : public CxxTest::TestSuite
29 {
30 public:
32  {
33  size_t inConsumed, outProduced;
34  u32 checksum;
35 
36  // generate random input udata
37  // (limit values to 0..7 so that the udata will actually be compressible)
38  const size_t usize = 10000;
39  u8 udata[usize];
40  for(size_t i = 0; i < usize; i++)
41  udata[i] = rand() & 0x07;
42 
43  // compress
44  u8* cdata; size_t csize;
45  {
46  boost::shared_ptr<ICodec> compressor_zlib = CreateCompressor_ZLib();
47  ICodec* c = compressor_zlib.get();
48  const size_t csizeMax = c->MaxOutputSize(usize);
49  cdata = new u8[csizeMax];
50  TS_ASSERT_OK(c->Process(udata, usize, cdata, csizeMax, inConsumed, outProduced));
51  TS_ASSERT_EQUALS(inConsumed, usize);
52  TS_ASSERT_LESS_THAN_EQUALS(outProduced, csizeMax);
53  u8* cdata2;
54  TS_ASSERT_OK(c->Finish(cdata2, csize, checksum));
55  TS_ASSERT_EQUALS(cdata, cdata2);
56  TS_ASSERT_EQUALS(csize, outProduced);
57  }
58 
59  // make sure the data changed during compression
60  TS_ASSERT(csize != usize || memcmp(udata, cdata, std::min(usize, csize)) != 0);
61 
62  // decompress
63  u8 ddata[usize];
64  {
65  boost::shared_ptr<ICodec> decompressor_zlib = CreateDecompressor_ZLib();
66  ICodec* d = decompressor_zlib.get();
67  TS_ASSERT_OK(decompressor_zlib->Process(cdata, csize, ddata, usize, inConsumed, outProduced));
68  TS_ASSERT_EQUALS(inConsumed, csize); // ZLib always outputs as much data as possible
69  TS_ASSERT_EQUALS(outProduced, usize); // .. so these figures are correct before Finish()
70  u8* ddata2; size_t dsize;
71  TS_ASSERT_OK(d->Finish(&ddata2, &dsize, &checksum));
72  TS_ASSERT_EQUALS(ddata, ddata2);
73  TS_ASSERT_EQUALS(dsize, outProduced);
74  }
75 
76  // verify udata survived intact
77  TS_ASSERT_SAME_DATA(udata, ddata, usize);
78 
79  delete[] cdata;
80  }
81 };
Definition: codec.h:34
virtual Status Finish(u32 &checksum, size_t &outProduced)=0
Flush buffers and make sure all output has been produced.
void test_compress_decompress_compare()
Definition: test_codec_zlib.h:31
uint8_t u8
Definition: types.h:37
Definition: test_codec_zlib.h:28
virtual size_t MaxOutputSize(size_t inSize) const =0
virtual Status Process(const u8 *in, size_t inSize, u8 *out, size_t outSize, size_t &inConsumed, size_t &outProduced)=0
process (i.e.
uint32_t u32
Definition: types.h:39
size_t rand(size_t min_inclusive, size_t max_exclusive)
return random integer in [min, max).
Definition: rand.cpp:53
#define TS_ASSERT_OK(expr)
Definition: self_test.h:106