aboutsummaryrefslogtreecommitdiff
path: root/src/unittest/test_serialization.cpp
diff options
context:
space:
mode:
authorElias Fleckenstein <54945686+EliasFleckenstein03@users.noreply.github.com>2020-11-04 16:44:42 +0100
committerGitHub <noreply@github.com>2020-11-04 16:44:42 +0100
commit5d9ae5a91c544fc7fbd475decf47cef7e09ef8fc (patch)
treec980d614fec4a5495798be3e79e033229062c3cd /src/unittest/test_serialization.cpp
parent28f6a79706b088c37268a59d90878220dc4ef9c7 (diff)
parent3af10766fd2b58b068e970266724d7eb10e9316b (diff)
downloaddragonfireclient-5d9ae5a91c544fc7fbd475decf47cef7e09ef8fc.tar.xz
Merge branch 'master' into master
Diffstat (limited to 'src/unittest/test_serialization.cpp')
-rw-r--r--src/unittest/test_serialization.cpp307
1 files changed, 228 insertions, 79 deletions
diff --git a/src/unittest/test_serialization.cpp b/src/unittest/test_serialization.cpp
index 51e28f144..80a92f65e 100644
--- a/src/unittest/test_serialization.cpp
+++ b/src/unittest/test_serialization.cpp
@@ -23,7 +23,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/serialize.h"
#include <cmath>
-class TestSerialization : public TestBase {
+class TestSerialization : public TestBase
+{
public:
TestSerialization() { TestManager::registerTestModule(this); }
const char *getName() { return "TestSerialization"; }
@@ -80,7 +81,7 @@ void TestSerialization::runTests(IGameDef *gamedef)
// To be used like this:
// mkstr("Some\0string\0with\0embedded\0nuls")
// since std::string("...") doesn't work as expected in that case.
-template<size_t N> std::string mkstr(const char (&s)[N])
+template <size_t N> std::string mkstr(const char (&s)[N])
{
return std::string(s, N - 1);
}
@@ -143,18 +144,19 @@ void TestSerialization::testSerializeWideString()
// Test basic string
UASSERT(serializeWideString(utf8_to_wide("Hello world!")) ==
- mkstr("\0\14\0H\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d\0!"));
+ mkstr("\0\14\0H\0e\0l\0l\0o\0 \0w\0o\0r\0l\0d\0!"));
// Test character range
UASSERT(serializeWideString(teststring2_w) ==
- mkstr("\1\0") + teststring2_w_encoded);
+ mkstr("\1\0") + teststring2_w_encoded);
}
void TestSerialization::testDeSerializeWideString()
{
// Test deserialize
{
- std::istringstream is(serializeWideString(teststring2_w), std::ios::binary);
+ std::istringstream is(
+ serializeWideString(teststring2_w), std::ios::binary);
UASSERT(deSerializeWideString(is) == teststring2_w);
UASSERT(!is.eof());
is.get();
@@ -169,13 +171,15 @@ void TestSerialization::testDeSerializeWideString()
// Test deserialize a string with an incomplete character
{
- std::istringstream is(mkstr("\x00\x07\0a\0b\0c\0d\0e\0f\0"), std::ios::binary);
+ std::istringstream is(
+ mkstr("\x00\x07\0a\0b\0c\0d\0e\0f\0"), std::ios::binary);
EXCEPTION_CHECK(SerializationError, deSerializeWideString(is));
}
// Test deserialize a string with incomplete data
{
- std::istringstream is(mkstr("\x00\x08\0a\0b\0c\0d\0e\0f"), std::ios::binary);
+ std::istringstream is(
+ mkstr("\x00\x08\0a\0b\0c\0d\0e\0f"), std::ios::binary);
EXCEPTION_CHECK(SerializationError, deSerializeWideString(is));
}
}
@@ -222,7 +226,6 @@ void TestSerialization::testDeSerializeLongString()
}
}
-
void TestSerialization::testSerializeJsonString()
{
// Test blank string
@@ -234,31 +237,52 @@ void TestSerialization::testSerializeJsonString()
// MSVC fails when directly using "\\\\"
std::string backslash = "\\";
UASSERT(serializeJsonString(teststring2) ==
- mkstr("\"") +
- "\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007" +
- "\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f" +
- "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" +
- "\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f" +
- " !\\\"" + teststring2.substr(0x23, 0x2f-0x23) +
- "\\/" + teststring2.substr(0x30, 0x5c-0x30) +
- backslash + backslash + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" +
- "\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u0086\\u0087" +
- "\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008e\\u008f" +
- "\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" +
- "\\u0098\\u0099\\u009a\\u009b\\u009c\\u009d\\u009e\\u009f" +
- "\\u00a0\\u00a1\\u00a2\\u00a3\\u00a4\\u00a5\\u00a6\\u00a7" +
- "\\u00a8\\u00a9\\u00aa\\u00ab\\u00ac\\u00ad\\u00ae\\u00af" +
- "\\u00b0\\u00b1\\u00b2\\u00b3\\u00b4\\u00b5\\u00b6\\u00b7" +
- "\\u00b8\\u00b9\\u00ba\\u00bb\\u00bc\\u00bd\\u00be\\u00bf" +
- "\\u00c0\\u00c1\\u00c2\\u00c3\\u00c4\\u00c5\\u00c6\\u00c7" +
- "\\u00c8\\u00c9\\u00ca\\u00cb\\u00cc\\u00cd\\u00ce\\u00cf" +
- "\\u00d0\\u00d1\\u00d2\\u00d3\\u00d4\\u00d5\\u00d6\\u00d7" +
- "\\u00d8\\u00d9\\u00da\\u00db\\u00dc\\u00dd\\u00de\\u00df" +
- "\\u00e0\\u00e1\\u00e2\\u00e3\\u00e4\\u00e5\\u00e6\\u00e7" +
- "\\u00e8\\u00e9\\u00ea\\u00eb\\u00ec\\u00ed\\u00ee\\u00ef" +
- "\\u00f0\\u00f1\\u00f2\\u00f3\\u00f4\\u00f5\\u00f6\\u00f7" +
- "\\u00f8\\u00f9\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff" +
- "\"");
+ mkstr("\"") +
+ "\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u000"
+ "6\\u0007" +
+ "\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f" +
+ "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u001"
+ "6\\u0017" +
+ "\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001"
+ "e\\u001f" +
+ " !\\\"" + teststring2.substr(0x23, 0x2f - 0x23) +
+ "\\/" + teststring2.substr(0x30, 0x5c - 0x30) +
+ backslash + backslash +
+ teststring2.substr(0x5d, 0x7f - 0x5d) +
+ "\\u007f" +
+ "\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u008"
+ "6\\u0087" +
+ "\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008"
+ "e\\u008f" +
+ "\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u009"
+ "6\\u0097" +
+ "\\u0098\\u0099\\u009a\\u009b\\u009c\\u009d\\u009"
+ "e\\u009f" +
+ "\\u00a0\\u00a1\\u00a2\\u00a3\\u00a4\\u00a5\\u00a"
+ "6\\u00a7" +
+ "\\u00a8\\u00a9\\u00aa\\u00ab\\u00ac\\u00ad\\u00a"
+ "e\\u00af" +
+ "\\u00b0\\u00b1\\u00b2\\u00b3\\u00b4\\u00b5\\u00b"
+ "6\\u00b7" +
+ "\\u00b8\\u00b9\\u00ba\\u00bb\\u00bc\\u00bd\\u00b"
+ "e\\u00bf" +
+ "\\u00c0\\u00c1\\u00c2\\u00c3\\u00c4\\u00c5\\u00c"
+ "6\\u00c7" +
+ "\\u00c8\\u00c9\\u00ca\\u00cb\\u00cc\\u00cd\\u00c"
+ "e\\u00cf" +
+ "\\u00d0\\u00d1\\u00d2\\u00d3\\u00d4\\u00d5\\u00d"
+ "6\\u00d7" +
+ "\\u00d8\\u00d9\\u00da\\u00db\\u00dc\\u00dd\\u00d"
+ "e\\u00df" +
+ "\\u00e0\\u00e1\\u00e2\\u00e3\\u00e4\\u00e5\\u00e"
+ "6\\u00e7" +
+ "\\u00e8\\u00e9\\u00ea\\u00eb\\u00ec\\u00ed\\u00e"
+ "e\\u00ef" +
+ "\\u00f0\\u00f1\\u00f2\\u00f3\\u00f4\\u00f5\\u00f"
+ "6\\u00f7" +
+ "\\u00f8\\u00f9\\u00fa\\u00fb\\u00fc\\u00fd\\u00f"
+ "e\\u00ff" +
+ "\"");
// Test deserialize
std::istringstream is(serializeJsonString(teststring2), std::ios::binary);
@@ -275,24 +299,21 @@ void TestSerialization::testSerializeHex()
UASSERT(serializeHexString("", true) == "");
// Test basic string
- UASSERT(serializeHexString("Hello world!") ==
- "48656c6c6f20776f726c6421");
+ UASSERT(serializeHexString("Hello world!") == "48656c6c6f20776f726c6421");
UASSERT(serializeHexString("Hello world!", true) ==
- "48 65 6c 6c 6f 20 77 6f 72 6c 64 21");
+ "48 65 6c 6c 6f 20 77 6f 72 6c 64 21");
// Test binary string
UASSERT(serializeHexString(mkstr("\x00\x0a\xb0\x63\x1f\x00\xff")) ==
- "000ab0631f00ff");
+ "000ab0631f00ff");
UASSERT(serializeHexString(mkstr("\x00\x0a\xb0\x63\x1f\x00\xff"), true) ==
- "00 0a b0 63 1f 00 ff");
+ "00 0a b0 63 1f 00 ff");
}
-
void TestSerialization::testStreamRead()
{
std::string datastr(
- (const char *)test_serialized_data,
- sizeof(test_serialized_data));
+ (const char *)test_serialized_data, sizeof(test_serialized_data));
std::istringstream is(datastr, std::ios_base::binary);
UASSERT(readU8(is) == 0x11);
@@ -329,7 +350,6 @@ void TestSerialization::testStreamRead()
UASSERT(is.rdbuf()->in_avail() == 0);
}
-
void TestSerialization::testStreamWrite()
{
std::ostringstream os(std::ios_base::binary);
@@ -375,7 +395,6 @@ void TestSerialization::testStreamWrite()
UASSERT(!memcmp(&data[0], test_serialized_data, sizeof(test_serialized_data)));
}
-
void TestSerialization::testVecPut()
{
std::vector<u8> buf;
@@ -415,7 +434,6 @@ void TestSerialization::testVecPut()
UASSERT(!memcmp(&buf[0], test_serialized_data, sizeof(test_serialized_data)));
}
-
void TestSerialization::testStringLengthLimits()
{
std::vector<u8> buf;
@@ -434,7 +452,6 @@ void TestSerialization::testStringLengthLimits()
putWideString(&buf, too_long_wide);
}
-
void TestSerialization::testBufReader()
{
u8 u8_data;
@@ -526,8 +543,7 @@ void TestSerialization::testBufReader()
EXCEPTION_CHECK(SerializationError, buf.getString());
EXCEPTION_CHECK(SerializationError, buf.getWideString());
EXCEPTION_CHECK(SerializationError, buf.getLongString());
- EXCEPTION_CHECK(SerializationError,
- buf.getRawData(raw_data, sizeof(raw_data)));
+ EXCEPTION_CHECK(SerializationError, buf.getRawData(raw_data, sizeof(raw_data)));
// See if we can skip backwards
buf.pos = 5;
@@ -632,29 +648,25 @@ void TestSerialization::testFloatFormat()
f32 fs, fm;
// Check precision of float calculations on this platform
- const std::unordered_map<f32, u32> float_results = {
- { 0.0f, 0x00000000UL },
- { 1.0f, 0x3F800000UL },
- { -1.0f, 0xBF800000UL },
- { 0.1f, 0x3DCCCCCDUL },
- { -0.1f, 0xBDCCCCCDUL },
- { 1945329.25f, 0x49ED778AUL },
- { -23298764.f, 0xCBB1C166UL },
- { 0.5f, 0x3F000000UL },
- { -0.5f, 0xBF000000UL }
- };
+ const std::unordered_map<f32, u32> float_results = {{0.0f, 0x00000000UL},
+ {1.0f, 0x3F800000UL}, {-1.0f, 0xBF800000UL}, {0.1f, 0x3DCCCCCDUL},
+ {-0.1f, 0xBDCCCCCDUL}, {1945329.25f, 0x49ED778AUL},
+ {-23298764.f, 0xCBB1C166UL}, {0.5f, 0x3F000000UL},
+ {-0.5f, 0xBF000000UL}};
for (const auto &v : float_results) {
i = f32Tou32Slow(v.first);
if (std::abs((s64)v.second - i) > 32) {
- printf("Inaccurate float values on %.9g, expected 0x%X, actual 0x%X\n",
- v.first, v.second, i);
+ printf("Inaccurate float values on %.9g, expected 0x%X, actual "
+ "0x%X\n",
+ v.first, v.second, i);
UASSERT(false);
}
fs = u32Tof32Slow(v.second);
if (std::fabs(v.first - fs) > std::fabs(v.first * 0.000005f)) {
- printf("Inaccurate float values on 0x%X, expected %.9g, actual 0x%.9g\n",
- v.second, v.first, fs);
+ printf("Inaccurate float values on 0x%X, expected %.9g, actual "
+ "0x%.9g\n",
+ v.second, v.first, fs);
UASSERT(false);
}
}
@@ -682,13 +694,15 @@ void TestSerialization::testFloatFormat()
memcpy(&fm, &i, 4);
fs = u32Tof32Slow(i);
if (fm != fs) {
- printf("u32Tof32Slow failed on 0x%X, expected %.9g, actual %.9g\n",
- i, fm, fs);
+ printf("u32Tof32Slow failed on 0x%X, expected %.9g, actual "
+ "%.9g\n",
+ i, fm, fs);
return false;
}
if (f32Tou32Slow(fs) != i) {
- printf("f32Tou32Slow failed on %.9g, expected 0x%X, actual 0x%X\n",
- fs, i, f32Tou32Slow(fs));
+ printf("f32Tou32Slow failed on %.9g, expected 0x%X, actual "
+ "0x%X\n",
+ fs, i, f32Tou32Slow(fs));
return false;
}
return true;
@@ -709,17 +723,152 @@ void TestSerialization::testFloatFormat()
}
const u8 TestSerialization::test_serialized_data[12 * 13 - 8] = {
- 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc,
- 0xdd, 0xee, 0xff, 0x80, 0x75, 0x30, 0xff, 0xff, 0xff, 0xfa, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xd5, 0x00, 0x00, 0xd1, 0x1e, 0xee, 0x1e,
- 0x5b, 0xc0, 0x80, 0x00, 0x02, 0x80, 0x7F, 0xFF, 0xFD, 0x80, 0x00, 0x07,
- 0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72, 0x21, 0x01, 0xf4, 0x01, 0xf4, 0x10,
- 0x6f, 0x02, 0x5c, 0xff, 0xe2, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00, 0x04,
- 0x38, 0xff, 0xff, 0xfe, 0x70, 0x00, 0x61, 0xa8, 0x36, 0x11, 0x51, 0x70,
- 0x5f, 0x00, 0x08, 0x00,
- 0x02, 0x00, 0x7e, 0x00, 'w', 0x00, 'o', 0x00, 'o', 0x00, 'f', 0x00, // \x02~woof~\x5455
- 0x7e, 0x54, 0x55, 0x00, 0x07, 0xa1, 0x20, 0x00, 0x98, 0xf5, 0x08, 0xff,
- 0xfd, 0x0f, 0xe4, 0xff, 0x80, 0x32, 0x80, 0x00, 0x00, 0x00, 0x17, 0x73,
- 0x6f, 0x6d,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