]> git.mxchange.org Git - simgear.git/blob - simgear/misc/path_test.cxx
94f9eaad296087a327e8669ecee9b157c45b9e9d
[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.str(), std::string("/Foo/bar/something.png"));
93     COMPARE(strcmp(pb.c_str(), "/Foo/bar/something.png"), 0);
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.str(), std::string("where/to/begin.txt"));
105     COMPARE(ra.dir(), std::string("where/to"));
106     COMPARE(ra.file(), std::string("begin.txt"));
107     COMPARE(ra.file_base(), std::string("begin"));
108     VERIFY(!ra.isAbsolute());
109     VERIFY(ra.isRelative());
110     
111 // dots in paths / missing extensions
112     SGPath pk("/Foo/bar.dot/thing");
113     COMPARE(pk.dir(), std::string("/Foo/bar.dot"));
114     COMPARE(pk.file(), std::string("thing"));
115     COMPARE(pk.base(), std::string("/Foo/bar.dot/thing"));
116     COMPARE(pk.file_base(), std::string("thing"));
117     COMPARE(pk.extension(), std::string());
118     
119 // multiple file extensions
120     SGPath pj("/Foo/zot.dot/thing.tar.gz");
121     COMPARE(pj.dir(), std::string("/Foo/zot.dot"));
122     COMPARE(pj.file(), std::string("thing.tar.gz"));
123     COMPARE(pj.base(), std::string("/Foo/zot.dot/thing.tar"));
124     COMPARE(pj.file_base(), std::string("thing"));
125     COMPARE(pj.extension(), std::string("gz"));
126     COMPARE(pj.complete_lower_extension(), std::string("tar.gz"));
127     
128 // path fixing
129     SGPath rd("where\\to\\begin.txt");
130     COMPARE(rd.str(), std::string("where/to/begin.txt"));
131     
132 // test modification
133 // append
134     SGPath d1("/usr/local");
135     SGPath pc = d1;
136     COMPARE(pc.str(), std::string("/usr/local"));
137     pc.append("include");
138     
139     COMPARE(pc.str(), std::string("/usr/local/include"));
140     COMPARE(pc.file(), std::string("include"));
141     
142 // add
143     pc.add("/opt/local");
144     COMPARE(pc.str(), std::string("/usr/local/include/:/opt/local"));
145     
146 // concat
147     SGPath pd = pb;
148     pd.concat("-1");
149     COMPARE(pd.str(), std::string("/Foo/bar/something.png-1"));
150     
151 // create with relative path
152     SGPath rb(d1, "include/foo");
153     COMPARE(rb.str(), std::string("/usr/local/include/foo"));
154     VERIFY(rb.isAbsolute());
155     
156 // lower-casing of file extensions
157     SGPath extA("FOO.ZIP");
158     COMPARE(extA.base(), "FOO");
159     COMPARE(extA.extension(), "ZIP");
160     COMPARE(extA.lower_extension(), "zip");
161     COMPARE(extA.complete_lower_extension(), "zip");
162     
163     SGPath extB("BAH/FOO.HTML.GZ");
164     COMPARE(extB.extension(), "GZ");
165     COMPARE(extB.base(), "BAH/FOO.HTML");
166     COMPARE(extB.lower_extension(), "gz");
167     COMPARE(extB.complete_lower_extension(), "html.gz");
168 #ifdef _WIN32
169     COMPARE(d1.str_native(), std::string("\\usr\\local"));
170     
171     SGPath winAbs("C:\\Windows\\System32");
172     COMPARE(winAbs.str(), std::string("C:/Windows/System32"));
173 #else
174     COMPARE(d1.str_native(), std::string("/usr/local"));
175 #endif
176   
177 // paths with only the file components
178     SGPath pf("something.txt.gz");
179     COMPARE(pf.base(), "something.txt");
180     COMPARE(pf.file(), "something.txt.gz");
181     COMPARE(pf.dir(), "");
182     COMPARE(pf.lower_extension(), "gz");
183     COMPARE(pf.complete_lower_extension(), "txt.gz");
184
185     COMPARE(pf.canRead(), true);
186     COMPARE(pf.canWrite(), true);
187
188     SGPath pp(&validateNone);
189     COMPARE(pp.canRead(), false);
190     COMPARE(pp.canWrite(), false);
191
192     pp.append("./test-dir/file.txt");
193     COMPARE(pp.create_dir(0700), -3);
194
195     pp.setPermissionChecker(&validateRead);
196     COMPARE(pp.canRead(), true);
197     COMPARE(pp.canWrite(), false);
198     COMPARE(pp.create_dir(0700), -3);
199
200     pp.setPermissionChecker(&validateWrite);
201     COMPARE(pp.canRead(), false);
202     COMPARE(pp.canWrite(), true);
203
204     test_dir();
205     
206     cout << "all tests passed OK" << endl;
207     return 0; // passed
208 }
209