]> git.localhorst.tv Git - gong.git/blob - tst/io/FilesystemTest.cpp
code, assets, and other stuff stolen from blank
[gong.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(gong::io::test::FilesystemTest);
8
9 using namespace std;
10
11
12 namespace gong {
13 namespace io {
14 namespace test {
15
16 void FilesystemTest::setUp() {
17         test_dir.reset(new TempDir());
18 }
19
20 void FilesystemTest::tearDown() {
21         test_dir.reset();
22 }
23
24
25 void FilesystemTest::testFile() {
26 #ifdef _WIN32
27         const string test_file = test_dir->Path() + "\\test-file.txt";
28 #else
29         const string test_file = test_dir->Path() + "/test-file";
30 #endif
31
32         CPPUNIT_ASSERT_MESSAGE(
33                 "inexistant file is file",
34                 !is_file(test_file));
35         CPPUNIT_ASSERT_EQUAL_MESSAGE(
36                 "mtime of inexistant file should be zero",
37                 time_t(0), file_mtime(test_file));
38         CPPUNIT_ASSERT_MESSAGE(
39                 "inexistant file is a directory",
40                 !is_dir(test_file));
41
42         { // create file
43                 ofstream file(test_file);
44                 file << "hello" << endl;
45         }
46         time_t now = time(nullptr);
47         CPPUNIT_ASSERT_MESSAGE(
48                 "existing file not a file",
49                 is_file(test_file));
50         CPPUNIT_ASSERT_MESSAGE(
51                 "mtime of existing file should be somewhere around now",
52                 // let's assume that creating the file takes less than five seconds
53                 abs(now - file_mtime(test_file) < 5));
54         CPPUNIT_ASSERT_MESSAGE(
55                 "regular file is a directory",
56                 !is_dir(test_file));
57
58         CPPUNIT_ASSERT_MESSAGE(
59                 "failed to remove test file",
60                 remove_file(test_file));
61
62         CPPUNIT_ASSERT_MESSAGE(
63                 "removed file is still a file",
64                 !is_file(test_file));
65         CPPUNIT_ASSERT_EQUAL_MESSAGE(
66                 "mtime of removed file should be zero",
67                 time_t(0), file_mtime(test_file));
68         CPPUNIT_ASSERT_MESSAGE(
69                 "removed file became a directory",
70                 !is_dir(test_file));
71 }
72
73 void FilesystemTest::testDirectory() {
74 #ifdef _WIN32
75         const string test_subdir = test_dir->Path() + "\\a";
76         const string test_subsubdir = test_subdir + "\\b";
77         const string test_file = test_subsubdir + "\\c.txt";
78 #else
79         const string test_subdir = test_dir->Path() + "/a";
80         const string test_subsubdir = test_subdir + "/b";
81         const string test_file = test_subsubdir + "/c";
82 #endif
83
84         CPPUNIT_ASSERT_MESSAGE(
85                 "inexistant directory is a file",
86                 !is_file(test_subdir));
87         CPPUNIT_ASSERT_MESSAGE(
88                 "inexistant directory is a directory",
89                 !is_dir(test_subdir));
90
91         CPPUNIT_ASSERT_MESSAGE(
92                 "failed to create test subdir",
93                 make_dir(test_subdir));
94         CPPUNIT_ASSERT_MESSAGE(
95                 "created directory is a file",
96                 !is_file(test_subdir));
97         CPPUNIT_ASSERT_MESSAGE(
98                 "created directory is not a directory",
99                 is_dir(test_subdir));
100
101         CPPUNIT_ASSERT_MESSAGE(
102                 "failed to remove test subdir",
103                 remove_dir(test_subdir));
104         CPPUNIT_ASSERT_MESSAGE(
105                 "removed directory became a file",
106                 !is_file(test_subdir));
107         CPPUNIT_ASSERT_MESSAGE(
108                 "removed directory is still a directory",
109                 !is_dir(test_subdir));
110
111         CPPUNIT_ASSERT_MESSAGE(
112                 "failed to create test subdirs",
113                 make_dirs(test_subsubdir));
114         CPPUNIT_ASSERT_MESSAGE(
115                 "created directory is a file",
116                 !is_file(test_subdir));
117         CPPUNIT_ASSERT_MESSAGE(
118                 "created directory is not a directory",
119                 is_dir(test_subdir));
120         CPPUNIT_ASSERT_MESSAGE(
121                 "created directory is a file",
122                 !is_file(test_subsubdir));
123         CPPUNIT_ASSERT_MESSAGE(
124                 "created directory is not a directory",
125                 is_dir(test_subsubdir));
126
127         { // create file
128                 ofstream file(test_file);
129                 file << "hello" << endl;
130         }
131         CPPUNIT_ASSERT_MESSAGE(
132                 "failed to create test file",
133                 is_file(test_file));
134
135         CPPUNIT_ASSERT_MESSAGE(
136                 "failed to remove test subdir",
137                 remove_dir(test_subdir));
138         CPPUNIT_ASSERT_MESSAGE(
139                 "removed directory became a file",
140                 !is_file(test_subdir));
141         CPPUNIT_ASSERT_MESSAGE(
142                 "removed directory is still a directory",
143                 !is_dir(test_subdir));
144         CPPUNIT_ASSERT_MESSAGE(
145                 "removed directory became a file",
146                 !is_file(test_subsubdir));
147         CPPUNIT_ASSERT_MESSAGE(
148                 "removed directory is still a directory",
149                 !is_dir(test_subsubdir));
150         CPPUNIT_ASSERT_MESSAGE(
151                 "removed file became a directory",
152                 !is_dir(test_file));
153         CPPUNIT_ASSERT_MESSAGE(
154                 "removed file is still a file",
155                 !is_file(test_file));
156 }
157
158 }
159 }
160 }