aboutsummaryrefslogtreecommitdiff
path: root/src/unittest
diff options
context:
space:
mode:
Diffstat (limited to 'src/unittest')
-rw-r--r--src/unittest/CMakeLists.txt1
-rw-r--r--src/unittest/test.cpp6
-rw-r--r--src/unittest/test_areastore.cpp31
-rw-r--r--src/unittest/test_inventory.cpp89
-rw-r--r--src/unittest/test_irrptr.cpp131
-rw-r--r--src/unittest/test_servermodmanager.cpp5
-rw-r--r--src/unittest/test_world/do_not_remove.txt0
-rw-r--r--src/unittest/test_world/world.mt1
8 files changed, 197 insertions, 67 deletions
diff --git a/src/unittest/CMakeLists.txt b/src/unittest/CMakeLists.txt
index 71aa1fa56..82f9a4a13 100644
--- a/src/unittest/CMakeLists.txt
+++ b/src/unittest/CMakeLists.txt
@@ -10,6 +10,7 @@ set (UNITTEST_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/test_connection.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_filepath.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_inventory.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/test_irrptr.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_map_settings_manager.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_mapnode.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_modchannels.cpp
diff --git a/src/unittest/test.cpp b/src/unittest/test.cpp
index 3ac8ffb19..a783ccd32 100644
--- a/src/unittest/test.cpp
+++ b/src/unittest/test.cpp
@@ -650,12 +650,12 @@ struct TestMapSector: public TestBase
// Create one with no heightmaps
ServerMapSector sector(&parent, v2s16(1,1));
- UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
- UASSERT(sector.getBlockNoCreateNoEx(1) == 0);
+ UASSERT(sector.getBlockNoCreateNoEx(0) == nullptr);
+ UASSERT(sector.getBlockNoCreateNoEx(1) == nullptr);
MapBlock * bref = sector.createBlankBlock(-2);
- UASSERT(sector.getBlockNoCreateNoEx(0) == 0);
+ UASSERT(sector.getBlockNoCreateNoEx(0) == nullptr);
UASSERT(sector.getBlockNoCreateNoEx(-2) == bref);
//TODO: Check for AlreadyExistsException
diff --git a/src/unittest/test_areastore.cpp b/src/unittest/test_areastore.cpp
index 62d446f5c..691cd69d2 100644
--- a/src/unittest/test_areastore.cpp
+++ b/src/unittest/test_areastore.cpp
@@ -128,11 +128,11 @@ void TestAreaStore::testSerialization()
VectorAreaStore store;
Area a(v3s16(-1, 0, 1), v3s16(0, 1, 2));
- a.data = "Area A";
+ a.data = "Area AA";
store.insertArea(&a);
Area b(v3s16(123, 456, 789), v3s16(32000, 100, 10));
- b.data = "Area B";
+ b.data = "Area BB";
store.insertArea(&b);
std::ostringstream os;
@@ -143,20 +143,31 @@ void TestAreaStore::testSerialization()
"\x00\x02" // Count
"\xFF\xFF\x00\x00\x00\x01" // Area A min edge
"\x00\x00\x00\x01\x00\x02" // Area A max edge
- "\x00\x06" // Area A data length
- "Area A" // Area A data
+ "\x00\x07" // Area A data length
+ "Area AA" // Area A data
"\x00\x7B\x00\x64\x00\x0A" // Area B min edge (last two swapped with max edge for sorting)
"\x7D\x00\x01\xC8\x03\x15" // Area B max edge (^)
- "\x00\x06" // Area B data length
- "Area B", // Area B data
+ "\x00\x07" // Area B data length
+ "Area BB" // Area B data
+ "\x00\x00\x00\x00" // ID A = 0
+ "\x00\x00\x00\x01", // ID B = 1
1 + 2 +
- 6 + 6 + 2 + 6 +
- 6 + 6 + 2 + 6);
- UASSERTEQ(std::string, str, str_wanted);
+ (6 + 6 + 2 + 7) * 2 + // min/max edge, length, data
+ 2 * 4); // Area IDs
+
+ UASSERTEQ(const std::string &, str, str_wanted);
std::istringstream is(str);
store.deserialize(is);
- UASSERTEQ(size_t, store.size(), 4); // deserialize() doesn't clear the store
+ // deserialize() doesn't clear the store
+ // But existing IDs are overridden
+ UASSERTEQ(size_t, store.size(), 2);
+
+ Area c(v3s16(33, -2, -6), v3s16(4, 77, -76));
+ c.data = "Area CC";
+ store.insertArea(&c);
+
+ UASSERTEQ(u32, c.id, 2);
}
diff --git a/src/unittest/test_inventory.cpp b/src/unittest/test_inventory.cpp
index 1a783afae..5f71636c4 100644
--- a/src/unittest/test_inventory.cpp
+++ b/src/unittest/test_inventory.cpp
@@ -33,8 +33,9 @@ public:
void testSerializeDeserialize(IItemDefManager *idef);
- static const char *serialized_inventory;
- static const char *serialized_inventory_2;
+ static const char *serialized_inventory_in;
+ static const char *serialized_inventory_out;
+ static const char *serialized_inventory_inc;
};
static TestInventory g_test_instance;
@@ -49,7 +50,7 @@ void TestInventory::runTests(IGameDef *gamedef)
void TestInventory::testSerializeDeserialize(IItemDefManager *idef)
{
Inventory inv(idef);
- std::istringstream is(serialized_inventory, std::ios::binary);
+ std::istringstream is(serialized_inventory_in, std::ios::binary);
inv.deSerialize(is);
UASSERT(inv.getList("0"));
@@ -62,82 +63,64 @@ void TestInventory::testSerializeDeserialize(IItemDefManager *idef)
inv.getList("main")->setWidth(5);
std::ostringstream inv_os(std::ios::binary);
- inv.serialize(inv_os);
- UASSERTEQ(std::string, inv_os.str(), serialized_inventory_2);
+ inv.serialize(inv_os, false);
+ UASSERTEQ(std::string, inv_os.str(), serialized_inventory_out);
+
+ inv.setModified(false);
+ inv_os.str("");
+ inv_os.clear();
+ inv.serialize(inv_os, true);
+ UASSERTEQ(std::string, inv_os.str(), serialized_inventory_inc);
+
+ ItemStack leftover = inv.getList("main")->takeItem(7, 99 - 12);
+ ItemStack wanted = ItemStack("default:dirt", 99 - 12, 0, idef);
+ UASSERT(leftover == wanted);
+ leftover = inv.getList("main")->getItem(7);
+ wanted.count = 12;
+ UASSERT(leftover == wanted);
}
-const char *TestInventory::serialized_inventory =
- "List 0 32\n"
+const char *TestInventory::serialized_inventory_in =
+ "List 0 10\n"
"Width 3\n"
"Empty\n"
"Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
"Item default:cobble 61\n"
"Empty\n"
"Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
"Item default:dirt 71\n"
"Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
"Item default:dirt 99\n"
"Item default:cobble 38\n"
"Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
+ "EndInventoryList\n"
+ "List abc 1\n"
+ "Item default:stick 3\n"
+ "Width 0\n"
"EndInventoryList\n"
"EndInventory\n";
-const char *TestInventory::serialized_inventory_2 =
- "List main 32\n"
+const char *TestInventory::serialized_inventory_out =
+ "List main 10\n"
"Width 5\n"
"Empty\n"
"Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
"Item default:cobble 61\n"
"Empty\n"
"Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
"Item default:dirt 71\n"
"Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
"Item default:dirt 99\n"
"Item default:cobble 38\n"
"Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
- "Empty\n"
"EndInventoryList\n"
+ "List abc 1\n"
+ "Width 0\n"
+ "Item default:stick 3\n"
+ "EndInventoryList\n"
+ "EndInventory\n";
+
+const char *TestInventory::serialized_inventory_inc =
+ "KeepList main\n"
+ "KeepList abc\n"
"EndInventory\n";
diff --git a/src/unittest/test_irrptr.cpp b/src/unittest/test_irrptr.cpp
new file mode 100644
index 000000000..aa857ff46
--- /dev/null
+++ b/src/unittest/test_irrptr.cpp
@@ -0,0 +1,131 @@
+/*
+Minetest
+Copyright (C) 2018 numzero, Lobachevskiy Vitaliy <numzer0@yandex.ru>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program 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. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "test.h"
+
+#include "exceptions.h"
+#include "irr_ptr.h"
+
+class TestIrrPtr : public TestBase
+{
+public:
+ TestIrrPtr() { TestManager::registerTestModule(this); }
+ const char *getName() { return "TestIrrPtr"; }
+
+ void runTests(IGameDef *gamedef);
+
+ void testRefCounting();
+ void testSelfAssignment();
+ void testNullHandling();
+};
+
+static TestIrrPtr g_test_instance;
+
+void TestIrrPtr::runTests(IGameDef *gamedef)
+{
+ TEST(testRefCounting);
+ TEST(testSelfAssignment);
+ TEST(testNullHandling);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+#define UASSERT_REFERENCE_COUNT(object, value, info) \
+ UTEST((object)->getReferenceCount() == value, \
+ info "Reference count is %d instead of " #value, \
+ (object)->getReferenceCount())
+
+void TestIrrPtr::testRefCounting()
+{
+ IReferenceCounted *obj = new IReferenceCounted(); // RC=1
+ obj->grab();
+ UASSERT_REFERENCE_COUNT(obj, 2, "Pre-condition failed: ");
+ {
+ irr_ptr<IReferenceCounted> p1{obj}; // move semantics
+ UASSERT(p1.get() == obj);
+ UASSERT_REFERENCE_COUNT(obj, 2, );
+
+ irr_ptr<IReferenceCounted> p2{p1}; // copy ctor
+ UASSERT(p1.get() == obj);
+ UASSERT(p2.get() == obj);
+ UASSERT_REFERENCE_COUNT(obj, 3, );
+
+ irr_ptr<IReferenceCounted> p3{std::move(p1)}; // move ctor
+ UASSERT(p1.get() == nullptr);
+ UASSERT(p3.get() == obj);
+ UASSERT_REFERENCE_COUNT(obj, 3, );
+
+ p1 = std::move(p2); // move assignment
+ UASSERT(p1.get() == obj);
+ UASSERT(p2.get() == nullptr);
+ UASSERT_REFERENCE_COUNT(obj, 3, );
+
+ p2 = p3; // copy assignment
+ UASSERT(p2.get() == obj);
+ UASSERT(p3.get() == obj);
+ UASSERT_REFERENCE_COUNT(obj, 4, );
+
+ p1.release();
+ UASSERT(p1.get() == nullptr);
+ UASSERT_REFERENCE_COUNT(obj, 4, );
+ }
+ UASSERT_REFERENCE_COUNT(obj, 2, );
+ obj->drop();
+ UTEST(obj->drop(), "Dropping failed: reference count is %d",
+ obj->getReferenceCount());
+}
+
+void TestIrrPtr::testSelfAssignment()
+{
+ irr_ptr<IReferenceCounted> p1{new IReferenceCounted()};
+ UASSERT(p1);
+ UASSERT_REFERENCE_COUNT(p1, 1, );
+ p1 = p1;
+ UASSERT(p1);
+ UASSERT_REFERENCE_COUNT(p1, 1, );
+ p1 = std::move(p1);
+ UASSERT(p1);
+ UASSERT_REFERENCE_COUNT(p1, 1, );
+}
+
+void TestIrrPtr::testNullHandling()
+{
+ // In the case of an error, it will probably crash with SEGV.
+ // Nevertheless, UASSERTs are used to catch possible corner cases.
+ irr_ptr<IReferenceCounted> p1{new IReferenceCounted()};
+ UASSERT(p1);
+ irr_ptr<IReferenceCounted> p2;
+ UASSERT(!p2);
+ irr_ptr<IReferenceCounted> p3{p2};
+ UASSERT(!p2);
+ UASSERT(!p3);
+ irr_ptr<IReferenceCounted> p4{std::move(p2)};
+ UASSERT(!p2);
+ UASSERT(!p4);
+ p2 = p2;
+ UASSERT(!p2);
+ p2 = std::move(p2);
+ UASSERT(!p2);
+ p3 = p2;
+ UASSERT(!p2);
+ UASSERT(!p3);
+ p3 = std::move(p2);
+ UASSERT(!p2);
+ UASSERT(!p3);
+}
diff --git a/src/unittest/test_servermodmanager.cpp b/src/unittest/test_servermodmanager.cpp
index 72ac7c6bf..0757323f4 100644
--- a/src/unittest/test_servermodmanager.cpp
+++ b/src/unittest/test_servermodmanager.cpp
@@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "test.h"
#include <algorithm>
#include "server/mods.h"
+#include "settings.h"
#include "test_config.h"
class TestServerModManager : public TestBase
@@ -85,6 +86,10 @@ void TestServerModManager::runTests(IGameDef *gamedef)
void TestServerModManager::testCreation()
{
+ std::string path = std::string(TEST_WORLDDIR) + DIR_DELIM + "world.mt";
+ Settings world_config;
+ world_config.set("gameid", "minimal");
+ UASSERTEQ(bool, world_config.updateConfigFile(path.c_str()), true);
ServerModManager sm(TEST_WORLDDIR);
}
diff --git a/src/unittest/test_world/do_not_remove.txt b/src/unittest/test_world/do_not_remove.txt
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/src/unittest/test_world/do_not_remove.txt
diff --git a/src/unittest/test_world/world.mt b/src/unittest/test_world/world.mt
deleted file mode 100644
index ab9b5413a..000000000
--- a/src/unittest/test_world/world.mt
+++ /dev/null
@@ -1 +0,0 @@
-gameid = minimal