3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "irrlichttypes_extrabloated.h"
26 #include "serialization.h"
30 class TestCompression : public TestBase {
32 TestCompression() { TestManager::registerTestModule(this); }
33 const char *getName() { return "TestCompression"; }
35 void runTests(IGameDef *gamedef);
37 void testRLECompression();
38 void testZlibCompression();
39 void testZlibLargeData();
41 void _testZlibLimit(u32 size, u32 limit);
44 static TestCompression g_test_instance;
46 void TestCompression::runTests(IGameDef *gamedef)
48 TEST(testRLECompression);
49 TEST(testZlibCompression);
50 TEST(testZlibLargeData);
54 ////////////////////////////////////////////////////////////////////////////////
56 void TestCompression::testRLECompression()
58 SharedBuffer<u8> fromdata(4);
64 std::ostringstream os(std::ios_base::binary);
65 compress(fromdata, os, 0);
67 std::string str_out = os.str();
69 infostream << "str_out.size()="<<str_out.size()<<std::endl;
70 infostream << "TestCompress: 1,5,5,1 -> ";
71 for (char i : str_out)
72 infostream << (u32) i << ",";
73 infostream << std::endl;
75 UASSERT(str_out.size() == 10);
77 UASSERT(str_out[0] == 0);
78 UASSERT(str_out[1] == 0);
79 UASSERT(str_out[2] == 0);
80 UASSERT(str_out[3] == 4);
81 UASSERT(str_out[4] == 0);
82 UASSERT(str_out[5] == 1);
83 UASSERT(str_out[6] == 1);
84 UASSERT(str_out[7] == 5);
85 UASSERT(str_out[8] == 0);
86 UASSERT(str_out[9] == 1);
88 std::istringstream is(str_out, std::ios_base::binary);
89 std::ostringstream os2(std::ios_base::binary);
91 decompress(is, os2, 0);
92 std::string str_out2 = os2.str();
94 infostream << "decompress: ";
95 for (char i : str_out2)
96 infostream << (u32) i << ",";
97 infostream << std::endl;
99 UASSERTEQ(size_t, str_out2.size(), fromdata.getSize());
101 for (u32 i = 0; i < str_out2.size(); i++)
102 UASSERT(str_out2[i] == fromdata[i]);
105 void TestCompression::testZlibCompression()
107 SharedBuffer<u8> fromdata(4);
113 std::ostringstream os(std::ios_base::binary);
114 compress(fromdata, os, SER_FMT_VER_HIGHEST_READ);
116 std::string str_out = os.str();
118 infostream << "str_out.size()=" << str_out.size() <<std::endl;
119 infostream << "TestCompress: 1,5,5,1 -> ";
120 for (char i : str_out)
121 infostream << (u32) i << ",";
122 infostream << std::endl;
124 std::istringstream is(str_out, std::ios_base::binary);
125 std::ostringstream os2(std::ios_base::binary);
127 decompress(is, os2, SER_FMT_VER_HIGHEST_READ);
128 std::string str_out2 = os2.str();
130 infostream << "decompress: ";
131 for (char i : str_out2)
132 infostream << (u32) i << ",";
133 infostream << std::endl;
135 UASSERTEQ(size_t, str_out2.size(), fromdata.getSize());
137 for (u32 i = 0; i < str_out2.size(); i++)
138 UASSERT(str_out2[i] == fromdata[i]);
141 void TestCompression::testZlibLargeData()
143 infostream << "Test: Testing zlib wrappers with a large amount "
144 "of pseudorandom data" << std::endl;
147 infostream << "Test: Input size of large compressZlib is "
148 << size << std::endl;
151 data_in.resize(size);
152 PseudoRandom pseudorandom(9420);
153 for (u32 i = 0; i < size; i++)
154 data_in[i] = pseudorandom.range(0, 255);
156 std::ostringstream os_compressed(std::ios::binary);
157 compressZlib(data_in, os_compressed);
158 infostream << "Test: Output size of large compressZlib is "
159 << os_compressed.str().size()<<std::endl;
161 std::istringstream is_compressed(os_compressed.str(), std::ios::binary);
162 std::ostringstream os_decompressed(std::ios::binary);
163 decompressZlib(is_compressed, os_decompressed);
164 infostream << "Test: Output size of large decompressZlib is "
165 << os_decompressed.str().size() << std::endl;
167 std::string str_decompressed = os_decompressed.str();
168 UASSERTEQ(size_t, str_decompressed.size(), data_in.size());
170 for (u32 i = 0; i < size && i < str_decompressed.size(); i++) {
171 UTEST(str_decompressed[i] == data_in[i],
172 "index out[%i]=%i differs from in[%i]=%i",
173 i, str_decompressed[i], i, data_in[i]);
177 void TestCompression::testZlibLimit()
180 _testZlibLimit(1024, 1023);
181 _testZlibLimit(1024, 1024);
182 _testZlibLimit(1024, 1025);
184 // test around buffer borders
185 u32 bufsize = 16384; // as in implementation
186 for (int s = -1; s <= 1; s++)
188 for (int l = -1; l <= 1; l++)
190 _testZlibLimit(bufsize + s, bufsize + l);
193 // span multiple buffers
194 _testZlibLimit(35000, 22000);
195 _testZlibLimit(22000, 35000);
198 void TestCompression::_testZlibLimit(u32 size, u32 limit)
200 infostream << "Test: Testing zlib wrappers with a decompression "
201 "memory limit of " << limit << std::endl;
203 infostream << "Test: Input size of compressZlib for limit is "
204 << size << std::endl;
206 // how much data we expect to get
207 u32 expected = size < limit ? size : limit;
209 // create recognizable data
211 data_in.resize(size);
212 for (u32 i = 0; i < size; i++)
213 data_in[i] = (u8)(i % 256);
215 std::ostringstream os_compressed(std::ios::binary);
216 compressZlib(data_in, os_compressed);
217 infostream << "Test: Output size of compressZlib for limit is "
218 << os_compressed.str().size()<<std::endl;
220 std::istringstream is_compressed(os_compressed.str(), std::ios::binary);
221 std::ostringstream os_decompressed(std::ios::binary);
222 decompressZlib(is_compressed, os_decompressed, limit);
223 infostream << "Test: Output size of decompressZlib with limit is "
224 << os_decompressed.str().size() << std::endl;
226 std::string str_decompressed = os_decompressed.str();
227 UASSERTEQ(size_t, str_decompressed.size(), expected);
229 for (u32 i = 0; i < size && i < str_decompressed.size(); i++) {
230 UTEST(str_decompressed[i] == data_in[i],
231 "index out[%i]=%i differs from in[%i]=%i",
232 i, str_decompressed[i], i, data_in[i]);