• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Return-To-The-Roots / libsiedler2 / 41320836336

01 Nov 2026 03:21PM UTC coverage: 86.26% (+3.0%) from 83.224%
41320836336

Pull #30

github

Flamefire
Add missing `noexcept` to PixelBuffer iterator functions
Pull Request #30: Add more tests

444 of 449 new or added lines in 18 files covered. (98.89%)

1 existing line in 1 file now uncovered.

4784 of 5546 relevant lines covered (86.26%)

4532.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.26
/tests/testText.cpp
1
// Copyright (C) 2005 - 2026 Settlers Freaks (sf-team at siedler25.org)
2
//
3
// SPDX-License-Identifier: GPL-2.0-or-later
4

5
#include "test/config.h"
6
#include "libsiedler2/Archiv.h"
7
#include "libsiedler2/ArchivItem_Text.h"
8
#include "libsiedler2/ErrorCodes.h"
9
#include "libsiedler2/libsiedler2.h"
10
#include <boost/filesystem.hpp>
11
#include <boost/test/unit_test.hpp>
12
#include <cstdlib>
13
#include <random>
14
#include <sstream>
15

16
namespace bfs = boost::filesystem;
17

18
namespace {
19
// LCOV_EXCL_START
20
struct FailBuf : std::streambuf
21
{
22
    int numActions = 0;
23
    int_type overflow(int_type) override
24
    {
25
        numActions++;
26
        return traits_type::eof();
27
    }
28
    int_type underflow() override
29
    {
30
        numActions++;
31
        return traits_type::eof();
32
    }
33
    std::streamsize xsputn(const char_type* /*s*/, std::streamsize /*count*/) override
34
    {
35
        numActions++;
36
        return traits_type::eof();
37
    }
38
    std::streamsize xsgetn(char_type* /*s*/, std::streamsize /*count*/) override
39
    {
40
        numActions++;
41
        return traits_type::eof();
42
    }
43
};
44
// LCOV_EXCL_STOP
45
} // namespace
46

47
BOOST_AUTO_TEST_SUITE(TextFiles)
48

49
static void createTxt(libsiedler2::Archiv& archiv, const bfs::path& outFile, unsigned numEntries)
3 ✔
50
{
51
    std::mt19937 rng{std::random_device{}()};
3 ✔
52
    std::uniform_int_distribution<> getRand(numEntries > 1 ? 0 : 3, 3); // Ignore randomness for single entries
3 ✔
53
    for(unsigned i = 0; i < numEntries; i++)
28 ✔
54
    {
55
        const auto what = getRand(rng);
25 ✔
56
        if(what == 0)
25 ✔
57
        {
58
            BOOST_TEST_MESSAGE("Adding empty entry " << i);
3 ✔
59
            archiv.push(nullptr);
3 ✔
60
        } else
61
        {
62
            libsiedler2::ArchivItem_Text txt;
44 ✔
63
            if(what == 1)
22 ✔
64
            {
65
                BOOST_TEST_MESSAGE("Adding duplicate entry " << i);
4 ✔
66
                txt.setText("Duplicate entry");
4 ✔
67
            } else if(what == 2)
18 ✔
68
                BOOST_TEST_MESSAGE("Adding empty string " << i);
9 ✔
69
            else // what == 3
70
            {
71
                BOOST_TEST_MESSAGE("Adding normal string " << i);
9 ✔
72
                std::stringstream ss;
18 ✔
73
                ss << "Example data " << i << " with\r\n some \r\n line breaks and special chars";
9 ✔
74
                std::string myText = ss.str();
18 ✔
75
                // Add an umlaut
76
                myText[myText.size() - 7] = '\xF6';
9 ✔
77
                txt.setText(myText);
9 ✔
78
            }
79
            archiv.pushC(txt);
22 ✔
80
        }
81
    }
82
    BOOST_TEST_REQUIRE(libsiedler2::Write(outFile, archiv) == 0);
3 ✔
83
}
3 ✔
84

85
BOOST_AUTO_TEST_CASE(ReadWriteENG)
2 ✔
86
{
87
    const bfs::path outFilepath = libsiedler2::test::outputPath / "outText.ENG";
2 ✔
88
    libsiedler2::Archiv archiv, archivIn;
2 ✔
89
    createTxt(archiv, outFilepath, 11);
1 ✔
90
    BOOST_TEST_REQUIRE(libsiedler2::Load(outFilepath, archivIn) == 0);
1 ✔
91
    BOOST_TEST_REQUIRE(archiv.size() == archivIn.size());
1 ✔
92
    for(unsigned i = 0; i < archiv.size(); i++)
12 ✔
93
    {
94
        const auto* txtOut = dynamic_cast<const libsiedler2::ArchivItem_Text*>(archiv[i]);
11 ✔
95
        const auto* txtIn = dynamic_cast<const libsiedler2::ArchivItem_Text*>(archivIn[i]);
11 ✔
96
        if(txtOut)
11 ✔
97
            BOOST_TEST(txtIn);
8 ✔
98
        else
99
            BOOST_TEST(!txtIn);
3 ✔
100
        if(txtIn && txtOut)
11 ✔
101
            BOOST_TEST(txtIn->getText() == txtOut->getText());
8 ✔
102
    }
103
}
1 ✔
104

