]> git.lizzy.rs Git - dragonfireclient.git/blob - src/unittest/test_compression.cpp
Enforce hiding nametag
[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 };
41
42 static TestCompression g_test_instance;
43
44 void TestCompression::runTests(IGameDef *gamedef)
45 {
46         TEST(testRLECompression);
47         TEST(testZlibCompression);
48         TEST(testZlibLargeData);
49 }
50
51 ////////////////////////////////////////////////////////////////////////////////
52
53 void TestCompression::testRLECompression()
54 {
55         SharedBuffer<u8> fromdata(4);
56         fromdata[0]=1;
57         fromdata[1]=5;
58         fromdata[2]=5;
59         fromdata[3]=1;
60
61         std::ostringstream os(std::ios_base::binary);
62         compress(fromdata, os, 0);
63
64         std::string str_out = os.str();
65
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;
71
72         UASSERT(str_out.size() == 10);
73
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);
84
85         std::istringstream is(str_out, std::ios_base::binary);
86         std::ostringstream os2(std::ios_base::binary);
87
88         decompress(is, os2, 0);
89         std::string str_out2 = os2.str();
90
91         infostream << "decompress: ";
92         for (u32 i = 0; i < str_out2.size(); i++)
93                 infostream << (u32)str_out2[i] << ",";
94         infostream << std::endl;
95
96         UASSERTEQ(size_t, str_out2.size(), fromdata.getSize());
97
98         for (u32 i = 0; i < str_out2.size(); i++)
99                 UASSERT(str_out2[i] == fromdata[i]);
100 }
101
102 void TestCompression::testZlibCompression()
103 {
104         SharedBuffer<u8> fromdata(4);
105         fromdata[0]=1;
106         fromdata[1]=5;
107         fromdata[2]=5;
108         fromdata[3]=1;
109
110         std::ostringstream os(std::ios_base::binary);
111         compress(fromdata, os, SER_FMT_VER_HIGHEST_READ);
112
113         std::string str_out = os.str();
114
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;
120
121         std::istringstream is(str_out, std::ios_base::binary);
122         std::ostringstream os2(std::ios_base::binary);
123
124         decompress(is, os2, SER_FMT_VER_HIGHEST_READ);
125         std::string str_out2 = os2.str();
126
127         infostream << "decompress: ";
128         for (u32 i = 0; i < str_out2.size(); i++)
129                 infostream << (u32)str_out2[i] << ",";
130         infostream << std::endl;
131
132         UASSERTEQ(size_t, str_out2.size(), fromdata.getSize());
133
134         for (u32 i = 0; i < str_out2.size(); i++)
135                 UASSERT(str_out2[i] == fromdata[i]);
136 }
137
138 void TestCompression::testZlibLargeData()
139 {
140         infostream << "Test: Testing zlib wrappers with a large amount "
141                 "of pseudorandom data" << std::endl;
142
143         u32 size = 50000;
144         infostream << "Test: Input size of large compressZlib is "
145                 << size << std::endl;
146
147         std::string data_in;
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);
152
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;
157
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;
163
164         std::string str_decompressed = os_decompressed.str();
165         UASSERTEQ(size_t, str_decompressed.size(), data_in.size());
166
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]);
171         }
172 }