]> git.mxchange.org Git - simgear.git/blob - simgear/misc/path_test.cxx
bfe8754612e3ba47815f8e45ad6ccf3ea24d7336
[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     std::cout << "Standard Locations:"
46               << "\n - Home:      " << SGPath::standardLocation(SGPath::HOME)
47               << "\n - Desktop:   " << SGPath::standardLocation(SGPath::DESKTOP)
48               << "\n - Downloads: " << SGPath::standardLocation(SGPath::DOWNLOADS)
49               << "\n - Documents: " << SGPath::standardLocation(SGPath::DOCUMENTS)
50               << "\n - Pictures:  " << SGPath::standardLocation(SGPath::PICTURES)
51               << std::endl;
52
53     VERIFY( !SGPath::standardLocation(SGPath::HOME     ).isNull() );
54     VERIFY( !SGPath::standardLocation(SGPath::DESKTOP  ).isNull() );
55     VERIFY( !SGPath::standardLocation(SGPath::DOWNLOADS).isNull() );
56     VERIFY( !SGPath::standardLocation(SGPath::DOCUMENTS).isNull() );
57     VERIFY( !SGPath::standardLocation(SGPath::PICTURES ).isNull() );
58 }
59
60 SGPath::Permissions validateNone(const SGPath&)
61 {
62   SGPath::Permissions p;
63   p.read = false;
64   p.write = false;
65   return p;
66 }
67
68 SGPath::Permissions validateRead(const SGPath&)
69 {
70   SGPath::Permissions p;
71   p.read = true;
72   p.write = false;
73   return p;
74 }
75
76 SGPath::Permissions validateWrite(const SGPath&)
77 {
78   SGPath::Permissions p;
79   p.read = false;
80   p.write = true;
81   return p;
82 }
83
84 int main(int argc, char* argv[])
85 {
86     SGPath pa;
87     VERIFY(pa.isNull());
88     COMPARE(pa.exists(), false);
89     
90 // test basic parsing
91     SGPath pb("/Foo/bar/something.png");
92     COMPARE(pb.utf8Str(), std::string("/Foo/bar/something.png"));
93     COMPARE(pb.local8BitStr(), std::string("/Foo/bar/something.png"));
94     COMPARE(pb.dir(), std::string("/Foo/bar"));
95     COMPARE(pb.file(), std::string("something.png"));
96     COMPARE(pb.base(), std::string("/Foo/bar/something"));
97     COMPARE(pb.file_base(), std::string("something"));
98     COMPARE(pb.extension(), std::string("png"));
99     VERIFY(pb.isAbsolute());
100     VERIFY(!pb.isRelative());
101     
102 // relative paths
103     SGPath ra("where/to/begin.txt");
104     COMPARE(ra.utf8Str(), std::string("where/to/begin.txt"));
105     COMPARE(ra.local8BitStr(), std::string("where/to/begin.txt"));
106     COMPARE(ra.dir(), std::string("where/to"));
107     COMPARE(ra.file(), std::string("begin.txt"));
108     COMPARE(ra.file_base(), std::string("begin"));
109     VERIFY(!ra.isAbsolute());
110     VERIFY(ra.isRelative());
111     
112 // dots in paths / missing extensions
113     SGPath pk("/Foo/bar.dot/thing");
114     COMPARE(pk.dir(), std::string("/Foo/bar.dot"));
115     COMPARE(pk.file(), std::string("thing"));
116     COMPARE(pk.base(), std::string("/Foo/bar.dot/thing"));
117     COMPARE(pk.file_base(), std::string("thing"));
118     COMPARE(pk.extension(), std::string());
119     
120 // multiple file extensions
121     SGPath pj("/Foo/zot.dot/thing.tar.gz");
122     COMPARE(pj.dir(), std::string("/Foo/zot.dot"));
123     COMPARE(pj.file(), std::string("thing.tar.gz"));
124     COMPARE(pj.base(), std::string("/Foo/zot.dot/thing.tar"));
125     COMPARE(pj.file_base(), std::string("thing"));
126     COMPARE(pj.extension(), std::string("gz"));
127     COMPARE(pj.complete_lower_extension(), std::string("tar.gz"));
128     
129 // path fixing
130     SGPath rd("where\\to\\begin.txt");
131     COMPARE(rd.utf8Str(), std::string("where/to/begin.txt"));
132     
133 // test modification
134 // append
135     SGPath d1("/usr/local");
136     SGPath pc = d1;
137     COMPARE(pc.utf8Str(), std::string("/usr/local"));
138     pc.append("include");
139     
140     COMPARE(pc.utf8Str(), std::string("/usr/local/include"));
141     COMPARE(pc.file(), std::string("include"));
142
143 // concat
144     SGPath pd = pb;
145     pd.concat("-1");
146     COMPARE(pd.utf8Str(), std::string("/Foo/bar/something.png-1"));
147     
148 // create with relative path
149     SGPath rb(d1, "include/foo");
150     COMPARE(rb.utf8Str(), std::string("/usr/local/include/foo"));
151     VERIFY(rb.isAbsolute());
152     
153 // lower-casing of file extensions
154     SGPath extA("FOO.ZIP");
155     COMPARE(extA.base(), "FOO");
156     COMPARE(extA.extension(), "ZIP");
157     COMPARE(extA.lower_extension(), "zip");
158     COMPARE(extA.complete_lower_extension(), "zip");
159     
160     SGPath extB("BAH/FOO.HTML.GZ");
161     COMPARE(extB.extension(), "GZ");
162     COMPARE(extB.base(), "BAH/FOO.HTML");
163     COMPARE(extB.lower_extension(), "gz");
164     COMPARE(extB.complete_lower_extension(), "html.gz");
165 #ifdef _WIN32
166     SGPath winAbs("C:\\Windows\\System32");
167     COMPARE(winAbs.local8BitStr(), std::string("C:/Windows/System32"));
168
169 #endif
170   
171 // paths with only the file components
172     SGPath pf("something.txt.gz");
173     COMPARE(pf.base(), "something.txt");
174     COMPARE(pf.file(), "something.txt.gz");
175     COMPARE(pf.dir(), "");
176     COMPARE(pf.lower_extension(), "gz");
177     COMPARE(pf.complete_lower_extension(), "txt.gz");
178
179     COMPARE(pf.canRead(), true);
180     COMPARE(pf.canWrite(), true);
181
182     SGPath pp(&validateNone);
183     COMPARE(pp.canRead(), false);
184     COMPARE(pp.canWrite(), false);
185
186     pp.append("./test-dir/file.txt");
187     COMPARE(pp.create_dir(0700), -3);
188
189     pp.setPermissionChecker(&validateRead);
190     COMPARE(pp.canRead(), true);
191     COMPARE(pp.canWrite(), false);
192     COMPARE(pp.create_dir(0700), -3);
193
194     pp.setPermissionChecker(&validateWrite);
195     COMPARE(pp.canRead(), false);
196     COMPARE(pp.canWrite(), true);
197
198     test_dir();
199     
200     cout << "all tests passed OK" << endl;
201     return 0; // passed
202 }
203