1 #include "FilesystemTest.hpp"
3 #include "io/filesystem.hpp"
7 CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::FilesystemTest);
15 void FilesystemTest::setUp() {
16 test_dir.reset(new TempDir());
19 void FilesystemTest::tearDown() {
24 void FilesystemTest::testFile() {
26 const string test_file = test_dir->Path() + "\\test-file.txt";
28 const string test_file = test_dir->Path() + "/test-file";
31 CPPUNIT_ASSERT_MESSAGE(
32 "inexistant file is file",
34 CPPUNIT_ASSERT_EQUAL_MESSAGE(
35 "mtime of inexistant file should be zero",
36 time_t(0), file_mtime(test_file));
37 CPPUNIT_ASSERT_MESSAGE(
38 "inexistant file is a directory",
42 ofstream file(test_file);
43 file << "hello" << endl;
45 time_t now = time(nullptr);
46 CPPUNIT_ASSERT_MESSAGE(
47 "existing file not a file",
49 CPPUNIT_ASSERT_MESSAGE(
50 "mtime of existing file should be somewhere around now",
51 // let's assume that creating the file takes less than five seconds
52 abs(now - file_mtime(test_file) < 5));
53 CPPUNIT_ASSERT_MESSAGE(
54 "regular file is a directory",
57 CPPUNIT_ASSERT_MESSAGE(
58 "failed to remove test file",
59 remove_file(test_file));
61 CPPUNIT_ASSERT_MESSAGE(
62 "removed file is still a file",
64 CPPUNIT_ASSERT_EQUAL_MESSAGE(
65 "mtime of removed file should be zero",
66 time_t(0), file_mtime(test_file));
67 CPPUNIT_ASSERT_MESSAGE(
68 "removed file became a directory",
72 void FilesystemTest::testDirectory() {
74 const string test_subdir = test_dir->Path() + "\\a";
75 const string test_subsubdir = test_subdir + "\\b";
76 const string test_file = test_subsubdir + "\\c.txt";
78 const string test_subdir = test_dir->Path() + "/a";
79 const string test_subsubdir = test_subdir + "/b";
80 const string test_file = test_subsubdir + "/c";
83 CPPUNIT_ASSERT_MESSAGE(
84 "inexistant directory is a file",
85 !is_file(test_subdir));
86 CPPUNIT_ASSERT_MESSAGE(
87 "inexistant directory is a directory",
88 !is_dir(test_subdir));
90 CPPUNIT_ASSERT_MESSAGE(
91 "failed to create test subdir",
92 make_dir(test_subdir));
93 CPPUNIT_ASSERT_MESSAGE(
94 "created directory is a file",
95 !is_file(test_subdir));
96 CPPUNIT_ASSERT_MESSAGE(
97 "created directory is not a directory",
100 CPPUNIT_ASSERT_MESSAGE(
101 "failed to remove test subdir",
102 remove_dir(test_subdir));
103 CPPUNIT_ASSERT_MESSAGE(
104 "removed directory became a file",
105 !is_file(test_subdir));
106 CPPUNIT_ASSERT_MESSAGE(
107 "removed directory is still a directory",
108 !is_dir(test_subdir));
110 CPPUNIT_ASSERT_MESSAGE(
111 "failed to create test subdirs",
112 make_dirs(test_subsubdir));
113 CPPUNIT_ASSERT_MESSAGE(
114 "created directory is a file",
115 !is_file(test_subdir));
116 CPPUNIT_ASSERT_MESSAGE(
117 "created directory is not a directory",
118 is_dir(test_subdir));
119 CPPUNIT_ASSERT_MESSAGE(
120 "created directory is a file",
121 !is_file(test_subsubdir));
122 CPPUNIT_ASSERT_MESSAGE(
123 "created directory is not a directory",
124 is_dir(test_subsubdir));
127 ofstream file(test_file);
128 file << "hello" << endl;
130 CPPUNIT_ASSERT_MESSAGE(
131 "failed to create test file",
134 CPPUNIT_ASSERT_MESSAGE(
135 "failed to remove test subdir",
136 remove_dir(test_subdir));
137 CPPUNIT_ASSERT_MESSAGE(
138 "removed directory became a file",
139 !is_file(test_subdir));
140 CPPUNIT_ASSERT_MESSAGE(
141 "removed directory is still a directory",
142 !is_dir(test_subdir));
143 CPPUNIT_ASSERT_MESSAGE(
144 "removed directory became a file",
145 !is_file(test_subsubdir));
146 CPPUNIT_ASSERT_MESSAGE(
147 "removed directory is still a directory",
148 !is_dir(test_subsubdir));
149 CPPUNIT_ASSERT_MESSAGE(
150 "removed file became a directory",
152 CPPUNIT_ASSERT_MESSAGE(
153 "removed file is still a file",
154 !is_file(test_file));