Skip to content

Commit 5c22a81

Browse files
committed
Add tests for serializer
1 parent 6d18824 commit 5c22a81

1 file changed

Lines changed: 82 additions & 0 deletions

File tree

tests/testSerializer.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright (c) 2016 - 2020 Settlers Freaks (sf-team at siedler25.org)
2+
//
3+
// This file is part of Return To The Roots.
4+
//
5+
// Return To The Roots is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 2 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// Return To The Roots 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 General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with Return To The Roots. If not, see <http://www.gnu.org/licenses/>.
17+
18+
#include "s25util/Serializer.h"
19+
#include <boost/test/unit_test.hpp>
20+
21+
BOOST_AUTO_TEST_SUITE(SerializerSuite)
22+
23+
BOOST_AUTO_TEST_CASE(PushPopSimpleTypes)
24+
{
25+
uint8_t u8 = 0x10;
26+
int8_t i8 = 0x11;
27+
uint16_t u16 = 0x1212;
28+
int16_t i16 = 0x1313;
29+
uint32_t u32 = 0x14141414;
30+
int32_t i32 = 0x15151515;
31+
uint64_t u64 = 0x1616161616161616;
32+
int64_t i64 = 0x1717171717171717;
33+
bool bt = true;
34+
bool bf = false;
35+
Serializer ser;
36+
ser.Push(u8);
37+
ser.Push(i8);
38+
ser.Push(u16);
39+
ser.Push(i16);
40+
ser.Push(u32);
41+
ser.Push(i32);
42+
ser.Push(u64);
43+
ser.Push(i64);
44+
ser.Push(bt);
45+
ser.Push(bf);
46+
BOOST_TEST_REQUIRE(ser.Pop<uint8_t>() == u8);
47+
BOOST_TEST_REQUIRE(ser.Pop<int8_t>() == i8);
48+
BOOST_TEST_REQUIRE(ser.Pop<uint16_t>() == u16);
49+
BOOST_TEST_REQUIRE(ser.Pop<int16_t>() == i16);
50+
BOOST_TEST_REQUIRE(ser.Pop<uint32_t>() == u32);
51+
BOOST_TEST_REQUIRE(ser.Pop<int32_t>() == i32);
52+
BOOST_TEST_REQUIRE(ser.Pop<uint64_t>() == u64);
53+
BOOST_TEST_REQUIRE(ser.Pop<int64_t>() == i64);
54+
BOOST_TEST_REQUIRE(ser.Pop<bool>() == bt);
55+
BOOST_TEST_REQUIRE(ser.Pop<bool>() == bf);
56+
}
57+
58+
BOOST_AUTO_TEST_CASE(PushPopVarSize)
59+
{
60+
Serializer ser;
61+
// Test corner cases of var size
62+
ser.PushVarSize(0);
63+
ser.PushVarSize(0x7F);
64+
ser.PushVarSize(0x80);
65+
ser.PushVarSize(0x3FFF);
66+
ser.PushVarSize(0x4000);
67+
ser.PushVarSize(0x1FFFFF);
68+
ser.PushVarSize(0xFFFFFFF);
69+
ser.PushVarSize(0x10000000);
70+
ser.PushVarSize(0xFFFFFFFF);
71+
BOOST_TEST_REQUIRE(ser.PopVarSize() == 0u);
72+
BOOST_TEST_REQUIRE(ser.PopVarSize() == 0x7Fu);
73+
BOOST_TEST_REQUIRE(ser.PopVarSize() == 0x80u);
74+
BOOST_TEST_REQUIRE(ser.PopVarSize() == 0x3FFFu);
75+
BOOST_TEST_REQUIRE(ser.PopVarSize() == 0x4000u);
76+
BOOST_TEST_REQUIRE(ser.PopVarSize() == 0x1FFFFFu);
77+
BOOST_TEST_REQUIRE(ser.PopVarSize() == 0xFFFFFFFu);
78+
BOOST_TEST_REQUIRE(ser.PopVarSize() == 0x10000000u);
79+
BOOST_TEST_REQUIRE(ser.PopVarSize() == 0xFFFFFFFFu);
80+
}
81+
82+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)