first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / 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     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");
68   }
69
70   {
71     // third test group.
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");
83
84     astring sequel;
85     ASSERT_TRUE(murpin1.compare_prefix(turkey, sequel), GROUP + "first should match but didn't");
86     clean_sequel(sequel);
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");
89     clean_sequel(sequel);
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");
92     clean_sequel(sequel);
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");
96
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");
117
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!");
121   }
122
123   {
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");
132
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");
138
139     ASSERT_FALSE(turkey.compare_suffix(murpin1), GROUP + "compare x.1 failed");
140   }
141
142   {
143     // fifth test group.
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);
152
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
155 //      expect.
156
157     }
158   }
159
160   {
161     // sixth test group.
162     astring GROUP = "sixth: testing pop and push ";
163     // test dossy paths.
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");
175     test2.pop();
176     test2.pop();
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");
181     test2.pop();
182     ASSERT_EQUAL(test2, filename("c:/"), GROUP + "dpop 3 failed");
183     test2.push("flug");
184     test2.push("blumen");
185     test2.push("klemper");
186     ASSERT_EQUAL(test2, filename("c:/flug/blumen/klemper"), GROUP + "dpush 1 failed");
187     // test unix paths.
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");
196     test4.pop();
197     test4.pop();
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");
202     test4.pop();
203     ASSERT_EQUAL(test4, filename("/"), GROUP + "upop 3 failed");
204     test4.push("flug");
205     test4.push("blumen");
206     test4.push("klemper");
207     ASSERT_EQUAL(test4, filename("/flug/blumen/klemper"), GROUP + "upush 1 failed");
208   }
209   {
210     // seventh test group.
211     astring GROUP = "seventh: testing pack and unpack ";
212     filename test1("/usr/local/athabasca");
213     byte_array packed;
214     int size_guess = test1.packed_size();
215     test1.pack(packed);
216     ASSERT_EQUAL(size_guess, packed.length(), GROUP + "packed_size 1 failed");
217     filename test2;
218     ASSERT_TRUE(test2.unpack(packed), GROUP + "unpack 1 failed");
219     ASSERT_EQUAL(test2, test1, GROUP + "packed contents differ, 1 failed");
220   }
221
222   return final_report();
223 }
224
225 HOOPLE_MAIN(test_filename, )
226