]> git.mxchange.org Git - simgear.git/blob - simgear/misc/path_test.cxx
Change location used for path tests
[simgear.git] / simgear / misc / path_test.cxx
1
2 #include <simgear/compiler.h>
3
4 #include <iostream>
5 #include <cstdlib>
6 #include <cstring>
7
8 using std::cout;
9 using std::cerr;
10 using std::endl;
11
12 #include <simgear/misc/test_macros.hxx>
13 #include <simgear/misc/sg_path.hxx>
14 #include <simgear/misc/sg_dir.hxx>
15 #include <simgear/misc/sgstream.hxx>
16
17 void test_dir()
18 {
19     simgear::Dir temp = simgear::Dir::tempDir("foo");
20     cout << "created:" << temp.path() << endl;
21   
22     VERIFY(temp.exists());
23     VERIFY(temp.path().isDir());
24     VERIFY(!temp.path().isFile());
25     
26     SGPath fileInDir = temp.file("foobaz");
27     VERIFY(!fileInDir.exists());
28     
29     if (!temp.remove(true)) {
30         cout << "remove failed!" << endl;
31     }
32     
33     cout << temp.path().modTime() << endl;
34
35     std::cout << "Standard Locations:"
36               << "\n - Home:      " << SGPath::standardLocation(SGPath::HOME)
37               << "\n - Desktop:   " << SGPath::standardLocation(SGPath::DESKTOP)
38               << "\n - Downloads: " << SGPath::standardLocation(SGPath::DOWNLOADS)
39               << "\n - Documents: " << SGPath::standardLocation(SGPath::DOCUMENTS)
40               << "\n - Pictures:  " << SGPath::standardLocation(SGPath::PICTURES)
41               << std::endl;
42
43     VERIFY( !SGPath::standardLocation(SGPath::HOME     ).isNull() );
44     VERIFY( !SGPath::standardLocation(SGPath::DESKTOP  ).isNull() );
45     VERIFY( !SGPath::standardLocation(SGPath::DOWNLOADS).isNull() );
46     VERIFY( !SGPath::standardLocation(SGPath::DOCUMENTS).isNull() );
47     VERIFY( !SGPath::standardLocation(SGPath::PICTURES ).isNull() );
48 }
49
50 SGPath::Permissions validateNone(const SGPath&)
51 {
52   SGPath::Permissions p;
53   p.read = false;
54   p.write = false;
55   return p;
56 }
57
58 SGPath::Permissions validateRead(const SGPath&)
59 {
60   SGPath::Permissions p;
61   p.read = true;
62   p.write = false;
63   return p;
64 }
65
66 SGPath::Permissions validateWrite(const SGPath&)
67 {
68   SGPath::Permissions p;
69   p.read = false;
70   p.write = true;
71   return p;
72 }
73
74 void test_path_dir()
75 {
76         simgear::Dir temp = simgear::Dir::tempDir("path_dir");
77         temp.remove(true);
78     SGPath p = temp.path();
79
80         VERIFY(p.isAbsolute());
81         COMPARE(p.create_dir(0755), 0);
82
83         SGPath sub = p / "subA" / "subB";
84         VERIFY(!sub.exists());
85
86         SGPath subFile = sub / "fileABC.txt";
87         COMPARE(subFile.create_dir(0755), 0);
88         VERIFY(!subFile.exists());
89
90         sub.set_cached(false);
91         VERIFY(sub.exists());
92         VERIFY(sub.isDir());
93
94         SGPath sub2 = p / "subA" / "fileA";
95         {
96                 sg_ofstream os(sub2);
97         VERIFY(os.is_open());
98                 for (int i = 0; i < 50; ++i) {
99                         os << "ABCD" << endl;
100                 }
101         }
102         VERIFY(sub2.isFile());
103         COMPARE(sub2.sizeInBytes(), 250);
104
105     SGPath sub3 = p / "subß" / "file𝕽";
106     sub3.create_dir(0755);
107
108     {
109         sg_ofstream os(sub3);
110         VERIFY(os.is_open());
111         for (int i = 0; i < 20; ++i) {
112             os << "EFGH" << endl;
113         }
114     }
115
116     sub3.set_cached(false);
117     VERIFY(sub3.exists());
118     COMPARE(sub3.sizeInBytes(), 100);
119     COMPARE(sub3.file(), "file𝕽");
120
121         simgear::Dir subD(p / "subA");
122         simgear::PathList dirChildren = subD.children(simgear::Dir::TYPE_DIR | simgear::Dir::NO_DOT_OR_DOTDOT);
123         COMPARE(dirChildren.size(), 1);
124         COMPARE(dirChildren[0], subD.path() / "subB");
125
126         simgear::PathList fileChildren = subD.children(simgear::Dir::TYPE_FILE | simgear::Dir::NO_DOT_OR_DOTDOT);
127         COMPARE(fileChildren.size(), 1);
128         COMPARE(fileChildren[0], subD.path() / "fileA");
129
130     simgear::Dir subS(sub3.dirPath());
131     fileChildren = subS.children(simgear::Dir::TYPE_FILE | simgear::Dir::NO_DOT_OR_DOTDOT);
132     COMPARE(fileChildren.size(), 1);
133     COMPARE(fileChildren[0], subS.path() / "file𝕽");
134
135 }
136
137 int main(int argc, char* argv[])
138 {
139     SGPath pa;
140     VERIFY(pa.isNull());
141     COMPARE(pa.exists(), false);
142     
143 // test basic parsing
144     SGPath pb("/Foo/bar/something.png");
145     COMPARE(pb.utf8Str(), std::string("/Foo/bar/something.png"));
146     COMPARE(pb.local8BitStr(), std::string("/Foo/bar/something.png"));
147     COMPARE(pb.dir(), std::string("/Foo/bar"));
148     COMPARE(pb.file(), std::string("something.png"));
149     COMPARE(pb.base(), std::string("/Foo/bar/something"));
150     COMPARE(pb.file_base(), std::string("something"));
151     COMPARE(pb.extension(), std::string("png"));
152     VERIFY(pb.isAbsolute());
153     VERIFY(!pb.isRelative());
154     
155 // relative paths
156     SGPath ra("where/to/begin.txt");
157     COMPARE(ra.utf8Str(), std::string("where/to/begin.txt"));
158     COMPARE(ra.local8BitStr(), std::string("where/to/begin.txt"));
159     COMPARE(ra.dir(), std::string("where/to"));
160     COMPARE(ra.file(), std::string("begin.txt"));
161     COMPARE(ra.file_base(), std::string("begin"));
162     VERIFY(!ra.isAbsolute());
163     VERIFY(ra.isRelative());
164     
165 // dots in paths / missing extensions
166     SGPath pk("/Foo/bar.dot/thing");
167     COMPARE(pk.dir(), std::string("/Foo/bar.dot"));
168     COMPARE(pk.file(), std::string("thing"));
169     COMPARE(pk.base(), std::string("/Foo/bar.dot/thing"));
170     COMPARE(pk.file_base(), std::string("thing"));
171     COMPARE(pk.extension(), std::string());
172     
173 // multiple file extensions
174     SGPath pj("/Foo/zot.dot/thing.tar.gz");
175     COMPARE(pj.dir(), std::string("/Foo/zot.dot"));
176     COMPARE(pj.file(), std::string("thing.tar.gz"));
177     COMPARE(pj.base(), std::string("/Foo/zot.dot/thing.tar"));
178     COMPARE(pj.file_base(), std::string("thing"));
179     COMPARE(pj.extension(), std::string("gz"));
180     COMPARE(pj.complete_lower_extension(), std::string("tar.gz"));
181     
182 // path fixing
183     SGPath rd("where\\to\\begin.txt");
184     COMPARE(rd.utf8Str(), std::string("where/to/begin.txt"));
185     
186 // test modification
187 // append
188     SGPath d1("/usr/local");
189     SGPath pc = d1;
190     COMPARE(pc.utf8Str(), std::string("/usr/local"));
191     pc.append("include");
192     
193     COMPARE(pc.utf8Str(), std::string("/usr/local/include"));
194     COMPARE(pc.file(), std::string("include"));
195
196 // concat
197     SGPath pd = pb;
198     pd.concat("-1");
199     COMPARE(pd.utf8Str(), std::string("/Foo/bar/something.png-1"));
200     
201 // create with relative path
202     SGPath rb(d1, "include/foo");
203     COMPARE(rb.utf8Str(), std::string("/usr/local/include/foo"));
204     VERIFY(rb.isAbsolute());
205     
206 // lower-casing of file extensions
207     SGPath extA("FOO.ZIP");
208     COMPARE(extA.base(), "FOO");
209     COMPARE(extA.extension(), "ZIP");
210     COMPARE(extA.lower_extension(), "zip");
211     COMPARE(extA.complete_lower_extension(), "zip");
212     
213     SGPath extB("BAH/FOO.HTML.GZ");
214     COMPARE(extB.extension(), "GZ");
215     COMPARE(extB.base(), "BAH/FOO.HTML");
216     COMPARE(extB.lower_extension(), "gz");
217     COMPARE(extB.complete_lower_extension(), "html.gz");
218 #ifdef _WIN32
219     SGPath winAbs("C:\\Windows\\System32");
220     COMPARE(winAbs.local8BitStr(), std::string("C:/Windows/System32"));
221
222 #endif
223   
224 // paths with only the file components
225     SGPath pf("something.txt.gz");
226     COMPARE(pf.base(), "something.txt");
227     COMPARE(pf.file(), "something.txt.gz");
228     COMPARE(pf.dir(), "");
229     COMPARE(pf.lower_extension(), "gz");
230     COMPARE(pf.complete_lower_extension(), "txt.gz");
231
232     COMPARE(pf.canRead(), true);
233     COMPARE(pf.canWrite(), true);
234
235     SGPath pp(&validateNone);
236     COMPARE(pp.canRead(), false);
237     COMPARE(pp.canWrite(), false);
238
239     pp.append("./test-dir/file.txt");
240     COMPARE(pp.create_dir(0700), -3);
241
242     pp.setPermissionChecker(&validateRead);
243     COMPARE(pp.canRead(), true);
244     COMPARE(pp.canWrite(), false);
245     COMPARE(pp.create_dir(0700), -3);
246
247     pp.setPermissionChecker(&validateWrite);
248     COMPARE(pp.canRead(), false);
249     COMPARE(pp.canWrite(), true);
250
251     test_dir();
252     
253         test_path_dir();
254
255     cout << "all tests passed OK" << endl;
256     return 0; // passed
257 }
258