]> git.lizzy.rs Git - dragonfireclient.git/blob - src/unittest/test_compression.cpp
refacto: hide mesh_cache inside the rendering engine
[dragonfireclient.git] / src / unittest / test_compression.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "test.h"
21
22 #include <sstream>
23
24 #include "irrlichttypes_extrabloated.h"
25 #include "log.h"
26 #include "serialization.h"
27 #include "nodedef.h"
28 #include "noise.h"
29
30 class TestCompression : public TestBase {
31 public:
32         TestCompression() { TestManager::registerTestModule(this); }
33         const char *getName() { return "TestCompression"; }
34
35         void runTests(IGameDef *gamedef);
36
37         void testRLECompression();
38         void testZlibCompression();
39         void testZlibLargeData();
40         void testZlibLimit();
41         void _testZlibLimit(u32 size, u32 limit);
42 };
43
44 static TestCompression g_test_instance;
45
46 void TestCompression::runTests(IGameDef *gamedef)
47 {
48         TEST(testRLECompression);
49         TEST(testZlibCompression);
50         TEST(testZlibLargeData);
51         TEST(testZlibLimit);
52 }
53
54 ////////////////////////////////////////////////////////////////////////////////
55
56 void TestCompression::testRLECompression()
57 {
58         SharedBuffer<u8> fromdata(4);
59         fromdata[0]=1;
60         fromdata[1]=5;
61         fromdata[2]=5;
62         fromdata[3]=1;
63
64         std::ostringstream os(std::ios_base::binary);
65         compress(fromdata, os, 0);
66
67         std::string str_out = os.str();
68
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;
74
75         UASSERT(str_out.size() == 10);
76
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);
87
88         std::istringstream is(str_out, std::ios_base::binary);
89         std::ostringstream os2(std::ios_base::binary);
90
91         decompress(is, os2, 0);
92         std::string str_out2 = os2.str();
93
94         infostream << "decompress: ";
95         for (char i : str_out2)
96                 infostream << (u32) i << ",";
97         infostream << std::endl;
98
99         UASSERTEQ(size_t, str_out2.size(), fromdata.getSize());
100
101         for (u32 i = 0; i < str_out2.size(); i++)
102                 UASSERT(str_out2[i] == fromdata[i]);
103 }
104
105 void TestCompression::testZlibCompression()
106 {
107         SharedBuffer<u8> fromdata(4);
108         fromdata[0]=1;
109         fromdata[1]=5;
110         fromdata[2]=5;
111         fromdata[3]=1;
112
113         std::ostringstream os(std::ios_base::binary);
114         compress(fromdata, os, SER_FMT_VER_HIGHEST_READ);
115
116         std::string str_out = os.str();
117
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;
123
124         std::istringstream is(str_out, std::ios_base::binary);
125         std::ostringstream os2(std::ios_base::binary);
126
127         decompress(is, os2, SER_FMT_VER_HIGHEST_READ);
128         std::string str_out2 = os2.str();
129
130         infostream << "decompress: ";
131         for (char i : str_out2)
132                 infostream << (u32) i << ",";
133         infostream << std::endl;
134
135         UASSERTEQ(size_t, str_out2.size(), fromdata.getSize());
136
137         for (u32 i = 0; i < str_out2.size(); i++)
138                 UASSERT(str_out2[i] == fromdata[i]);
139 }
140
141 void TestCompression::testZlibLargeData()
142 {
143         infostream << "Test: Testing zlib wrappers with a large amount "
144                 "of pseudorandom data" << std::endl;
145
146         u32 size = 50000;
147         infostream << "Test: Input size of large compressZlib is "
148                 << size << std::endl;
149
150         std::string data_in;
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);
155
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;
160
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;
166
167         std::string str_decompressed = os_decompressed.str();
168         UASSERTEQ(size_t, str_decompressed.size(), data_in.size());
169
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]);
174         }
175 }
176
177 void TestCompression::testZlibLimit()
178 {
179         // edge cases
180         _testZlibLimit(1024, 1023);
181         _testZlibLimit(1024, 1024);
182         _testZlibLimit(1024, 1025);
183
184         // test around buffer borders
185         u32 bufsize = 16384; // as in implementation
186         for (int s = -1; s <= 1; s++)
187         {
188                 for (int l = -1; l <= 1; l++)
189                 {
190                         _testZlibLimit(bufsize + s, bufsize + l);
191                 }
192         }
193         // span multiple buffers
194         _testZlibLimit(35000, 22000);
195         _testZlibLimit(22000, 35000);
196 }
197
198 void TestCompression::_testZlibLimit(u32 size, u32 limit)
199 {
200         infostream << "Test: Testing zlib wrappers with a decompression "
201                 "memory limit of " << limit << std::endl;
202
203         infostream << "Test: Input size of compressZlib for limit is "
204                 << size << std::endl;
205
206         // how much data we expect to get
207         u32 expected = size < limit ? size : limit;
208
209         // create recognizable data
210         std::string data_in;
211         data_in.resize(size);
212         for (u32 i = 0; i < size; i++)
213                 data_in[i] = (u8)(i % 256);
214
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;
219
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;
225
226         std::string str_decompressed = os_decompressed.str();
227         UASSERTEQ(size_t, str_decompressed.size(), expected);
228
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]);
233         }
234 }
235