Pyrogenesis  trunk
test_compression.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/compression.h"
27 
28 class TestCompression : public CxxTest::TestSuite
29 {
30 public:
32  {
33  // generate random input data
34  // (limit values to 0..7 so that the data will actually be compressible)
35  const size_t data_size = 10000;
36  u8 data[data_size];
37  for(size_t i = 0; i < data_size; i++)
38  data[i] = rand() & 0x07;
39 
40  u8* cdata; size_t csize;
41  u8 udata[data_size];
42 
43  // compress
44  uintptr_t c = comp_alloc(CT_COMPRESSION, CM_DEFLATE);
45  {
46  TS_ASSERT(c != 0);
47  const size_t csizeBound = comp_max_output_size(c, data_size);
48  TS_ASSERT_OK(comp_alloc_output(c, csizeBound));
49  const ssize_t cdata_produced = comp_feed(c, data, data_size);
50  TS_ASSERT(cdata_produced >= 0);
51  u32 checksum;
52  TS_ASSERT_OK(comp_finish(c, &cdata, &csize, &checksum));
53  TS_ASSERT(cdata_produced <= (ssize_t)csize); // can't have produced more than total
54  }
55 
56  // decompress
57  uintptr_t d = comp_alloc(CT_DECOMPRESSION, CM_DEFLATE);
58  {
59  TS_ASSERT(d != 0);
60  comp_set_output(d, udata, data_size);
61  const ssize_t udata_produced = comp_feed(d, cdata, csize);
62  TS_ASSERT(udata_produced >= 0);
63  u8* udata_final; size_t usize_final; u32 checksum;
64  TS_ASSERT_OK(comp_finish(d, &udata_final, &usize_final, &checksum));
65  TS_ASSERT(udata_produced <= (ssize_t)usize_final); // can't have produced more than total
66  TS_ASSERT_EQUALS(udata_final, udata); // output buffer address is same
67  TS_ASSERT_EQUALS(usize_final, data_size); // correct amount of output
68  }
69 
70  comp_free(c);
71  comp_free(d);
72 
73  // verify data survived intact
74  TS_ASSERT_SAME_DATA(data, udata, data_size);
75  }
76 };
uint8_t u8
Definition: types.h:37
uint32_t u32
Definition: types.h:39
Definition: test_compression.h:28
intptr_t ssize_t
Definition: wposix_types.h:82
void test_compress_decompress_compare()
Definition: test_compression.h:31
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