aboutsummaryrefslogtreecommitdiff
path: root/src/unittest
diff options
context:
space:
mode:
Diffstat (limited to 'src/unittest')
-rw-r--r--src/unittest/CMakeLists.txt6
-rw-r--r--src/unittest/test.cpp6
-rw-r--r--src/unittest/test_address.cpp2
-rw-r--r--src/unittest/test_config.h.in1
-rw-r--r--src/unittest/test_connection.cpp10
-rw-r--r--src/unittest/test_eventmanager.cpp4
-rw-r--r--src/unittest/test_gettext.cpp43
-rw-r--r--src/unittest/test_irrptr.cpp4
-rw-r--r--src/unittest/test_lua.cpp79
-rw-r--r--src/unittest/test_map.cpp68
-rw-r--r--src/unittest/test_mod/test_mod/init.lua1
-rw-r--r--src/unittest/test_mod/test_mod/mod.conf2
-rw-r--r--src/unittest/test_modmetadatadatabase.cpp253
-rw-r--r--src/unittest/test_player.cpp39
-rw-r--r--src/unittest/test_server_shutdown_state.cpp4
-rw-r--r--src/unittest/test_servermodmanager.cpp21
-rw-r--r--src/unittest/test_socket.cpp18
-rw-r--r--src/unittest/test_utilities.cpp36
-rw-r--r--src/unittest/test_voxelarea.cpp18
19 files changed, 535 insertions, 80 deletions
diff --git a/src/unittest/CMakeLists.txt b/src/unittest/CMakeLists.txt
index 5703b8906..84f769e87 100644
--- a/src/unittest/CMakeLists.txt
+++ b/src/unittest/CMakeLists.txt
@@ -11,14 +11,16 @@ set (UNITTEST_SRCS
${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_lua.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/test_map.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_map_settings_manager.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_mapnode.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_modchannels.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/test_modmetadatadatabase.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_nodedef.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_noderesolver.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_noise.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_objdef.cpp
- ${CMAKE_CURRENT_SOURCE_DIR}/test_player.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_profiler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_random.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_schematic.cpp
@@ -33,6 +35,7 @@ set (UNITTEST_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/test_voxelarea.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_voxelalgorithms.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_voxelmanipulator.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/test_gettext.cpp
PARENT_SCOPE)
set (UNITTEST_CLIENT_SRCS
@@ -44,6 +47,7 @@ set (UNITTEST_CLIENT_SRCS
set (TEST_WORLDDIR ${CMAKE_CURRENT_SOURCE_DIR}/test_world)
set (TEST_SUBGAME_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../games/devtest)
+set (TEST_MOD_PATH ${CMAKE_CURRENT_SOURCE_DIR}/test_mod)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/test_config.h.in"
diff --git a/src/unittest/test.cpp b/src/unittest/test.cpp
index d4841d559..4fd4b930b 100644
--- a/src/unittest/test.cpp
+++ b/src/unittest/test.cpp
@@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "gamedef.h"
#include "modchannels.h"
#include "content/mods.h"
+#include "database/database-dummy.h"
#include "util/numeric.h"
#include "porting.h"
@@ -57,6 +58,7 @@ public:
scene::ISceneManager *getSceneManager() { return m_scenemgr; }
IRollbackManager *getRollbackManager() { return m_rollbackmgr; }
EmergeManager *getEmergeManager() { return m_emergemgr; }
+ ModMetadataDatabase *getModStorageDatabase() { return m_mod_storage_database; }
scene::IAnimatedMesh *getMesh(const std::string &filename) { return NULL; }
bool checkLocalPrivilege(const std::string &priv) { return false; }
@@ -70,7 +72,6 @@ public:
return testmodspec;
}
virtual const ModSpec* getModSpec(const std::string &modname) const { return NULL; }
- virtual std::string getModStoragePath() const { return "."; }
virtual bool registerModStorage(ModMetadata *meta) { return true; }
virtual void unregisterModStorage(const std::string &name) {}
bool joinModChannel(const std::string &channel);
@@ -91,11 +92,13 @@ private:
scene::ISceneManager *m_scenemgr = nullptr;
IRollbackManager *m_rollbackmgr = nullptr;
EmergeManager *m_emergemgr = nullptr;
+ ModMetadataDatabase *m_mod_storage_database = nullptr;
std::unique_ptr<ModChannelMgr> m_modchannel_mgr;
};
TestGameDef::TestGameDef() :
+ m_mod_storage_database(new Database_Dummy()),
m_modchannel_mgr(new ModChannelMgr())
{
m_itemdef = createItemDefManager();
@@ -109,6 +112,7 @@ TestGameDef::~TestGameDef()
{
delete m_itemdef;
delete m_nodedef;
+ delete m_mod_storage_database;
}
diff --git a/src/unittest/test_address.cpp b/src/unittest/test_address.cpp
index 35d4effb6..f46135577 100644
--- a/src/unittest/test_address.cpp
+++ b/src/unittest/test_address.cpp
@@ -56,7 +56,7 @@ void TestAddress::testIsLocalhost()
UASSERT(!Address(172, 45, 37, 68, 0).isLocalhost());
// v6
- std::unique_ptr<IPv6AddressBytes> ipv6Bytes(new IPv6AddressBytes());
+ auto ipv6Bytes = std::make_unique<IPv6AddressBytes>();
std::vector<u8> ipv6RawAddr = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
memcpy(ipv6Bytes->bytes, &ipv6RawAddr[0], 16);
UASSERT(Address(ipv6Bytes.get(), 0).isLocalhost())
diff --git a/src/unittest/test_config.h.in b/src/unittest/test_config.h.in
index 36850b00d..50d2398e4 100644
--- a/src/unittest/test_config.h.in
+++ b/src/unittest/test_config.h.in
@@ -4,3 +4,4 @@
#define TEST_WORLDDIR "@TEST_WORLDDIR@"
#define TEST_SUBGAME_PATH "@TEST_SUBGAME_PATH@"
+#define TEST_MOD_PATH "@TEST_MOD_PATH@"
diff --git a/src/unittest/test_connection.cpp b/src/unittest/test_connection.cpp
index 23b7e9105..04fea90d6 100644
--- a/src/unittest/test_connection.cpp
+++ b/src/unittest/test_connection.cpp
@@ -124,7 +124,7 @@ void TestConnection::testHelpers()
Address a(127,0,0,1, 10);
const u16 seqnum = 34352;
- con::BufferedPacket p1 = con::makePacket(a, data1,
+ con::BufferedPacketPtr p1 = con::makePacket(a, data1,
proto_id, peer_id, channel);
/*
We should now have a packet with this data:
@@ -135,10 +135,10 @@ void TestConnection::testHelpers()
Data:
[7] u8 data1[0]
*/
- UASSERT(readU32(&p1.data[0]) == proto_id);
- UASSERT(readU16(&p1.data[4]) == peer_id);
- UASSERT(readU8(&p1.data[6]) == channel);
- UASSERT(readU8(&p1.data[7]) == data1[0]);
+ UASSERT(readU32(&p1->data[0]) == proto_id);
+ UASSERT(readU16(&p1->data[4]) == peer_id);
+ UASSERT(readU8(&p1->data[6]) == channel);
+ UASSERT(readU8(&p1->data[7]) == data1[0]);
//infostream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
diff --git a/src/unittest/test_eventmanager.cpp b/src/unittest/test_eventmanager.cpp
index bb0e59336..fec57f9fe 100644
--- a/src/unittest/test_eventmanager.cpp
+++ b/src/unittest/test_eventmanager.cpp
@@ -82,7 +82,7 @@ void TestEventManager::testDeregister()
void TestEventManager::testRealEvent()
{
EventManager ev;
- std::unique_ptr<EventManagerTest> emt(new EventManagerTest());
+ auto emt = std::make_unique<EventManagerTest>();
ev.reg(MtEvent::PLAYER_REGAIN_GROUND, EventManagerTest::eventTest, emt.get());
// Put event & verify event value
@@ -93,7 +93,7 @@ void TestEventManager::testRealEvent()
void TestEventManager::testRealEventAfterDereg()
{
EventManager ev;
- std::unique_ptr<EventManagerTest> emt(new EventManagerTest());
+ auto emt = std::make_unique<EventManagerTest>();
ev.reg(MtEvent::PLAYER_REGAIN_GROUND, EventManagerTest::eventTest, emt.get());
// Put event & verify event value
diff --git a/src/unittest/test_gettext.cpp b/src/unittest/test_gettext.cpp
new file mode 100644
index 000000000..338a416d7
--- /dev/null
+++ b/src/unittest/test_gettext.cpp
@@ -0,0 +1,43 @@
+#include "test.h"
+#include "porting.h"
+#include "gettext.h"
+
+class TestGettext : public TestBase
+{
+public:
+ TestGettext() {
+ TestManager::registerTestModule(this);
+ }
+
+ const char *getName() { return "TestGettext"; }
+
+ void runTests(IGameDef *gamedef);
+
+ void testFmtgettext();
+};
+
+static TestGettext g_test_instance;
+
+void TestGettext::runTests(IGameDef *gamedef)
+{
+ TEST(testFmtgettext);
+}
+
+// Make sure updatepo.sh does not pick up the strings
+#define dummyname fmtgettext
+
+void TestGettext::testFmtgettext()
+{
+ std::string buf = dummyname("sample text %d", 12);
+ UASSERTEQ(std::string, buf, "sample text 12");
+
+ std::string src, expect;
+ src = "You are about to join this server with the name \"%s\".\n";
+ expect = "You are about to join this server with the name \"foo\".\n";
+ for (int i = 0; i < 20; i++) {
+ src.append("loooong text");
+ expect.append("loooong text");
+ }
+ buf = dummyname(src.c_str(), "foo");
+ UASSERTEQ(const std::string &, buf, expect);
+}
diff --git a/src/unittest/test_irrptr.cpp b/src/unittest/test_irrptr.cpp
index 3484f1514..2fb7cfcd6 100644
--- a/src/unittest/test_irrptr.cpp
+++ b/src/unittest/test_irrptr.cpp
@@ -93,7 +93,9 @@ void TestIrrPtr::testRefCounting()
#if defined(__clang__)
#pragma GCC diagnostic push
- #pragma GCC diagnostic ignored "-Wself-assign-overloaded"
+ #if __clang_major__ >= 7
+ #pragma GCC diagnostic ignored "-Wself-assign-overloaded"
+ #endif
#pragma GCC diagnostic ignored "-Wself-move"
#endif
diff --git a/src/unittest/test_lua.cpp b/src/unittest/test_lua.cpp
new file mode 100644
index 000000000..fc8f895af
--- /dev/null
+++ b/src/unittest/test_lua.cpp
@@ -0,0 +1,79 @@
+/*
+Minetest
+Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2021 TurkeyMcMac, Jude Melton-Houghton <jwmhjwmh@gmail.com>
+
+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"
+
+extern "C" {
+#include <lua.h>
+#include <lauxlib.h>
+}
+
+class TestLua : public TestBase
+{
+public:
+ TestLua() { TestManager::registerTestModule(this); }
+ const char *getName() { return "TestLua"; }
+
+ void runTests(IGameDef *gamedef);
+
+ void testLuaDestructors();
+};
+
+static TestLua g_test_instance;
+
+void TestLua::runTests(IGameDef *gamedef)
+{
+ TEST(testLuaDestructors);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+namespace
+{
+
+ class DestructorDetector {
+ bool *did_destruct;
+ public:
+ DestructorDetector(bool *did_destruct) : did_destruct(did_destruct)
+ {
+ *did_destruct = false;
+ }
+ ~DestructorDetector()
+ {
+ *did_destruct = true;
+ }
+ };
+
+}
+
+void TestLua::testLuaDestructors()
+{
+ bool did_destruct = false;
+
+ lua_State *L = luaL_newstate();
+ lua_cpcall(L, [](lua_State *L) -> int {
+ DestructorDetector d(reinterpret_cast<bool*>(lua_touserdata(L, 1)));
+ luaL_error(L, "error");
+ return 0;
+ }, &did_destruct);
+ lua_close(L);
+
+ UASSERT(did_destruct);
+}
diff --git a/src/unittest/test_map.cpp b/src/unittest/test_map.cpp
new file mode 100644
index 000000000..82e55e1aa
--- /dev/null
+++ b/src/unittest/test_map.cpp
@@ -0,0 +1,68 @@
+/*
+Minetest
+
+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 <cstdio>
+#include "mapblock.h"
+
+class TestMap : public TestBase
+{
+public:
+ TestMap() { TestManager::registerTestModule(this); }
+ const char *getName() { return "TestMap"; }
+
+ void runTests(IGameDef *gamedef);
+
+ void testMaxMapgenLimit();
+};
+
+static TestMap g_test_instance;
+
+void TestMap::runTests(IGameDef *gamedef)
+{
+ TEST(testMaxMapgenLimit);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+void TestMap::testMaxMapgenLimit()
+{
+ // limit must end on a mapblock boundary
+ UASSERTEQ(int, MAX_MAP_GENERATION_LIMIT % MAP_BLOCKSIZE, MAP_BLOCKSIZE - 1);
+
+ // objectpos_over_limit should do exactly this except the last node
+ // actually spans from LIMIT-0.5 to LIMIT+0.5
+ float limit_times_bs = MAX_MAP_GENERATION_LIMIT * BS;
+ UASSERT(objectpos_over_limit(v3f(limit_times_bs-BS/2)) == false);
+ UASSERT(objectpos_over_limit(v3f(limit_times_bs)) == false);
+ UASSERT(objectpos_over_limit(v3f(limit_times_bs+BS/2)) == false);
+ UASSERT(objectpos_over_limit(v3f(limit_times_bs+BS)) == true);
+
+ UASSERT(objectpos_over_limit(v3f(-limit_times_bs+BS/2)) == false);
+ UASSERT(objectpos_over_limit(v3f(-limit_times_bs)) == false);
+ UASSERT(objectpos_over_limit(v3f(-limit_times_bs-BS/2)) == false);
+ UASSERT(objectpos_over_limit(v3f(-limit_times_bs-BS)) == true);
+
+ // blockpos_over_max_limit
+ s16 limit_block = MAX_MAP_GENERATION_LIMIT / MAP_BLOCKSIZE;
+ UASSERT(blockpos_over_max_limit(v3s16(limit_block)) == false);
+ UASSERT(blockpos_over_max_limit(v3s16(limit_block+1)) == true);
+ UASSERT(blockpos_over_max_limit(v3s16(-limit_block)) == false);
+ UASSERT(blockpos_over_max_limit(v3s16(-limit_block-1)) == true);
+}
diff --git a/src/unittest/test_mod/test_mod/init.lua b/src/unittest/test_mod/test_mod/init.lua
new file mode 100644
index 000000000..724a863f5
--- /dev/null
+++ b/src/unittest/test_mod/test_mod/init.lua
@@ -0,0 +1 @@
+-- deliberately empty
diff --git a/src/unittest/test_mod/test_mod/mod.conf b/src/unittest/test_mod/test_mod/mod.conf
new file mode 100644
index 000000000..56c64b2d8
--- /dev/null
+++ b/src/unittest/test_mod/test_mod/mod.conf
@@ -0,0 +1,2 @@
+name = test_mod
+description = A mod doing nothing, to test if MINETEST_MOD_PATH is recognised
diff --git a/src/unittest/test_modmetadatadatabase.cpp b/src/unittest/test_modmetadatadatabase.cpp
new file mode 100644
index 000000000..be97fae5e
--- /dev/null
+++ b/src/unittest/test_modmetadatadatabase.cpp
@@ -0,0 +1,253 @@
+/*
+Minetest
+Copyright (C) 2018 bendeutsch, Ben Deutsch <ben@bendeutsch.de>
+Copyright (C) 2021 TurkeyMcMac, Jude Melton-Houghton <jwmhjwmh@gmail.com>
+
+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.
+*/
+
+// This file is an edited copy of test_authdatabase.cpp
+
+#include "test.h"
+
+#include <algorithm>
+#include "database/database-files.h"
+#include "database/database-sqlite3.h"
+#include "filesys.h"
+
+namespace
+{
+// Anonymous namespace to create classes that are only
+// visible to this file
+//
+// These are helpers that return a *ModMetadataDatabase and
+// allow us to run the same tests on different databases and
+// database acquisition strategies.
+
+class ModMetadataDatabaseProvider
+{
+public:
+ virtual ~ModMetadataDatabaseProvider() = default;
+ virtual ModMetadataDatabase *getModMetadataDatabase() = 0;
+};
+
+class FixedProvider : public ModMetadataDatabaseProvider
+{
+public:
+ FixedProvider(ModMetadataDatabase *mod_meta_db) : mod_meta_db(mod_meta_db){};
+ virtual ~FixedProvider(){};
+ virtual ModMetadataDatabase *getModMetadataDatabase() { return mod_meta_db; };
+
+private:
+ ModMetadataDatabase *mod_meta_db;
+};
+
+class FilesProvider : public ModMetadataDatabaseProvider
+{
+public:
+ FilesProvider(const std::string &dir) : dir(dir){};
+ virtual ~FilesProvider()
+ {
+ if (mod_meta_db)
+ mod_meta_db->endSave();
+ delete mod_meta_db;
+ }
+ virtual ModMetadataDatabase *getModMetadataDatabase()
+ {
+ if (mod_meta_db)
+ mod_meta_db->endSave();
+ delete mod_meta_db;
+ mod_meta_db = new ModMetadataDatabaseFiles(dir);
+ mod_meta_db->beginSave();
+ return mod_meta_db;
+ };
+
+private:
+ std::string dir;
+ ModMetadataDatabase *mod_meta_db = nullptr;
+};
+
+class SQLite3Provider : public ModMetadataDatabaseProvider
+{
+public:
+ SQLite3Provider(const std::string &dir) : dir(dir){};
+ virtual ~SQLite3Provider()
+ {
+ if (mod_meta_db)
+ mod_meta_db->endSave();
+ delete mod_meta_db;
+ }
+ virtual ModMetadataDatabase *getModMetadataDatabase()
+ {
+ if (mod_meta_db)
+ mod_meta_db->endSave();
+ delete mod_meta_db;
+ mod_meta_db = new ModMetadataDatabaseSQLite3(dir);
+ mod_meta_db->beginSave();
+ return mod_meta_db;
+ };
+
+private:
+ std::string dir;
+ ModMetadataDatabase *mod_meta_db = nullptr;
+};
+}
+
+class TestModMetadataDatabase : public TestBase
+{
+public:
+ TestModMetadataDatabase() { TestManager::registerTestModule(this); }
+ const char *getName() { return "TestModMetadataDatabase"; }
+
+ void runTests(IGameDef *gamedef);
+ void runTestsForCurrentDB();
+
+ void testRecallFail();
+ void testCreate();
+ void testRecall();
+ void testChange();
+ void testRecallChanged();
+ void testListMods();
+ void testRemove();
+
+private:
+ ModMetadataDatabaseProvider *mod_meta_provider;
+};
+
+static TestModMetadataDatabase g_test_instance;
+
+void TestModMetadataDatabase::runTests(IGameDef *gamedef)
+{
+ // fixed directory, for persistence
+ thread_local const std::string test_dir = getTestTempDirectory();
+
+ // Each set of tests is run twice for each database type:
+ // one where we reuse the same ModMetadataDatabase object (to test local caching),
+ // and one where we create a new ModMetadataDatabase object for each call
+ // (to test actual persistence).
+
+ rawstream << "-------- Files database (same object)" << std::endl;
+
+ ModMetadataDatabase *mod_meta_db = new ModMetadataDatabaseFiles(test_dir);
+ mod_meta_provider = new FixedProvider(mod_meta_db);
+
+ runTestsForCurrentDB();
+
+ delete mod_meta_db;
+ delete mod_meta_provider;
+
+ // reset database
+ fs::RecursiveDelete(test_dir + DIR_DELIM + "mod_storage");
+
+ rawstream << "-------- Files database (new objects)" << std::endl;
+
+ mod_meta_provider = new FilesProvider(test_dir);
+
+ runTestsForCurrentDB();
+
+ delete mod_meta_provider;
+
+ rawstream << "-------- SQLite3 database (same object)" << std::endl;
+
+ mod_meta_db = new ModMetadataDatabaseSQLite3(test_dir);
+ mod_meta_provider = new FixedProvider(mod_meta_db);
+
+ runTestsForCurrentDB();
+
+ delete mod_meta_db;
+ delete mod_meta_provider;
+
+ // reset database
+ fs::DeleteSingleFileOrEmptyDirectory(test_dir + DIR_DELIM + "mod_storage.sqlite");
+
+ rawstream << "-------- SQLite3 database (new objects)" << std::endl;
+
+ mod_meta_provider = new SQLite3Provider(test_dir);
+
+ runTestsForCurrentDB();
+
+ delete mod_meta_provider;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+void TestModMetadataDatabase::runTestsForCurrentDB()
+{
+ TEST(testRecallFail);
+ TEST(testCreate);
+ TEST(testRecall);
+ TEST(testChange);
+ TEST(testRecallChanged);
+ TEST(testListMods);
+ TEST(testRemove);
+ TEST(testRecallFail);
+}
+
+void TestModMetadataDatabase::testRecallFail()
+{
+ ModMetadataDatabase *mod_meta_db = mod_meta_provider->getModMetadataDatabase();
+ StringMap recalled;
+ mod_meta_db->getModEntries("mod1", &recalled);
+ UASSERT(recalled.empty());
+}
+
+void TestModMetadataDatabase::testCreate()
+{
+ ModMetadataDatabase *mod_meta_db = mod_meta_provider->getModMetadataDatabase();
+ StringMap recalled;
+ UASSERT(mod_meta_db->setModEntry("mod1", "key1", "value1"));
+}
+
+void TestModMetadataDatabase::testRecall()
+{
+ ModMetadataDatabase *mod_meta_db = mod_meta_provider->getModMetadataDatabase();
+ StringMap recalled;
+ mod_meta_db->getModEntries("mod1", &recalled);
+ UASSERT(recalled.size() == 1);
+ UASSERT(recalled["key1"] == "value1");
+}
+
+void TestModMetadataDatabase::testChange()
+{
+ ModMetadataDatabase *mod_meta_db = mod_meta_provider->getModMetadataDatabase();
+ StringMap recalled;
+ UASSERT(mod_meta_db->setModEntry("mod1", "key1", "value2"));
+}
+
+void TestModMetadataDatabase::testRecallChanged()
+{
+ ModMetadataDatabase *mod_meta_db = mod_meta_provider->getModMetadataDatabase();
+ StringMap recalled;
+ mod_meta_db->getModEntries("mod1", &recalled);
+ UASSERT(recalled.size() == 1);
+ UASSERT(recalled["key1"] == "value2");
+}
+
+void TestModMetadataDatabase::testListMods()
+{
+ ModMetadataDatabase *mod_meta_db = mod_meta_provider->getModMetadataDatabase();
+ UASSERT(mod_meta_db->setModEntry("mod2", "key1", "value1"));
+ std::vector<std::string> mod_list;
+ mod_meta_db->listMods(&mod_list);
+ UASSERT(mod_list.size() == 2);
+ UASSERT(std::find(mod_list.cbegin(), mod_list.cend(), "mod1") != mod_list.cend());
+ UASSERT(std::find(mod_list.cbegin(), mod_list.cend(), "mod2") != mod_list.cend());
+}
+
+void TestModMetadataDatabase::testRemove()
+{
+ ModMetadataDatabase *mod_meta_db = mod_meta_provider->getModMetadataDatabase();
+ UASSERT(mod_meta_db->removeModEntry("mod1", "key1"));
+}
diff --git a/src/unittest/test_player.cpp b/src/unittest/test_player.cpp
deleted file mode 100644
index 6990b4016..000000000
--- a/src/unittest/test_player.cpp
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
-Minetest
-Copyright (C) 2010-2016 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
-
-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 "remoteplayer.h"
-#include "server.h"
-
-class TestPlayer : public TestBase
-{
-public:
- TestPlayer() { TestManager::registerTestModule(this); }
- const char *getName() { return "TestPlayer"; }
-
- void runTests(IGameDef *gamedef);
-};
-
-static TestPlayer g_test_instance;
-
-void TestPlayer::runTests(IGameDef *gamedef)
-{
-}
diff --git a/src/unittest/test_server_shutdown_state.cpp b/src/unittest/test_server_shutdown_state.cpp
index fbb76ff6a..50305e725 100644
--- a/src/unittest/test_server_shutdown_state.cpp
+++ b/src/unittest/test_server_shutdown_state.cpp
@@ -26,12 +26,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
class FakeServer : public Server
{
public:
- // clang-format off
FakeServer() : Server("fakeworld", SubgameSpec("fakespec", "fakespec"), true,
Address(), true, nullptr)
{
}
- // clang-format on
private:
void SendChatMessage(session_t peer_id, const ChatMessage &message)
@@ -95,7 +93,7 @@ void TestServerShutdownState::testTrigger()
void TestServerShutdownState::testTick()
{
- std::unique_ptr<FakeServer> fakeServer(new FakeServer());
+ auto fakeServer = std::make_unique<FakeServer>();
Server::ShutdownState ss;
ss.trigger(28.0f, "testtrigger", true);
ss.tick(0.0f, fakeServer.get());
diff --git a/src/unittest/test_servermodmanager.cpp b/src/unittest/test_servermodmanager.cpp
index e3edb0c32..4c473d8b5 100644
--- a/src/unittest/test_servermodmanager.cpp
+++ b/src/unittest/test_servermodmanager.cpp
@@ -48,14 +48,20 @@ static TestServerModManager g_test_instance;
void TestServerModManager::runTests(IGameDef *gamedef)
{
const char *saved_env_mt_subgame_path = getenv("MINETEST_SUBGAME_PATH");
+ const char *saved_env_mt_mod_path = getenv("MINETEST_MOD_PATH");
#ifdef WIN32
{
std::string subgame_path("MINETEST_SUBGAME_PATH=");
subgame_path.append(TEST_SUBGAME_PATH);
_putenv(subgame_path.c_str());
+
+ std::string mod_path("MINETEST_MOD_PATH=");
+ mod_path.append(TEST_MOD_PATH);
+ _putenv(mod_path.c_str());
}
#else
setenv("MINETEST_SUBGAME_PATH", TEST_SUBGAME_PATH, 1);
+ setenv("MINETEST_MOD_PATH", TEST_MOD_PATH, 1);
#endif
TEST(testCreation);
@@ -75,12 +81,21 @@ void TestServerModManager::runTests(IGameDef *gamedef)
if (saved_env_mt_subgame_path)
subgame_path.append(saved_env_mt_subgame_path);
_putenv(subgame_path.c_str());
+
+ std::string mod_path("MINETEST_MOD_PATH=");
+ if (saved_env_mt_mod_path)
+ mod_path.append(saved_env_mt_mod_path);
+ _putenv(mod_path.c_str());
}
#else
if (saved_env_mt_subgame_path)
setenv("MINETEST_SUBGAME_PATH", saved_env_mt_subgame_path, 1);
else
unsetenv("MINETEST_SUBGAME_PATH");
+ if (saved_env_mt_mod_path)
+ setenv("MINETEST_MOD_PATH", saved_env_mt_mod_path, 1);
+ else
+ unsetenv("MINETEST_MOD_PATH");
#endif
}
@@ -89,6 +104,7 @@ void TestServerModManager::testCreation()
std::string path = std::string(TEST_WORLDDIR) + DIR_DELIM + "world.mt";
Settings world_config;
world_config.set("gameid", "devtest");
+ world_config.set("load_mod_test_mod", "true");
UASSERTEQ(bool, world_config.updateConfigFile(path.c_str()), true);
ServerModManager sm(TEST_WORLDDIR);
}
@@ -119,16 +135,21 @@ void TestServerModManager::testGetMods()
UASSERTEQ(bool, mods.empty(), false);
// Ensure we found basenodes mod (part of devtest)
+ // and test_mod (for testing MINETEST_MOD_PATH).
bool default_found = false;
+ bool test_mod_found = false;
for (const auto &m : mods) {
if (m.name == "basenodes")
default_found = true;
+ if (m.name == "test_mod")
+ test_mod_found = true;
// Verify if paths are not empty
UASSERTEQ(bool, m.path.empty(), false);
}
UASSERTEQ(bool, default_found, true);
+ UASSERTEQ(bool, test_mod_found, true);
}
void TestServerModManager::testGetModspec()
diff --git a/src/unittest/test_socket.cpp b/src/unittest/test_socket.cpp
index 6d5cf334d..620021b59 100644
--- a/src/unittest/test_socket.cpp
+++ b/src/unittest/test_socket.cpp
@@ -97,11 +97,11 @@ void TestSocket::testIPv4Socket()
UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer)) == 0);
if (address != Address(0, 0, 0, 0, port)) {
- UASSERT(sender.getAddress().sin_addr.s_addr ==
- address.getAddress().sin_addr.s_addr);
+ UASSERT(sender.getAddress().s_addr ==
+ address.getAddress().s_addr);
} else {
- UASSERT(sender.getAddress().sin_addr.s_addr ==
- Address(127, 0, 0, 1, 0).getAddress().sin_addr.s_addr);
+ UASSERT(sender.getAddress().s_addr ==
+ Address(127, 0, 0, 1, 0).getAddress().s_addr);
}
}
@@ -128,7 +128,7 @@ void TestSocket::testIPv6Socket()
socket6.Bind(address6);
- try {
+ {
socket6.Send(Address(&bytes, port), sendbuffer, sizeof(sendbuffer));
sleep_ms(50);
@@ -142,10 +142,8 @@ void TestSocket::testIPv6Socket()
}
//FIXME: This fails on some systems
UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer)) == 0);
- UASSERT(memcmp(sender.getAddress6().sin6_addr.s6_addr,
- Address(&bytes, 0).getAddress6().sin6_addr.s6_addr, 16) == 0);
- } catch (SendFailedException &e) {
- errorstream << "IPv6 support enabled but not available!"
- << std::endl;
+
+ UASSERT(memcmp(sender.getAddress6().s6_addr,
+ Address(&bytes, 0).getAddress6().s6_addr, 16) == 0);
}
}
diff --git a/src/unittest/test_utilities.cpp b/src/unittest/test_utilities.cpp
index 039110d54..98a143d1f 100644
--- a/src/unittest/test_utilities.cpp
+++ b/src/unittest/test_utilities.cpp
@@ -43,7 +43,6 @@ public:
void testPadString();
void testStartsWith();
void testStrEqual();
- void testStringTrim();
void testStrToIntConversion();
void testStringReplace();
void testStringAllowed();
@@ -58,6 +57,7 @@ public:
void testStringJoin();
void testEulerConversion();
void testBase64();
+ void testSanitizeDirName();
};
static TestUtilities g_test_instance;
@@ -75,7 +75,6 @@ void TestUtilities::runTests(IGameDef *gamedef)
TEST(testPadString);
TEST(testStartsWith);
TEST(testStrEqual);
- TEST(testStringTrim);
TEST(testStrToIntConversion);
TEST(testStringReplace);
TEST(testStringAllowed);
@@ -90,6 +89,7 @@ void TestUtilities::runTests(IGameDef *gamedef)
TEST(testStringJoin);
TEST(testEulerConversion);
TEST(testBase64);
+ TEST(testSanitizeDirName);
}
////////////////////////////////////////////////////////////////////////////////
@@ -190,6 +190,8 @@ void TestUtilities::testTrim()
UASSERT(trim("dirt_with_grass") == "dirt_with_grass");
UASSERT(trim("\n \t\r Foo bAR \r\n\t\t ") == "Foo bAR");
UASSERT(trim("\n \t\r \r\n\t\t ") == "");
+ UASSERT(trim(" a") == "a");
+ UASSERT(trim("a ") == "a");
}
@@ -255,15 +257,6 @@ void TestUtilities::testStrEqual()
}
-void TestUtilities::testStringTrim()
-{
- UASSERT(trim(" a") == "a");
- UASSERT(trim(" a ") == "a");
- UASSERT(trim("a ") == "a");
- UASSERT(trim("") == "");
-}
-
-
void TestUtilities::testStrToIntConversion()
{
UASSERT(mystoi("123", 0, 1000) == 123);
@@ -392,9 +385,9 @@ void TestUtilities::testIsPowerOfTwo()
UASSERT(is_power_of_two(2) == true);
UASSERT(is_power_of_two(3) == false);
for (int exponent = 2; exponent <= 31; ++exponent) {
- UASSERT(is_power_of_two((1 << exponent) - 1) == false);
- UASSERT(is_power_of_two((1 << exponent)) == true);
- UASSERT(is_power_of_two((1 << exponent) + 1) == false);
+ UASSERT(is_power_of_two((1U << exponent) - 1) == false);
+ UASSERT(is_power_of_two((1U << exponent)) == true);
+ UASSERT(is_power_of_two((1U << exponent) + 1) == false);
}
UASSERT(is_power_of_two(U32_MAX) == false);
}
@@ -629,4 +622,17 @@ void TestUtilities::testBase64()
UASSERT(base64_is_valid("AAA=A") == false);
UASSERT(base64_is_valid("AAAA=A") == false);
UASSERT(base64_is_valid("AAAAA=A") == false);
-} \ No newline at end of file
+}
+
+
+void TestUtilities::testSanitizeDirName()
+{
+ UASSERT(sanitizeDirName("a", "~") == "a");
+ UASSERT(sanitizeDirName(" ", "~") == "__");
+ UASSERT(sanitizeDirName(" a ", "~") == "_a_");
+ UASSERT(sanitizeDirName("COM1", "~") == "~COM1");
+ UASSERT(sanitizeDirName("COM1", ":") == "_COM1");
+ UASSERT(sanitizeDirName("cOm\u00B2", "~") == "~cOm\u00B2");
+ UASSERT(sanitizeDirName("cOnIn$", "~") == "~cOnIn$");
+ UASSERT(sanitizeDirName(" cOnIn$ ", "~") == "_cOnIn$_");
+}
diff --git a/src/unittest/test_voxelarea.cpp b/src/unittest/test_voxelarea.cpp
index 6ec0412d5..a79c9778e 100644
--- a/src/unittest/test_voxelarea.cpp
+++ b/src/unittest/test_voxelarea.cpp
@@ -30,6 +30,7 @@ public:
void test_addarea();
void test_pad();
+ void test_extent();
void test_volume();
void test_contains_voxelarea();
void test_contains_point();
@@ -65,6 +66,7 @@ void TestVoxelArea::runTests(IGameDef *gamedef)
{
TEST(test_addarea);
TEST(test_pad);
+ TEST(test_extent);
TEST(test_volume);
TEST(test_contains_voxelarea);
TEST(test_contains_point);
@@ -113,10 +115,22 @@ void TestVoxelArea::test_pad()
UASSERT(v1.MaxEdge == v3s16(-47, -9347, 969));
}
+void TestVoxelArea::test_extent()
+{
+ VoxelArea v1(v3s16(-1337, -547, -789), v3s16(-147, 447, 669));
+ UASSERT(v1.getExtent() == v3s16(1191, 995, 1459));
+
+ VoxelArea v2(v3s16(32493, -32507, 32752), v3s16(32508, -32492, 32767));
+ UASSERT(v2.getExtent() == v3s16(16, 16, 16));
+}
+
void TestVoxelArea::test_volume()
{
- VoxelArea v1(v3s16(-1337, 447, -789), v3s16(-147, -9547, 669));
- UASSERTEQ(s32, v1.getVolume(), -184657133);
+ VoxelArea v1(v3s16(-1337, -547, -789), v3s16(-147, 447, 669));
+ UASSERTEQ(s32, v1.getVolume(), 1728980655);
+
+ VoxelArea v2(v3s16(32493, -32507, 32752), v3s16(32508, -32492, 32767));
+ UASSERTEQ(s32, v2.getVolume(), 4096);
}
void TestVoxelArea::test_contains_voxelarea()