]> git.localhorst.tv Git - blank.git/commitdiff
unit tests for filesystem I/O
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 14 Nov 2016 16:08:55 +0000 (17:08 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 14 Nov 2016 16:08:55 +0000 (17:08 +0100)
tst/io/FilesystemTest.cpp [new file with mode: 0644]
tst/io/FilesystemTest.hpp [new file with mode: 0644]

diff --git a/tst/io/FilesystemTest.cpp b/tst/io/FilesystemTest.cpp
new file mode 100644 (file)
index 0000000..cd4e0ab
--- /dev/null
@@ -0,0 +1,163 @@
+#include "FilesystemTest.hpp"
+
+#include "io/filesystem.hpp"
+
+#include <algorithm>
+
+CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::FilesystemTest);
+
+using namespace std;
+
+
+namespace blank {
+namespace test {
+
+void FilesystemTest::setUp() {
+       test_dir = "test-dir";
+       CPPUNIT_ASSERT_MESSAGE(
+               "failed to create test dir",
+               make_dir(test_dir));
+}
+
+void FilesystemTest::tearDown() {
+       CPPUNIT_ASSERT_MESSAGE(
+               "failed to remove test dir",
+               remove_dir(test_dir));
+}
+
+
+void FilesystemTest::testFile() {
+#ifdef _WIN32
+       const string test_file = test_dir + "\\test-file.txt";
+#else
+       const string test_file = test_dir + "/test-file";
+#endif
+
+       CPPUNIT_ASSERT_MESSAGE(
+               "inexistant file is file",
+               !is_file(test_file));
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "mtime of inexistant file should be zero",
+               time_t(0), file_mtime(test_file));
+       CPPUNIT_ASSERT_MESSAGE(
+               "inexistant file is a directory",
+               !is_dir(test_file));
+
+       { // create file
+               ofstream file(test_file);
+               file << "hello" << endl;
+       }
+       time_t now = time(nullptr);
+       CPPUNIT_ASSERT_MESSAGE(
+               "existing file not a file",
+               is_file(test_file));
+       CPPUNIT_ASSERT_MESSAGE(
+               "mtime of existing file should be somewhere around now",
+               // let's assume that creating the file takes less than five seconds
+               abs(now - file_mtime(test_file) < 5));
+       CPPUNIT_ASSERT_MESSAGE(
+               "regular file is a directory",
+               !is_dir(test_file));
+
+       CPPUNIT_ASSERT_MESSAGE(
+               "failed to remove test file",
+               remove_file(test_file));
+
+       CPPUNIT_ASSERT_MESSAGE(
+               "removed file is still a file",
+               !is_file(test_file));
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "mtime of removed file should be zero",
+               time_t(0), file_mtime(test_file));
+       CPPUNIT_ASSERT_MESSAGE(
+               "removed file became a directory",
+               !is_dir(test_file));
+}
+
+void FilesystemTest::testDirectory() {
+#ifdef _WIN32
+       const string test_subdir = test_dir + "\\a";
+       const string test_subsubdir = test_subdir + "\\b";
+       const string test_file = test_subsubdir + "\\c.txt";
+#else
+       const string test_subdir = test_dir + "/a";
+       const string test_subsubdir = test_subdir + "/b";
+       const string test_file = test_subsubdir + "/c";
+#endif
+
+       CPPUNIT_ASSERT_MESSAGE(
+               "inexistant directory is a file",
+               !is_file(test_subdir));
+       CPPUNIT_ASSERT_MESSAGE(
+               "inexistant directory is a directory",
+               !is_dir(test_subdir));
+
+       CPPUNIT_ASSERT_MESSAGE(
+               "failed to create test subdir",
+               make_dir(test_subdir));
+       CPPUNIT_ASSERT_MESSAGE(
+               "created directory is a file",
+               !is_file(test_subdir));
+       CPPUNIT_ASSERT_MESSAGE(
+               "created directory is not a directory",
+               is_dir(test_subdir));
+
+       CPPUNIT_ASSERT_MESSAGE(
+               "failed to remove test subdir",
+               remove_dir(test_subdir));
+       CPPUNIT_ASSERT_MESSAGE(
+               "removed directory became a file",
+               !is_file(test_subdir));
+       CPPUNIT_ASSERT_MESSAGE(
+               "removed directory is still a directory",
+               !is_dir(test_subdir));
+
+       CPPUNIT_ASSERT_MESSAGE(
+               "failed to create test subdirs",
+               make_dirs(test_subsubdir));
+       CPPUNIT_ASSERT_MESSAGE(
+               "created directory is a file",
+               !is_file(test_subdir));
+       CPPUNIT_ASSERT_MESSAGE(
+               "created directory is not a directory",
+               is_dir(test_subdir));
+       CPPUNIT_ASSERT_MESSAGE(
+               "created directory is a file",
+               !is_file(test_subsubdir));
+       CPPUNIT_ASSERT_MESSAGE(
+               "created directory is not a directory",
+               is_dir(test_subsubdir));
+
+       { // create file
+               ofstream file(test_file);
+               file << "hello" << endl;
+       }
+       CPPUNIT_ASSERT_MESSAGE(
+               "failed to create test file",
+               is_file(test_file));
+
+       CPPUNIT_ASSERT_MESSAGE(
+               "failed to remove test subdir",
+               remove_dir(test_subdir));
+       CPPUNIT_ASSERT_MESSAGE(
+               "removed directory became a file",
+               !is_file(test_subdir));
+       CPPUNIT_ASSERT_MESSAGE(
+               "removed directory is still a directory",
+               !is_dir(test_subdir));
+       CPPUNIT_ASSERT_MESSAGE(
+               "removed directory became a file",
+               !is_file(test_subsubdir));
+       CPPUNIT_ASSERT_MESSAGE(
+               "removed directory is still a directory",
+               !is_dir(test_subsubdir));
+       CPPUNIT_ASSERT_MESSAGE(
+               "removed file became a directory",
+               !is_dir(test_file));
+       CPPUNIT_ASSERT_MESSAGE(
+               "removed file is still a file",
+               !is_file(test_file));
+}
+
+}
+}
diff --git a/tst/io/FilesystemTest.hpp b/tst/io/FilesystemTest.hpp
new file mode 100644 (file)
index 0000000..2b30c5e
--- /dev/null
@@ -0,0 +1,36 @@
+#ifndef BLANK_TEST_IO_FILESYSTEMTEST_HPP
+#define BLANK_TEST_IO_FILESYSTEMTEST_HPP
+
+#include <string>
+#include <cppunit/extensions/HelperMacros.h>
+
+namespace blank {
+
+namespace test {
+
+class FilesystemTest
+: public CppUnit::TestFixture {
+
+CPPUNIT_TEST_SUITE(FilesystemTest);
+
+CPPUNIT_TEST(testFile);
+CPPUNIT_TEST(testDirectory);
+
+CPPUNIT_TEST_SUITE_END();
+
+public:
+       void setUp();
+       void tearDown();
+
+       void testFile();
+       void testDirectory();
+
+private:
+       std::string test_dir;
+
+};
+
+}
+}
+
+#endif