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