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

Return-To-The-Roots / libsiedler2 / 3862018572

pending completion
3862018572

push

github

Alexander Grund
CI: Update markdown-lint to 1.5.0

5060 of 6247 relevant lines covered (81.0%)

4336.08 hits per line

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

98.88
/tests/testText.cpp
1
// Copyright (C) 2005 - 2021 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/libsiedler2.h"
9
#include <boost/filesystem.hpp>
10
#include <boost/test/unit_test.hpp>
11
#include <cstdlib>
12
#include <random>
13

14
namespace bfs = boost::filesystem;
15

16
BOOST_AUTO_TEST_SUITE(TextFiles)
1✔
17

18
static void createTxt(libsiedler2::Archiv& archiv, const bfs::path& outFile, unsigned numEntries)
3✔
19
{
20
    std::mt19937 rng{std::random_device{}()};
3✔
21
    std::uniform_int_distribution<> getRand(numEntries > 1 ? 0 : 3, 3); // Ignore randomness for single entries
3✔
22
    for(unsigned i = 0; i < numEntries; i++)
28✔
23
    {
24
        const auto what = getRand(rng);
25✔
25
        if(what == 0)
25✔
26
        {
27
            BOOST_TEST_MESSAGE("Adding empty entry " << i);
4✔
28
            archiv.push(nullptr);
4✔
29
        } else
30
        {
31
            libsiedler2::ArchivItem_Text txt;
42✔
32
            if(what == 1)
21✔
33
            {
34
                BOOST_TEST_MESSAGE("Adding duplicate entry " << i);
6✔
35
                txt.setText("Duplicate entry");
6✔
36
            } else if(what == 2)
15✔
37
                BOOST_TEST_MESSAGE("Adding empty string " << i);
10✔
38
            else // what == 3
39
            {
40
                BOOST_TEST_MESSAGE("Adding normal string " << i);
5✔
41
                std::stringstream ss;
10✔
42
                ss << "Example data " << i << " with\r\n some \r\n line breaks and special chars";
5✔
43
                std::string myText = ss.str();
10✔
44
                // Add an umlaut
45
                myText[myText.size() - 7] = '\xF6';
5✔
46
                txt.setText(myText);
5✔
47
            }
48
            archiv.pushC(txt);
21✔
49
        }
50
    }
51
    BOOST_TEST_REQUIRE(libsiedler2::Write(outFile, archiv) == 0);
3✔
52
}
3✔
53

54
BOOST_AUTO_TEST_CASE(ReadWriteENG)
3✔
55
{
56
    const bfs::path outFilepath = libsiedler2::test::outputPath / "outText.ENG";
2✔
57
    libsiedler2::Archiv archiv, archivIn;
2✔
58
    createTxt(archiv, outFilepath, 11);
1✔
59
    BOOST_TEST_REQUIRE(libsiedler2::Load(outFilepath, archivIn) == 0);
1✔
60
    BOOST_TEST_REQUIRE(archiv.size() == archivIn.size());
1✔
61
    for(unsigned i = 0; i < archiv.size(); i++)
12✔
62
    {
63
        const auto* txtOut = dynamic_cast<const libsiedler2::ArchivItem_Text*>(archiv[i]);
11✔
64
        const auto* txtIn = dynamic_cast<const libsiedler2::ArchivItem_Text*>(archivIn[i]);
11✔
65
        if(txtOut)
11✔
66
            BOOST_TEST(txtIn);
11✔
67
        else
68
            BOOST_TEST(!txtIn);
×
69
        if(txtIn && txtOut)
11✔
70
            BOOST_TEST(txtIn->getText() == txtOut->getText());
11✔
71
    }
72
}
1✔
73

74
BOOST_AUTO_TEST_CASE(ReadWriteGER)
3✔
75
{
76
    const bfs::path outFilepath = libsiedler2::test::outputPath / "outText.GER";
2✔
77
    libsiedler2::Archiv archiv, archivIn;
2✔
78
    createTxt(archiv, outFilepath, 13);
1✔
79
    BOOST_TEST_REQUIRE(libsiedler2::Load(outFilepath, archivIn) == 0);
1✔
80
    BOOST_TEST_REQUIRE(archiv.size() == archivIn.size());
1✔
81
    for(unsigned i = 0; i < archiv.size(); i++)
14✔
82
    {
83
        const auto* txtOut = dynamic_cast<const libsiedler2::ArchivItem_Text*>(archiv[i]);
13✔
84
        const auto* txtIn = dynamic_cast<const libsiedler2::ArchivItem_Text*>(archivIn[i]);
13✔
85
        if(txtOut)
13✔
86
            BOOST_TEST(txtIn);
9✔
87
        else
88
            BOOST_TEST(!txtIn);
4✔
89
        if(txtIn && txtOut)
13✔
90
            BOOST_TEST(txtIn->getText() == txtOut->getText());
9✔
91
    }
92
}
1✔
93

