1 #include "FilesystemTest.hpp"
3 #include "io/filesystem.hpp"
7 CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::FilesystemTest);
15 void FilesystemTest::setUp() {
16 test_dir = "test-dir";
17 CPPUNIT_ASSERT_MESSAGE(
18 "failed to create test dir",
22 void FilesystemTest::tearDown() {
23 CPPUNIT_ASSERT_MESSAGE(
24 "failed to remove test dir",
25 remove_dir(test_dir));
29 void FilesystemTest::testFile() {
31 const string test_file = test_dir + "\\test-file.txt";
33 const string test_file = test_dir + "/test-file";
36 CPPUNIT_ASSERT_MESSAGE(
37 "inexistant file is file",
39 CPPUNIT_ASSERT_EQUAL_MESSAGE(
40 "mtime of inexistant file should be zero",
41 time_t(0), file_mtime(test_file));
42 CPPUNIT_ASSERT_MESSAGE(
43 "inexistant file is a directory",
47 ofstream file(test_file);
48 file << "hello" << endl;
50 time_t now = time(nullptr);
51 CPPUNIT_ASSERT_MESSAGE(
52 "existing file not a file",
54 CPPUNIT_ASSERT_MESSAGE(
55 "mtime of existing file should be somewhere around now",
56 // let's assume that creating the file takes less than five seconds
57 abs(now - file_mtime(test_file) < 5));
58 CPPUNIT_ASSERT_MESSAGE(
59 "regular file is a directory",
62 CPPUNIT_ASSERT_MESSAGE(
63 "failed to remove test file",
64 remove_file(test_file));
66 CPPUNIT_ASSERT_MESSAGE(
67 "removed file is still a file",
69 CPPUNIT_ASSERT_EQUAL_MESSAGE(
70 "mtime of removed file should be zero",
71 time_t(0), file_mtime(test_file));
72 CPPUNIT_ASSERT_MESSAGE(
73 "removed file became a directory",
77 void FilesystemTest::testDirectory() {
79 const string test_subdir = test_dir + "\\a";
80 const string test_subsubdir = test_subdir + "\\b";
81 const string test_file = test_subsubdir + "\\c.txt";
83 const string test_subdir = test_dir + "/a";
84 const string test_subsubdir = test_subdir + "/b";
85 const string test_file = test_subsubdir + "/c";
88 CPPUNIT_ASSERT_MESSAGE(
89 "inexistant directory is a file",
90 !is_file(test_subdir));
91 CPPUNIT_ASSERT_MESSAGE(
92 "inexistant directory is a directory",
93 !is_dir(test_subdir));
95 CPPUNIT_ASSERT_MESSAGE(
96 "failed to create test subdir",
97 make_dir(test_subdir));
98 CPPUNIT_ASSERT_MESSAGE(
99 "created directory is a file",
100 !is_file(test_subdir));
101 CPPUNIT_ASSERT_MESSAGE(
102 "created directory is not a directory",
103 is_dir(test_subdir));
105 CPPUNIT_ASSERT_MESSAGE(
106 "failed to remove test subdir",
107 remove_dir(test_subdir));
108 CPPUNIT_ASSERT_MESSAGE(
109 "removed directory became a file",
110 !is_file(test_subdir));
111 CPPUNIT_ASSERT_MESSAGE(
112 "removed directory is still a directory",
113 !is_dir(test_subdir));
115 CPPUNIT_ASSERT_MESSAGE(
116 "failed to create test subdirs",
117 make_dirs(test_subsubdir));
118 CPPUNIT_ASSERT_MESSAGE(
119 "created directory is a file",
120 !is_file(test_subdir));
121 CPPUNIT_ASSERT_MESSAGE(
122 "created directory is not a directory",
123 is_dir(test_subdir));
124 CPPUNIT_ASSERT_MESSAGE(
125 "created directory is a file",
126 !is_file(test_subsubdir));
127 CPPUNIT_ASSERT_MESSAGE(
128 "created directory is not a directory",
129 is_dir(test_subsubdir));
132 ofstream file(test_file);
133 file << "hello" << endl;
135 CPPUNIT_ASSERT_MESSAGE(
136 "failed to create test file",
139 CPPUNIT_ASSERT_MESSAGE(
140 "failed to remove test subdir",
141 remove_dir(test_subdir));
142 CPPUNIT_ASSERT_MESSAGE(
143 "removed directory became a file",
144 !is_file(test_subdir));
145 CPPUNIT_ASSERT_MESSAGE(
146 "removed directory is still a directory",
147 !is_dir(test_subdir));
148 CPPUNIT_ASSERT_MESSAGE(
149 "removed directory became a file",
150 !is_file(test_subsubdir));
151 CPPUNIT_ASSERT_MESSAGE(
152 "removed directory is still a directory",
153 !is_dir(test_subsubdir));
154 CPPUNIT_ASSERT_MESSAGE(
155 "removed file became a directory",
157 CPPUNIT_ASSERT_MESSAGE(
158 "removed file is still a file",
159 !is_file(test_file));