diff options
author | Andrei Karas <akaras@inbox.ru> | 2017-02-13 17:30:55 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2017-02-13 17:30:55 +0300 |
commit | ee0ec6535de5b3b9fef83af96de7f72dece0f8b1 (patch) | |
tree | 73d0489fbc007d259f9e78e01f034028c8262cf9 | |
parent | 0a2cde31f777ec41893222f71bceb799bb78e590 (diff) | |
download | manaplus-ee0ec6535de5b3b9fef83af96de7f72dece0f8b1.tar.gz manaplus-ee0ec6535de5b3b9fef83af96de7f72dece0f8b1.tar.bz2 manaplus-ee0ec6535de5b3b9fef83af96de7f72dece0f8b1.tar.xz manaplus-ee0ec6535de5b3b9fef83af96de7f72dece0f8b1.zip |
Add some tests for virtfs.
-rw-r--r-- | data/test/CMakeLists.txt | 1 | ||||
-rw-r--r-- | data/test/Makefile.am | 1 | ||||
-rw-r--r-- | data/test/test.txt | 2 | ||||
-rw-r--r-- | src/Makefile.am | 1 | ||||
-rw-r--r-- | src/utils/virtfs_unittest.cc | 210 |
5 files changed, 215 insertions, 0 deletions
diff --git a/data/test/CMakeLists.txt b/data/test/CMakeLists.txt index 504a3b85a..73802bc56 100644 --- a/data/test/CMakeLists.txt +++ b/data/test/CMakeLists.txt @@ -12,6 +12,7 @@ SET(FILES quests.xml serverlistplus.xml simplefile.txt + test.txt test.zip testintmap.xml units.xml diff --git a/data/test/Makefile.am b/data/test/Makefile.am index ba9cde3e8..de8aeacc8 100644 --- a/data/test/Makefile.am +++ b/data/test/Makefile.am @@ -14,6 +14,7 @@ test_DATA = \ quests.xml \ serverlistplus.xml \ simplefile.txt \ + test.txt \ test.zip \ testintmap.xml \ units.xml diff --git a/data/test/test.txt b/data/test/test.txt new file mode 100644 index 000000000..6a14a1b0f --- /dev/null +++ b/data/test/test.txt @@ -0,0 +1,2 @@ +test line 1 +test line 2
\ No newline at end of file diff --git a/src/Makefile.am b/src/Makefile.am index a3b6147b3..e8c713f6b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1913,6 +1913,7 @@ manaplustests_SOURCES = ${SRC} \ enums/enums_unittest.cc \ utils/dumplibs_unittest.cc \ utils/checkutils_unittest.cc \ + utils/virtfs_unittest.cc \ utils/xml_unittest.cc \ utils/timer_unittest.cc \ utils/xmlutils_unittest.cc \ diff --git a/src/utils/virtfs_unittest.cc b/src/utils/virtfs_unittest.cc new file mode 100644 index 000000000..56f800e00 --- /dev/null +++ b/src/utils/virtfs_unittest.cc @@ -0,0 +1,210 @@ +/* + * The ManaPlus Client + * Copyright (C) 2016-2017 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "catch.hpp" + +#include "utils/checkutils.h" +#include "utils/virtfs.h" + +#include "debug.h" + +TEST_CASE("VirtFs dirSeparator") +{ + REQUIRE(dirSeparator != nullptr); + REQUIRE(VirtFs::getDirSeparator() == std::string(dirSeparator)); + VirtFs::updateDirSeparator(); + REQUIRE(dirSeparator != nullptr); + REQUIRE(VirtFs::getDirSeparator() == std::string(dirSeparator)); +} + +TEST_CASE("VirtFs getBaseDir") +{ + REQUIRE(VirtFs::getBaseDir() != nullptr); +} + +TEST_CASE("VirtFs getUserDir") +{ + REQUIRE(VirtFs::getUserDir() != nullptr); +} + +TEST_CASE("VirtFs exists") +{ + logger = new Logger(); + VirtFs::addDirToSearchPath("data", Append_false); + VirtFs::addDirToSearchPath("../data", Append_false); + + REQUIRE(VirtFs::exists("test/units.xml") == true); + REQUIRE(VirtFs::exists("test/units123.xml") == false); + REQUIRE(VirtFs::exists("tesQ/units.xml") == false); + REQUIRE(VirtFs::exists("units.xml") == false); + + VirtFs::addDirToSearchPath("data/test", Append_false); + VirtFs::addDirToSearchPath("../data/test", Append_false); + + REQUIRE(VirtFs::exists("test/units.xml") == true); + REQUIRE(VirtFs::exists("test/units123.xml") == false); + REQUIRE(VirtFs::exists("tesQ/units.xml") == false); + REQUIRE(VirtFs::exists("units.xml") == true); + + VirtFs::removeDirFromSearchPath("data/test"); + VirtFs::removeDirFromSearchPath("../data/test"); + + REQUIRE(VirtFs::exists("test/units.xml") == true); + REQUIRE(VirtFs::exists("test/units123.xml") == false); + REQUIRE(VirtFs::exists("tesQ/units.xml") == false); + REQUIRE(VirtFs::exists("units.xml") == false); + + VirtFs::removeDirFromSearchPath("data"); + VirtFs::removeDirFromSearchPath("../data"); +} + +TEST_CASE("VirtFs enumerateFiles") +{ + // +++ need implement +} + +TEST_CASE("VirtFs isDirectory") +{ + logger = new Logger(); + VirtFs::addDirToSearchPath("data", Append_false); + VirtFs::addDirToSearchPath("../data", Append_false); + + REQUIRE(VirtFs::isDirectory("test/units.xml") == false); + REQUIRE(VirtFs::isDirectory("test/units123.xml") == false); + REQUIRE(VirtFs::isDirectory("tesQ/units.xml") == false); + REQUIRE(VirtFs::isDirectory("units.xml") == false); + REQUIRE(VirtFs::isDirectory("test") == true); + REQUIRE(VirtFs::isDirectory("testQ") == false); + + VirtFs::addDirToSearchPath("data/test", Append_false); + VirtFs::addDirToSearchPath("../data/test", Append_false); + + REQUIRE(VirtFs::isDirectory("test/units.xml") == false); + REQUIRE(VirtFs::isDirectory("test/units123.xml") == false); + REQUIRE(VirtFs::isDirectory("tesQ/units.xml") == false); + REQUIRE(VirtFs::isDirectory("units.xml") == false); + REQUIRE(VirtFs::isDirectory("test") == true); + REQUIRE(VirtFs::isDirectory("testQ") == false); + + VirtFs::removeDirFromSearchPath("data/test"); + VirtFs::removeDirFromSearchPath("../data/test"); + + REQUIRE(VirtFs::isDirectory("test/units.xml") == false); + REQUIRE(VirtFs::isDirectory("test/units123.xml") == false); + REQUIRE(VirtFs::isDirectory("tesQ/units.xml") == false); + REQUIRE(VirtFs::isDirectory("units.xml") == false); + REQUIRE(VirtFs::isDirectory("test") == true); + REQUIRE(VirtFs::isDirectory("testQ") == false); + + VirtFs::removeDirFromSearchPath("data"); + VirtFs::removeDirFromSearchPath("../data"); +} + +TEST_CASE("VirtFs openRead") +{ + logger = new Logger(); + VirtFs::addDirToSearchPath("data", Append_false); + VirtFs::addDirToSearchPath("../data", Append_false); + + VirtFile *file = nullptr; + + file = VirtFs::openRead("test/units.xml"); + REQUIRE(file != nullptr); + VirtFs::close(file); + file = VirtFs::openRead("test/units123.xml"); + REQUIRE(file == nullptr); + file = VirtFs::openRead("tesQ/units.xml"); + REQUIRE(file == nullptr); + file = VirtFs::openRead("units.xml"); + REQUIRE(file == nullptr); +// file = VirtFs::openRead("test"); +// REQUIRE(file == nullptr); + file = VirtFs::openRead("testQ"); + REQUIRE(file == nullptr); + + VirtFs::addDirToSearchPath("data/test", Append_false); + VirtFs::addDirToSearchPath("../data/test", Append_false); + + file = VirtFs::openRead("test/units.xml"); + REQUIRE(file != nullptr); + VirtFs::close(file); + file = VirtFs::openRead("test/units123.xml"); + REQUIRE(file == nullptr); + file = VirtFs::openRead("tesQ/units.xml"); + REQUIRE(file == nullptr); + file = VirtFs::openRead("units.xml"); + REQUIRE(file != nullptr); + VirtFs::close(file); +// file = VirtFs::openRead("test"); +// REQUIRE(file == nullptr); + file = VirtFs::openRead("testQ"); + REQUIRE(file == nullptr); + + VirtFs::removeDirFromSearchPath("data/test"); + VirtFs::removeDirFromSearchPath("../data/test"); + + file = VirtFs::openRead("test/units.xml"); + REQUIRE(file != nullptr); + VirtFs::close(file); + file = VirtFs::openRead("test/units123.xml"); + REQUIRE(file == nullptr); + file = VirtFs::openRead("tesQ/units.xml"); + REQUIRE(file == nullptr); + file = VirtFs::openRead("units.xml"); + REQUIRE(file == nullptr); +// file = VirtFs::openRead("test"); +// REQUIRE(file == nullptr); + file = VirtFs::openRead("testQ"); + REQUIRE(file == nullptr); + + VirtFs::removeDirFromSearchPath("data"); + VirtFs::removeDirFromSearchPath("../data"); +} + +TEST_CASE("VirtFs read") +{ + VirtFs::addDirToSearchPath("data", Append_false); + VirtFs::addDirToSearchPath("../data", Append_false); + + VirtFile *file = VirtFs::openRead("test/test.txt"); + REQUIRE(file != nullptr); + REQUIRE(VirtFs::fileLength(file) == 23); + const int fileSize = VirtFs::fileLength(file); + + void *restrict buffer = calloc(fileSize + 1, 1); + REQUIRE(VirtFs::read(file, buffer, 1, fileSize) == fileSize); + REQUIRE(strcmp(static_cast<char*>(buffer), + "test line 1\ntest line 2") == 0); + REQUIRE(VirtFs::tell(file) == fileSize); + + free(buffer); + buffer = calloc(fileSize + 1, 1); + REQUIRE(VirtFs::seek(file, 12) != 0); + REQUIRE(VirtFs::tell(file) == 12); + REQUIRE(VirtFs::read(file, buffer, 1, 11) == 11); + REQUIRE(strcmp(static_cast<char*>(buffer), + "test line 2") == 0); + + VirtFs::close(file); + free(buffer); + + VirtFs::removeDirFromSearchPath("data"); + VirtFs::removeDirFromSearchPath("../data"); +} |