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