The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
test_serialization.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2009 - 2025
3 by Karol Nowak <grywacz@gmail.com>
4 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY.
12
13 See the COPYING file for more details.
14*/
15
16#define GETTEXT_DOMAIN "wesnoth-test"
17
18#include <array>
19#include <vector>
20#include <string>
24#include <boost/test/unit_test.hpp>
25
26namespace std {
27static std::ostream& operator<<(std::ostream& str, const std::pair<const std::string, std::string>& p)
28{
29 return str << '(' << p.first << " => " << p.second << ')';
30}
31}
32
33BOOST_AUTO_TEST_SUITE ( test_serialization_utils_and_unicode )
34
35BOOST_AUTO_TEST_CASE( utils_join_test )
36{
37 std::vector<std::string> fruit;
38
39 BOOST_CHECK( utils::join(fruit).empty() );
40 BOOST_CHECK( utils::join(fruit, "---").empty() );
41
42 fruit.push_back("apples");
43
44 BOOST_CHECK( utils::join(fruit) == "apples" );
45 BOOST_CHECK( utils::join(fruit, "---") == "apples" );
46
47 fruit.push_back("oranges");
48 fruit.push_back("lemons");
49
50 BOOST_CHECK( utils::join(fruit) == "apples,oranges,lemons" );
51 BOOST_CHECK( utils::join(fruit, "---") == "apples---oranges---lemons" );
52}
53
54BOOST_AUTO_TEST_CASE( utils_split_test )
55{
56 const std::string test_string = "a, , bb, ccc || d, ee,, fff | | g, , hh, iii";
57
58 {
59 auto split = utils::split(test_string);
60 std::array expect = {"a", "bb", "ccc || d", "ee", "fff | | g", "hh", "iii"};
61 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
62 }
63 {
64 auto split = utils::split(test_string, ',', utils::REMOVE_EMPTY | utils::STRIP_SPACES);
65 std::array expect = {"a", "bb", "ccc || d", "ee", "fff | | g", "hh", "iii"};
66 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
67 }
68 {
69 auto split = utils::split(test_string, ',', utils::REMOVE_EMPTY);
70 std::array expect = {"a", " ", " bb", " ccc || d", " ee", " fff | | g", " ", " hh", " iii"};
71 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
72 }
73 {
74 auto split = utils::split(test_string, ',', utils::STRIP_SPACES);
75 std::array expect = {"a", "", "bb", "ccc || d", "ee", "", "fff | | g", "", "hh", "iii"};
76 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
77 }
78 {
79 auto split = utils::split(test_string, ',', 0);
80 std::array expect = {"a", " ", " bb", " ccc || d", " ee", "", " fff | | g", " ", " hh", " iii"};
81 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
82 }
83 {
84 auto split = utils::split(test_string, '|');
85 std::array expect = {"a, , bb, ccc", "d, ee,, fff", "g, , hh, iii"};
86 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
87 }
88 {
89 auto split = utils::split(test_string, '|', utils::REMOVE_EMPTY | utils::STRIP_SPACES);
90 std::array expect = {"a, , bb, ccc", "d, ee,, fff", "g, , hh, iii"};
91 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
92 }
93 {
94 auto split = utils::split(test_string, '|', utils::REMOVE_EMPTY);
95 std::array expect = {"a, , bb, ccc ", " d, ee,, fff ", " ", " g, , hh, iii"};
96 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
97 }
98 {
99 auto split = utils::split(test_string, '|', utils::STRIP_SPACES);
100 std::array expect = {"a, , bb, ccc", "", "d, ee,, fff", "", "g, , hh, iii"};
101 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
102 }
103 {
104 auto split = utils::split(test_string, '|', 0);
105 std::array expect = {"a, , bb, ccc ", "", " d, ee,, fff ", " ", " g, , hh, iii"};
106 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
107 }
108}
109
110BOOST_AUTO_TEST_CASE( utils_quoted_split_test )
111{
112 const std::string test_string = "a, `, bb, ccc || d, ee,, fff | `| g, `, hh, iii";
113
114 {
115 auto split = utils::quoted_split(test_string, ',', utils::REMOVE_EMPTY | utils::STRIP_SPACES, '`');
116 std::array expect = {"a", "`, bb", "ccc || d", "ee", "fff | `| g", "`, hh", "iii"};
117 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
118 }
119 {
120 auto split = utils::quoted_split(test_string, ',', utils::REMOVE_EMPTY, '`');
121 std::array expect = {"a", " `, bb", " ccc || d", " ee", " fff | `| g", " `, hh", " iii"};
122 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
123 }
124 {
125 auto split = utils::quoted_split(test_string, ',', utils::STRIP_SPACES, '`');
126 std::array expect = {"a", "`, bb", "ccc || d", "ee", "", "fff | `| g", "`, hh", "iii"};
127 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
128 }
129 {
130 auto split = utils::quoted_split(test_string, ',', 0, '`');
131 std::array expect = {"a", " `, bb", " ccc || d", " ee", "", " fff | `| g", " `, hh", " iii"};
132 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
133 }
134 {
135 auto split = utils::quoted_split(test_string, '|', utils::REMOVE_EMPTY | utils::STRIP_SPACES, '`');
136 std::array expect = {"a, `, bb, ccc", "d, ee,, fff", "`| g, `, hh, iii"};
137 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
138 }
139 {
140 auto split = utils::quoted_split(test_string, '|', utils::REMOVE_EMPTY, '`');
141 std::array expect = {"a, `, bb, ccc ", " d, ee,, fff ", " `| g, `, hh, iii"};
142 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
143 }
144 {
145 auto split = utils::quoted_split(test_string, '|', utils::STRIP_SPACES, '`');
146 std::array expect = {"a, `, bb, ccc", "", "d, ee,, fff", "`| g, `, hh, iii"};
147 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
148 }
149 {
150 auto split = utils::quoted_split(test_string, '|', 0, '`');
151 std::array expect = {"a, `, bb, ccc ", "", " d, ee,, fff ", " `| g, `, hh, iii"};
152 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
153 }
154}
155
156BOOST_AUTO_TEST_CASE( utils_map_split_test )
157{
158 const std::string test_string = "a = b:2,, c = d:.9, e = f:5;; x = r:12, y = b:9.2,, z = g:45";
159
160 {
161 auto split = utils::map_split(test_string);
162 std::map<std::string, std::string> expect = {
163 {"a = b", "2"},
164 {"c = d", ".9"},
165 {"e = f", "5;; x = r:12"},
166 {"y = b", "9.2"},
167 {"z = g", "45"}
168 };
169 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
170 }
171
172 {
173 auto split = utils::map_split(test_string, ',', ':', utils::REMOVE_EMPTY | utils::STRIP_SPACES);
174 std::map<std::string, std::string> expect = {
175 {"a = b", "2"},
176 {"c = d", ".9"},
177 {"e = f", "5;; x = r:12"},
178 {"y = b", "9.2"},
179 {"z = g", "45"},
180 };
181 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
182 }
183 {
184 auto split = utils::map_split(test_string, ';', ':', utils::REMOVE_EMPTY | utils::STRIP_SPACES);
185 std::map<std::string, std::string> expect = {
186 {"a = b", "2,, c = d:.9, e = f:5"},
187 {"x = r", "12, y = b:9.2,, z = g:45"},
188 };
189 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
190 }
191 {
192 auto split = utils::map_split(test_string, ',', '=', utils::REMOVE_EMPTY | utils::STRIP_SPACES);
193 std::map<std::string, std::string> expect = {
194 {"a ", " b:2"},
195 {"c ", " d:.9"},
196 {"e ", " f:5;; x = r:12"},
197 {"y ", " b:9.2"},
198 {"z ", " g:45"},
199 };
200 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
201 }
202 {
203 auto split = utils::map_split(test_string, ';', '.', utils::REMOVE_EMPTY | utils::STRIP_SPACES);
204 std::map<std::string, std::string> expect = {
205 {"a = b:2,, c = d:", "9, e = f:5"},
206 {"x = r:12, y = b:9", "2,, z = g:45"}
207 };
208 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
209 }
210
211 {
212 auto split = utils::map_split(test_string, ',', ':', utils::REMOVE_EMPTY);
213 std::map<std::string, std::string> expect = {
214 {"a = b", "2"},
215 {" c = d", ".9"},
216 {" e = f", "5;; x = r:12"},
217 {" y = b", "9.2"},
218 {" z = g", "45"},
219 };
220 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
221 }
222 {
223 auto split = utils::map_split(test_string, ';', ':', utils::REMOVE_EMPTY);
224 std::map<std::string, std::string> expect = {
225 {"a = b", "2,, c = d:.9, e = f:5"},
226 {" x = r", "12, y = b:9.2,, z = g:45"},
227 };
228 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
229 }
230 {
231 auto split = utils::map_split(test_string, ',', '=', utils::REMOVE_EMPTY);
232 std::map<std::string, std::string> expect = {
233 {"a ", " b:2"},
234 {" c ", " d:.9"},
235 {" e ", " f:5;; x = r:12"},
236 {" y ", " b:9.2"},
237 {" z ", " g:45"},
238 };
239 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
240 }
241 {
242 auto split = utils::map_split(test_string, ';', '.', utils::REMOVE_EMPTY);
243 std::map<std::string, std::string> expect = {
244 {"a = b:2,, c = d:", "9, e = f:5"},
245 {" x = r:12, y = b:9", "2,, z = g:45"}
246 };
247 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
248 }
249
250 {
251 auto split = utils::map_split(test_string, ',', ':', utils::STRIP_SPACES);
252 std::map<std::string, std::string> expect = {
253 {"a = b", "2"},
254 {"", ""},
255 {"c = d", ".9"},
256 {"e = f", "5;; x = r:12"},
257 {"y = b", "9.2"},
258 {"", ""},
259 {"z = g", "45"},
260 };
261 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
262 }
263 {
264 auto split = utils::map_split(test_string, ';', ':', utils::STRIP_SPACES);
265 std::map<std::string, std::string> expect = {
266 {"a = b", "2,, c = d:.9, e = f:5"},
267 {"", ""},
268 {"x = r", "12, y = b:9.2,, z = g:45"},
269 };
270 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
271 }
272 {
273 auto split = utils::map_split(test_string, ',', '=', utils::STRIP_SPACES);
274 std::map<std::string, std::string> expect = {
275 {"a ", " b:2"},
276 {"", ""},
277 {"c ", " d:.9"},
278 {"e ", " f:5;; x = r:12"},
279 {"y ", " b:9.2"},
280 {"", ""},
281 {"z ", " g:45"},
282 };
283 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
284 }
285 {
286 auto split = utils::map_split(test_string, ';', '.', utils::STRIP_SPACES);
287 std::map<std::string, std::string> expect = {
288 {"a = b:2,, c = d:", "9, e = f:5"},
289 {"", ""},
290 {"x = r:12, y = b:9", "2,, z = g:45"}
291 };
292 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
293 }
294
295 {
296 auto split = utils::map_split(test_string, ',', ':', 0);
297 std::map<std::string, std::string> expect = {
298 {"a = b", "2"},
299 {"", ""},
300 {" c = d", ".9"},
301 {" e = f", "5;; x = r:12"},
302 {" y = b", "9.2"},
303 {"", ""},
304 {" z = g", "45"},
305 };
306 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
307 }
308 {
309 auto split = utils::map_split(test_string, ';', ':', 0);
310 std::map<std::string, std::string> expect = {
311 {"a = b", "2,, c = d:.9, e = f:5"},
312 {"", ""},
313 {" x = r", "12, y = b:9.2,, z = g:45"},
314 };
315 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
316 }
317 {
318 auto split = utils::map_split(test_string, ',', '=', 0);
319 std::map<std::string, std::string> expect = {
320 {"a ", " b:2"},
321 {"", ""},
322 {" c ", " d:.9"},
323 {" e ", " f:5;; x = r:12"},
324 {" y ", " b:9.2"},
325 {"", ""},
326 {" z ", " g:45"},
327 };
328 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
329 }
330 {
331 auto split = utils::map_split(test_string, ';', '.', 0);
332 std::map<std::string, std::string> expect = {
333 {"a = b:2,, c = d:", "9, e = f:5"},
334 {"", ""},
335 {" x = r:12, y = b:9", "2,, z = g:45"}
336 };
337 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
338 }
339}
340
341BOOST_AUTO_TEST_CASE( utils_parenthetical_split_test )
342{
343 {
344 auto split = utils::parenthetical_split("a ( b ) c { d } e ( f { g } ) h", 0, "({", ")}");
345 std::array expect = {"a", "b", "c", "d", "e", "f { g }", "h"};
346 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
347 }
348 {
349 auto split = utils::parenthetical_split("a ( b ) c { d } e ( f { g } ) h", 0, "({", ")}", utils::STRIP_SPACES);
350 std::array expect = {"a", "b", "c", "d", "e", "f { g }", "h"};
351 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
352 }
353 {
354 auto split = utils::parenthetical_split("a ( b ) c { d } e ( f { g } ) h", 0, "({", ")}", 0);
355 std::array expect = {"a ", " b ", " c ", " d ", " e ", " f { g } ", " h"};
356 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
357 }
358
359 {
360 auto split = utils::parenthetical_split("a, (b, c), {d, e},, f(g,g), h{i,i}", ',', "({", ")}");
361 std::array expect = {"a", "(b, c)", "{d, e}", "f(g,g)", "h{i,i}"};
362 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
363 }
364 {
365 auto split = utils::parenthetical_split("a, (b, c), {d, e},, f(g,g), h{i,i}", ',', "({", ")}", utils::REMOVE_EMPTY | utils::STRIP_SPACES);
366 std::array expect = {"a", "(b, c)", "{d, e}", "f(g,g)", "h{i,i}"};
367 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
368 }
369 {
370 auto split = utils::parenthetical_split("a, (b, c), {d, e},, f(g,g), h{i,i}", ',', "({", ")}", utils::REMOVE_EMPTY);
371 std::array expect = {"a", " (b, c)", " {d, e}", " f(g,g)", " h{i,i}"};
372 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
373 }
374 {
375 auto split = utils::parenthetical_split("a, (b, c), {d, e},, f(g,g), h{i,i}", ',', "({", ")}", utils::STRIP_SPACES);
376 std::array expect = {"a", "(b, c)", "{d, e}", "", "f(g,g)", "h{i,i}"};
377 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
378 }
379 {
380 auto split = utils::parenthetical_split("a, (b, c), {d, e},, f(g,g), h{i,i}", ',', "({", ")}", 0);
381 std::array expect = {"a", " (b, c)", " {d, e}", "", " f(g,g)", " h{i,i}"};
382 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
383 }
384}
385
386BOOST_AUTO_TEST_CASE( utils_square_parenthetical_split )
387{
388 {
389 auto split = utils::square_parenthetical_split(" a ,, b ,, c ");
390 std::array expect = {"a", "b", "c"};
391 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
392 }
393 {
394 auto split = utils::square_parenthetical_split(" a ,, b ,, c ", ',', "([", ")]", utils::REMOVE_EMPTY | utils::STRIP_SPACES);
395 std::array expect = {"a", "b", "c"};
396 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
397 }
398 {
399 auto split = utils::square_parenthetical_split(" a ,, b ,, c ", ',', "([", ")]", utils::REMOVE_EMPTY);
400 std::array expect = {" a ", " b ", " c "};
401 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
402 }
403 {
404 auto split = utils::square_parenthetical_split(" a ,, b ,, c ", ',', "([", ")]", utils::STRIP_SPACES);
405 std::array expect = {"a", "", "b", "", "c"};
406 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
407 }
408 {
409 auto split = utils::square_parenthetical_split(" a ,, b ,, c ", ',', "([", ")]", 0);
410 std::array expect = {" a ", "", " b ", "", " c "};
411 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
412 }
413 {
414 auto split = utils::square_parenthetical_split("[a*3]");
415 std::array expect = {"a", "a", "a"};
416 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
417 }
418 {
419 auto split = utils::square_parenthetical_split("q[a*3,b,c*2]");
420 std::array expect = {"qa", "qa", "qa", "qb", "qc", "qc"};
421 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
422 }
423 {
424 auto split = utils::square_parenthetical_split("q[a,b,c]");
425 std::array expect = {"qa", "qb", "qc"};
426 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
427 }
428 {
429 auto split = utils::square_parenthetical_split("q[0~5]");
430 std::array expect = {"q0", "q1", "q2", "q3", "q4", "q5"};
431 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
432 }
433 {
434 auto split = utils::square_parenthetical_split("q[5~0]");
435 std::array expect = {"q5", "q4", "q3", "q2", "q1", "q0"};
436 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
437 }
438 {
439 auto split = utils::square_parenthetical_split("q[001~005]");
440 std::array expect = {"q001", "q002", "q003", "q004", "q005"};
441 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
442 }
443 {
444 auto split = utils::square_parenthetical_split("q[007~012]");
445 std::array expect = {"q007", "q008", "q009", "q010", "q011", "q012"};
446 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
447 }
448 {
449 auto split = utils::square_parenthetical_split("q[095~100]");
450 std::array expect = {"q095", "q096", "q097", "q098", "q099", "q100"};
451 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
452 }
453 {
454 auto split = utils::square_parenthetical_split("q[095~098]");
455 std::array expect = {"q095", "q096", "q097", "q098"};
456 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
457 }
458 {
459 auto split = utils::square_parenthetical_split("a[1~3](1,[5,6,7]),b[8,9]");
460 std::array expect = {"a1(1,5)", "a2(1,6)", "a3(1,7)", "b8", "b9"};
461 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
462 }
463 {
464 auto split = utils::square_parenthetical_split("abc[07~10]");
465 std::array expect = {"abc07", "abc08", "abc09", "abc10"};
466 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
467 }
468 {
469 auto split = utils::square_parenthetical_split("a[1,2]b[3~4]:c[5,6]");
470 std::array expect = {"a1b3:c5", "a2b4:c6"};
471 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
472 }
473 {
474 auto split = utils::square_parenthetical_split("abc[3~1].png");
475 std::array expect = {"abc3.png", "abc2.png", "abc1.png"};
476 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
477 }
478 {
479 auto split = utils::square_parenthetical_split("abc[3,1].png");
480 std::array expect = {"abc3.png", "abc1.png"};
481 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
482 }
483 {
484 auto split = utils::square_parenthetical_split("abc[de,xyz]");
485 std::array expect = {"abcde", "abcxyz"};
486 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
487 }
488 {
489 auto split = utils::square_parenthetical_split("abc[1*3]");
490 std::array expect = {"abc1", "abc1", "abc1"};
491 BOOST_CHECK_EQUAL_COLLECTIONS(split.begin(), split.end(), expect.begin(), expect.end());
492 }
493}
494
495BOOST_AUTO_TEST_CASE( utils_unicode_test )
496{
497 std::string unicode = "ünicod€ check";
498 BOOST_CHECK( utf8::size(unicode) == 13 );
499
500 int euro = utf8::index(unicode,6);
501 BOOST_CHECK( unicode.substr(euro,utf8::index(unicode,7)-euro) == "€" );
502
503 BOOST_CHECK( utf8::truncate(unicode,3) == "üni");
504
505 std::string apple_u8("apple");
506 std::u32string apple_u4 = unicode_cast<std::u32string>(apple_u8);
507 std::u16string apple_u16 = unicode_cast<std::u16string>(apple_u4);
508
509 BOOST_CHECK( apple_u4.size() == 5 );
510 BOOST_CHECK_EQUAL( apple_u8, unicode_cast<std::string>(apple_u4) );
511 BOOST_CHECK_EQUAL( apple_u8, unicode_cast<std::string>(apple_u16) );
512 BOOST_CHECK( apple_u4 == unicode_cast<std::u32string>(apple_u16) );
513 BOOST_CHECK( apple_u16 == unicode_cast<std::u16string>(apple_u4) );
514 BOOST_CHECK_EQUAL( apple_u8.size(), apple_u16.size() );
515
516 std::u32string water_u4;
517 water_u4.push_back(0x6C34);
518 std::string water_u8 = unicode_cast<std::string>(water_u4);
519
520#if defined(_WIN32) || defined(_WIN64)
521 // Windows complains it can't be represented in the currentl code-page.
522 // So instead, check directly for its UTF-8 representation.
523 BOOST_CHECK_EQUAL(water_u8, "\xE6\xB0\xB4");
524#else
525 BOOST_CHECK_EQUAL(water_u8, "\u6C34");
526#endif
527
528#if defined(_WIN32) || defined(_WIN64)
529 // Same as above.
530 std::string nonbmp_u8("\xF0\x90\x80\x80");
531#else
532 std::string nonbmp_u8("\U00010000");
533#endif
534 std::u32string nonbmp_u4 = unicode_cast<std::u32string>(nonbmp_u8);
535 std::u16string nonbmp_u16 = unicode_cast<std::u16string>(nonbmp_u4);
536
537 BOOST_CHECK_EQUAL(nonbmp_u8.size(), 4u);
538 BOOST_CHECK_EQUAL(nonbmp_u8, unicode_cast<std::string>(nonbmp_u4));
539 BOOST_CHECK_EQUAL(nonbmp_u8, unicode_cast<std::string>(nonbmp_u16));
540 BOOST_CHECK(nonbmp_u16 == unicode_cast<std::u16string>(nonbmp_u4));
541 BOOST_CHECK(nonbmp_u4 == unicode_cast<std::u32string>(nonbmp_u16));
542}
543
544BOOST_AUTO_TEST_CASE( test_lowercase )
545{
546 BOOST_CHECK_EQUAL ( utf8::lowercase("FOO") , "foo" );
547 BOOST_CHECK_EQUAL ( utf8::lowercase("foo") , "foo" );
548 BOOST_CHECK_EQUAL ( utf8::lowercase("FoO") , "foo" );
549 BOOST_CHECK_EQUAL ( utf8::lowercase("fO0") , "fo0" );
550}
551
552BOOST_AUTO_TEST_CASE( test_wildcard_string_match )
553{
554 const std::string str = "foo bar baz";
555
556 BOOST_CHECK(utils::wildcard_string_match(str, "*bar*"));
557 BOOST_CHECK(utils::wildcard_string_match(str, "+bar+"));
558
559 BOOST_CHECK(!utils::wildcard_string_match(str, "*BAR*"));
560 BOOST_CHECK(!utils::wildcard_string_match(str, "+BAR+"));
561 BOOST_CHECK(!utils::wildcard_string_match(str, "bar"));
562
563 BOOST_CHECK(utils::wildcard_string_match(str, "*ba? b*"));
564 BOOST_CHECK(utils::wildcard_string_match(str, "+ba? b+"));
565 BOOST_CHECK(utils::wildcard_string_match(str, "*?a?*"));
566 BOOST_CHECK(utils::wildcard_string_match(str, "+?a?+"));
567
568 BOOST_CHECK(!utils::wildcard_string_match(str, "foo? "));
569 BOOST_CHECK(!utils::wildcard_string_match(str, "?foo"));
570
571 std::string superfluous_mask;
572
573 superfluous_mask = std::string(str.length(), '?');
574 BOOST_CHECK(utils::wildcard_string_match(str, superfluous_mask));
575 BOOST_CHECK(!utils::wildcard_string_match(str, superfluous_mask + '?'));
576
577 superfluous_mask = std::string(str.length(), '*');
578 BOOST_CHECK(utils::wildcard_string_match(str, superfluous_mask));
579 BOOST_CHECK(utils::wildcard_string_match(str, superfluous_mask + '*'));
580
581 superfluous_mask = std::string(str.length(), '+');
582 BOOST_CHECK(utils::wildcard_string_match(str, superfluous_mask));
583 BOOST_CHECK(!utils::wildcard_string_match(str, superfluous_mask + '+'));
584
585 BOOST_CHECK(utils::wildcard_string_match("", ""));
586 BOOST_CHECK(!utils::wildcard_string_match(str, ""));
587
588 BOOST_CHECK(utils::wildcard_string_match("", "*"));
589 BOOST_CHECK(utils::wildcard_string_match("", "***"));
590 BOOST_CHECK(!utils::wildcard_string_match("", "+"));
591 BOOST_CHECK(!utils::wildcard_string_match("", "*bar"));
592 BOOST_CHECK(!utils::wildcard_string_match("", "***?**"));
593 BOOST_CHECK(!utils::wildcard_string_match("", "+++?++"));
594 BOOST_CHECK(!utils::wildcard_string_match("", "?"));
595 BOOST_CHECK(!utils::wildcard_string_match("", "???"));
596
597 BOOST_CHECK(utils::wildcard_string_match("hello.txt", "*.txt"));
598 BOOST_CHECK(utils::wildcard_string_match("hello.txt", "h*t"));
599 BOOST_CHECK(utils::wildcard_string_match("hello.txt", "?ello.*"));
600 BOOST_CHECK(!utils::wildcard_string_match("hello.txt", "*.doc"));
601 BOOST_CHECK(utils::wildcard_string_match("file123.tmp", "file?2*.tmp"));
602 BOOST_CHECK(utils::wildcard_string_match("test", "t*t"));
603 BOOST_CHECK(utils::wildcard_string_match("multiple.dots.file", "*.dots.*"));
604 BOOST_CHECK(!utils::wildcard_string_match("case.txt", "CASE.TXT"));
605 BOOST_CHECK(utils::wildcard_string_match("single", "?ingle"));
606 BOOST_CHECK(!utils::wildcard_string_match("different", "diff*txt"));
607
608 BOOST_CHECK(utils::wildcard_string_match("foo bar baz bar baz", "*bar baz"));
609 BOOST_CHECK(!utils::wildcard_string_match("abc", "ab*d"));
610 BOOST_CHECK(utils::wildcard_string_match("abcccd", "*ccd"));
611 BOOST_CHECK(utils::wildcard_string_match("mississipissippi", "*issip*ss*"));
612 BOOST_CHECK(!utils::wildcard_string_match("xxxx*zzzzzzzzy*f", "xxxx*zzy*fffff"));
613 BOOST_CHECK(utils::wildcard_string_match("xxxx*zzzzzzzzy*f", "xxx*zzy*f"));
614 BOOST_CHECK(!utils::wildcard_string_match("xxxxzzzzzzzzyf", "xxxx*zzy*fffff"));
615 BOOST_CHECK(utils::wildcard_string_match("xxxxzzzzzzzzyf", "xxxx*zzy*f"));
616 BOOST_CHECK(utils::wildcard_string_match("xyxyxyzyxyz", "xy*z*xyz"));
617 BOOST_CHECK(utils::wildcard_string_match("mississippi", "*sip*"));
618 BOOST_CHECK(utils::wildcard_string_match("xyxyxyxyz", "xy*xyz"));
619 BOOST_CHECK(utils::wildcard_string_match("mississippi", "mi*sip*"));
620 BOOST_CHECK(utils::wildcard_string_match("ababac", "*abac*"));
621 BOOST_CHECK(utils::wildcard_string_match("ababac", "*abac*"));
622 BOOST_CHECK(utils::wildcard_string_match("aaazz", "a*zz*"));
623 BOOST_CHECK(!utils::wildcard_string_match("a12b12", "*12*23"));
624 BOOST_CHECK(!utils::wildcard_string_match("a12b12", "a12b"));
625 BOOST_CHECK(utils::wildcard_string_match("a12b12", "*12*12*"));
626 BOOST_CHECK(utils::wildcard_string_match("caaab", "*a?b"));
627 BOOST_CHECK(utils::wildcard_string_match("*", "*"));
628 BOOST_CHECK(utils::wildcard_string_match("a*abab", "a*b"));
629 BOOST_CHECK(utils::wildcard_string_match("a*r", "a*"));
630 BOOST_CHECK(!utils::wildcard_string_match("a*ar", "a*aar"));
631 BOOST_CHECK(utils::wildcard_string_match("XYXYXYZYXYz", "XY*Z*XYz"));
632 BOOST_CHECK(utils::wildcard_string_match("missisSIPpi", "*SIP*"));
633 BOOST_CHECK(utils::wildcard_string_match("mississipPI", "*issip*PI"));
634 BOOST_CHECK(utils::wildcard_string_match("xyxyxyxyz", "xy*xyz"));
635 BOOST_CHECK(utils::wildcard_string_match("miSsissippi", "mi*sip*"));
636 BOOST_CHECK(utils::wildcard_string_match("c+od", "**c+d"));
637}
638
639BOOST_AUTO_TEST_CASE( test_base64_encodings )
640{
641 const std::vector<uint8_t> empty;
642 const std::string empty_b64;
643 const std::string empty_c64;
644 const std::vector<uint8_t> foo = {'f', 'o', 'o'};
645 const std::string foo_b64 = "Zm9v";
646 const std::string foo_c64 = "axqP";
647 const std::vector<uint8_t> foob = {'f', 'o', 'o', 'b'};
648 const std::string foob_b64 = "Zm9vYg==";
649 const std::string foob_c64 = "axqPW/";
650
651 std::vector<uint8_t> many_bytes;
652
653 many_bytes.resize(1024);
654 for(int i = 0; i < 1024; ++i) {
655 many_bytes[i] = i % 256;
656 }
657
658 BOOST_CHECK(base64::encode(empty).empty());
659 BOOST_CHECK_EQUAL(base64::encode(foo), foo_b64);
660 BOOST_CHECK_EQUAL(base64::encode(foob), foob_b64);
661
662 BOOST_CHECK(base64::decode(empty_b64).empty());
663 // Not using CHECK_EQUAL because vector<uint8_t> is not printable
664 BOOST_CHECK(base64::decode(foo_b64) == foo);
665 BOOST_CHECK(base64::decode(foob_b64) == foob);
666
667 BOOST_CHECK(crypt64::encode(empty).empty());
668 BOOST_CHECK_EQUAL(crypt64::encode(foo), foo_c64);
669 BOOST_CHECK_EQUAL(crypt64::encode(foob), foob_c64);
670
671 BOOST_CHECK(crypt64::decode(empty_c64).empty());
672 // Not using CHECK_EQUAL because vector<uint8_t> is not printable
673 BOOST_CHECK(crypt64::decode(foo_c64) == foo);
674 BOOST_CHECK(crypt64::decode(foob_c64) == foob);
675
676 BOOST_CHECK_EQUAL(crypt64::decode('.'), 0);
677 BOOST_CHECK_EQUAL(crypt64::decode('z'), 63);
678 BOOST_CHECK_EQUAL(crypt64::encode(0), '.');
679 BOOST_CHECK_EQUAL(crypt64::encode(63), 'z');
680
681 BOOST_CHECK(base64::decode(base64::encode(many_bytes)) == many_bytes);
682 BOOST_CHECK(crypt64::decode(crypt64::encode(many_bytes)) == many_bytes);
683}
684
685BOOST_AUTO_TEST_SUITE_END()
std::string encode(utils::byte_view bytes)
Definition base64.cpp:223
std::vector< uint8_t > decode(std::string_view in)
Definition base64.cpp:219
std::string encode(utils::byte_view bytes)
Definition base64.cpp:233
std::vector< uint8_t > decode(std::string_view in)
Definition base64.cpp:229
STL namespace.
static std::ostream & operator<<(std::ostream &str, const std::pair< const std::string, std::string > &p)
std::string lowercase(std::string_view s)
Returns a lowercased version of the string.
Definition unicode.cpp:50
std::size_t size(std::string_view str)
Length in characters of a UTF-8 string.
Definition unicode.cpp:81
std::string & truncate(std::string &str, const std::size_t size)
Truncates a UTF-8 string to the specified number of characters.
Definition unicode.cpp:118
std::size_t index(std::string_view str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition unicode.cpp:70
@ STRIP_SPACES
REMOVE_EMPTY: remove empty elements.
std::map< std::string, std::string > map_split(const std::string &val, char major, char minor, int flags, const std::string &default_value)
Splits a string based on two separators into a map.
std::vector< std::string > quoted_split(const std::string &val, char c, int flags, char quote)
This function is identical to split(), except it does not split when it otherwise would if the previo...
std::vector< std::string > parenthetical_split(std::string_view val, const char separator, std::string_view left, std::string_view right, const int flags)
Splits a string based either on a separator, except then the text appears within specified parenthesi...
bool wildcard_string_match(std::string_view str, std::string_view pat) noexcept
Performs pattern matching with wildcards.
std::string join(const Range &v, const std::string &s=",")
Generates a new string joining container items in a list.
std::vector< std::string > square_parenthetical_split(const std::string &val, const char separator, const std::string &left, const std::string &right, const int flags)
Similar to parenthetical_split, but also expands embedded square brackets.
std::vector< std::string > split(const config_attribute_value &val)
BOOST_AUTO_TEST_SUITE(filesystem)
mock_party p
BOOST_AUTO_TEST_CASE(utils_join_test)
#define i