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();
42 static TestCompression g_test_instance;
44 void TestCompression::runTests(IGameDef *gamedef)
46 TEST(testRLECompression);
47 TEST(testZlibCompression);
48 TEST(testZlibLargeData);
51 ////////////////////////////////////////////////////////////////////////////////
53 void TestCompression::testRLECompression()
55 SharedBuffer<u8> fromdata(4);
61 std::ostringstream os(std::ios_base::binary);
62 compress(fromdata, os, 0);
64 std::string str_out = os.str();
66 infostream << "str_out.size()="<<str_out.size()<<std::endl;
67 infostream << "TestCompress: 1,5,5,1 -> ";
68 for (u32 i = 0; i < str_out.size(); i++)
69 infostream << (u32)str_out[i] << ",";
70 infostream << std::endl;
72 UASSERT(str_out.size() == 10);
74 UASSERT(str_out[0] == 0);
75 UASSERT(str_out[1] == 0);
76 UASSERT(str_out[2] == 0);
77 UASSERT(str_out[3] == 4);
78 UASSERT(str_out[4] == 0);
79 UASSERT(str_out[5] == 1);
80 UASSERT(str_out[6] == 1);
81 UASSERT(str_out[7] == 5);
82 UASSERT(str_out[8] == 0);
83 UASSERT(str_out[9] == 1);
85 std::istringstream is(str_out, std::ios_base::binary);
86 std::ostringstream os2(std::ios_base::binary);
88 decompress(is, os2, 0);
89 std::string str_out2 = os2.str();
91 infostream << "decompress: ";
92 for (u32 i = 0; i < str_out2.size(); i++)
93 infostream << (u32)str_out2[i] << ",";
94 infostream << std::endl;
96 UASSERTEQ(size_t, str_out2.size(), fromdata.getSize());
98 for (u32 i = 0; i < str_out2.size(); i++)
99 UASSERT(str_out2[i] == fromdata[i]);
102 void TestCompression::testZlibCompression()
104 SharedBuffer<u8> fromdata(4);
110 std::ostringstream os(std::ios_base::binary);
111 compress(fromdata, os, SER_FMT_VER_HIGHEST_READ);
113 std::string str_out = os.str();
115 infostream << "str_out.size()=" << str_out.size() <<std::endl;
116 infostream << "TestCompress: 1,5,5,1 -> ";
117 for (u32 i = 0; i < str_out.size(); i++)
118 infostream << (u32)str_out[i] << ",";
119 infostream << std::endl;
121 std::istringstream is(str_out, std::ios_base::binary);
122 std::ostringstream os2(std::ios_base::binary);
124 decompress(is, os2, SER_FMT_VER_HIGHEST_READ);
125 std::string str_out2 = os2.str();
127 infostream << "decompress: ";
128 for (u32 i = 0; i < str_out2.size(); i++)
129 infostream << (u32)str_out2[i] << ",";
130 infostream << std::endl;
132 UASSERTEQ(size_t, str_out2.size(), fromdata.getSize());
134 for (u32 i = 0; i < str_out2.size(); i++)
135 UASSERT(str_out2[i] == fromdata[i]);
138 void TestCompression::testZlibLargeData()
140 infostream << "Test: Testing zlib wrappers with a large amount "
141 "of pseudorandom data" << std::endl;
144 infostream << "Test: Input size of large compressZlib is "
145 << size << std::endl;
148 data_in.resize(size);
149 PseudoRandom pseudorandom(9420);
150 for (u32 i = 0; i < size; i++)
151 data_in[i] = pseudorandom.range(0, 255);
153 std::ostringstream os_compressed(std::ios::binary);
154 compressZlib(data_in, os_compressed);
155 infostream << "Test: Output size of large compressZlib is "
156 << os_compressed.str().size()<<std::endl;
158 std::istringstream is_compressed(os_compressed.str(), std::ios::binary);
159 std::ostringstream os_decompressed(std::ios::binary);
160 decompressZlib(is_compressed, os_decompressed);
161 infostream << "Test: Output size of large decompressZlib is "
162 << os_decompressed.str().size() << std::endl;
164 std::string str_decompressed = os_decompressed.str();
165 UASSERTEQ(size_t, str_decompressed.size(), data_in.size());
167 for (u32 i = 0; i < size && i < str_decompressed.size(); i++) {
168 UTEST(str_decompressed[i] == data_in[i],
169 "index out[%i]=%i differs from in[%i]=%i",
170 i, str_decompressed[i], i, data_in[i]);