From: Daniel Karbach Date: Mon, 14 Nov 2016 16:08:55 +0000 (+0100) Subject: unit tests for filesystem I/O X-Git-Url: http://git.localhorst.tv/?p=blank.git;a=commitdiff_plain;h=292a23aab0c4d837cb69aafde45a4d0cab980782 unit tests for filesystem I/O --- diff --git a/tst/io/FilesystemTest.cpp b/tst/io/FilesystemTest.cpp new file mode 100644 index 0000000..cd4e0ab --- /dev/null +++ b/tst/io/FilesystemTest.cpp @@ -0,0 +1,163 @@ +#include "FilesystemTest.hpp" + +#include "io/filesystem.hpp" + +#include + +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 index 0000000..2b30c5e --- /dev/null +++ b/tst/io/FilesystemTest.hpp @@ -0,0 +1,36 @@ +#ifndef BLANK_TEST_IO_FILESYSTEMTEST_HPP +#define BLANK_TEST_IO_FILESYSTEMTEST_HPP + +#include +#include + +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