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