diff --git a/include/test/filesystem_test.h b/include/test/filesystem_test.h new file mode 100644 index 000000000..5910ae465 --- /dev/null +++ b/include/test/filesystem_test.h @@ -0,0 +1,28 @@ +/** + * @file filesystem_test.h + * @author Chase Geigle + * + * All files in META are dual-licensed under the MIT and NCSA licenses. For more + * details, consult the file LICENSE.mit and LICENSE.ncsa in the root of the + * project. + */ + +#ifndef META_FILESYSTEM_TEST_H_ +#define META_FILESYSTEM_TEST_H_ + +#include "test/unit_test.h" + +namespace meta +{ +namespace testing +{ + +/** + * Runs all the filesystem tests. + * @return the number of tests failed + */ +int filesystem_tests(); + +} +} +#endif diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 9cf1b4b05..06afddda0 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -3,6 +3,7 @@ add_subdirectory(tools) add_library(meta-testing analyzer_test.cpp classifier_test.cpp compression_test.cpp + filesystem_test.cpp filter_test.cpp forward_index_test.cpp inverted_index_test.cpp diff --git a/src/test/filesystem_test.cpp b/src/test/filesystem_test.cpp new file mode 100644 index 000000000..d6846a680 --- /dev/null +++ b/src/test/filesystem_test.cpp @@ -0,0 +1,48 @@ +/** + * @file filesystem_test.cpp + * @author Chase Geigle + */ + +#include "test/filesystem_test.h" +#include "util/filesystem.h" + +namespace meta +{ +namespace testing +{ + +namespace +{ +void num_lines_normal() +{ + { + std::string data = "this is a test\ntwo lines\n"; + std::ofstream file{"filesystem-temp.txt", std::ios::binary}; + file.write(data.c_str(), data.length()); + } + ASSERT_EQUAL(filesystem::num_lines("filesystem-temp.txt"), uint64_t{2}); +} + +void num_lines_notrailing() +{ + { + std::string data = "this is a test\ntwo lines but no last newline"; + std::ofstream file{"filesystem-temp.txt", std::ios::binary}; + file.write(data.c_str(), data.length()); + } + ASSERT_EQUAL(filesystem::num_lines("filesystem-temp.txt"), uint64_t{2}); +} +} + +int filesystem_tests() +{ + int failed = 0; + filesystem::delete_file("filessytem-temp.txt"); + failed += testing::run_test("num-lines-normal", num_lines_normal); + filesystem::delete_file("filessytem-temp.txt"); + failed += testing::run_test("num-lines-notrailing", num_lines_notrailing); + filesystem::delete_file("filessytem-temp.txt"); + return failed; +} +} +} diff --git a/src/test/tools/unit-test.cpp b/src/test/tools/unit-test.cpp index aadf37bec..895ebdb4d 100644 --- a/src/test/tools/unit-test.cpp +++ b/src/test/tools/unit-test.cpp @@ -21,6 +21,7 @@ #include "test/graph_test.h" #include "test/compression_test.h" #include "test/parser_test.h" +#include "test/filesystem_test.h" #include "util/printing.h" using namespace meta; @@ -47,6 +48,7 @@ int main(int argc, char* argv[]) std::cerr << " \"compression\": runs compression reading and writing tests" << std::endl; std::cerr << " \"graph\": runs undirected and directed graph tests" << std::endl; std::cerr << " \"parser\": runs parser tests" << std::endl; + std::cerr << " \"filesystem\": runs filesystem tests" << std::endl; return 1; } @@ -84,6 +86,8 @@ int main(int argc, char* argv[]) num_failed += testing::graph_tests(); if (all || args.find("parser") != args.end()) num_failed += testing::parser_tests(); + if (all || args.find("filesystem") != args.end()) + num_failed += testing::filesystem_tests(); return num_failed; } diff --git a/src/test/unit_tests.cmake b/src/test/unit_tests.cmake index 11abacb23..418f0cf5f 100644 --- a/src/test/unit_tests.cmake +++ b/src/test/unit_tests.cmake @@ -53,3 +53,7 @@ set_tests_properties(graph PROPERTIES TIMEOUT 10 WORKING_DIRECTORY add_test(parser ${UNIT_TEST_EXE} parser) set_tests_properties(parser PROPERTIES TIMEOUT 10 WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}) + +add_test(filesystem ${UNIT_TEST_EXE} filesystem) +set_tests_properties(filesystem PROPERTIES TIMEOUT 10 WORKING_DIRECTORY + ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})