Blender  V3.3
util_path_test.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #include "testing/testing.h"
5 
6 #include "util/path.h"
7 
9 
10 /* ******** Tests for path_filename() ******** */
11 
12 #ifndef _WIN32
13 TEST(util_path_filename, simple_unix)
14 {
15  string str = path_filename("/tmp/foo.txt");
16  EXPECT_EQ(str, "foo.txt");
17 }
18 
19 TEST(util_path_filename, root_unix)
20 {
21  string str = path_filename("/");
22  EXPECT_EQ(str, "/");
23 }
24 
25 TEST(util_path_filename, last_slash_unix)
26 {
27  string str = path_filename("/tmp/foo.txt/");
28  EXPECT_EQ(str, ".");
29 }
30 
31 TEST(util_path_filename, alternate_slash_unix)
32 {
33  string str = path_filename("/tmp\\foo.txt");
34  EXPECT_EQ(str, "tmp\\foo.txt");
35 }
36 #endif /* !_WIN32 */
37 
38 TEST(util_path_filename, file_only)
39 {
40  string str = path_filename("foo.txt");
41  EXPECT_EQ(str, "foo.txt");
42 }
43 
44 TEST(util_path_filename, empty)
45 {
46  string str = path_filename("");
47  EXPECT_EQ(str, "");
48 }
49 
50 #ifdef _WIN32
51 TEST(util_path_filename, simple_windows)
52 {
53  string str = path_filename("C:\\tmp\\foo.txt");
54  EXPECT_EQ(str, "foo.txt");
55 }
56 
57 TEST(util_path_filename, root_windows)
58 {
59  string str = path_filename("C:\\");
60  EXPECT_EQ(str, "\\");
61 }
62 
63 TEST(util_path_filename, last_slash_windows)
64 {
65  string str = path_filename("C:\\tmp\\foo.txt\\");
66  EXPECT_EQ(str, ".");
67 }
68 
69 TEST(util_path_filename, alternate_slash_windows)
70 {
71  string str = path_filename("C:\\tmp/foo.txt");
72  EXPECT_EQ(str, "foo.txt");
73 }
74 #endif /* _WIN32 */
75 
76 /* ******** Tests for path_dirname() ******** */
77 
78 #ifndef _WIN32
79 TEST(util_path_dirname, simple_unix)
80 {
81  string str = path_dirname("/tmp/foo.txt");
82  EXPECT_EQ(str, "/tmp");
83 }
84 
85 TEST(util_path_dirname, root_unix)
86 {
87  string str = path_dirname("/");
88  EXPECT_EQ(str, "");
89 }
90 
91 TEST(util_path_dirname, last_slash_unix)
92 {
93  string str = path_dirname("/tmp/foo.txt/");
94  EXPECT_EQ(str, "/tmp/foo.txt");
95 }
96 
97 TEST(util_path_dirname, alternate_slash_unix)
98 {
99  string str = path_dirname("/tmp\\foo.txt");
100  EXPECT_EQ(str, "/");
101 }
102 #endif /* !_WIN32 */
103 
104 TEST(util_path_dirname, file_only)
105 {
106  string str = path_dirname("foo.txt");
107  EXPECT_EQ(str, "");
108 }
109 
110 TEST(util_path_dirname, empty)
111 {
112  string str = path_dirname("");
113  EXPECT_EQ(str, "");
114 }
115 
116 #ifdef _WIN32
117 TEST(util_path_dirname, simple_windows)
118 {
119  string str = path_dirname("C:\\tmp\\foo.txt");
120  EXPECT_EQ(str, "C:\\tmp");
121 }
122 
123 TEST(util_path_dirname, root_windows)
124 {
125  string str = path_dirname("C:\\");
126  EXPECT_EQ(str, "C:");
127 }
128 
129 TEST(util_path_dirname, last_slash_windows)
130 {
131  string str = path_dirname("C:\\tmp\\foo.txt\\");
132  EXPECT_EQ(str, "C:\\tmp\\foo.txt");
133 }
134 
135 TEST(util_path_dirname, alternate_slash_windows)
136 {
137  string str = path_dirname("C:\\tmp/foo.txt");
138  EXPECT_EQ(str, "C:\\tmp");
139 }
140 #endif /* _WIN32 */
141 
142 /* ******** Tests for path_join() ******** */
143 
144 TEST(util_path_join, empty_both)
145 {
146  string str = path_join("", "");
147  EXPECT_EQ(str, "");
148 }
149 
150 TEST(util_path_join, empty_directory)
151 {
152  string str = path_join("", "foo.txt");
153  EXPECT_EQ(str, "foo.txt");
154 }
155 
156 TEST(util_path_join, empty_filename)
157 {
158  string str = path_join("foo", "");
159  EXPECT_EQ(str, "foo");
160 }
161 
162 #ifndef _WIN32
163 TEST(util_path_join, simple_unix)
164 {
165  string str = path_join("foo", "bar");
166  EXPECT_EQ(str, "foo/bar");
167 }
168 
169 TEST(util_path_join, directory_slash_unix)
170 {
171  string str = path_join("foo/", "bar");
172  EXPECT_EQ(str, "foo/bar");
173 }
174 
175 TEST(util_path_join, filename_slash_unix)
176 {
177  string str = path_join("foo", "/bar");
178  EXPECT_EQ(str, "foo/bar");
179 }
180 
181 TEST(util_path_join, both_slash_unix)
182 {
183  string str = path_join("foo/", "/bar");
184  EXPECT_EQ(str, "foo//bar");
185 }
186 
187 TEST(util_path_join, directory_alternate_slash_unix)
188 {
189  string str = path_join("foo\\", "bar");
190  EXPECT_EQ(str, "foo\\/bar");
191 }
192 
193 TEST(util_path_join, filename_alternate_slash_unix)
194 {
195  string str = path_join("foo", "\\bar");
196  EXPECT_EQ(str, "foo/\\bar");
197 }
198 
199 TEST(util_path_join, both_alternate_slash_unix)
200 {
201  string str = path_join("foo", "\\bar");
202  EXPECT_EQ(str, "foo/\\bar");
203 }
204 
205 TEST(util_path_join, empty_dir_filename_slash_unix)
206 {
207  string str = path_join("", "/foo.txt");
208  EXPECT_EQ(str, "/foo.txt");
209 }
210 
211 TEST(util_path_join, empty_dir_filename_alternate_slash_unix)
212 {
213  string str = path_join("", "\\foo.txt");
214  EXPECT_EQ(str, "\\foo.txt");
215 }
216 
217 TEST(util_path_join, empty_filename_dir_slash_unix)
218 {
219  string str = path_join("foo/", "");
220  EXPECT_EQ(str, "foo/");
221 }
222 
223 TEST(util_path_join, empty_filename_dir_alternate_slash_unix)
224 {
225  string str = path_join("foo\\", "");
226  EXPECT_EQ(str, "foo\\");
227 }
228 #else /* !_WIN32 */
229 TEST(util_path_join, simple_windows)
230 {
231  string str = path_join("foo", "bar");
232  EXPECT_EQ(str, "foo\\bar");
233 }
234 
235 TEST(util_path_join, directory_slash_windows)
236 {
237  string str = path_join("foo\\", "bar");
238  EXPECT_EQ(str, "foo\\bar");
239 }
240 
241 TEST(util_path_join, filename_slash_windows)
242 {
243  string str = path_join("foo", "\\bar");
244  EXPECT_EQ(str, "foo\\bar");
245 }
246 
247 TEST(util_path_join, both_slash_windows)
248 {
249  string str = path_join("foo\\", "\\bar");
250  EXPECT_EQ(str, "foo\\\\bar");
251 }
252 
253 TEST(util_path_join, directory_alternate_slash_windows)
254 {
255  string str = path_join("foo/", "bar");
256  EXPECT_EQ(str, "foo/bar");
257 }
258 
259 TEST(util_path_join, filename_alternate_slash_windows)
260 {
261  string str = path_join("foo", "/bar");
262  EXPECT_EQ(str, "foo/bar");
263 }
264 
265 TEST(util_path_join, both_alternate_slash_windows)
266 {
267  string str = path_join("foo/", "/bar");
268  EXPECT_EQ(str, "foo//bar");
269 }
270 
271 TEST(util_path_join, empty_dir_filename_slash_windows)
272 {
273  string str = path_join("", "\\foo.txt");
274  EXPECT_EQ(str, "\\foo.txt");
275 }
276 
277 TEST(util_path_join, empty_dir_filename_alternate_slash_windows)
278 {
279  string str = path_join("", "/foo.txt");
280  EXPECT_EQ(str, "/foo.txt");
281 }
282 
283 TEST(util_path_join, empty_filename_dir_slash_windows)
284 {
285  string str = path_join("foo\\", "");
286  EXPECT_EQ(str, "foo\\");
287 }
288 
289 TEST(util_path_join, empty_filename_dir_alternate_slash_windows)
290 {
291  string str = path_join("foo/", "");
292  EXPECT_EQ(str, "foo/");
293 }
294 #endif /* !_WIN32 */
295 
296 /* ******** Tests for path_escape() ******** */
297 
298 TEST(util_path_escape, no_escape_chars)
299 {
300  string str = path_escape("/tmp/foo/bar");
301  EXPECT_EQ(str, "/tmp/foo/bar");
302 }
303 
304 TEST(util_path_escape, simple)
305 {
306  string str = path_escape("/tmp/foo bar");
307  EXPECT_EQ(str, "/tmp/foo\\ bar");
308 }
309 
310 TEST(util_path_escape, simple_end)
311 {
312  string str = path_escape("/tmp/foo/bar ");
313  EXPECT_EQ(str, "/tmp/foo/bar\\ ");
314 }
315 
316 TEST(util_path_escape, multiple)
317 {
318  string str = path_escape("/tmp/foo bar");
319  EXPECT_EQ(str, "/tmp/foo\\ \\ bar");
320 }
321 
322 TEST(util_path_escape, simple_multiple_end)
323 {
324  string str = path_escape("/tmp/foo/bar ");
325  EXPECT_EQ(str, "/tmp/foo/bar\\ \\ ");
326 }
327 
328 /* ******** Tests for path_is_relative() ******** */
329 
330 TEST(util_path_is_relative, filename)
331 {
332  bool is_relative = path_is_relative("foo.txt");
333  EXPECT_TRUE(is_relative);
334 }
335 
336 #ifndef _WIN32
337 TEST(util_path_is_relative, absolute_unix)
338 {
339  bool is_relative = path_is_relative("/tmp/foo.txt");
340  EXPECT_FALSE(is_relative);
341 }
342 
343 TEST(util_path_is_relative, relative_dir_unix)
344 {
345  bool is_relative = path_is_relative("tmp/foo.txt");
346  EXPECT_TRUE(is_relative);
347 }
348 
349 TEST(util_path_is_relative, absolute_windir_on_unix)
350 {
351  bool is_relative = path_is_relative("C:\\tmp\\foo.txt");
352  EXPECT_TRUE(is_relative);
353 }
354 
355 TEST(util_path_is_relative, relative_windir_on_unix)
356 {
357  bool is_relative = path_is_relative("tmp\\foo.txt");
358  EXPECT_TRUE(is_relative);
359 }
360 #endif /* !_WIN32 */
361 
362 #ifdef _WIN32
363 TEST(util_path_is_relative, absolute_windows)
364 {
365  bool is_relative = path_is_relative("C:\\tmp\\foo.txt");
366  EXPECT_FALSE(is_relative);
367 }
368 
369 TEST(util_path_is_relative, relative_dir_windows)
370 {
371  bool is_relative = path_is_relative("tmp\\foo.txt");
372  EXPECT_TRUE(is_relative);
373 }
374 
375 TEST(util_path_is_relative, absolute_unixdir_on_windows)
376 {
377  bool is_relative = path_is_relative("/tmp/foo.txt");
378  EXPECT_TRUE(is_relative);
379 }
380 
381 TEST(util_path_is_relative, relative_unixdir_on_windows)
382 {
383  bool is_relative = path_is_relative("tmp/foo.txt");
384  EXPECT_TRUE(is_relative);
385 }
386 #endif /* _WIN32 */
387 
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
#define str(s)
string path_dirname(const string &path)
Definition: path.cpp:399
bool path_is_relative(const string &path)
Definition: path.cpp:442
string path_join(const string &dir, const string &file)
Definition: path.cpp:413
string path_escape(const string &path)
Definition: path.cpp:435
string path_filename(const string &path)
Definition: path.cpp:376
CCL_NAMESPACE_BEGIN TEST(util_path_filename, simple_unix)