aboutsummaryrefslogtreecommitdiff
path: root/src/unittest
diff options
context:
space:
mode:
Diffstat (limited to 'src/unittest')
-rw-r--r--src/unittest/test_compression.cpp63
-rw-r--r--src/unittest/test_settings.cpp37
-rw-r--r--src/unittest/test_utilities.cpp20
3 files changed, 120 insertions, 0 deletions
diff --git a/src/unittest/test_compression.cpp b/src/unittest/test_compression.cpp
index 7d0378131..dfcadd4b2 100644
--- a/src/unittest/test_compression.cpp
+++ b/src/unittest/test_compression.cpp
@@ -37,6 +37,8 @@ public:
void testRLECompression();
void testZlibCompression();
void testZlibLargeData();
+ void testZlibLimit();
+ void _testZlibLimit(u32 size, u32 limit);
};
static TestCompression g_test_instance;
@@ -46,6 +48,7 @@ void TestCompression::runTests(IGameDef *gamedef)
TEST(testRLECompression);
TEST(testZlibCompression);
TEST(testZlibLargeData);
+ TEST(testZlibLimit);
}
////////////////////////////////////////////////////////////////////////////////
@@ -170,3 +173,63 @@ void TestCompression::testZlibLargeData()
i, str_decompressed[i], i, data_in[i]);
}
}
+
+void TestCompression::testZlibLimit()
+{
+ // edge cases
+ _testZlibLimit(1024, 1023);
+ _testZlibLimit(1024, 1024);
+ _testZlibLimit(1024, 1025);
+
+ // test around buffer borders
+ u32 bufsize = 16384; // as in implementation
+ for (int s = -1; s <= 1; s++)
+ {
+ for (int l = -1; l <= 1; l++)
+ {
+ _testZlibLimit(bufsize + s, bufsize + l);
+ }
+ }
+ // span multiple buffers
+ _testZlibLimit(35000, 22000);
+ _testZlibLimit(22000, 35000);
+}
+
+void TestCompression::_testZlibLimit(u32 size, u32 limit)
+{
+ infostream << "Test: Testing zlib wrappers with a decompression "
+ "memory limit of " << limit << std::endl;
+
+ infostream << "Test: Input size of compressZlib for limit is "
+ << size << std::endl;
+
+ // how much data we expect to get
+ u32 expected = size < limit ? size : limit;
+
+ // create recognizable data
+ std::string data_in;
+ data_in.resize(size);
+ for (u32 i = 0; i < size; i++)
+ data_in[i] = (u8)(i % 256);
+
+ std::ostringstream os_compressed(std::ios::binary);
+ compressZlib(data_in, os_compressed);
+ infostream << "Test: Output size of compressZlib for limit is "
+ << os_compressed.str().size()<<std::endl;
+
+ std::istringstream is_compressed(os_compressed.str(), std::ios::binary);
+ std::ostringstream os_decompressed(std::ios::binary);
+ decompressZlib(is_compressed, os_decompressed, limit);
+ infostream << "Test: Output size of decompressZlib with limit is "
+ << os_decompressed.str().size() << std::endl;
+
+ std::string str_decompressed = os_decompressed.str();
+ UASSERTEQ(size_t, str_decompressed.size(), expected);
+
+ for (u32 i = 0; i < size && i < str_decompressed.size(); i++) {
+ UTEST(str_decompressed[i] == data_in[i],
+ "index out[%i]=%i differs from in[%i]=%i",
+ i, str_decompressed[i], i, data_in[i]);
+ }
+}
+
diff --git a/src/unittest/test_settings.cpp b/src/unittest/test_settings.cpp
index b2666559e..aa56f3e06 100644
--- a/src/unittest/test_settings.cpp
+++ b/src/unittest/test_settings.cpp
@@ -31,6 +31,7 @@ public:
void runTests(IGameDef *gamedef);
void testAllSettings();
+ void testFlagDesc();
static const char *config_text_before;
static const std::string config_text_after;
@@ -41,6 +42,7 @@ static TestSettings g_test_instance;
void TestSettings::runTests(IGameDef *gamedef)
{
TEST(testAllSettings);
+ TEST(testFlagDesc);
}
////////////////////////////////////////////////////////////////////////////////
@@ -206,3 +208,38 @@ void TestSettings::testAllSettings()
UASSERT(!"Setting not found!");
}
}
+
+void TestSettings::testFlagDesc()
+{
+ Settings s;
+ FlagDesc flagdesc[] = {
+ { "biomes", 0x01 },
+ { "trees", 0x02 },
+ { "jungles", 0x04 },
+ { "oranges", 0x08 },
+ { "tables", 0x10 },
+ { nullptr, 0 }
+ };
+
+ // Enabled: biomes, jungles, oranges (default)
+ s.setDefault("test_desc", flagdesc, readFlagString(
+ "biomes,notrees,jungles,oranges", flagdesc, nullptr));
+ UASSERT(s.getFlagStr("test_desc", flagdesc, nullptr) == (0x01 | 0x04 | 0x08));
+
+ // Enabled: jungles, oranges, tables
+ s.set("test_desc", "nobiomes,tables");
+ UASSERT(s.getFlagStr("test_desc", flagdesc, nullptr) == (0x04 | 0x08 | 0x10));
+
+ // Enabled: (nothing)
+ s.set("test_desc", "nobiomes,nojungles,nooranges,notables");
+ UASSERT(s.getFlagStr("test_desc", flagdesc, nullptr) == 0x00);
+
+ // Numeric flag tests (override)
+ // Enabled: trees, tables
+ s.setDefault("test_flags", flagdesc, 0x02 | 0x10);
+ UASSERT(s.getFlagStr("test_flags", flagdesc, nullptr) == (0x02 | 0x10));
+
+ // Enabled: tables
+ s.set("test_flags", "16");
+ UASSERT(s.getFlagStr("test_flags", flagdesc, nullptr) == 0x10);
+}
diff --git a/src/unittest/test_utilities.cpp b/src/unittest/test_utilities.cpp
index 8e8958d18..447b591e1 100644
--- a/src/unittest/test_utilities.cpp
+++ b/src/unittest/test_utilities.cpp
@@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "test.h"
#include <cmath>
+#include "util/enriched_string.h"
#include "util/numeric.h"
#include "util/string.h"
@@ -49,6 +50,7 @@ public:
void testUTF8();
void testRemoveEscapes();
void testWrapRows();
+ void testEnrichedString();
void testIsNumber();
void testIsPowerOfTwo();
void testMyround();
@@ -79,6 +81,7 @@ void TestUtilities::runTests(IGameDef *gamedef)
TEST(testUTF8);
TEST(testRemoveEscapes);
TEST(testWrapRows);
+ TEST(testEnrichedString);
TEST(testIsNumber);
TEST(testIsPowerOfTwo);
TEST(testMyround);
@@ -344,6 +347,23 @@ void TestUtilities::testWrapRows()
}
}
+void TestUtilities::testEnrichedString()
+{
+ EnrichedString str(L"Test bar");
+ irr::video::SColor color(0xFF, 0, 0, 0xFF);
+
+ UASSERT(str.substr(1, 3).getString() == L"est");
+ str += L" BUZZ";
+ UASSERT(str.substr(9, std::string::npos).getString() == L"BUZZ");
+ str.setDefaultColor(color); // Blue foreground
+ UASSERT(str.getColors()[5] == color);
+ // Green background, then white and yellow text
+ str = L"\x1b(b@#0F0)Regular \x1b(c@#FF0)yellow";
+ UASSERT(str.getColors()[2] == 0xFFFFFFFF);
+ str.setDefaultColor(color); // Blue foreground
+ UASSERT(str.getColors()[13] == 0xFFFFFF00); // Still yellow text
+ UASSERT(str.getBackground() == 0xFF00FF00); // Green background
+}
void TestUtilities::testIsNumber()
{