]> git.localhorst.tv Git - blank.git/blob - tst/io/FilesystemTest.cpp
unit tests for filesystem I/O
[blank.git] / tst / io / FilesystemTest.cpp
1 #include "FilesystemTest.hpp"
2
3 #include "io/filesystem.hpp"
4
5 #include <algorithm>
6
7 CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::FilesystemTest);
8
9 using namespace std;
10
11
12 namespace blank {
13 namespace test {
14
15 void FilesystemTest::setUp() {
16         test_dir = "test-dir";
17         CPPUNIT_ASSERT_MESSAGE(
18                 "failed to create test dir",
19                 make_dir(test_dir));
20 }
21
22 void FilesystemTest::tearDown() {
23         CPPUNIT_ASSERT_MESSAGE(
24                 "failed to remove test dir",
25                 remove_dir(test_dir));
26 }
27
28
29 void FilesystemTest::testFile() {
30 #ifdef _WIN32
31         const string test_file = test_dir + "\\test-file.txt";
32 #else
33         const string test_file = test_dir + "/test-file";
34 #endif
35
36         CPPUNIT_ASSERT_MESSAGE(
37                 "inexistant file is file",
38                 !is_file(test_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",
44                 !is_dir(test_file));
45
46         { // create file
47                 ofstream file(test_file);
48                 file << "hello" << endl;
49         }
50         time_t now = time(nullptr);
51         CPPUNIT_ASSERT_MESSAGE(
52                 "existing file not a file",
53                 is_file(test_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",
60                 !is_dir(test_file));
61
62         CPPUNIT_ASSERT_MESSAGE(
63                 "failed to remove test file",
64                 remove_file(test_file));
65
66         CPPUNIT_ASSERT_MESSAGE(
67                 "removed file is still a file",
68                 !is_file(test_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",
74                 !is_dir(test_file));
75 }
76
77 void FilesystemTest::testDirectory() {
78 #ifdef _WIN32
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";
82 #else
83         const string test_subdir = test_dir + "/a";
84         const string test_subsubdir = test_subdir + "/b";
85         const string test_file = test_subsubdir + "/c";
86 #endif
87
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));
94
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));
104
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));
114
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));
130
131         { // create file
132                 ofstream file(test_file);
133                 file << "hello" << endl;
134         }
135         CPPUNIT_ASSERT_MESSAGE(
136                 "failed to create test file",
137                 is_file(test_file));
138
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",
156                 !is_dir(test_file));
157         CPPUNIT_ASSERT_MESSAGE(
158                 "removed file is still a file",
159                 !is_file(test_file));
160 }
161
162 }
163 }