94
BOOST_AUTO_TEST_CASE(ReadWritePlain)
3✔
95
{
96
    const bfs::path outFilepath = libsiedler2::test::outputPath / "outTextPlain.GER";
2✔
97
    libsiedler2::Archiv archiv, archivIn;
2✔
98
    createTxt(archiv, outFilepath, 1);
1✔
99
    BOOST_TEST_REQUIRE(libsiedler2::Load(outFilepath, archivIn) == 0);
1✔
100
    BOOST_TEST_REQUIRE(archivIn.size() == 1u);
1✔
101
    const auto* txtOut = dynamic_cast<const libsiedler2::ArchivItem_Text*>(archiv[0]);
1✔
102
    const auto* txtIn = dynamic_cast<const libsiedler2::ArchivItem_Text*>(archivIn[0]);
1✔
103
    BOOST_TEST_REQUIRE((txtOut && txtIn));
1✔
104
    BOOST_TEST(txtIn->getText() == txtOut->getText());
1✔
105
}
1✔
106

107
BOOST_AUTO_TEST_CASE(ReadWriteSingleEmptyEntry)
3✔
108
{
109
    const bfs::path outFilepath = libsiedler2::test::outputPath / "outTextEmpty.ENG";
2✔
110
    libsiedler2::Archiv archiv, archivIn;
2✔
111
    archiv.push(nullptr);
1✔
112
    BOOST_TEST_REQUIRE(libsiedler2::Write(outFilepath, archiv) == 0);
1✔
113
    BOOST_TEST_REQUIRE(libsiedler2::Load(outFilepath, archivIn) == 0);
1✔
114
    BOOST_TEST_REQUIRE(archivIn.size() == 1u);
1✔
115
    BOOST_TEST_REQUIRE(!archivIn[0]);
1✔
116
}
1✔
117

118
BOOST_AUTO_TEST_CASE(ReadTxtAsLst)
3✔
119
{
120
    const bfs::path inFilepath = libsiedler2::test::inputPath / "txtAsLst.lst";
2✔
121
    libsiedler2::Archiv archiv;
2✔
122
    BOOST_TEST_REQUIRE(libsiedler2::Load(inFilepath, archiv) == 0);
1✔
123
    BOOST_TEST_REQUIRE(archiv.size() == 7u);
1✔
124
    for(unsigned i = 0; i < archiv.size(); i++)
8✔
125
    {
126
        const auto* txt = dynamic_cast<const libsiedler2::ArchivItem_Text*>(archiv[i]);
7✔
127
        // Item 4 is empty
128
        if(i == 4)
7✔
129
            BOOST_TEST_REQUIRE(!txt);
1✔
130
        else
131
        {
132
            BOOST_TEST_REQUIRE(txt);
6✔
133
            BOOST_TEST_REQUIRE(!txt->getText().empty());
6✔
134
        }
135
    }
136
}
1✔
137

138
BOOST_AUTO_TEST_CASE(ReadOriginalFiles)
3✔
139
{
140
    if(!libsiedler2::test::hasS2Data)
141
        return;
1✔
142
    for(const auto& fileEntry : bfs::recursive_directory_iterator(libsiedler2::test::s2Path))
143
    {
144
        if(is_regular_file(fileEntry.status()) && fileEntry.path().extension() == ".ENG")
145
        {
146
            BOOST_TEST_INFO_SCOPE("For " << fileEntry.path());
147
            libsiedler2::Archiv archiv;
148
            // Check only that they can be loaded and have at least 1 entry
149
            BOOST_TEST_REQUIRE(libsiedler2::Load(fileEntry.path(), archiv) == 0);
150
            BOOST_TEST_REQUIRE(archiv.size() > 0u);
151
            BOOST_TEST(archiv[0]);
152
        }
153
    }
154
}
155

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

© 2026 Coveralls, Inc