2 #include <simgear/compiler.h>
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>
19 simgear::Dir temp = simgear::Dir::tempDir("foo");
20 cout << "created:" << temp.path() << endl;
22 VERIFY(temp.exists());
23 VERIFY(temp.path().isDir());
24 VERIFY(!temp.path().isFile());
26 SGPath fileInDir = temp.file("foobaz");
27 VERIFY(!fileInDir.exists());
29 if (!temp.remove(true)) {
30 cout << "remove failed!" << endl;
33 cout << temp.path().modTime() << endl;
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)
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() );
50 SGPath::Permissions validateNone(const SGPath&)
52 SGPath::Permissions p;
58 SGPath::Permissions validateRead(const SGPath&)
60 SGPath::Permissions p;
66 SGPath::Permissions validateWrite(const SGPath&)
68 SGPath::Permissions p;
76 SGPath p(simgear::Dir::current().path());
78 simgear::Dir(p).remove(true);
80 VERIFY(p.isAbsolute());
81 COMPARE(p.create_dir(0755), 0);
83 SGPath sub = p / "subA" / "subB";
84 VERIFY(!sub.exists());
86 SGPath subFile = sub / "fileABC.txt";
87 COMPARE(subFile.create_dir(0755), 0);
88 VERIFY(!subFile.exists());
90 sub.set_cached(false);
94 SGPath sub2 = p / "subA" / "fileA";
98 for (int i = 0; i < 50; ++i) {
102 VERIFY(sub2.isFile());
103 COMPARE(sub2.sizeInBytes(), 250);
105 SGPath sub3 = p / "subß" / "file𝕽";
106 sub3.create_dir(0755);
109 sg_ofstream os(sub3);
110 VERIFY(os.is_open());
111 for (int i = 0; i < 20; ++i) {
112 os << "EFGH" << endl;
116 sub3.set_cached(false);
117 VERIFY(sub3.exists());
118 COMPARE(sub3.sizeInBytes(), 100);
119 COMPARE(sub3.file(), "file𝕽");
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");
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");
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𝕽");
137 int main(int argc, char* argv[])
141 COMPARE(pa.exists(), false);
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());
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());
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());
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"));
183 SGPath rd("where\\to\\begin.txt");
184 COMPARE(rd.utf8Str(), std::string("where/to/begin.txt"));
188 SGPath d1("/usr/local");
190 COMPARE(pc.utf8Str(), std::string("/usr/local"));
191 pc.append("include");
193 COMPARE(pc.utf8Str(), std::string("/usr/local/include"));
194 COMPARE(pc.file(), std::string("include"));
199 COMPARE(pd.utf8Str(), std::string("/Foo/bar/something.png-1"));
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());
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");
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");
219 SGPath winAbs("C:\\Windows\\System32");
220 COMPARE(winAbs.local8BitStr(), std::string("C:/Windows/System32"));
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");
232 COMPARE(pf.canRead(), true);
233 COMPARE(pf.canWrite(), true);
235 SGPath pp(&validateNone);
236 COMPARE(pp.canRead(), false);
237 COMPARE(pp.canWrite(), false);
239 pp.append("./test-dir/file.txt");
240 COMPARE(pp.create_dir(0700), -3);
242 pp.setPermissionChecker(&validateRead);
243 COMPARE(pp.canRead(), true);
244 COMPARE(pp.canWrite(), false);
245 COMPARE(pp.create_dir(0700), -3);
247 pp.setPermissionChecker(&validateWrite);
248 COMPARE(pp.canRead(), false);
249 COMPARE(pp.canWrite(), true);
255 cout << "all tests passed OK" << endl;