The Battle for Wesnoth  1.15.2+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 <vector>
18 #include <string>
19 #include "serialization/base64.hpp"
22 #include <boost/test/auto_unit_test.hpp>
23 
24 BOOST_AUTO_TEST_SUITE ( test_serialization_utils_and_unicode )
25 
26 BOOST_AUTO_TEST_CASE( utils_join_test )
27 {
28  std::vector<std::string> fruit;
29 
30  BOOST_CHECK( utils::join(fruit).empty() );
31 
32  fruit.push_back("apples");
33 
34  BOOST_CHECK( utils::join(fruit) == "apples" );
35 
36  fruit.push_back("oranges");
37  fruit.push_back("lemons");
38 
39  BOOST_CHECK( utils::join(fruit) == "apples,oranges,lemons" );
40 }
41 
42 BOOST_AUTO_TEST_CASE( utils_unicode_test )
43 {
44  std::string unicode = "ünicod€ check";
45  BOOST_CHECK( utf8::size(unicode) == 13 );
46 
47  int euro = utf8::index(unicode,6);
48  BOOST_CHECK( unicode.substr(euro,utf8::index(unicode,7)-euro) == "€" );
49 
50  BOOST_CHECK( utf8::truncate(unicode,3) == "üni");
51 
52  std::string apple_u8("apple");
53  std::u32string apple_u4 = unicode_cast<std::u32string>(apple_u8);
54  std::u16string apple_u16 = unicode_cast<std::u16string>(apple_u4);
55 
56  BOOST_CHECK( apple_u4.size() == 5 );
57  BOOST_CHECK_EQUAL( apple_u8, unicode_cast<std::string>(apple_u4) );
58  BOOST_CHECK_EQUAL( apple_u8, unicode_cast<std::string>(apple_u16) );
59  BOOST_CHECK( apple_u4 == unicode_cast<std::u32string>(apple_u16) );
60  BOOST_CHECK( apple_u16 == unicode_cast<std::u16string>(apple_u4) );
61  BOOST_CHECK_EQUAL( apple_u8.size(), apple_u16.size() );
62 
63  std::u32string water_u4;
64  water_u4.push_back(0x6C34);
65  std::string water_u8 = unicode_cast<std::string>(water_u4);
66  std::u16string water_u16 = unicode_cast<std::u16string>(water_u4);
67 
68  BOOST_CHECK_EQUAL(water_u4[0], static_cast<char32_t>(water_u16[0]));
69 #if defined(_WIN32) || defined(_WIN64)
70  // Windows complains it can't be represented in the currentl code-page.
71  // So instead, check directly for its UTF-8 representation.
72  BOOST_CHECK_EQUAL(water_u8, "\xE6\xB0\xB4");
73 #else
74  BOOST_CHECK_EQUAL(water_u8, "\u6C34");
75 #endif
76 
77 #if defined(_WIN32) || defined(_WIN64)
78  // Same as above.
79  std::string nonbmp_u8("\xF0\x90\x80\x80");
80 #else
81  std::string nonbmp_u8("\U00010000");
82 #endif
83  std::u32string nonbmp_u4 = unicode_cast<std::u32string>(nonbmp_u8);
84  std::u16string nonbmp_u16 = unicode_cast<std::u16string>(nonbmp_u4);
85 
86  BOOST_CHECK_EQUAL(nonbmp_u8.size(), 4u);
87  BOOST_CHECK_EQUAL(nonbmp_u4[0], 0x10000u);
88  BOOST_CHECK_EQUAL(nonbmp_u16[0], 0xD800);
89  BOOST_CHECK_EQUAL(nonbmp_u16[1], 0xDC00);
90  BOOST_CHECK_EQUAL(nonbmp_u8, unicode_cast<std::string>(nonbmp_u4));
91  BOOST_CHECK_EQUAL(nonbmp_u8, unicode_cast<std::string>(nonbmp_u16));
92  BOOST_CHECK(nonbmp_u16 == unicode_cast<std::u16string>(nonbmp_u4));
93  BOOST_CHECK(nonbmp_u4 == unicode_cast<std::u32string>(nonbmp_u16));
94 }
95 
96 BOOST_AUTO_TEST_CASE( test_lowercase )
97 {
98  BOOST_CHECK_EQUAL ( utf8::lowercase("FOO") , "foo" );
99  BOOST_CHECK_EQUAL ( utf8::lowercase("foo") , "foo" );
100  BOOST_CHECK_EQUAL ( utf8::lowercase("FoO") , "foo" );
101  BOOST_CHECK_EQUAL ( utf8::lowercase("fO0") , "fo0" );
102 }
103 
104 BOOST_AUTO_TEST_CASE( test_wildcard_string_match )
105 {
106  const std::string str = "foo bar baz";
107 
108  BOOST_CHECK(utils::wildcard_string_match(str, "*bar*"));
109  BOOST_CHECK(utils::wildcard_string_match(str, "+bar+"));
110 
111  BOOST_CHECK(!utils::wildcard_string_match(str, "*BAR*"));
112  BOOST_CHECK(!utils::wildcard_string_match(str, "+BAR+"));
113  BOOST_CHECK(!utils::wildcard_string_match(str, "bar"));
114 
115  BOOST_CHECK(utils::wildcard_string_match(str, "*ba? b*"));
116  BOOST_CHECK(utils::wildcard_string_match(str, "+ba? b+"));
117  BOOST_CHECK(utils::wildcard_string_match(str, "*?a?*"));
118  BOOST_CHECK(utils::wildcard_string_match(str, "+?a?+"));
119 
120  BOOST_CHECK(!utils::wildcard_string_match(str, "foo? "));
121  BOOST_CHECK(!utils::wildcard_string_match(str, "?foo"));
122 
123  std::string superfluous_mask;
124 
125  superfluous_mask = std::string(str.length(), '?');
126  BOOST_CHECK(utils::wildcard_string_match(str, superfluous_mask));
127  BOOST_CHECK(utils::wildcard_string_match(str, superfluous_mask + '?'));
128 
129  superfluous_mask = std::string(str.length(), '*');
130  BOOST_CHECK(utils::wildcard_string_match(str, superfluous_mask));
131  BOOST_CHECK(utils::wildcard_string_match(str, superfluous_mask + '*'));
132 
133  superfluous_mask = std::string(str.length(), '+');
134  BOOST_CHECK(utils::wildcard_string_match(str, superfluous_mask));
135  BOOST_CHECK(!utils::wildcard_string_match(str, superfluous_mask + '+'));
136 
137  BOOST_CHECK(utils::wildcard_string_match("", ""));
138  BOOST_CHECK(!utils::wildcard_string_match(str, ""));
139 
140  BOOST_CHECK(utils::wildcard_string_match("", "*"));
141  BOOST_CHECK(utils::wildcard_string_match("", "***"));
142  BOOST_CHECK(!utils::wildcard_string_match("", "+"));
143  BOOST_CHECK(!utils::wildcard_string_match("", "*bar"));
144  BOOST_CHECK(!utils::wildcard_string_match("", "***?**"));
145  BOOST_CHECK(!utils::wildcard_string_match("", "+++?++"));
146  BOOST_CHECK(!utils::wildcard_string_match("", "?"));
147  BOOST_CHECK(!utils::wildcard_string_match("", "???"));
148 }
149 
150 BOOST_AUTO_TEST_CASE( test_base64_encodings )
151 {
152  const std::vector<uint8_t> empty;
153  const std::string empty_b64;
154  const std::string empty_c64;
155  const std::vector<uint8_t> foo = {'f', 'o', 'o'};
156  const std::string foo_b64 = "Zm9v";
157  const std::string foo_c64 = "axqP";
158  const std::vector<uint8_t> foob = {'f', 'o', 'o', 'b'};
159  const std::string foob_b64 = "Zm9vYg==";
160  const std::string foob_c64 = "axqPW/";
161 
162  std::vector<uint8_t> many_bytes;
163 
164  many_bytes.resize(1024);
165  for(int i = 0; i < 1024; ++i) {
166  many_bytes[i] = i % 256;
167  }
168 
169  BOOST_CHECK(base64::encode({empty.data(), empty.size()}).empty());
170  BOOST_CHECK_EQUAL(base64::encode({foo.data(), foo.size()}), foo_b64);
171  BOOST_CHECK_EQUAL(base64::encode({foob.data(), foob.size()}), foob_b64);
172 
173  BOOST_CHECK(base64::decode(empty_b64).empty());
174  // Not using CHECK_EQUAL because vector<uint8_t> is not printable
175  BOOST_CHECK(base64::decode(foo_b64) == foo);
176  BOOST_CHECK(base64::decode(foob_b64) == foob);
177 
178  BOOST_CHECK(crypt64::encode({empty.data(), empty.size()}).empty());
179  BOOST_CHECK_EQUAL(crypt64::encode({foo.data(), foo.size()}), foo_c64);
180  BOOST_CHECK_EQUAL(crypt64::encode({foob.data(), foob.size()}), foob_c64);
181 
182  BOOST_CHECK(crypt64::decode(empty_c64).empty());
183  // Not using CHECK_EQUAL because vector<uint8_t> is not printable
184  BOOST_CHECK(crypt64::decode(foo_c64) == foo);
185  BOOST_CHECK(crypt64::decode(foob_c64) == foob);
186 
187  BOOST_CHECK_EQUAL(crypt64::decode('.'), 0);
188  BOOST_CHECK_EQUAL(crypt64::decode('z'), 63);
189  BOOST_CHECK_EQUAL(crypt64::encode(0), '.');
190  BOOST_CHECK_EQUAL(crypt64::encode(63), 'z');
191 
192  BOOST_CHECK(base64::decode(base64::encode({many_bytes.data(), many_bytes.size()})) == many_bytes);
193  BOOST_CHECK(crypt64::decode(crypt64::encode({many_bytes.data(), many_bytes.size()})) == many_bytes);
194 }
195 
196 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.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
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
std::vector< uint8_t > decode(utils::string_view in)
Definition: base64.cpp:215
BOOST_AUTO_TEST_CASE(utils_join_test)
std::size_t i
Definition: function.cpp:933
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
std::vector< uint8_t > decode(utils::string_view in)
Definition: base64.cpp:225