OpenTTD Source: src/tests/string_builder.cpp Source File

OpenTTD Source 15.3

string_builder.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "../stdafx.h"
11#include "../3rdparty/catch2/catch.hpp"
12#include "../core/string_builder.hpp"
13#include "../safeguards.h"
14
15TEST_CASE("StringBuilder - basic")
16{
17 std::string buffer;
18 StringBuilder builder(buffer);
19
20 CHECK(!builder.AnyBytesWritten());
21 CHECK(builder.GetBytesWritten() == 0);
22 CHECK(builder.GetWrittenData() == ""sv);
23
24 builder.Put("ab");
25 builder += "cdef";
26
27 CHECK(builder.AnyBytesWritten());
28 CHECK(builder.GetBytesWritten() == 6);
29 CHECK(builder.GetWrittenData() == "abcdef"sv);
30
31 CHECK(buffer == "abcdef"sv);
32}
33
34TEST_CASE("StringBuilder - binary")
35{
36 std::string buffer;
37 StringBuilder builder(buffer);
38
39 builder.PutUint8(1);
40 builder.PutSint8(-1);
41 builder.PutUint16LE(0x201);
42 builder.PutSint16LE(-0x201);
43 builder.PutUint32LE(0x30201);
44 builder.PutSint32LE(-0x30201);
45 builder.PutUint64LE(0x7060504030201);
46 builder.PutSint64LE(-0x7060504030201);
47
48 CHECK(buffer == "\x01\xFF\x01\x02\xFF\xFD\x01\x02\x03\x00\xFF\xFD\xFC\xFF\x01\x02\x03\04\x05\x06\x07\x00\xFF\xFD\xFC\xFB\xFA\xF9\xF8\xFF"sv);
49}
50
51TEST_CASE("StringBuilder - text")
52{
53 std::string buffer;
54 StringBuilder builder(buffer);
55
56 builder.PutChar('a');
57 builder.PutUtf8(0x1234);
58 builder.PutChar(' ');
59 builder.PutIntegerBase<uint32_t>(1234, 10);
60 builder.PutChar(' ');
61 builder.PutIntegerBase<uint32_t>(0x7FFF, 16);
62 builder.PutChar(' ');
63 builder.PutIntegerBase<int32_t>(-1234, 10);
64 builder.PutChar(' ');
65 builder.PutIntegerBase<int32_t>(-0x7FFF, 16);
66 builder.PutChar(' ');
67 builder.PutIntegerBase<uint64_t>(1'234'567'890'123, 10);
68 builder.PutChar(' ');
69 builder.PutIntegerBase<uint64_t>(0x1234567890, 16);
70 builder.PutChar(' ');
71 builder.PutIntegerBase<int64_t>(-1'234'567'890'123, 10);
72 builder.PutChar(' ');
73 builder.PutIntegerBase<int64_t>(-0x1234567890, 16);
74
75 CHECK(buffer == "a\u1234 1234 7fff -1234 -7fff 1234567890123 1234567890 -1234567890123 -1234567890"sv);
76}
StringBuilder
Compose data into a growing std::string.
Definition string_builder.hpp:65