3 * Author : Chris Koeritz
5 * Copyright (c) 1993-$now By Author. This program is free software; you can *
6 * redistribute it and/or modify it under the terms of the GNU General Public *
7 * License as published by the Free Software Foundation; either version 2 of *
8 * the License or (at your option) any later version. This is online at: *
9 * http://www.fsf.org/copyleft/gpl.html *
10 * Please send any updates to: fred@gruntose.com *
13 #define DEBUG_FILENAME_TEST
15 #include <application/hoople_main.h>
16 #include <basis/functions.h>
17 #include <basis/guards.h>
18 #include <basis/astring.h>
19 #include <loggers/critical_events.h>
20 #include <loggers/program_wide_logger.h>
21 #include <filesystem/filename.h>
22 #include <structures/static_memory_gremlin.h>
23 #include <structures/string_array.h>
24 #include <unit_test/unit_base.h>
26 using namespace application;
27 using namespace basis;
28 using namespace mathematics;
29 using namespace filesystem;
30 using namespace loggers;
31 using namespace structures;
32 using namespace textual;
33 using namespace timely;
34 using namespace unit_test;
36 class test_filename : virtual public unit_base, public virtual application_shell
39 test_filename() : application_shell() {}
40 DEFINE_CLASS_NAME("test_filename");
41 virtual int execute();
42 void clean_sequel(astring &sequel);
45 void test_filename::clean_sequel(astring &sequel)
46 { sequel.replace_all('\\', '/'); }
48 int test_filename::execute()
53 filename gorgeola("");
54 ASSERT_FALSE(gorgeola.exists(), "an empty filename should not exist");
58 //hmmm: totally hosed here due to differences on win32.
59 // we need to start in a proper state:
60 // the test strings should all be constructed using the virtual unix root! then the results will match.
62 // until we can fix this, gotta bail on running these.
67 astring GROUP = "separate-- ";
68 filename turkey("/omega/ralph/turkey/buzzard.txt");
71 turkey.separate(rooted, pieces);
72 ASSERT_TRUE(rooted, GROUP + "the rooted value is erreonous.");
73 ASSERT_TRUE(pieces[0].equal_to("omega"), GROUP + "the first piece didn't match.");
74 ASSERT_TRUE(pieces[1].equal_to("ralph"), GROUP + "the second piece didn't match.");
75 ASSERT_TRUE(pieces[2].equal_to("turkey"), GROUP + "the third piece didn't match.");
76 ASSERT_TRUE(pieces[3].equal_to("buzzard.txt"), GROUP + "the fourth piece didn't match.");
77 ASSERT_EQUAL(pieces.length(), 4, GROUP + "the list was the wrong length");
82 astring GROUP = "third: test compare_prefix ";
83 filename turkey("/omega/ralph/turkey/buzzard.txt");
84 filename murpin1("/omega");
85 filename murpin2("/omega/ralph");
86 filename murpin3("/omega/ralph/turkey");
87 filename murpin4("/omega/ralph/turkey/buzzard.txt");
88 filename murpin_x1("ralph/turkey/buzzard.txt");
89 filename murpin_x2("/omega/ralph/turkey/buzzard.txt2");
90 filename murpin_x3("/omega/turkey/buzzard.txt");
91 filename murpin_x4("/omega/ralph/turkey/b0/buzzard.txt");
92 filename murpin_x5("moomega/ralph/turkey");
95 ASSERT_TRUE(murpin1.compare_prefix(turkey, sequel), GROUP + "first should match but didn't");
97 ASSERT_TRUE(sequel.equal_to("ralph/turkey/buzzard.txt"), GROUP + "first sequel was wrong");
98 ASSERT_TRUE(murpin2.compare_prefix(turkey, sequel), GROUP + "second should match but didn't");
100 ASSERT_TRUE(sequel.equal_to("turkey/buzzard.txt"), GROUP + "second sequel was wrong");
101 ASSERT_TRUE(murpin3.compare_prefix(turkey, sequel), GROUP + "third should match but didn't");
102 clean_sequel(sequel);
103 ASSERT_TRUE(sequel.equal_to("buzzard.txt"), GROUP + "third sequel was wrong");
104 ASSERT_TRUE(murpin4.compare_prefix(turkey, sequel), GROUP + "fourth should match but didn't");
105 ASSERT_FALSE(sequel.t(), GROUP + "fourth had a sequel but shouldn't");
107 ASSERT_FALSE(murpin_x1.compare_prefix(turkey, sequel),
108 GROUP + "x-first should not match but did");
109 ASSERT_FALSE(sequel.t(),
110 GROUP + "x-first had a sequel but shouldn't");
111 ASSERT_FALSE(murpin_x2.compare_prefix(turkey, sequel),
112 GROUP + "x-second should not match but did");
113 ASSERT_FALSE(sequel.t(),
114 GROUP + "x-second had a sequel but shouldn't");
115 ASSERT_FALSE(murpin_x3.compare_prefix(turkey, sequel),
116 GROUP + "x-third should not match but did");
117 ASSERT_FALSE(sequel.t(),
118 GROUP + "x-third had a sequel but shouldn't");
119 ASSERT_FALSE(murpin_x4.compare_prefix(turkey, sequel),
120 GROUP + "x-fourth should not match but did");
121 ASSERT_FALSE(sequel.t(),
122 GROUP + "x-fourth had a sequel but shouldn't");
123 ASSERT_FALSE(murpin_x5.compare_prefix(turkey, sequel),
124 GROUP + "x-fifth should not match but did");
125 ASSERT_FALSE(sequel.t(),
126 GROUP + "x-fifth had a sequel but shouldn't");
128 // check that the functions returning no sequel are still correct.
129 ASSERT_TRUE(murpin1.compare_prefix(turkey), GROUP + "the two versions differed!");
130 ASSERT_FALSE(murpin_x1.compare_prefix(turkey), GROUP + "x-the two versions differed!");
134 // fourth test group.
135 astring GROUP = "fourth: test compare_suffix ";
136 filename turkey("/omega/ralph/turkey/buzzard.txt");
137 filename murpin1("turkey\\buzzard.txt");
138 filename murpin2("turkey/buzzard.txt");
139 filename murpin3("ralph/turkey/buzzard.txt");
140 filename murpin4("omega/ralph/turkey/buzzard.txt");
141 filename murpin5("/omega/ralph/turkey/buzzard.txt");
143 ASSERT_TRUE(murpin1.compare_suffix(turkey), GROUP + "compare 1 failed");
144 ASSERT_TRUE(murpin2.compare_suffix(turkey), GROUP + "compare 2 failed");
145 ASSERT_TRUE(murpin3.compare_suffix(turkey), GROUP + "compare 3 failed");
146 ASSERT_TRUE(murpin4.compare_suffix(turkey), GROUP + "compare 4 failed");
147 ASSERT_TRUE(murpin5.compare_suffix(turkey), GROUP + "compare 5 failed");
149 ASSERT_FALSE(turkey.compare_suffix(murpin1), GROUP + "compare x.1 failed");
154 // tests out the canonicalization method on any parameters given on
155 // the command line, including the program name.
156 astring GROUP = "fifth: canonicalize command-line paths ";
157 // log(GROUP, ALWAYS_PRINT);
158 for (int i = 0; i < application::_global_argc; i++) {
159 filename canony(application::_global_argv[i]);
160 // log(a_sprintf("parm %d:\n\tfrom \"%s\"\n\t to \"%s\"", i, application::_global_argv[i],
161 // canony.raw().s()), ALWAYS_PRINT);
163 //hmmm: the above wasn't really a test so much as a look at what we did.
164 // we should run the canonicalizer against a set of known paths so we can know what to
172 astring GROUP = "sixth: testing pop and push ";
174 filename test1("c:/flug/blumen/klemper/smooden");
175 //log(astring("base=") + test1.basename(), ALWAYS_PRINT);
176 ASSERT_EQUAL(test1.basename(), astring("smooden"), GROUP + "basename 1 failed");
177 //log(astring("got past basename 1 test that was failing."));
178 ASSERT_EQUAL(test1.dirname(), filename("c:/flug/blumen/klemper"),
179 GROUP + "d-dirname 1 failed");
180 //log(astring("got past a test or so after that."));
181 filename test2 = test1;
182 astring popped = test2.pop();
183 ASSERT_EQUAL(popped, astring("smooden"), GROUP + "dpop 1 return failed");
184 ASSERT_EQUAL(test2, filename("c:/flug/blumen/klemper"), GROUP + "dpop 1 failed");
187 ASSERT_EQUAL(test2, filename("c:/flug"), GROUP + "dpop 2 failed");
188 popped = test2.pop();
189 ASSERT_EQUAL(popped, astring("flug"), GROUP + "dpop 1 return failed");
190 ASSERT_EQUAL(test2, filename("c:/"), GROUP + "dpop 3 failed");
192 ASSERT_EQUAL(test2, filename("c:/"), GROUP + "dpop 3 failed");
194 test2.push("blumen");
195 test2.push("klemper");
196 ASSERT_EQUAL(test2, filename("c:/flug/blumen/klemper"), GROUP + "dpush 1 failed");
198 filename test3("/flug/blumen/klemper/smooden");
199 ASSERT_EQUAL(test3.basename(), astring("smooden"), GROUP + "basename 1 failed");
200 ASSERT_EQUAL(test3.dirname(), filename("/flug/blumen/klemper"),
201 GROUP + "u-dirname 1 failed");
202 filename test4 = test3;
203 popped = test4.pop();
204 ASSERT_EQUAL(popped, astring("smooden"), GROUP + "upop 1 return failed");
205 ASSERT_EQUAL(test4, filename("/flug/blumen/klemper"), GROUP + "upop 1 failed");
208 ASSERT_EQUAL(test4, filename("/flug"), GROUP + "upop 2 failed");
209 popped = test4.pop();
210 ASSERT_EQUAL(popped, astring("flug"), GROUP + "upop 1 return failed");
211 ASSERT_EQUAL(test4, filename("/"), GROUP + "upop 3 failed");
213 ASSERT_EQUAL(test4, filename("/"), GROUP + "upop 3 failed");
215 test4.push("blumen");
216 test4.push("klemper");
217 ASSERT_EQUAL(test4, filename("/flug/blumen/klemper"), GROUP + "upush 1 failed");
220 // seventh test group.
221 astring GROUP = "seventh: testing pack and unpack ";
222 filename test1("/usr/local/athabasca");
224 int size_guess = test1.packed_size();
226 ASSERT_EQUAL(size_guess, packed.length(), GROUP + "packed_size 1 failed");
228 ASSERT_TRUE(test2.unpack(packed), GROUP + "unpack 1 failed");
229 ASSERT_EQUAL(test2, test1, GROUP + "packed contents differ, 1 failed");
233 // eighth test group is only for windows side.
234 astring GROUP = "eighth: cygwin and msys paths ";
235 filename test1("/cygdrive/q/marbles");
236 ASSERT_EQUAL(test1, astring("q:/marbles"), GROUP + "test 1 failed");
237 filename test2("/cygdrive/r");
238 ASSERT_EQUAL(test2, astring("r:/"), GROUP + "test 2 failed");
239 filename test3("/cygdrive/r/");
240 ASSERT_EQUAL(test3, astring("r:/"), GROUP + "test 3 failed");
241 filename test4("/cygdrive//");
242 ASSERT_EQUAL(test4, astring("/cygdrive"), GROUP + "test 4 failed");
243 filename test5("/cygdrive/");
244 ASSERT_EQUAL(test5, astring("/cygdrive"), GROUP + "test 5 failed");
245 filename test6("/cygdrive");
246 ASSERT_EQUAL(test6, astring("/cygdrive"), GROUP + "test 6 failed");
247 filename test7("/klaunspendle");
248 ASSERT_EQUAL(test7, astring("/klaunspendle"), GROUP + "test 7 failed");
249 filename test8("z:/klaunspendle");
250 ASSERT_EQUAL(test8, astring("z:/klaunspendle"), GROUP + "test 8 failed");
252 filename test10("/q/borkage");
253 ASSERT_EQUAL(test10, astring("q:/borkage"), GROUP + "test 10 failed");
254 filename test11("/q/r");
255 ASSERT_EQUAL(test11, astring("q:/r"), GROUP + "test 11 failed");
256 filename test12("/q/r/");
257 ASSERT_EQUAL(test12, astring("q:/r"), GROUP + "test 12 failed");
258 filename test13("/q/r/x");
259 ASSERT_EQUAL(test13, astring("q:/r/x"), GROUP + "test 13 failed");
260 filename test14("/r/");
261 ASSERT_EQUAL(test14, astring("r:/"), GROUP + "test 14 failed");
262 filename test15("/r");
263 ASSERT_EQUAL(test15, astring("r:/"), GROUP + "test 15 failed");
264 filename test16("/");
265 ASSERT_EQUAL(test16, astring("/"), GROUP + "test 16 failed");
266 filename test17("r/");
267 ASSERT_EQUAL(test17, astring("r/"), GROUP + "test 17 failed");
268 filename test18("/kr/soop");
269 ASSERT_EQUAL(test18, astring("/kr/soop"), GROUP + "test 18 failed");
275 return final_report();
278 HOOPLE_MAIN(test_filename, )