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