From 7220ca906dfc804bd508336f5bed252bcec62ae0 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Sun, 26 Apr 2015 01:24:19 -0400 Subject: Tests: Modularize unit testing Split unit tests into separate files under src/unittest/ Give better unittest diagnostics Clean up some code --- src/unittest/test_filepath.cpp | 261 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 261 insertions(+) create mode 100644 src/unittest/test_filepath.cpp (limited to 'src/unittest/test_filepath.cpp') diff --git a/src/unittest/test_filepath.cpp b/src/unittest/test_filepath.cpp new file mode 100644 index 000000000..6ea7ac076 --- /dev/null +++ b/src/unittest/test_filepath.cpp @@ -0,0 +1,261 @@ +/* +Minetest +Copyright (C) 2013 celeron55, Perttu Ahola + +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 + +#include "log.h" +#include "serialization.h" +#include "nodedef.h" +#include "noise.h" + +class TestFilePath : public TestBase { +public: + TestFilePath() { TestManager::registerTestModule(this); } + const char *getName() { return "TestFilePath"; } + + void runTests(IGameDef *gamedef); + + void testIsDirDelimiter(); + void testPathStartsWith(); + void testRemoveLastPathComponent(); + void testRemoveLastPathComponentWithTrailingDelimiter(); + void testRemoveRelativePathComponent(); +}; + +static TestFilePath g_test_instance; + +void TestFilePath::runTests(IGameDef *gamedef) +{ + TEST(testIsDirDelimiter); + TEST(testPathStartsWith); + TEST(testRemoveLastPathComponent); + TEST(testRemoveLastPathComponentWithTrailingDelimiter); + TEST(testRemoveRelativePathComponent); +} + +//////////////////////////////////////////////////////////////////////////////// + +// adjusts a POSIX path to system-specific conventions +// -> changes '/' to DIR_DELIM +// -> absolute paths start with "C:\\" on windows +std::string p(std::string path) +{ + for (size_t i = 0; i < path.size(); ++i) { + if (path[i] == '/') { + path.replace(i, 1, DIR_DELIM); + i += std::string(DIR_DELIM).size() - 1; // generally a no-op + } + } + + #ifdef _WIN32 + if (path[0] == '\\') + path = "C:" + path; + #endif + + return path; +} + + +void TestFilePath::testIsDirDelimiter() +{ + UASSERT(fs::IsDirDelimiter('/') == true); + UASSERT(fs::IsDirDelimiter('A') == false); + UASSERT(fs::IsDirDelimiter(0) == false); +#ifdef _WIN32 + UASSERT(fs::IsDirDelimiter('\\') == true); +#else + UASSERT(fs::IsDirDelimiter('\\') == false); +#endif +} + + +void TestFilePath::testPathStartsWith() +{ + const int numpaths = 12; + std::string paths[numpaths] = { + "", + p("/"), + p("/home/user/minetest"), + p("/home/user/minetest/bin"), + p("/home/user/.minetest"), + p("/tmp/dir/file"), + p("/tmp/file/"), + p("/tmP/file"), + p("/tmp"), + p("/tmp/dir"), + p("/home/user2/minetest/worlds"), + p("/home/user2/minetest/world"), + }; + /* + expected fs::PathStartsWith results + 0 = returns false + 1 = returns true + 2 = returns false on windows, true elsewhere + 3 = returns true on windows, false elsewhere + 4 = returns true if and only if + FILESYS_CASE_INSENSITIVE is true + */ + int expected_results[numpaths][numpaths] = { + {1,2,0,0,0,0,0,0,0,0,0,0}, + {1,1,0,0,0,0,0,0,0,0,0,0}, + {1,1,1,0,0,0,0,0,0,0,0,0}, + {1,1,1,1,0,0,0,0,0,0,0,0}, + {1,1,0,0,1,0,0,0,0,0,0,0}, + {1,1,0,0,0,1,0,0,1,1,0,0}, + {1,1,0,0,0,0,1,4,1,0,0,0}, + {1,1,0,0,0,0,4,1,4,0,0,0}, + {1,1,0,0,0,0,0,0,1,0,0,0}, + {1,1,0,0,0,0,0,0,1,1,0,0}, + {1,1,0,0,0,0,0,0,0,0,1,0}, + {1,1,0,0,0,0,0,0,0,0,0,1}, + }; + + for (int i = 0; i < numpaths; i++) + for (int j = 0; j < numpaths; j++){ + /*verbosestream<<"testing fs::PathStartsWith(\"" + <