diff options
Diffstat (limited to 'src/unittest')
-rw-r--r-- | src/unittest/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/unittest/test.cpp | 10 | ||||
-rw-r--r-- | src/unittest/test.h | 2 | ||||
-rw-r--r-- | src/unittest/test_address.cpp | 2 | ||||
-rw-r--r-- | src/unittest/test_eventmanager.cpp | 4 | ||||
-rw-r--r-- | src/unittest/test_irrptr.cpp | 4 | ||||
-rw-r--r-- | src/unittest/test_lua.cpp | 79 | ||||
-rw-r--r-- | src/unittest/test_nodedef.cpp | 2 | ||||
-rw-r--r-- | src/unittest/test_noise.cpp | 54 | ||||
-rw-r--r-- | src/unittest/test_serialization.cpp | 78 | ||||
-rw-r--r-- | src/unittest/test_server_shutdown_state.cpp | 4 | ||||
-rw-r--r-- | src/unittest/test_servermodmanager.cpp | 8 | ||||
-rw-r--r-- | src/unittest/test_utilities.cpp | 28 | ||||
-rw-r--r-- | src/unittest/test_voxelarea.cpp | 4 |
14 files changed, 236 insertions, 44 deletions
diff --git a/src/unittest/CMakeLists.txt b/src/unittest/CMakeLists.txt index 92f31ecac..84f769e87 100644 --- a/src/unittest/CMakeLists.txt +++ b/src/unittest/CMakeLists.txt @@ -11,6 +11,7 @@ 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 diff --git a/src/unittest/test.cpp b/src/unittest/test.cpp index f223d567e..af30c209d 100644 --- a/src/unittest/test.cpp +++ b/src/unittest/test.cpp @@ -360,7 +360,7 @@ struct TestMapBlock: public TestBase MapNode node; bool position_valid; - core::list<v3s16> validity_exceptions; + std::list<v3s16> validity_exceptions; TC() { @@ -371,7 +371,7 @@ struct TestMapBlock: public TestBase { //return position_valid ^ (p==position_valid_exception); bool exception = false; - for(core::list<v3s16>::Iterator i=validity_exceptions.begin(); + for(std::list<v3s16>::iterator i=validity_exceptions.begin(); i != validity_exceptions.end(); i++) { if(p == *i) @@ -533,7 +533,7 @@ struct TestMapBlock: public TestBase parent.node.setContent(CONTENT_AIR); parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN); parent.node.setLight(LIGHTBANK_NIGHT, 0); - core::map<v3s16, bool> light_sources; + std::map<v3s16, bool> light_sources; // The bottom block is invalid, because we have a shadowing node UASSERT(b.propagateSunlight(light_sources) == false); UASSERT(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN); @@ -560,7 +560,7 @@ struct TestMapBlock: public TestBase parent.position_valid = true; b.setIsUnderground(true); parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2); - core::map<v3s16, bool> light_sources; + std::map<v3s16, bool> light_sources; // The block below should be valid because there shouldn't be // sunlight in there either UASSERT(b.propagateSunlight(light_sources, true) == true); @@ -601,7 +601,7 @@ struct TestMapBlock: public TestBase } // Lighting value for the valid nodes parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2); - core::map<v3s16, bool> light_sources; + std::map<v3s16, bool> light_sources; // Bottom block is not valid UASSERT(b.propagateSunlight(light_sources) == false); } diff --git a/src/unittest/test.h b/src/unittest/test.h index 1102f6d33..79ea09471 100644 --- a/src/unittest/test.h +++ b/src/unittest/test.h @@ -80,7 +80,7 @@ class TestFailedException : public std::exception { << #expected << std::endl \ << " at " << fs::GetFilenameFromPath(__FILE__) << ":" \ << __LINE__ << std::endl \ - << " actual: " << a << std::endl << " expected: " \ + << " actual : " << a << std::endl << " expected: " \ << e << std::endl; \ throw TestFailedException(); \ } \ 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_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_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_nodedef.cpp b/src/unittest/test_nodedef.cpp index 66ca0ccbc..acf669783 100644 --- a/src/unittest/test_nodedef.cpp +++ b/src/unittest/test_nodedef.cpp @@ -60,7 +60,7 @@ void TestNodeDef::testContentFeaturesSerialization() std::istringstream is(os.str(), std::ios::binary); ContentFeatures f2; - f2.deSerialize(is); + f2.deSerialize(is, LATEST_PROTOCOL_VERSION); UASSERT(f.walkable == f2.walkable); UASSERT(f.node_box.type == f2.node_box.type); diff --git a/src/unittest/test_noise.cpp b/src/unittest/test_noise.cpp index 421f3b66e..12b155f46 100644 --- a/src/unittest/test_noise.cpp +++ b/src/unittest/test_noise.cpp @@ -30,8 +30,14 @@ public: void runTests(IGameDef *gamedef); + void testNoise2dAtOriginWithZeroSeed(); + void testNoise2dWithMaxSeed(); + void testNoise2dWithFunPrimes(); void testNoise2dPoint(); void testNoise2dBulk(); + void testNoise3dAtOriginWithZeroSeed(); + void testNoise3dWithMaxSeed(); + void testNoise3dWithFunPrimes(); void testNoise3dPoint(); void testNoise3dBulk(); void testNoiseInvalidParams(); @@ -44,8 +50,14 @@ static TestNoise g_test_instance; void TestNoise::runTests(IGameDef *gamedef) { + TEST(testNoise2dAtOriginWithZeroSeed); + TEST(testNoise2dWithMaxSeed); + TEST(testNoise2dWithFunPrimes); TEST(testNoise2dPoint); TEST(testNoise2dBulk); + TEST(testNoise3dAtOriginWithZeroSeed); + TEST(testNoise3dWithMaxSeed); + TEST(testNoise3dWithFunPrimes); TEST(testNoise3dPoint); TEST(testNoise3dBulk); TEST(testNoiseInvalidParams); @@ -53,6 +65,27 @@ void TestNoise::runTests(IGameDef *gamedef) //////////////////////////////////////////////////////////////////////////////// +void TestNoise::testNoise2dAtOriginWithZeroSeed() +{ + float actual{ noise2d(0, 0, 0) }; + constexpr float expected{ -0.281791f }; + UASSERT(std::fabs(actual - expected) <= 0.00001); +} + +void TestNoise::testNoise2dWithMaxSeed() +{ + float actual{ noise2d(4096, 4096, 2147483647) }; + constexpr float expected{ 0.950606f }; + UASSERT(std::fabs(actual - expected) <= 0.00001); +} + +void TestNoise::testNoise2dWithFunPrimes() +{ + float actual{ noise2d(-3947, -2333, 7027) }; + constexpr float expected{ -0.294907f }; + UASSERT(std::fabs(actual - expected) <= 0.00001); +} + void TestNoise::testNoise2dPoint() { NoiseParams np_normal(20, 40, v3f(50, 50, 50), 9, 5, 0.6, 2.0); @@ -79,6 +112,27 @@ void TestNoise::testNoise2dBulk() } } +void TestNoise::testNoise3dAtOriginWithZeroSeed() +{ + float actual{ noise2d(0, 0, 0) }; + constexpr float expected{ -0.281791f }; + UASSERT(std::fabs(actual - expected) <= 0.00001); +} + +void TestNoise::testNoise3dWithMaxSeed() +{ + float actual{ noise3d(4096, 4096, 4096, 2147483647) }; + constexpr float expected{ -0.775243f }; + UASSERT(std::fabs(actual - expected) <= 0.00001); +} + +void TestNoise::testNoise3dWithFunPrimes() +{ + float actual{ noise2d(3903, -1723, 7411) }; + constexpr float expected{ 0.989124f }; + UASSERT(std::fabs(actual - expected) <= 0.00001); +} + void TestNoise::testNoise3dPoint() { NoiseParams np_normal(20, 40, v3f(50, 50, 50), 9, 5, 0.6, 2.0); diff --git a/src/unittest/test_serialization.cpp b/src/unittest/test_serialization.cpp index 660d77d02..ff6b57507 100644 --- a/src/unittest/test_serialization.cpp +++ b/src/unittest/test_serialization.cpp @@ -169,23 +169,50 @@ void TestSerialization::testDeSerializeLongString() void TestSerialization::testSerializeJsonString() { + std::istringstream is(std::ios::binary); + const auto reset_is = [&] (const std::string &s) { + is.clear(); + is.str(s); + }; + const auto assert_at_eof = [] (std::istream &is) { + is.get(); + UASSERT(is.eof()); + }; + // Test blank string - UASSERT(serializeJsonString("") == "\"\""); + UASSERTEQ(std::string, serializeJsonString(""), "\"\""); + reset_is("\"\""); + UASSERTEQ(std::string, deSerializeJsonString(is), ""); + assert_at_eof(is); // Test basic string - UASSERT(serializeJsonString("Hello world!") == "\"Hello world!\""); + UASSERTEQ(std::string, serializeJsonString("Hello world!"), "\"Hello world!\""); + reset_is("\"Hello world!\""); + UASSERTEQ(std::string, deSerializeJsonString(is), "Hello world!"); + assert_at_eof(is); + + // Test optional serialization + const std::pair<const char*, const char*> test_pairs[] = { + { "abc", "abc" }, + { "x y z", "\"x y z\"" }, + { "\"", "\"\\\"\"" }, + }; + for (auto it : test_pairs) { + UASSERTEQ(std::string, serializeJsonStringIfNeeded(it.first), it.second); + reset_is(it.second); + UASSERTEQ(std::string, deSerializeJsonStringIfNeeded(is), it.first); + assert_at_eof(is); + } - // MSVC fails when directly using "\\\\" - std::string backslash = "\\"; - UASSERT(serializeJsonString(teststring2) == - mkstr("\"") + + // Test all byte values + const std::string bs = "\\"; // MSVC fails when directly using "\\\\" + const std::string expected = mkstr("\"") + "\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007" + "\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f" + "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" + "\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f" + - " !\\\"" + teststring2.substr(0x23, 0x2f-0x23) + - "\\/" + teststring2.substr(0x30, 0x5c-0x30) + - backslash + backslash + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" + + " !\\\"" + teststring2.substr(0x23, 0x5c-0x23) + + bs + bs + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" + "\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u0086\\u0087" + "\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008e\\u008f" + "\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" + @@ -202,14 +229,31 @@ void TestSerialization::testSerializeJsonString() "\\u00e8\\u00e9\\u00ea\\u00eb\\u00ec\\u00ed\\u00ee\\u00ef" + "\\u00f0\\u00f1\\u00f2\\u00f3\\u00f4\\u00f5\\u00f6\\u00f7" + "\\u00f8\\u00f9\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff" + - "\""); - - // Test deserialize - std::istringstream is(serializeJsonString(teststring2), std::ios::binary); - UASSERT(deSerializeJsonString(is) == teststring2); - UASSERT(!is.eof()); - is.get(); - UASSERT(is.eof()); + "\""; + std::string serialized = serializeJsonString(teststring2); + UASSERTEQ(std::string, serialized, expected); + + reset_is(serialized); + UASSERTEQ(std::string, deSerializeJsonString(is), teststring2); + UASSERT(!is.eof()); // should have stopped at " so eof must not be set yet + assert_at_eof(is); + + // Test that deserialization leaves rest of stream alone + std::string tmp; + reset_is("\"foo\"bar"); + UASSERTEQ(std::string, deSerializeJsonString(is), "foo"); + std::getline(is, tmp, '\0'); + UASSERTEQ(std::string, tmp, "bar"); + + reset_is("\"x y z\"bar"); + UASSERTEQ(std::string, deSerializeJsonStringIfNeeded(is), "x y z"); + std::getline(is, tmp, '\0'); + UASSERTEQ(std::string, tmp, "bar"); + + reset_is("foo bar"); + UASSERTEQ(std::string, deSerializeJsonStringIfNeeded(is), "foo"); + std::getline(is, tmp, '\0'); + UASSERTEQ(std::string, tmp, " bar"); } 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 4c473d8b5..91bf5d3a4 100644 --- a/src/unittest/test_servermodmanager.cpp +++ b/src/unittest/test_servermodmanager.cpp @@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "server/mods.h" #include "settings.h" #include "test_config.h" +#include "util/string.h" class TestServerModManager : public TestBase { @@ -190,4 +191,11 @@ void TestServerModManager::testGetModMediaPaths() std::vector<std::string> result; sm.getModsMediaPaths(result); UASSERTEQ(bool, result.empty(), false); + + // Test media overriding: + // unittests depends on basenodes to override default_dirt.png, + // thus the unittests texture path must come first in the returned media paths to take priority + auto it = std::find(result.begin(), result.end(), sm.getModSpec("unittests")->path + DIR_DELIM + "textures"); + UASSERT(it != result.end()); + UASSERT(std::find(++it, result.end(), sm.getModSpec("basenodes")->path + DIR_DELIM + "textures") != result.end()); } diff --git a/src/unittest/test_utilities.cpp b/src/unittest/test_utilities.cpp index 743fe4462..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); @@ -630,3 +623,16 @@ void TestUtilities::testBase64() UASSERT(base64_is_valid("AAAA=A") == false); UASSERT(base64_is_valid("AAAAA=A") == false); } + + +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 9826d2ee7..a79c9778e 100644 --- a/src/unittest/test_voxelarea.cpp +++ b/src/unittest/test_voxelarea.cpp @@ -120,7 +120,7 @@ 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, 32753), v3s16(32508, -32492, 32768)); + VoxelArea v2(v3s16(32493, -32507, 32752), v3s16(32508, -32492, 32767)); UASSERT(v2.getExtent() == v3s16(16, 16, 16)); } @@ -129,7 +129,7 @@ void TestVoxelArea::test_volume() VoxelArea v1(v3s16(-1337, -547, -789), v3s16(-147, 447, 669)); UASSERTEQ(s32, v1.getVolume(), 1728980655); - VoxelArea v2(v3s16(32493, -32507, 32753), v3s16(32508, -32492, 32768)); + VoxelArea v2(v3s16(32493, -32507, 32752), v3s16(32508, -32492, 32767)); UASSERTEQ(s32, v2.getVolume(), 4096); } |