3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "serialization.h"
29 class TestFilePath : public TestBase {
31 TestFilePath() { TestManager::registerTestModule(this); }
32 const char *getName() { return "TestFilePath"; }
34 void runTests(IGameDef *gamedef);
36 void testIsDirDelimiter();
37 void testPathStartsWith();
38 void testRemoveLastPathComponent();
39 void testRemoveLastPathComponentWithTrailingDelimiter();
40 void testRemoveRelativePathComponent();
43 static TestFilePath g_test_instance;
45 void TestFilePath::runTests(IGameDef *gamedef)
47 TEST(testIsDirDelimiter);
48 TEST(testPathStartsWith);
49 TEST(testRemoveLastPathComponent);
50 TEST(testRemoveLastPathComponentWithTrailingDelimiter);
51 TEST(testRemoveRelativePathComponent);
54 ////////////////////////////////////////////////////////////////////////////////
56 // adjusts a POSIX path to system-specific conventions
57 // -> changes '/' to DIR_DELIM
58 // -> absolute paths start with "C:\\" on windows
59 std::string p(std::string path)
61 for (size_t i = 0; i < path.size(); ++i) {
63 path.replace(i, 1, DIR_DELIM);
64 i += std::string(DIR_DELIM).size() - 1; // generally a no-op
77 void TestFilePath::testIsDirDelimiter()
79 UASSERT(fs::IsDirDelimiter('/') == true);
80 UASSERT(fs::IsDirDelimiter('A') == false);
81 UASSERT(fs::IsDirDelimiter(0) == false);
83 UASSERT(fs::IsDirDelimiter('\\') == true);
85 UASSERT(fs::IsDirDelimiter('\\') == false);
90 void TestFilePath::testPathStartsWith()
92 const int numpaths = 12;
93 std::string paths[numpaths] = {
96 p("/home/user/minetest"),
97 p("/home/user/minetest/bin"),
98 p("/home/user/.minetest"),
104 p("/home/user2/minetest/worlds"),
105 p("/home/user2/minetest/world"),
108 expected fs::PathStartsWith results
111 2 = returns false on windows, true elsewhere
112 3 = returns true on windows, false elsewhere
113 4 = returns true if and only if
114 FILESYS_CASE_INSENSITIVE is true
116 int expected_results[numpaths][numpaths] = {
117 {1,2,0,0,0,0,0,0,0,0,0,0},
118 {1,1,0,0,0,0,0,0,0,0,0,0},
119 {1,1,1,0,0,0,0,0,0,0,0,0},
120 {1,1,1,1,0,0,0,0,0,0,0,0},
121 {1,1,0,0,1,0,0,0,0,0,0,0},
122 {1,1,0,0,0,1,0,0,1,1,0,0},
123 {1,1,0,0,0,0,1,4,1,0,0,0},
124 {1,1,0,0,0,0,4,1,4,0,0,0},
125 {1,1,0,0,0,0,0,0,1,0,0,0},
126 {1,1,0,0,0,0,0,0,1,1,0,0},
127 {1,1,0,0,0,0,0,0,0,0,1,0},
128 {1,1,0,0,0,0,0,0,0,0,0,1},
131 for (int i = 0; i < numpaths; i++)
132 for (int j = 0; j < numpaths; j++){
133 /*verbosestream<<"testing fs::PathStartsWith(\""
135 <<paths[j]<<"\")"<<std::endl;*/
136 bool starts = fs::PathStartsWith(paths[i], paths[j]);
137 int expected = expected_results[i][j];
139 UASSERT(starts == false);
141 else if(expected == 1){
142 UASSERT(starts == true);
145 else if(expected == 2){
146 UASSERT(starts == false);
148 else if(expected == 3){
149 UASSERT(starts == true);
152 else if(expected == 2){
153 UASSERT(starts == true);
155 else if(expected == 3){
156 UASSERT(starts == false);
159 else if(expected == 4){
160 UASSERT(starts == (bool)FILESYS_CASE_INSENSITIVE);
166 void TestFilePath::testRemoveLastPathComponent()
168 std::string path, result, removed;
170 UASSERT(fs::RemoveLastPathComponent("") == "");
171 path = p("/home/user/minetest/bin/..//worlds/world1");
172 result = fs::RemoveLastPathComponent(path, &removed, 0);
173 UASSERT(result == path);
174 UASSERT(removed == "");
175 result = fs::RemoveLastPathComponent(path, &removed, 1);
176 UASSERT(result == p("/home/user/minetest/bin/..//worlds"));
177 UASSERT(removed == p("world1"));
178 result = fs::RemoveLastPathComponent(path, &removed, 2);
179 UASSERT(result == p("/home/user/minetest/bin/.."));
180 UASSERT(removed == p("worlds/world1"));
181 result = fs::RemoveLastPathComponent(path, &removed, 3);
182 UASSERT(result == p("/home/user/minetest/bin"));
183 UASSERT(removed == p("../worlds/world1"));
184 result = fs::RemoveLastPathComponent(path, &removed, 4);
185 UASSERT(result == p("/home/user/minetest"));
186 UASSERT(removed == p("bin/../worlds/world1"));
187 result = fs::RemoveLastPathComponent(path, &removed, 5);
188 UASSERT(result == p("/home/user"));
189 UASSERT(removed == p("minetest/bin/../worlds/world1"));
190 result = fs::RemoveLastPathComponent(path, &removed, 6);
191 UASSERT(result == p("/home"));
192 UASSERT(removed == p("user/minetest/bin/../worlds/world1"));
193 result = fs::RemoveLastPathComponent(path, &removed, 7);
195 UASSERT(result == "C:");
197 UASSERT(result == "");
199 UASSERT(removed == p("home/user/minetest/bin/../worlds/world1"));
203 void TestFilePath::testRemoveLastPathComponentWithTrailingDelimiter()
205 std::string path, result, removed;
207 path = p("/home/user/minetest/bin/..//worlds/world1/");
208 result = fs::RemoveLastPathComponent(path, &removed, 0);
209 UASSERT(result == path);
210 UASSERT(removed == "");
211 result = fs::RemoveLastPathComponent(path, &removed, 1);
212 UASSERT(result == p("/home/user/minetest/bin/..//worlds"));
213 UASSERT(removed == p("world1"));
214 result = fs::RemoveLastPathComponent(path, &removed, 2);
215 UASSERT(result == p("/home/user/minetest/bin/.."));
216 UASSERT(removed == p("worlds/world1"));
217 result = fs::RemoveLastPathComponent(path, &removed, 3);
218 UASSERT(result == p("/home/user/minetest/bin"));
219 UASSERT(removed == p("../worlds/world1"));
220 result = fs::RemoveLastPathComponent(path, &removed, 4);
221 UASSERT(result == p("/home/user/minetest"));
222 UASSERT(removed == p("bin/../worlds/world1"));
223 result = fs::RemoveLastPathComponent(path, &removed, 5);
224 UASSERT(result == p("/home/user"));
225 UASSERT(removed == p("minetest/bin/../worlds/world1"));
226 result = fs::RemoveLastPathComponent(path, &removed, 6);
227 UASSERT(result == p("/home"));
228 UASSERT(removed == p("user/minetest/bin/../worlds/world1"));
229 result = fs::RemoveLastPathComponent(path, &removed, 7);
231 UASSERT(result == "C:");
233 UASSERT(result == "");
235 UASSERT(removed == p("home/user/minetest/bin/../worlds/world1"));
239 void TestFilePath::testRemoveRelativePathComponent()
241 std::string path, result, removed;
243 path = p("/home/user/minetest/bin");
244 result = fs::RemoveRelativePathComponents(path);
245 UASSERT(result == path);
246 path = p("/home/user/minetest/bin/../worlds/world1");
247 result = fs::RemoveRelativePathComponents(path);
248 UASSERT(result == p("/home/user/minetest/worlds/world1"));
249 path = p("/home/user/minetest/bin/../worlds/world1/");
250 result = fs::RemoveRelativePathComponents(path);
251 UASSERT(result == p("/home/user/minetest/worlds/world1"));
253 result = fs::RemoveRelativePathComponents(path);
254 UASSERT(result == "");
256 result = fs::RemoveRelativePathComponents(path);
257 UASSERT(result == "");
258 path = p("./subdir/../..");
259 result = fs::RemoveRelativePathComponents(path);
260 UASSERT(result == "");
261 path = p("/a/b/c/.././../d/../e/f/g/../h/i/j/../../../..");
262 result = fs::RemoveRelativePathComponents(path);
263 UASSERT(result == p("/a/e"));