]> git.lizzy.rs Git - dragonfireclient.git/blob - src/unittest/test_serialization.cpp
Enforce hiding nametag
[dragonfireclient.git] / src / unittest / test_serialization.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 "util/string.h"
23 #include "util/serialize.h"
24
25 class TestSerialization : public TestBase {
26 public:
27         TestSerialization() { TestManager::registerTestModule(this); }
28         const char *getName() { return "TestSerialization"; }
29
30         void runTests(IGameDef *gamedef);
31         void buildTestStrings();
32
33         void testSerializeString();
34         void testSerializeWideString();
35         void testSerializeLongString();
36         void testSerializeJsonString();
37
38         std::string teststring2;
39         std::wstring teststring2_w;
40         std::string teststring2_w_encoded;
41 };
42
43 static TestSerialization g_test_instance;
44
45 void TestSerialization::runTests(IGameDef *gamedef)
46 {
47         buildTestStrings();
48
49         TEST(testSerializeString);
50         TEST(testSerializeWideString);
51         TEST(testSerializeLongString);
52         TEST(testSerializeJsonString);
53 }
54
55 ////////////////////////////////////////////////////////////////////////////////
56
57 // To be used like this:
58 //   mkstr("Some\0string\0with\0embedded\0nuls")
59 // since std::string("...") doesn't work as expected in that case.
60 template<size_t N> std::string mkstr(const char (&s)[N])
61 {
62         return std::string(s, N - 1);
63 }
64
65 void TestSerialization::buildTestStrings()
66 {
67         std::ostringstream tmp_os;
68         std::wostringstream tmp_os_w;
69         std::ostringstream tmp_os_w_encoded;
70         for (int i = 0; i < 256; i++) {
71                 tmp_os << (char)i;
72                 tmp_os_w << (wchar_t)i;
73                 tmp_os_w_encoded << (char)0 << (char)i;
74         }
75         teststring2 = tmp_os.str();
76         teststring2_w = tmp_os_w.str();
77         teststring2_w_encoded = tmp_os_w_encoded.str();
78 }
79
80 void TestSerialization::testSerializeString()
81 {
82         // Test blank string
83         UASSERT(serializeString("Hello world!") == mkstr("\0\14Hello world!"));
84
85         // Test basic string
86         UASSERT(serializeString("") == mkstr("\0\0"));
87
88         // Test character range
89         UASSERT(serializeString(teststring2) == mkstr("\1\0") + teststring2);
90
91         // Test deserialize
92         std::istringstream is(serializeString(teststring2), std::ios::binary);
93         UASSERT(deSerializeString(is) == teststring2);
94         UASSERT(!is.eof());
95         is.get();
96         UASSERT(is.eof());
97 }
98
99 void TestSerialization::testSerializeWideString()
100 {
101         // Test blank string
102         UASSERT(serializeWideString(L"") == mkstr("\0\0"));
103
104         // Test basic string
105         UASSERT(serializeWideString(narrow_to_wide("Hello world!")) ==
106                 mkstr("\0\14\0H\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d\0!"));
107
108         // Test character range
109         UASSERT(serializeWideString(teststring2_w) ==
110                 mkstr("\1\0") + teststring2_w_encoded);
111
112         // Test deserialize
113         std::istringstream is(serializeWideString(teststring2_w), std::ios::binary);
114         UASSERT(deSerializeWideString(is) == teststring2_w);
115         UASSERT(!is.eof());
116         is.get();
117         UASSERT(is.eof());
118 }
119
120 void TestSerialization::testSerializeLongString()
121 {
122         // Test blank string
123         UASSERT(serializeLongString("") == mkstr("\0\0\0\0"));
124
125         // Test basic string
126         UASSERT(serializeLongString("Hello world!") == mkstr("\0\0\0\14Hello world!"));
127
128         // Test character range
129         UASSERT(serializeLongString(teststring2) == mkstr("\0\0\1\0") + teststring2);
130
131         // Test deserialize
132         std::istringstream is(serializeLongString(teststring2), std::ios::binary);
133         UASSERT(deSerializeLongString(is) == teststring2);
134         UASSERT(!is.eof());
135         is.get();
136         UASSERT(is.eof());
137 }
138
139 void TestSerialization::testSerializeJsonString()
140 {
141         // Test blank string
142         UASSERT(serializeJsonString("") == "\"\"");
143
144         // Test basic string
145         UASSERT(serializeJsonString("Hello world!") == "\"Hello world!\"");
146
147         // MSVC fails when directly using "\\\\"
148         std::string backslash = "\\";
149         UASSERT(serializeJsonString(teststring2) ==
150                 mkstr("\"") +
151                 "\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007" +
152                 "\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f" +
153                 "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" +
154                 "\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f" +
155                 " !\\\"" + teststring2.substr(0x23, 0x2f-0x23) +
156                 "\\/" + teststring2.substr(0x30, 0x5c-0x30) +
157                 backslash + backslash + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" +
158                 "\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u0086\\u0087" +
159                 "\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008e\\u008f" +
160                 "\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" +
161                 "\\u0098\\u0099\\u009a\\u009b\\u009c\\u009d\\u009e\\u009f" +
162                 "\\u00a0\\u00a1\\u00a2\\u00a3\\u00a4\\u00a5\\u00a6\\u00a7" +
163                 "\\u00a8\\u00a9\\u00aa\\u00ab\\u00ac\\u00ad\\u00ae\\u00af" +
164                 "\\u00b0\\u00b1\\u00b2\\u00b3\\u00b4\\u00b5\\u00b6\\u00b7" +
165                 "\\u00b8\\u00b9\\u00ba\\u00bb\\u00bc\\u00bd\\u00be\\u00bf" +
166                 "\\u00c0\\u00c1\\u00c2\\u00c3\\u00c4\\u00c5\\u00c6\\u00c7" +
167                 "\\u00c8\\u00c9\\u00ca\\u00cb\\u00cc\\u00cd\\u00ce\\u00cf" +
168                 "\\u00d0\\u00d1\\u00d2\\u00d3\\u00d4\\u00d5\\u00d6\\u00d7" +
169                 "\\u00d8\\u00d9\\u00da\\u00db\\u00dc\\u00dd\\u00de\\u00df" +
170                 "\\u00e0\\u00e1\\u00e2\\u00e3\\u00e4\\u00e5\\u00e6\\u00e7" +
171                 "\\u00e8\\u00e9\\u00ea\\u00eb\\u00ec\\u00ed\\u00ee\\u00ef" +
172                 "\\u00f0\\u00f1\\u00f2\\u00f3\\u00f4\\u00f5\\u00f6\\u00f7" +
173                 "\\u00f8\\u00f9\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff" +
174                 "\"");
175
176         // Test deserialize
177         std::istringstream is(serializeJsonString(teststring2), std::ios::binary);
178         UASSERT(deSerializeJsonString(is) == teststring2);
179         UASSERT(!is.eof());
180         is.get();
181         UASSERT(is.eof());
182 }