]> git.mxchange.org Git - simgear.git/blob - simgear/misc/path_test.cxx
Fix #1783: repeated error message on console
[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 #ifdef _WIN32
145     COMPARE(pc.str(), std::string("/usr/local/include/;/opt/local"));
146 #else
147     COMPARE(pc.str(), std::string("/usr/local/include/:/opt/local"));
148 #endif
149
150 // concat
151     SGPath pd = pb;
152     pd.concat("-1");
153     COMPARE(pd.str(), std::string("/Foo/bar/something.png-1"));
154     
155 // create with relative path
156     SGPath rb(d1, "include/foo");
157     COMPARE(rb.str(), std::string("/usr/local/include/foo"));
158     VERIFY(rb.isAbsolute());
159     
160 // lower-casing of file extensions
161     SGPath extA("FOO.ZIP");
162     COMPARE(extA.base(), "FOO");
163     COMPARE(extA.extension(), "ZIP");
164     COMPARE(extA.lower_extension(), "zip");
165     COMPARE(extA.complete_lower_extension(), "zip");
166     
167     SGPath extB("BAH/FOO.HTML.GZ");
168     COMPARE(extB.extension(), "GZ");
169     COMPARE(extB.base(), "BAH/FOO.HTML");
170     COMPARE(extB.lower_extension(), "gz");
171     COMPARE(extB.complete_lower_extension(), "html.gz");
172 #ifdef _WIN32
173     COMPARE(d1.str_native(), std::string("\\usr\\local"));
174     
175     SGPath winAbs("C:\\Windows\\System32");
176     COMPARE(winAbs.str(), std::string("C:/Windows/System32"));
177 #else
178     COMPARE(d1.str_native(), std::string("/usr/local"));
179 #endif
180   
181 // paths with only the file components
182     SGPath pf("something.txt.gz");
183     COMPARE(pf.base(), "something.txt");
184     COMPARE(pf.file(), "something.txt.gz");
185     COMPARE(pf.dir(), "");
186     COMPARE(pf.lower_extension(), "gz");
187     COMPARE(pf.complete_lower_extension(), "txt.gz");
188
189     COMPARE(pf.canRead(), true);
190     COMPARE(pf.canWrite(), true);
191
192     SGPath pp(&validateNone);
193     COMPARE(pp.canRead(), false);
194     COMPARE(pp.canWrite(), false);
195
196     pp.append("./test-dir/file.txt");
197     COMPARE(pp.create_dir(0700), -3);
198
199     pp.setPermissionChecker(&validateRead);
200     COMPARE(pp.canRead(), true);
201     COMPARE(pp.canWrite(), false);
202     COMPARE(pp.create_dir(0700), -3);
203
204     pp.setPermissionChecker(&validateWrite);
205     COMPARE(pp.canRead(), false);
206     COMPARE(pp.canWrite(), true);
207
208     test_dir();
209     
210     cout << "all tests passed OK" << endl;
211     return 0; // passed
212 }
213