105
BOOST_AUTO_TEST_CASE(ReadWriteGER)
2 ✔
106
{
107
    const bfs::path outFilepath = libsiedler2::test::outputPath / "outText.GER";
2 ✔
108
    libsiedler2::Archiv archiv, archivIn;
2 ✔
109
    createTxt(archiv, outFilepath, 13);
1 ✔
110
    BOOST_TEST_REQUIRE(libsiedler2::Load(outFilepath, archivIn) == 0);
1 ✔
111
    BOOST_TEST_REQUIRE(archiv.size() == archivIn.size());
1 ✔
112
    for(unsigned i = 0; i < archiv.size(); i++)
14 ✔
113
    {
114
        const auto* txtOut = dynamic_cast<const libsiedler2::ArchivItem_Text*>(archiv[i]);
13 ✔
115
        const auto* txtIn = dynamic_cast<const libsiedler2::ArchivItem_Text*>(archivIn[i]);
13 ✔
116
        if(txtOut)
13 ✔
117
            BOOST_TEST(txtIn);
13 ✔
118
        else
UNCOV
119
            BOOST_TEST(!txtIn);
×
120
        if(txtIn && txtOut)
13 ✔
121
            BOOST_TEST(txtIn->getText() == txtOut->getText());
13 ✔
122
    }
123
}
1 ✔
124

125
BOOST_AUTO_TEST_CASE(ReadWritePlain)
2 ✔
126
{
127
    const bfs::path outFilepath = libsiedler2::test::outputPath / "outTextPlain.GER";
2 ✔
128
    libsiedler2::Archiv archiv, archivIn;
2 ✔
129
    createTxt(archiv, outFilepath, 1);
1 ✔
130
    BOOST_TEST_REQUIRE(libsiedler2::Load(outFilepath, archivIn) == 0);
1 ✔
131
    BOOST_TEST_REQUIRE(archivIn.size() == 1u);
1 ✔
132
    const auto* txtOut = dynamic_cast<const libsiedler2::ArchivItem_Text*>(archiv[0]);
1 ✔
133
    const auto* txtIn = dynamic_cast<const libsiedler2::ArchivItem_Text*>(archivIn[0]);
1 ✔
134
    BOOST_TEST_REQUIRE((txtOut && txtIn));
1 ✔
135
    BOOST_TEST(txtIn->getText() == txtOut->getText());
1 ✔
136
}
1 ✔
137

138
BOOST_AUTO_TEST_CASE(ReadWriteSingleEmptyEntry)
2 ✔
139
{
140
    const bfs::path outFilepath = libsiedler2::test::outputPath / "outTextEmpty.ENG";
2 ✔
141
    libsiedler2::Archiv archiv, archivIn;
2 ✔
142
    archiv.push(nullptr);
1 ✔
143
    BOOST_TEST_REQUIRE(libsiedler2::Write(outFilepath, archiv) == 0);
1 ✔
144
    BOOST_TEST_REQUIRE(libsiedler2::Load(outFilepath, archivIn) == 0);
1 ✔
145
    BOOST_TEST_REQUIRE(archivIn.size() == 1u);
1 ✔
146
    BOOST_TEST_REQUIRE(!archivIn[0]);
1 ✔
147
}
1 ✔
148

149
BOOST_AUTO_TEST_CASE(ReadTxtAsLst)
2 ✔
150
{
151
    const bfs::path inFilepath = libsiedler2::test::inputPath / "txtAsLst.lst";
2 ✔
152
    libsiedler2::Archiv archiv;
2 ✔
153
    BOOST_TEST_REQUIRE(libsiedler2::Load(inFilepath, archiv) == 0);
1 ✔
154
    BOOST_TEST_REQUIRE(archiv.size() == 7u);
1 ✔
155
    for(unsigned i = 0; i < archiv.size(); i++)
8 ✔
156
    {
157
        const auto* txt = dynamic_cast<const libsiedler2::ArchivItem_Text*>(archiv[i]);
7 ✔
158
        // Item 4 is empty
159
        if(i == 4)
7 ✔
160
            BOOST_TEST_REQUIRE(!txt);
1 ✔
161
        else
162
        {
163
            BOOST_TEST_REQUIRE(txt);
6 ✔
164
            BOOST_TEST_REQUIRE(!txt->getText().empty());
6 ✔
165
        }
166
    }
167
}
1 ✔
168

