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