]> git.mxchange.org Git - simgear.git/blob - simgear/misc/path_test.cxx
d4a490c46fcaa068a6c68bde036a725f3c88a04d
[simgear.git] / simgear / misc / path_test.cxx
1
2 #include <simgear/compiler.h>
3
4 #include <iostream>
5 #include <cstdlib>
6
7 using std::cout;
8 using std::cerr;
9 using std::endl;
10
11 #define COMPARE(a, b) \
12     if ((a) != (b))  { \
13         cerr << "failed:" << #a << " != " << #b << endl; \
14         exit(1); \
15     }
16
17 #define VERIFY(a) \
18     if (!(a))  { \
19         cerr << "failed:" << #a << endl; \
20         exit(1); \
21     }
22     
23 #include <simgear/misc/sg_path.hxx>
24 #include <simgear/misc/sg_dir.hxx>
25
26 void test_dir()
27 {
28     simgear::Dir temp = simgear::Dir::tempDir("foo");
29     cout << "created:" << temp.path().str() << endl;
30   
31     VERIFY(temp.exists());
32     VERIFY(temp.path().isDir());
33     VERIFY(!temp.path().isFile());
34     
35     SGPath fileInDir = temp.file("foobaz");
36     VERIFY(!fileInDir.exists());
37     
38     if (!temp.remove(true)) {
39         cout << "remove failed!" << endl;
40     }
41     
42     cout << temp.path().modTime() << endl;
43 }
44
45 int main(int argc, char* argv[])
46 {
47     SGPath pa;
48     VERIFY(pa.isNull());
49     COMPARE(pa.exists(), false);
50     
51 // test basic parsing
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());
62     
63 // relative paths
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());
71     
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());
79     
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"));
88     
89 // path fixing
90     SGPath rd("where\\to\\begin.txt");
91     COMPARE(rd.str(), std::string("where/to/begin.txt"));
92     
93 // test modification
94 // append
95     SGPath d1("/usr/local");
96     SGPath pc = d1;
97     COMPARE(pc.str(), std::string("/usr/local"));
98     pc.append("include");
99     
100     COMPARE(pc.str(), std::string("/usr/local/include"));
101     COMPARE(pc.file(), std::string("include"));
102     
103 // add
104     pc.add("/opt/local");
105     COMPARE(pc.str(), std::string("/usr/local/include/:/opt/local"));
106     
107 // concat
108     SGPath pd = pb;
109     pd.concat("-1");
110     COMPARE(pd.str(), std::string("/Foo/bar/something.png-1"));
111     
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());
116     
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");
123     
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");
129 #ifdef _WIN32
130     COMPARE(d1.str_native(), std::string("\\usr\\local"));
131     
132     SGPath winAbs("C:\\Windows\\System32");
133     COMPARE(winAbs.str(), std::string("C:/Windows/System32"));
134 #else
135     COMPARE(d1.str_native(), std::string("/usr/local"));
136 #endif
137     
138     test_dir();
139     
140     cout << "all tests passed OK" << endl;
141     return 0; // passed
142 }
143