]> git.lizzy.rs Git - dragonfireclient.git/blob - src/unittest/test_filepath.cpp
Revert "Replace MyEventReceiver KeyList with std::unordered_set" (#10622)
[dragonfireclient.git] / src / unittest / test_filepath.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "test.h"
21
22 #include <sstream>
23
24 #include "log.h"
25 #include "serialization.h"
26 #include "nodedef.h"
27 #include "noise.h"
28
29 class TestFilePath : public TestBase {
30 public:
31         TestFilePath() { TestManager::registerTestModule(this); }
32         const char *getName() { return "TestFilePath"; }
33
34         void runTests(IGameDef *gamedef);
35
36         void testIsDirDelimiter();
37         void testPathStartsWith();
38         void testRemoveLastPathComponent();
39         void testRemoveLastPathComponentWithTrailingDelimiter();
40         void testRemoveRelativePathComponent();
41 };
42
43 static TestFilePath g_test_instance;
44
45 void TestFilePath::runTests(IGameDef *gamedef)
46 {
47         TEST(testIsDirDelimiter);
48         TEST(testPathStartsWith);
49         TEST(testRemoveLastPathComponent);
50         TEST(testRemoveLastPathComponentWithTrailingDelimiter);
51         TEST(testRemoveRelativePathComponent);
52 }
53
54 ////////////////////////////////////////////////////////////////////////////////
55
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)
60 {
61         for (size_t i = 0; i < path.size(); ++i) {
62                 if (path[i] == '/') {
63                         path.replace(i, 1, DIR_DELIM);
64                         i += std::string(DIR_DELIM).size() - 1; // generally a no-op
65                 }
66         }
67
68         #ifdef _WIN32
69         if (path[0] == '\\')
70                 path = "C:" + path;
71         #endif
72
73         return path;
74 }
75
76
77 void TestFilePath::testIsDirDelimiter()
78 {
79         UASSERT(fs::IsDirDelimiter('/') == true);
80         UASSERT(fs::IsDirDelimiter('A') == false);
81         UASSERT(fs::IsDirDelimiter(0) == false);
82 #ifdef _WIN32
83         UASSERT(fs::IsDirDelimiter('\\') == true);
84 #else
85         UASSERT(fs::IsDirDelimiter('\\') == false);
86 #endif
87 }
88
89
90 void TestFilePath::testPathStartsWith()
91 {
92         const int numpaths = 12;
93         std::string paths[numpaths] = {
94                 "",
95                 p("/"),
96                 p("/home/user/minetest"),
97                 p("/home/user/minetest/bin"),
98                 p("/home/user/.minetest"),
99                 p("/tmp/dir/file"),
100                 p("/tmp/file/"),
101                 p("/tmP/file"),
102                 p("/tmp"),
103                 p("/tmp/dir"),
104                 p("/home/user2/minetest/worlds"),
105                 p("/home/user2/minetest/world"),
106         };
107         /*
108                 expected fs::PathStartsWith results
109                 0 = returns false
110                 1 = returns true
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
115         */
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},
129         };
130
131         for (int i = 0; i < numpaths; i++)
132         for (int j = 0; j < numpaths; j++){
133                 /*verbosestream<<"testing fs::PathStartsWith(\""
134                         <<paths[i]<<"\", \""
135                         <<paths[j]<<"\")"<<std::endl;*/
136                 bool starts = fs::PathStartsWith(paths[i], paths[j]);
137                 int expected = expected_results[i][j];
138                 if(expected == 0){
139                         UASSERT(starts == false);
140                 }
141                 else if(expected == 1){
142                         UASSERT(starts == true);
143                 }
144                 #ifdef _WIN32
145                 else if(expected == 2){
146                         UASSERT(starts == false);
147                 }
148                 else if(expected == 3){
149                         UASSERT(starts == true);
150                 }
151                 #else
152                 else if(expected == 2){
153                         UASSERT(starts == true);
154                 }
155                 else if(expected == 3){
156                         UASSERT(starts == false);
157                 }
158                 #endif
159                 else if(expected == 4){
160                         UASSERT(starts == (bool)FILESYS_CASE_INSENSITIVE);
161                 }
162         }
163 }
164
165
166 void TestFilePath::testRemoveLastPathComponent()
167 {
168         std::string path, result, removed;
169
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);
194 #ifdef _WIN32
195         UASSERT(result == "C:");
196 #else
197         UASSERT(result == "");
198 #endif
199         UASSERT(removed == p("home/user/minetest/bin/../worlds/world1"));
200 }
201
202
203 void TestFilePath::testRemoveLastPathComponentWithTrailingDelimiter()
204 {
205         std::string path, result, removed;
206
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);
230 #ifdef _WIN32
231         UASSERT(result == "C:");
232 #else
233         UASSERT(result == "");
234 #endif
235         UASSERT(removed == p("home/user/minetest/bin/../worlds/world1"));
236 }
237
238
239 void TestFilePath::testRemoveRelativePathComponent()
240 {
241         std::string path, result, removed;
242
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"));
252         path = p(".");
253         result = fs::RemoveRelativePathComponents(path);
254         UASSERT(result == "");
255         path = p("../a");
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"));
264 }