169
BOOST_AUTO_TEST_CASE(ReadOriginalFiles)
2 ✔
170
{
171
    if(!libsiedler2::test::hasS2Data)
172
        return;
1 ✔
173
    for(const auto& fileEntry : bfs::recursive_directory_iterator(libsiedler2::test::s2Path))
174
    {
175
        if(is_regular_file(fileEntry.status()) && fileEntry.path().extension() == ".ENG")
176
        {
177
            BOOST_TEST_INFO_SCOPE("For " << fileEntry.path());
178
            libsiedler2::Archiv archiv;
179
            // Check only that they can be loaded and have at least 1 entry
180
            BOOST_TEST_REQUIRE(libsiedler2::Load(fileEntry.path(), archiv) == 0);
181
            BOOST_TEST_REQUIRE(archiv.size() > 0u);
182
            BOOST_TEST(archiv[0]);
183
        }
184
    }
185
}
186

187
BOOST_AUTO_TEST_CASE(LoadRemovesNullTerminator)
2 ✔
188
{
189
    std::stringstream ss;
2 ✔
190
    const char data[] = {'H', 'e', 'l', 'l', 'o', '\0'};
1 ✔
191
    ss.write(data, sizeof(data));
1 ✔
192
    libsiedler2::ArchivItem_Text txt;
2 ✔
193
    BOOST_TEST(txt.load(ss, false) == 0);
1 ✔
194
    BOOST_TEST(txt.getText() == "Hello");
1 ✔
195
}
1 ✔
196

197
BOOST_AUTO_TEST_CASE(LoadHonorsEndOfFileMarker)
2 ✔
198
{
199
    std::stringstream ss;
2 ✔
200
    ss << "Before\x16" // ASCII "SUB"
201
       << "After";
1 ✔
202
    libsiedler2::ArchivItem_Text txt;
2 ✔
203
    BOOST_TEST(txt.load(ss, true) == 0);
1 ✔
204
    BOOST_TEST(txt.getText() == "Before");
1 ✔
205
}
1 ✔
206

207
BOOST_AUTO_TEST_CASE(ReadWriteErrors)
2 ✔
208
{
209
    libsiedler2::ArchivItem_Text txt;
2 ✔
210
    std::stringstream badStream;
2 ✔
211
    badStream.setstate(std::ios::badbit);
1 ✔
212
    BOOST_TEST(txt.load(badStream) == libsiedler2::ErrorCode::FILE_NOT_ACCESSIBLE);
1 ✔
213
    BOOST_TEST(txt.write(badStream) == libsiedler2::ErrorCode::FILE_NOT_ACCESSIBLE);
1 ✔
214

215
    FailBuf buf;
2 ✔
216
    std::ostream os(&buf);
2 ✔
217
    txt.setText("test");
1 ✔
218
    BOOST_TEST(txt.write(os) == libsiedler2::ErrorCode::UNEXPECTED_EOF);
1 ✔
219
}
1 ✔
220

221
BOOST_AUTO_TEST_CASE(WriteEmptyTextReturnsNone)
2 ✔
222
{
223
    libsiedler2::ArchivItem_Text txt;
2 ✔
224
    txt.setText("");
1 ✔
225
    FailBuf buf;
2 ✔
226
    std::ostringstream os;
2 ✔
227
    static_cast<std::ostream&>(os).rdbuf(&buf);
1 ✔
228
    // An attempted write would fail, so this only succeeds if it really didn't even try to write anything
229
    BOOST_TEST(txt.write(os) == 0);
1 ✔
230
    BOOST_TEST(buf.numActions == 0);
1 ✔
231
}
1 ✔
232

233
BOOST_AUTO_TEST_CASE(GetFileTextWithAndWithoutConversion)
2 ✔
234
{
235
    libsiedler2::ArchivItem_Text txt;
2 ✔
236
    txt.setText("line1\nline2", false);
1 ✔
237
    BOOST_TEST(txt.getFileText(false) == "line1\nline2");
1 ✔
238
    BOOST_TEST(txt.getFileText(true) == "line1@@line2");
1 ✔
239

240
    txt.setText("hello@@world", true);
1 ✔
241
    BOOST_TEST(txt.getText() == "hello\nworld");
1 ✔
242
    txt.setText("hello@@world", false);
1 ✔
243
    BOOST_TEST(txt.getText() == "hello@@world");
1 ✔
244

245
    txt.setText("line1\r\nline2\r@@line3@@line4\rline5", true);
1 ✔
246
    BOOST_TEST(txt.getText() == "line1\n\nline2\n\nline3\nline4\nline5");
1 ✔
247
    txt.setText("line1\r\nline2\r@@line3@@line4\rline5", false);
1 ✔
248
    BOOST_TEST(txt.getText() == "line1\nline2\n@@line3@@line4\nline5");
1 ✔
249
}
1 ✔
250

251
BOOST_AUTO_TEST_SUITE_END()
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc