16 #define GETTEXT_DOMAIN "wesnoth-test"
24 #include <boost/test/unit_test.hpp>
27 static std::ostream&
operator<<(std::ostream& str,
const std::pair<const std::string, std::string>&
p)
29 return str <<
'(' <<
p.first <<
" => " <<
p.second <<
')';
37 std::vector<std::string> fruit;
42 fruit.push_back(
"apples");
45 BOOST_CHECK(
utils::join(fruit,
"---") ==
"apples" );
47 fruit.push_back(
"oranges");
48 fruit.push_back(
"lemons");
50 BOOST_CHECK(
utils::join(fruit) ==
"apples,oranges,lemons" );
51 BOOST_CHECK(
utils::join(fruit,
"---") ==
"apples---oranges---lemons" );
56 const std::string test_string =
"a, , bb, ccc || d, ee,, fff | | g, , hh, iii";
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
112 const std::string test_string =
"a, `, bb, ccc || d, ee,, fff | `| g, `, hh, iii";
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());
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());
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());
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());
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());
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());
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());
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());
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";
162 std::map<std::string, std::string> expect = {
165 {
"e = f",
"5;; x = r:12"},
169 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
174 std::map<std::string, std::string> expect = {
177 {
"e = f",
"5;; x = r:12"},
181 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
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"},
189 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
193 std::map<std::string, std::string> expect = {
196 {
"e ",
" f:5;; x = r:12"},
200 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
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"}
208 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
213 std::map<std::string, std::string> expect = {
216 {
" e = f",
"5;; x = r:12"},
220 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
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"},
228 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
232 std::map<std::string, std::string> expect = {
235 {
" e ",
" f:5;; x = r:12"},
239 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
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"}
247 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
252 std::map<std::string, std::string> expect = {
256 {
"e = f",
"5;; x = r:12"},
261 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
265 std::map<std::string, std::string> expect = {
266 {
"a = b",
"2,, c = d:.9, e = f:5"},
268 {
"x = r",
"12, y = b:9.2,, z = g:45"},
270 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
274 std::map<std::string, std::string> expect = {
278 {
"e ",
" f:5;; x = r:12"},
283 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
287 std::map<std::string, std::string> expect = {
288 {
"a = b:2,, c = d:",
"9, e = f:5"},
290 {
"x = r:12, y = b:9",
"2,, z = g:45"}
292 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
297 std::map<std::string, std::string> expect = {
301 {
" e = f",
"5;; x = r:12"},
306 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
310 std::map<std::string, std::string> expect = {
311 {
"a = b",
"2,, c = d:.9, e = f:5"},
313 {
" x = r",
"12, y = b:9.2,, z = g:45"},
315 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
319 std::map<std::string, std::string> expect = {
323 {
" e ",
" f:5;; x = r:12"},
328 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
332 std::map<std::string, std::string> expect = {
333 {
"a = b:2,, c = d:",
"9, e = f:5"},
335 {
" x = r:12, y = b:9",
"2,, z = g:45"}
337 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
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());
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());
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());
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());
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());
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());
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());
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());
390 std::array expect = {
"a",
"b",
"c"};
391 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
395 std::array expect = {
"a",
"b",
"c"};
396 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
400 std::array expect = {
" a ",
" b ",
" c "};
401 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
405 std::array expect = {
"a",
"",
"b",
"",
"c"};
406 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
410 std::array expect = {
" a ",
"",
" b ",
"",
" c "};
411 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
415 std::array expect = {
"a",
"a",
"a"};
416 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
420 std::array expect = {
"qa",
"qa",
"qa",
"qb",
"qc",
"qc"};
421 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
425 std::array expect = {
"qa",
"qb",
"qc"};
426 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
430 std::array expect = {
"q1",
"q2",
"q3",
"q4",
"q5"};
431 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
435 std::array expect = {
"q5",
"q4",
"q3",
"q2",
"q1"};
436 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
440 std::array expect = {
"q001",
"q002",
"q003",
"q004",
"q005"};
441 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
445 std::array expect = {
"q007",
"q008",
"q009",
"q010",
"q011",
"q012"};
446 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
450 std::array expect = {
"a1(1,5)",
"a2(1,6)",
"a3(1,7)",
"b8",
"b9"};
451 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
455 std::array expect = {
"abc07",
"abc08",
"abc09",
"abc10"};
456 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
460 std::array expect = {
"a1b3:c5",
"a2b4:c6"};
461 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
465 std::array expect = {
"abc3.png",
"abc2.png",
"abc1.png"};
466 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
470 std::array expect = {
"abc3.png",
"abc1.png"};
471 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
475 std::array expect = {
"abcde",
"abcxyz"};
476 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
480 std::array expect = {
"abc1",
"abc1",
"abc1"};
481 BOOST_CHECK_EQUAL_COLLECTIONS(
split.begin(),
split.end(), expect.begin(), expect.end());
487 std::string unicode =
"ünicod€ check";
491 BOOST_CHECK( unicode.substr(euro,
utf8::index(unicode,7)-euro) ==
"€" );
495 std::string apple_u8(
"apple");
496 std::u32string apple_u4 = unicode_cast<std::u32string>(apple_u8);
497 std::u16string apple_u16 = unicode_cast<std::u16string>(apple_u4);
499 BOOST_CHECK( apple_u4.size() == 5 );
500 BOOST_CHECK_EQUAL( apple_u8, unicode_cast<std::string>(apple_u4) );
501 BOOST_CHECK_EQUAL( apple_u8, unicode_cast<std::string>(apple_u16) );
502 BOOST_CHECK( apple_u4 == unicode_cast<std::u32string>(apple_u16) );
503 BOOST_CHECK( apple_u16 == unicode_cast<std::u16string>(apple_u4) );
504 BOOST_CHECK_EQUAL( apple_u8.size(), apple_u16.size() );
506 std::u32string water_u4;
507 water_u4.push_back(0x6C34);
508 std::string water_u8 = unicode_cast<std::string>(water_u4);
510 #if defined(_WIN32) || defined(_WIN64)
513 BOOST_CHECK_EQUAL(water_u8,
"\xE6\xB0\xB4");
515 BOOST_CHECK_EQUAL(water_u8,
"\u6C34");
518 #if defined(_WIN32) || defined(_WIN64)
520 std::string nonbmp_u8(
"\xF0\x90\x80\x80");
522 std::string nonbmp_u8(
"\U00010000");
524 std::u32string nonbmp_u4 = unicode_cast<std::u32string>(nonbmp_u8);
525 std::u16string nonbmp_u16 = unicode_cast<std::u16string>(nonbmp_u4);
527 BOOST_CHECK_EQUAL(nonbmp_u8.size(), 4u);
528 BOOST_CHECK_EQUAL(nonbmp_u8, unicode_cast<std::string>(nonbmp_u4));
529 BOOST_CHECK_EQUAL(nonbmp_u8, unicode_cast<std::string>(nonbmp_u16));
530 BOOST_CHECK(nonbmp_u16 == unicode_cast<std::u16string>(nonbmp_u4));
531 BOOST_CHECK(nonbmp_u4 == unicode_cast<std::u32string>(nonbmp_u16));
544 const std::string str =
"foo bar baz";
561 std::string superfluous_mask;
563 superfluous_mask = std::string(str.length(),
'?');
567 superfluous_mask = std::string(str.length(),
'*');
571 superfluous_mask = std::string(str.length(),
'+');
590 const std::vector<uint8_t> empty;
591 const std::string empty_b64;
592 const std::string empty_c64;
593 const std::vector<uint8_t> foo = {
'f',
'o',
'o'};
594 const std::string foo_b64 =
"Zm9v";
595 const std::string foo_c64 =
"axqP";
596 const std::vector<uint8_t> foob = {
'f',
'o',
'o',
'b'};
597 const std::string foob_b64 =
"Zm9vYg==";
598 const std::string foob_c64 =
"axqPW/";
600 std::vector<uint8_t> many_bytes;
602 many_bytes.resize(1024);
603 for(
int i = 0;
i < 1024; ++
i) {
604 many_bytes[
i] =
i % 256;
607 BOOST_CHECK(
base64::encode({empty.data(), empty.size()}).empty());
608 BOOST_CHECK_EQUAL(
base64::encode({foo.data(), foo.size()}), foo_b64);
609 BOOST_CHECK_EQUAL(
base64::encode({foob.data(), foob.size()}), foob_b64);
618 BOOST_CHECK_EQUAL(
crypt64::encode({foob.data(), foob.size()}), foob_c64);
634 BOOST_AUTO_TEST_SUITE_END()
std::ostream & operator<<(std::ostream &s, const ai::attack_result &r)
std::string encode(utils::byte_string_view bytes)
std::vector< uint8_t > decode(std::string_view in)
std::string encode(utils::byte_string_view bytes)
std::vector< uint8_t > decode(std::string_view in)
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.
std::string lowercase(const std::string &s)
Returns a lowercased version of the string.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
std::string & truncate(std::string &str, const std::size_t size)
Truncates a UTF-8 string to the specified number of characters.
@ 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(const std::string &str, const std::string &match)
Match using '*' as any number of characters (including none), '+' as one or more characters,...
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::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
std::vector< std::string > split(const config_attribute_value &val)
BOOST_AUTO_TEST_SUITE(filesystem)
BOOST_AUTO_TEST_CASE(utils_join_test)