2 #include <simgear/compiler.h>
11 #define COMPARE(a, b) \
13 cerr << "failed:" << #a << " != " << #b << endl; \
19 cerr << "failed:" << #a << endl; \
23 #include <simgear/misc/sg_path.hxx>
24 #include <simgear/misc/sg_dir.hxx>
28 simgear::Dir temp = simgear::Dir::tempDir("foo");
29 cout << "created:" << temp.path().str() << endl;
31 VERIFY(temp.exists());
32 VERIFY(temp.path().isDir());
33 VERIFY(!temp.path().isFile());
35 SGPath fileInDir = temp.file("foobaz");
36 VERIFY(!fileInDir.exists());
38 if (!temp.remove(true)) {
39 cout << "remove failed!" << endl;
42 cout << temp.path().modTime() << endl;
45 int main(int argc, char* argv[])
49 COMPARE(pa.exists(), false);
52 SGPath pb("/Foo/bar/something.png");
53 COMPARE(pb.str(), std::string("/Foo/bar/something.png"));
54 COMPARE(strcmp(pb.c_str(), "/Foo/bar/something.png"), 0);
55 COMPARE(pb.dir(), std::string("/Foo/bar"));
56 COMPARE(pb.file(), std::string("something.png"));
57 COMPARE(pb.base(), std::string("/Foo/bar/something"));
58 COMPARE(pb.file_base(), std::string("something"));
59 COMPARE(pb.extension(), std::string("png"));
60 VERIFY(pb.isAbsolute());
61 VERIFY(!pb.isRelative());
64 SGPath ra("where/to/begin.txt");
65 COMPARE(ra.str(), std::string("where/to/begin.txt"));
66 COMPARE(ra.dir(), std::string("where/to"));
67 COMPARE(ra.file(), std::string("begin.txt"));
68 COMPARE(ra.file_base(), std::string("begin"));
69 VERIFY(!ra.isAbsolute());
70 VERIFY(ra.isRelative());
72 // dots in paths / missing extensions
73 SGPath pk("/Foo/bar.dot/thing");
74 COMPARE(pk.dir(), std::string("/Foo/bar.dot"));
75 COMPARE(pk.file(), std::string("thing"));
76 COMPARE(pk.base(), std::string("/Foo/bar.dot/thing"));
77 COMPARE(pk.file_base(), std::string("thing"));
78 COMPARE(pk.extension(), std::string());
80 // multiple file extensions
81 SGPath pj("/Foo/zot.dot/thing.tar.gz");
82 COMPARE(pj.dir(), std::string("/Foo/zot.dot"));
83 COMPARE(pj.file(), std::string("thing.tar.gz"));
84 COMPARE(pj.base(), std::string("/Foo/zot.dot/thing"));
85 COMPARE(pj.file_base(), std::string("thing"));
86 COMPARE(pj.extension(), std::string("gz"));
87 COMPARE(pj.complete_lower_extension(), std::string("tar.gz"));
90 SGPath rd("where\\to\\begin.txt");
91 COMPARE(rd.str(), std::string("where/to/begin.txt"));
95 SGPath d1("/usr/local");
97 COMPARE(pc.str(), std::string("/usr/local"));
100 COMPARE(pc.str(), std::string("/usr/local/include"));
101 COMPARE(pc.file(), std::string("include"));
104 pc.add("/opt/local");
105 COMPARE(pc.str(), std::string("/usr/local/include/:/opt/local"));
110 COMPARE(pd.str(), std::string("/Foo/bar/something.png-1"));
112 // create with relative path
113 SGPath rb(d1, "include/foo");
114 COMPARE(rb.str(), std::string("/usr/local/include/foo"));
115 VERIFY(rb.isAbsolute());
117 // lower-casing of file extensions
118 SGPath extA("FOO.ZIP");
119 COMPARE(extA.base(), "FOO");
120 COMPARE(extA.extension(), "ZIP");
121 COMPARE(extA.lower_extension(), "zip");
122 COMPARE(extA.complete_lower_extension(), "zip");
124 SGPath extB("BAH/FOO.HTML.GZ");
125 COMPARE(extB.extension(), "GZ");
126 COMPARE(extB.base(), "BAH/FOO");
127 COMPARE(extB.lower_extension(), "gz");
128 COMPARE(extB.complete_lower_extension(), "html.gz");
130 COMPARE(d1.str_native(), std::string("\\usr\\local"));
132 SGPath winAbs("C:\\Windows\\System32");
133 COMPARE(winAbs.str(), std::string("C:/Windows/System32"));
135 COMPARE(d1.str_native(), std::string("/usr/local"));
140 cout << "all tests passed OK" << endl;