]> git.mxchange.org Git - simgear.git/blob - simgear/misc/path_test.cxx
Fix a bug affecting TerraGear, and extend unit-tests to cover this. (SGPath::file...
[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 #define COMPARE(a, b) \
13     if ((a) != (b))  { \
14         cerr << "failed:" << #a << " != " << #b << endl; \
15         exit(1); \
16     }
17
18 #define VERIFY(a) \
19     if (!(a))  { \
20         cerr << "failed:" << #a << endl; \
21         exit(1); \
22     }
23     
24 #include <simgear/misc/sg_path.hxx>
25 #include <simgear/misc/sg_dir.hxx>
26
27 void test_dir()
28 {
29     simgear::Dir temp = simgear::Dir::tempDir("foo");
30     cout << "created:" << temp.path() << endl;
31   
32     VERIFY(temp.exists());
33     VERIFY(temp.path().isDir());
34     VERIFY(!temp.path().isFile());
35     
36     SGPath fileInDir = temp.file("foobaz");
37     VERIFY(!fileInDir.exists());
38     
39     if (!temp.remove(true)) {
40         cout << "remove failed!" << endl;
41     }
42     
43     cout << temp.path().modTime() << endl;
44 }
45
46 int main(int argc, char* argv[])
47 {
48     SGPath pa;
49     VERIFY(pa.isNull());
50     COMPARE(pa.exists(), false);
51     
52 // test basic parsing
53     SGPath pb("/Foo/bar/something.png");
54     COMPARE(pb.str(), std::string("/Foo/bar/something.png"));
55     COMPARE(strcmp(pb.c_str(), "/Foo/bar/something.png"), 0);
56     COMPARE(pb.dir(), std::string("/Foo/bar"));
57     COMPARE(pb.file(), std::string("something.png"));
58     COMPARE(pb.base(), std::string("/Foo/bar/something"));
59     COMPARE(pb.file_base(), std::string("something"));
60     COMPARE(pb.extension(), std::string("png"));
61     VERIFY(pb.isAbsolute());
62     VERIFY(!pb.isRelative());
63     
64 // relative paths
65     SGPath ra("where/to/begin.txt");
66     COMPARE(ra.str(), std::string("where/to/begin.txt"));
67     COMPARE(ra.dir(), std::string("where/to"));
68     COMPARE(ra.file(), std::string("begin.txt"));
69     COMPARE(ra.file_base(), std::string("begin"));
70     VERIFY(!ra.isAbsolute());
71     VERIFY(ra.isRelative());
72     
73 // dots in paths / missing extensions
74     SGPath pk("/Foo/bar.dot/thing");
75     COMPARE(pk.dir(), std::string("/Foo/bar.dot"));
76     COMPARE(pk.file(), std::string("thing"));
77     COMPARE(pk.base(), std::string("/Foo/bar.dot/thing"));
78     COMPARE(pk.file_base(), std::string("thing"));
79     COMPARE(pk.extension(), std::string());
80     
81 // multiple file extensions
82     SGPath pj("/Foo/zot.dot/thing.tar.gz");
83     COMPARE(pj.dir(), std::string("/Foo/zot.dot"));
84     COMPARE(pj.file(), std::string("thing.tar.gz"));
85     COMPARE(pj.base(), std::string("/Foo/zot.dot/thing.tar"));
86     COMPARE(pj.file_base(), std::string("thing"));
87     COMPARE(pj.extension(), std::string("gz"));
88     COMPARE(pj.complete_lower_extension(), std::string("tar.gz"));
89     
90 // path fixing
91     SGPath rd("where\\to\\begin.txt");
92     COMPARE(rd.str(), std::string("where/to/begin.txt"));
93     
94 // test modification
95 // append
96     SGPath d1("/usr/local");
97     SGPath pc = d1;
98     COMPARE(pc.str(), std::string("/usr/local"));
99     pc.append("include");
100     
101     COMPARE(pc.str(), std::string("/usr/local/include"));
102     COMPARE(pc.file(), std::string("include"));
103     
104 // add
105     pc.add("/opt/local");
106     COMPARE(pc.str(), std::string("/usr/local/include/:/opt/local"));
107     
108 // concat
109     SGPath pd = pb;
110     pd.concat("-1");
111     COMPARE(pd.str(), std::string("/Foo/bar/something.png-1"));
112     
113 // create with relative path
114     SGPath rb(d1, "include/foo");
115     COMPARE(rb.str(), std::string("/usr/local/include/foo"));
116     VERIFY(rb.isAbsolute());
117     
118 // lower-casing of file extensions
119     SGPath extA("FOO.ZIP");
120     COMPARE(extA.base(), "FOO");
121     COMPARE(extA.extension(), "ZIP");
122     COMPARE(extA.lower_extension(), "zip");
123     COMPARE(extA.complete_lower_extension(), "zip");
124     
125     SGPath extB("BAH/FOO.HTML.GZ");
126     COMPARE(extB.extension(), "GZ");
127     COMPARE(extB.base(), "BAH/FOO.HTML");
128     COMPARE(extB.lower_extension(), "gz");
129     COMPARE(extB.complete_lower_extension(), "html.gz");
130 #ifdef _WIN32
131     COMPARE(d1.str_native(), std::string("\\usr\\local"));
132     
133     SGPath winAbs("C:\\Windows\\System32");
134     COMPARE(winAbs.str(), std::string("C:/Windows/System32"));
135 #else
136     COMPARE(d1.str_native(), std::string("/usr/local"));
137 #endif
138   
139 // paths with only the file components
140     SGPath pf("something.txt.gz");
141     COMPARE(pf.base(), "something.txt");
142     COMPARE(pf.file(), "something.txt.gz");
143     COMPARE(pf.dir(), "");
144     COMPARE(pf.lower_extension(), "gz");
145     COMPARE(pf.complete_lower_extension(), "txt.gz");
146     
147     test_dir();
148     
149     cout << "all tests passed OK" << endl;
150     return 0; // passed
151 }
152