Line data Source code
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 1 : class TestCompression : public TestBase {
31 : public:
32 1 : TestCompression() { TestManager::registerTestModule(this); }
33 0 : const char *getName() { return "TestCompression"; }
34 :
35 : void runTests(IGameDef *gamedef);
36 :
37 : void testRLECompression();
38 : void testZlibCompression();
39 : void testZlibLargeData();
40 : };
41 :
42 1 : static TestCompression g_test_instance;
43 :
44 0 : void TestCompression::runTests(IGameDef *gamedef)
45 : {
46 0 : TEST(testRLECompression);
47 0 : TEST(testZlibCompression);
48 0 : TEST(testZlibLargeData);
49 0 : }
50 :
51 : ////////////////////////////////////////////////////////////////////////////////
52 :
53 0 : void TestCompression::testRLECompression()
54 : {
55 0 : SharedBuffer<u8> fromdata(4);
56 0 : fromdata[0]=1;
57 0 : fromdata[1]=5;
58 0 : fromdata[2]=5;
59 0 : fromdata[3]=1;
60 :
61 0 : std::ostringstream os(std::ios_base::binary);
62 0 : compress(fromdata, os, 0);
63 :
64 0 : std::string str_out = os.str();
65 :
66 0 : infostream << "str_out.size()="<<str_out.size()<<std::endl;
67 0 : infostream << "TestCompress: 1,5,5,1 -> ";
68 0 : for (u32 i = 0; i < str_out.size(); i++)
69 0 : infostream << (u32)str_out[i] << ",";
70 0 : infostream << std::endl;
71 :
72 0 : UASSERT(str_out.size() == 10);
73 :
74 0 : UASSERT(str_out[0] == 0);
75 0 : UASSERT(str_out[1] == 0);
76 0 : UASSERT(str_out[2] == 0);
77 0 : UASSERT(str_out[3] == 4);
78 0 : UASSERT(str_out[4] == 0);
79 0 : UASSERT(str_out[5] == 1);
80 0 : UASSERT(str_out[6] == 1);
81 0 : UASSERT(str_out[7] == 5);
82 0 : UASSERT(str_out[8] == 0);
83 0 : UASSERT(str_out[9] == 1);
84 :
85 0 : std::istringstream is(str_out, std::ios_base::binary);
86 0 : std::ostringstream os2(std::ios_base::binary);
87 :
88 0 : decompress(is, os2, 0);
89 0 : std::string str_out2 = os2.str();
90 :
91 0 : infostream << "decompress: ";
92 0 : for (u32 i = 0; i < str_out2.size(); i++)
93 0 : infostream << (u32)str_out2[i] << ",";
94 0 : infostream << std::endl;
95 :
96 0 : UASSERTEQ(size_t, str_out2.size(), fromdata.getSize());
97 :
98 0 : for (u32 i = 0; i < str_out2.size(); i++)
99 0 : UASSERT(str_out2[i] == fromdata[i]);
100 0 : }
101 :
102 0 : void TestCompression::testZlibCompression()
103 : {
104 0 : SharedBuffer<u8> fromdata(4);
105 0 : fromdata[0]=1;
106 0 : fromdata[1]=5;
107 0 : fromdata[2]=5;
108 0 : fromdata[3]=1;
109 :
110 0 : std::ostringstream os(std::ios_base::binary);
111 0 : compress(fromdata, os, SER_FMT_VER_HIGHEST_READ);
112 :
113 0 : std::string str_out = os.str();
114 :
115 0 : infostream << "str_out.size()=" << str_out.size() <<std::endl;
116 0 : infostream << "TestCompress: 1,5,5,1 -> ";
117 0 : for (u32 i = 0; i < str_out.size(); i++)
118 0 : infostream << (u32)str_out[i] << ",";
119 0 : infostream << std::endl;
120 :
121 0 : std::istringstream is(str_out, std::ios_base::binary);
122 0 : std::ostringstream os2(std::ios_base::binary);
123 :
124 0 : decompress(is, os2, SER_FMT_VER_HIGHEST_READ);
125 0 : std::string str_out2 = os2.str();
126 :
127 0 : infostream << "decompress: ";
128 0 : for (u32 i = 0; i < str_out2.size(); i++)
129 0 : infostream << (u32)str_out2[i] << ",";
130 0 : infostream << std::endl;
131 :
132 0 : UASSERTEQ(size_t, str_out2.size(), fromdata.getSize());
133 :
134 0 : for (u32 i = 0; i < str_out2.size(); i++)
135 0 : UASSERT(str_out2[i] == fromdata[i]);
136 0 : }
137 :
138 0 : void TestCompression::testZlibLargeData()
139 : {
140 : infostream << "Test: Testing zlib wrappers with a large amount "
141 0 : "of pseudorandom data" << std::endl;
142 :
143 0 : u32 size = 50000;
144 0 : infostream << "Test: Input size of large compressZlib is "
145 0 : << size << std::endl;
146 :
147 0 : std::string data_in;
148 0 : data_in.resize(size);
149 0 : PseudoRandom pseudorandom(9420);
150 0 : for (u32 i = 0; i < size; i++)
151 0 : data_in[i] = pseudorandom.range(0, 255);
152 :
153 0 : std::ostringstream os_compressed(std::ios::binary);
154 0 : compressZlib(data_in, os_compressed);
155 0 : infostream << "Test: Output size of large compressZlib is "
156 0 : << os_compressed.str().size()<<std::endl;
157 :
158 0 : std::istringstream is_compressed(os_compressed.str(), std::ios::binary);
159 0 : std::ostringstream os_decompressed(std::ios::binary);
160 0 : decompressZlib(is_compressed, os_decompressed);
161 0 : infostream << "Test: Output size of large decompressZlib is "
162 0 : << os_decompressed.str().size() << std::endl;
163 :
164 0 : std::string str_decompressed = os_decompressed.str();
165 0 : UASSERTEQ(size_t, str_decompressed.size(), data_in.size());
166 :
167 0 : for (u32 i = 0; i < size && i < str_decompressed.size(); i++) {
168 0 : UTEST(str_decompressed[i] == data_in[i],
169 : "index out[%i]=%i differs from in[%i]=%i",
170 0 : i, str_decompressed[i], i, data_in[i]);
171 : }
172 3 : }
|