c91c032040563061ab02b025d1913ae7f220be77
[feisty_meow.git] / nucleus / library / tests_filesystem / test_filename.cpp
1 /*
2 *  Name   : test_filename
3 *  Author : Chris Koeritz
4 **
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                               *
11 */
12
13 #define DEBUG_FILENAME_TEST
14
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>
25
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;
35
36 class test_filename : virtual public unit_base, public virtual application_shell
37 {
38 public:
39   test_filename() : application_shell() {}
40   DEFINE_CLASS_NAME("test_filename");
41   virtual int execute();
42   void clean_sequel(astring &sequel);
43 };
44
45 void test_filename::clean_sequel(astring &sequel)
46 { sequel.replace_all('\\', '/'); }
47
48 int test_filename::execute()
49 {
50   FUNCDEF("execute")
51   {
52     // first test group.
53     filename gorgeola("");
54     ASSERT_FALSE(gorgeola.exists(), "an empty filename should not exist");
55   }
56
57   {
58     // second test group.
59     astring GROUP = "separate-- ";
60     filename turkey("/omega/ralph/turkey/buzzard.txt");
61     string_array pieces;
62     bool rooted;
63     turkey.separate(rooted, pieces);
64     ASSERT_TRUE(rooted, GROUP + "the rooted value is erreonous.");
65     ASSERT_TRUE(pieces[0].equal_to("omega"), GROUP + "the first piece didn't match.");
66     ASSERT_TRUE(pieces[1].equal_to("ralph"), GROUP + "the second piece didn't match.");
67     ASSERT_TRUE(pieces[2].equal_to("turkey"), GROUP + "the third piece didn't match.");
68     ASSERT_TRUE(pieces[3].equal_to("buzzard.txt"), GROUP + "the fourth piece didn't match.");
69     ASSERT_EQUAL(pieces.length(), 4, GROUP + "the list was the wrong length");
70   }
71
72   {
73     // third test group.
74     astring GROUP = "third: test compare_prefix ";
75     filename turkey("/omega/ralph/turkey/buzzard.txt");
76     filename murpin1("/omega");
77     filename murpin2("/omega/ralph");
78     filename murpin3("/omega/ralph/turkey");
79     filename murpin4("/omega/ralph/turkey/buzzard.txt");
80     filename murpin_x1("ralph/turkey/buzzard.txt");
81     filename murpin_x2("/omega/ralph/turkey/buzzard.txt2");
82     filename murpin_x3("/omega/turkey/buzzard.txt");
83     filename murpin_x4("/omega/ralph/turkey/b0/buzzard.txt");
84     filename murpin_x5("moomega/ralph/turkey");
85
86     astring sequel;
87     ASSERT_TRUE(murpin1.compare_prefix(turkey, sequel), GROUP + "first should match but didn't");
88     clean_sequel(sequel);
89     ASSERT_TRUE(sequel.equal_to("ralph/turkey/buzzard.txt"), GROUP + "first sequel was wrong");
90     ASSERT_TRUE(murpin2.compare_prefix(turkey, sequel), GROUP + "second should match but didn't");
91     clean_sequel(sequel);
92     ASSERT_TRUE(sequel.equal_to("turkey/buzzard.txt"), GROUP + "second sequel was wrong");
93     ASSERT_TRUE(murpin3.compare_prefix(turkey, sequel), GROUP + "third should match but didn't");
94     clean_sequel(sequel);
95     ASSERT_TRUE(sequel.equal_to("buzzard.txt"), GROUP + "third sequel was wrong");
96     ASSERT_TRUE(murpin4.compare_prefix(turkey, sequel), GROUP + "fourth should match but didn't");
97     ASSERT_FALSE(sequel.t(), GROUP + "fourth had a sequel but shouldn't");
98
99     ASSERT_FALSE(murpin_x1.compare_prefix(turkey, sequel),
100         GROUP + "x-first should not match but did");
101     ASSERT_FALSE(sequel.t(),
102         GROUP + "x-first had a sequel but shouldn't");
103     ASSERT_FALSE(murpin_x2.compare_prefix(turkey, sequel),
104         GROUP + "x-second should not match but did");
105     ASSERT_FALSE(sequel.t(),
106         GROUP + "x-second had a sequel but shouldn't");
107     ASSERT_FALSE(murpin_x3.compare_prefix(turkey, sequel),
108         GROUP + "x-third should not match but did");
109     ASSERT_FALSE(sequel.t(),
110         GROUP + "x-third had a sequel but shouldn't");
111     ASSERT_FALSE(murpin_x4.compare_prefix(turkey, sequel),
112         GROUP + "x-fourth should not match but did");
113     ASSERT_FALSE(sequel.t(),
114         GROUP + "x-fourth had a sequel but shouldn't");
115     ASSERT_FALSE(murpin_x5.compare_prefix(turkey, sequel),
116         GROUP + "x-fifth should not match but did");
117     ASSERT_FALSE(sequel.t(),
118         GROUP + "x-fifth had a sequel but shouldn't");
119
120     // check that the functions returning no sequel are still correct.
121     ASSERT_TRUE(murpin1.compare_prefix(turkey), GROUP + "the two versions differed!");
122     ASSERT_FALSE(murpin_x1.compare_prefix(turkey), GROUP + "x-the two versions differed!");
123   }
124
125   {
126     // fourth test group.
127     astring GROUP = "fourth: test compare_suffix ";
128     filename turkey("/omega/ralph/turkey/buzzard.txt");
129     filename murpin1("turkey\\buzzard.txt");
130     filename murpin2("turkey/buzzard.txt");
131     filename murpin3("ralph/turkey/buzzard.txt");
132     filename murpin4("omega/ralph/turkey/buzzard.txt");
133     filename murpin5("/omega/ralph/turkey/buzzard.txt");
134
135     ASSERT_TRUE(murpin1.compare_suffix(turkey), GROUP + "compare 1 failed");
136     ASSERT_TRUE(murpin2.compare_suffix(turkey), GROUP + "compare 2 failed");
137     ASSERT_TRUE(murpin3.compare_suffix(turkey), GROUP + "compare 3 failed");
138     ASSERT_TRUE(murpin4.compare_suffix(turkey), GROUP + "compare 4 failed");
139     ASSERT_TRUE(murpin5.compare_suffix(turkey), GROUP + "compare 5 failed");
140
141     ASSERT_FALSE(turkey.compare_suffix(murpin1), GROUP + "compare x.1 failed");
142   }
143
144   {
145     // fifth test group.
146     // tests out the canonicalization method on any parameters given on
147     // the command line, including the program name.
148     astring GROUP = "fifth: canonicalize command-line paths ";
149 //    log(GROUP, ALWAYS_PRINT);
150     for (int i = 0; i < application::_global_argc; i++) {
151       filename canony(application::_global_argv[i]);
152 //      log(a_sprintf("parm %d:\n\tfrom \"%s\"\n\t  to \"%s\"", i, application::_global_argv[i],
153 //           canony.raw().s()), ALWAYS_PRINT);
154
155 //hmmm: the above wasn't really a test so much as a look at what we did.
156 //      we should run the canonicalizer against a set of known paths so we can know what to
157 //      expect.
158
159     }
160   }
161
162   {
163     // sixth test group.
164     astring GROUP = "sixth: testing pop and push ";
165     // test dossy paths.
166     filename test1("c:/flug/blumen/klemper/smooden");
167 //log(astring("base=") + test1.basename(), ALWAYS_PRINT);
168     ASSERT_EQUAL(test1.basename(), astring("smooden"), GROUP + "basename 1 failed");
169 //log(astring("got past basename 1 test that was failing."));
170     ASSERT_EQUAL(test1.dirname(), filename("c:/flug/blumen/klemper"),
171         GROUP + "d-dirname 1 failed");
172 //log(astring("got past a test or so after that."));
173     filename test2 = test1;
174     astring popped = test2.pop();
175     ASSERT_EQUAL(popped, astring("smooden"), GROUP + "dpop 1 return failed");
176     ASSERT_EQUAL(test2, filename("c:/flug/blumen/klemper"), GROUP + "dpop 1 failed");
177     test2.pop();
178     test2.pop();
179     ASSERT_EQUAL(test2, filename("c:/flug"), GROUP + "dpop 2 failed");
180     popped = test2.pop();
181     ASSERT_EQUAL(popped, astring("flug"), GROUP + "dpop 1 return failed");
182     ASSERT_EQUAL(test2, filename("c:/"), GROUP + "dpop 3 failed");
183     test2.pop();
184     ASSERT_EQUAL(test2, filename("c:/"), GROUP + "dpop 3 failed");
185     test2.push("flug");
186     test2.push("blumen");
187     test2.push("klemper");
188     ASSERT_EQUAL(test2, filename("c:/flug/blumen/klemper"), GROUP + "dpush 1 failed");
189     // test unix paths.
190     filename test3("/flug/blumen/klemper/smooden");
191     ASSERT_EQUAL(test3.basename(), astring("smooden"), GROUP + "basename 1 failed");
192     ASSERT_EQUAL(test3.dirname(), filename("/flug/blumen/klemper"),
193         GROUP + "u-dirname 1 failed");
194     filename test4 = test3;
195     popped = test4.pop();
196     ASSERT_EQUAL(popped, astring("smooden"), GROUP + "upop 1 return failed");
197     ASSERT_EQUAL(test4, filename("/flug/blumen/klemper"), GROUP + "upop 1 failed");
198     test4.pop();
199     test4.pop();
200     ASSERT_EQUAL(test4, filename("/flug"), GROUP + "upop 2 failed");
201     popped = test4.pop();
202     ASSERT_EQUAL(popped, astring("flug"), GROUP + "upop 1 return failed");
203     ASSERT_EQUAL(test4, filename("/"), GROUP + "upop 3 failed");
204     test4.pop();
205     ASSERT_EQUAL(test4, filename("/"), GROUP + "upop 3 failed");
206     test4.push("flug");
207     test4.push("blumen");
208     test4.push("klemper");
209     ASSERT_EQUAL(test4, filename("/flug/blumen/klemper"), GROUP + "upush 1 failed");
210   }
211   {
212     // seventh test group.
213     astring GROUP = "seventh: testing pack and unpack ";
214     filename test1("/usr/local/athabasca");
215     byte_array packed;
216     int size_guess = test1.packed_size();
217     test1.pack(packed);
218     ASSERT_EQUAL(size_guess, packed.length(), GROUP + "packed_size 1 failed");
219     filename test2;
220     ASSERT_TRUE(test2.unpack(packed), GROUP + "unpack 1 failed");
221     ASSERT_EQUAL(test2, test1, GROUP + "packed contents differ, 1 failed");
222   }
223 #ifdef __WIN32__
224   {
225     // eighth test group is only for windows side.
226 //hmmm: might be nice to get the build machine launching this on a windows vm.
227     astring GROUP = "eighth: cygwin and msys paths ";
228     filename test1("/cygdrive/q/marbles");
229     ASSERT_EQUAL(test1, astring("q:\\marbles"), GROUP + "test 1 failed");
230     filename test2("/cygdrive/r");
231     ASSERT_EQUAL(test2, astring("r:\\"), GROUP + "test 2 failed");
232     filename test3("/cygdrive/r/");
233     ASSERT_EQUAL(test3, astring("r:\\"), GROUP + "test 3 failed");
234     filename test4("/cygdrive//");
235     ASSERT_EQUAL(test4, astring("\\cygdrive"), GROUP + "test 4 failed");
236     filename test5("/cygdrive/");
237     ASSERT_EQUAL(test5, astring("\\cygdrive"), GROUP + "test 5 failed");
238     filename test6("/cygdrive");
239     ASSERT_EQUAL(test6, astring("\\cygdrive"), GROUP + "test 6 failed");
240     filename test7("/klaunspendle");
241     ASSERT_EQUAL(test7, astring("\\klaunspendle"), GROUP + "test 7 failed");
242     filename test8("z:/klaunspendle");
243     ASSERT_EQUAL(test8, astring("z:\\klaunspendle"), GROUP + "test 8 failed");
244
245     filename test10("/q/borkage");
246     ASSERT_EQUAL(test10, astring("q:\\borkage"), GROUP + "test 10 failed");
247     filename test11("/q/r");
248     ASSERT_EQUAL(test11, astring("q:\\r"), GROUP + "test 11 failed");
249     filename test12("/q/r/");
250     ASSERT_EQUAL(test12, astring("q:\\r"), GROUP + "test 12 failed");
251     filename test13("/q/r/x");
252     ASSERT_EQUAL(test13, astring("q:\\r\\x"), GROUP + "test 13 failed");
253     filename test14("/r/");
254     ASSERT_EQUAL(test14, astring("r:\\"), GROUP + "test 14 failed");
255     filename test15("/r");
256     ASSERT_EQUAL(test15, astring("r:\\"), GROUP + "test 15 failed");
257     filename test16("/");
258     ASSERT_EQUAL(test16, astring("\\"), GROUP + "test 16 failed");
259     filename test17("r/");
260     ASSERT_EQUAL(test17, astring("r\\"), GROUP + "test 17 failed");
261     filename test18("/kr/soop");
262     ASSERT_EQUAL(test18, astring("\\kr\\soop"), GROUP + "test 18 failed");
263   }
264 #endif
265
266   return final_report();
267 }
268
269 HOOPLE_MAIN(test_filename, )
270