OpenTTD Source 20250522-master-g467f832c2f
string_inplace.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 <http://www.gnu.org/licenses/>.
6 */
7
10#include "../stdafx.h"
11#include "../3rdparty/catch2/catch.hpp"
12#include "../core/string_inplace.hpp"
13#include "../safeguards.h"
14
15TEST_CASE("InPlaceReplacement")
16{
17 std::array<char, 4> buffer{1, 2, 3, 4};
18 InPlaceReplacement inplace(buffer);
19
20 CHECK(!inplace.builder.AnyBytesWritten());
21 CHECK(inplace.builder.GetBytesWritten() == 0);
22 CHECK(inplace.builder.GetWrittenData() == ""sv);
23 CHECK(!inplace.builder.AnyBytesUnused());
24 CHECK(inplace.builder.GetBytesUnused() == 0);
25 CHECK(!inplace.consumer.AnyBytesRead());
26 CHECK(inplace.consumer.GetBytesRead() == 0);
27 CHECK(inplace.consumer.AnyBytesLeft());
28 CHECK(inplace.consumer.GetBytesLeft() == 4);
29
30 CHECK(inplace.consumer.ReadUint16LE() == 0x201);
31
32 CHECK(inplace.builder.GetBytesWritten() == 0);
33 CHECK(inplace.builder.GetBytesUnused() == 2);
34 CHECK(inplace.consumer.GetBytesRead() == 2);
35 CHECK(inplace.consumer.GetBytesLeft() == 2);
36
37 inplace.builder.PutUint8(11);
38
39 CHECK(inplace.builder.GetBytesWritten() == 1);
40 CHECK(inplace.builder.GetBytesUnused() == 1);
41 CHECK(inplace.consumer.GetBytesRead() == 2);
42 CHECK(inplace.consumer.GetBytesLeft() == 2);
43
44 inplace.builder.PutUint8(12);
45
46 CHECK(inplace.builder.GetBytesWritten() == 2);
47 CHECK(inplace.builder.GetBytesUnused() == 0);
48 CHECK(inplace.consumer.GetBytesRead() == 2);
49 CHECK(inplace.consumer.GetBytesLeft() == 2);
50
51 CHECK(buffer[0] == 11);
52 CHECK(buffer[1] == 12);
53 CHECK(buffer[2] == 3);
54 CHECK(buffer[3] == 4);
55}
Compose data into a fixed size buffer, which is consumed at the same time.