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");
59 astring GROUP = "separate-- ";
60 filename turkey("/omega/ralph/turkey/buzzard.txt");
62 turkey.separate(pieces);
63 ASSERT_TRUE(pieces[1].equal_to("omega"), GROUP + "the first piece didn't match.");
64 ASSERT_TRUE(pieces[2].equal_to("ralph"), GROUP + "the second piece didn't match.");
65 ASSERT_TRUE(pieces[3].equal_to("turkey"), GROUP + "the third piece didn't match.");
66 ASSERT_TRUE(pieces[4].equal_to("buzzard.txt"), GROUP + "the fourth piece didn't match.");
67 ASSERT_EQUAL(pieces.length(), 5, GROUP + "the list was the wrong length");
72 astring GROUP = "third: test compare_prefix ";
73 filename turkey("/omega/ralph/turkey/buzzard.txt");
74 filename murpin1("/omega");
75 filename murpin2("/omega/ralph");
76 filename murpin3("/omega/ralph/turkey");
77 filename murpin4("/omega/ralph/turkey/buzzard.txt");
78 filename murpin_x1("ralph/turkey/buzzard.txt");
79 filename murpin_x2("/omega/ralph/turkey/buzzard.txt2");
80 filename murpin_x3("/omega/turkey/buzzard.txt");
81 filename murpin_x4("/omega/ralph/turkey/b0/buzzard.txt");
82 filename murpin_x5("moomega/ralph/turkey");
85 ASSERT_TRUE(murpin1.compare_prefix(turkey, sequel), GROUP + "first should match but didn't");
87 ASSERT_TRUE(sequel.equal_to("ralph/turkey/buzzard.txt"), GROUP + "first sequel was wrong");
88 ASSERT_TRUE(murpin2.compare_prefix(turkey, sequel), GROUP + "second should match but didn't");
90 ASSERT_TRUE(sequel.equal_to("turkey/buzzard.txt"), GROUP + "second sequel was wrong");
91 ASSERT_TRUE(murpin3.compare_prefix(turkey, sequel), GROUP + "third should match but didn't");
93 ASSERT_TRUE(sequel.equal_to("buzzard.txt"), GROUP + "third sequel was wrong");
94 ASSERT_TRUE(murpin4.compare_prefix(turkey, sequel), GROUP + "fourth should match but didn't");
95 ASSERT_FALSE(sequel.t(), GROUP + "fourth had a sequel but shouldn't");
97 ASSERT_FALSE(murpin_x1.compare_prefix(turkey, sequel),
98 GROUP + "x-first should not match but did");
99 ASSERT_FALSE(sequel.t(),
100 GROUP + "x-first had a sequel but shouldn't");
101 ASSERT_FALSE(murpin_x2.compare_prefix(turkey, sequel),
102 GROUP + "x-second should not match but did");
103 ASSERT_FALSE(sequel.t(),
104 GROUP + "x-second had a sequel but shouldn't");
105 ASSERT_FALSE(murpin_x3.compare_prefix(turkey, sequel),
106 GROUP + "x-third should not match but did");
107 ASSERT_FALSE(sequel.t(),
108 GROUP + "x-third had a sequel but shouldn't");
109 ASSERT_FALSE(murpin_x4.compare_prefix(turkey, sequel),
110 GROUP + "x-fourth should not match but did");
111 ASSERT_FALSE(sequel.t(),
112 GROUP + "x-fourth had a sequel but shouldn't");
113 ASSERT_FALSE(murpin_x5.compare_prefix(turkey, sequel),
114 GROUP + "x-fifth should not match but did");
115 ASSERT_FALSE(sequel.t(),
116 GROUP + "x-fifth had a sequel but shouldn't");
118 // check that the functions returning no sequel are still correct.
119 ASSERT_TRUE(murpin1.compare_prefix(turkey), GROUP + "the two versions differed!");
120 ASSERT_FALSE(murpin_x1.compare_prefix(turkey), GROUP + "x-the two versions differed!");
124 // fourth test group.
125 astring GROUP = "fourth: test compare_suffix ";
126 filename turkey("/omega/ralph/turkey/buzzard.txt");
127 filename murpin1("turkey\\buzzard.txt");
128 filename murpin2("turkey/buzzard.txt");
129 filename murpin3("ralph/turkey/buzzard.txt");
130 filename murpin4("omega/ralph/turkey/buzzard.txt");
131 filename murpin5("/omega/ralph/turkey/buzzard.txt");
133 ASSERT_TRUE(murpin1.compare_suffix(turkey), GROUP + "compare 1 failed");
134 ASSERT_TRUE(murpin2.compare_suffix(turkey), GROUP + "compare 2 failed");
135 ASSERT_TRUE(murpin3.compare_suffix(turkey), GROUP + "compare 3 failed");
136 ASSERT_TRUE(murpin4.compare_suffix(turkey), GROUP + "compare 4 failed");
137 ASSERT_TRUE(murpin5.compare_suffix(turkey), GROUP + "compare 5 failed");
139 ASSERT_FALSE(turkey.compare_suffix(murpin1), GROUP + "compare x.1 failed");
144 // tests out the canonicalization method on any parameters given on
145 // the command line, including the program name.
146 astring GROUP = "fifth: canonicalize command-line paths ";
147 // log(GROUP, ALWAYS_PRINT);
148 for (int i = 0; i < application::_global_argc; i++) {
149 filename canony(application::_global_argv[i]);
150 // log(a_sprintf("parm %d:\n\tfrom \"%s\"\n\t to \"%s\"", i, application::_global_argv[i],
151 // canony.raw().s()), ALWAYS_PRINT);
153 //hmmm: the above wasn't really a test so much as a look at what we did.
154 // we should run the canonicalizer against a set of known paths so we can know what to
162 astring GROUP = "sixth: testing pop and push ";
164 filename test1("c:/flug/blumen/klemper/smooden");
165 //log(astring("base=") + test1.basename(), ALWAYS_PRINT);
166 ASSERT_EQUAL(test1.basename(), astring("smooden"), GROUP + "basename 1 failed");
167 //log(astring("got past basename 1 test that was failing."));
168 ASSERT_EQUAL(test1.dirname(), filename("c:/flug/blumen/klemper"),
169 GROUP + "d-dirname 1 failed");
170 //log(astring("got past a test or so after that."));
171 filename test2 = test1;
172 astring popped = test2.pop();
173 ASSERT_EQUAL(popped, astring("smooden"), GROUP + "dpop 1 return failed");
174 ASSERT_EQUAL(test2, filename("c:/flug/blumen/klemper"), GROUP + "dpop 1 failed");
177 ASSERT_EQUAL(test2, filename("c:/flug"), GROUP + "dpop 2 failed");
178 popped = test2.pop();
179 ASSERT_EQUAL(popped, astring("flug"), GROUP + "dpop 1 return failed");
180 ASSERT_EQUAL(test2, filename("c:/"), GROUP + "dpop 3 failed");
182 ASSERT_EQUAL(test2, filename("c:/"), GROUP + "dpop 3 failed");
184 test2.push("blumen");
185 test2.push("klemper");
186 ASSERT_EQUAL(test2, filename("c:/flug/blumen/klemper"), GROUP + "dpush 1 failed");
188 filename test3("/flug/blumen/klemper/smooden");
189 ASSERT_EQUAL(test3.basename(), astring("smooden"), GROUP + "basename 1 failed");
190 ASSERT_EQUAL(test3.dirname(), filename("/flug/blumen/klemper"),
191 GROUP + "u-dirname 1 failed");
192 filename test4 = test3;
193 popped = test4.pop();
194 ASSERT_EQUAL(popped, astring("smooden"), GROUP + "upop 1 return failed");
195 ASSERT_EQUAL(test4, filename("/flug/blumen/klemper"), GROUP + "upop 1 failed");
198 ASSERT_EQUAL(test4, filename("/flug"), GROUP + "upop 2 failed");
199 popped = test4.pop();
200 ASSERT_EQUAL(popped, astring("flug"), GROUP + "upop 1 return failed");
201 ASSERT_EQUAL(test4, filename("/"), GROUP + "upop 3 failed");
203 ASSERT_EQUAL(test4, filename("/"), GROUP + "upop 3 failed");
205 test4.push("blumen");
206 test4.push("klemper");
207 ASSERT_EQUAL(test4, filename("/flug/blumen/klemper"), GROUP + "upush 1 failed");
210 // seventh test group.
211 astring GROUP = "seventh: testing pack and unpack ";
212 filename test1("/usr/local/athabasca");
214 int size_guess = test1.packed_size();
216 ASSERT_EQUAL(size_guess, packed.length(), GROUP + "packed_size 1 failed");
218 ASSERT_TRUE(test2.unpack(packed), GROUP + "unpack 1 failed");
219 ASSERT_EQUAL(test2, test1, GROUP + "packed contents differ, 1 failed");
223 // eighth test group is only for windows side.
224 //hmmm: might be nice to get the build machine launching this on a windows vm.
225 astring GROUP = "eighth: cygwin and msys paths";
226 filename test1("/cygdrive/q/marbles");
227 ASSERT_EQUAL(test1, astring("q:\\marbles"), GROUP + "test 1 failed");
228 filename test2("/cygdrive/r");
229 ASSERT_EQUAL(test2, astring("r:\\"), GROUP + "test 2 failed");
230 filename test3("/cygdrive/r/");
231 ASSERT_EQUAL(test3, astring("r:\\"), GROUP + "test 3 failed");
232 filename test4("/cygdrive//");
233 ASSERT_EQUAL(test4, astring("\\cygdrive"), GROUP + "test 4 failed");
234 filename test5("/cygdrive/");
235 ASSERT_EQUAL(test5, astring("\\cygdrive"), GROUP + "test 5 failed");
236 filename test6("/cygdrive");
237 ASSERT_EQUAL(test6, astring("\\cygdrive"), GROUP + "test 6 failed");
238 filename test7("/klaunspendle");
239 ASSERT_EQUAL(test7, astring("\\klaunspendle"), GROUP + "test 7 failed");
240 filename test8("z:/klaunspendle");
241 ASSERT_EQUAL(test8, astring("z:\\klaunspendle"), GROUP + "test 8 failed");
243 filename test10("/q/borkage");
244 ASSERT_EQUAL(test10, astring("q:\\borkage"), GROUP + "test 10 failed");
245 filename test11("/q/r");
246 ASSERT_EQUAL(test11, astring("q:\\r"), GROUP + "test 11 failed");
247 filename test12("/q/r/");
248 ASSERT_EQUAL(test12, astring("q:\\r"), GROUP + "test 12 failed");
249 filename test13("/q/r/x");
250 ASSERT_EQUAL(test13, astring("q:\\r\\x"), GROUP + "test 13 failed");
251 filename test14("/r/");
252 ASSERT_EQUAL(test14, astring("r:\\"), GROUP + "test 14 failed");
253 filename test15("/r");
254 ASSERT_EQUAL(test15, astring("r:"), GROUP + "test 15 failed");
255 filename test16("/");
256 ASSERT_EQUAL(test16, astring("\\"), GROUP + "test 16 failed");
257 filename test17("r/");
258 ASSERT_EQUAL(test17, astring("r\\"), GROUP + "test 17 failed");
259 filename test18("/kr/soop");
260 ASSERT_NOT_EQUAL(test18, astring("\\kr\\soop"), GROUP + "test 18 failed");
264 return final_report();
267 HOOPLE_MAIN(test_filename, )