Pyrogenesis  trunk
test_codec_zlib.h
Go to the documentation of this file.
1 /* Copyright (C) 2021 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 #include <random>
29 
30 class TestCodecZLib : public CxxTest::TestSuite
31 {
32 public:
34  {
35  size_t inConsumed, outProduced;
36  u32 checksum;
37 
38  // generate random input udata
39  // (limit values to 0..7 so that the udata will actually be compressible)
40  std::mt19937 engine(42);
41  std::uniform_int_distribution<u8> distribution(0x00, 0x07);
42  const size_t usize = 10000;
43  u8 udata[usize];
44  for(size_t i = 0; i < usize; i++)
45  udata[i] = distribution(engine);
46 
47  // compress
48  u8* cdata; size_t csize;
49  {
50  boost::shared_ptr<ICodec> compressor_zlib = CreateCompressor_ZLib();
51  ICodec* c = compressor_zlib.get();
52  const size_t csizeMax = c->MaxOutputSize(usize);
53  cdata = new u8[csizeMax];
54  TS_ASSERT_OK(c->Process(udata, usize, cdata, csizeMax, inConsumed, outProduced));
55  TS_ASSERT_EQUALS(inConsumed, usize);
56  TS_ASSERT_LESS_THAN_EQUALS(outProduced, csizeMax);
57  u8* cdata2;
58  TS_ASSERT_OK(c->Finish(cdata2, csize, checksum));
59  TS_ASSERT_EQUALS(cdata, cdata2);
60  TS_ASSERT_EQUALS(csize, outProduced);
61  }
62 
63  // make sure the data changed during compression
64  TS_ASSERT(csize != usize || memcmp(udata, cdata, std::min(usize, csize)) != 0);
65 
66  // decompress
67  u8 ddata[usize];
68  {
69  boost::shared_ptr<ICodec> decompressor_zlib = CreateDecompressor_ZLib();
70  ICodec* d = decompressor_zlib.get();
71  TS_ASSERT_OK(decompressor_zlib->Process(cdata, csize, ddata, usize, inConsumed, outProduced));
72  TS_ASSERT_EQUALS(inConsumed, csize); // ZLib always outputs as much data as possible
73  TS_ASSERT_EQUALS(outProduced, usize); // .. so these figures are correct before Finish()
74  u8* ddata2; size_t dsize;
75  TS_ASSERT_OK(d->Finish(&ddata2, &dsize, &checksum));
76  TS_ASSERT_EQUALS(ddata, ddata2);
77  TS_ASSERT_EQUALS(dsize, outProduced);
78  }
79 
80  // verify udata survived intact
81  TS_ASSERT_SAME_DATA(udata, ddata, usize);
82 
83  delete[] cdata;
84  }
85 };
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:33
uint8_t u8
Definition: types.h:37
Definition: test_codec_zlib.h:30
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
#define TS_ASSERT_OK(expr)
Definition: self_test.h:126