The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_serialization.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2016 by Karol Nowak <[email protected]>
3  Part of the Battle for Wesnoth Project http://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>
21 #include <boost/test/auto_unit_test.hpp>
22 
23 BOOST_AUTO_TEST_SUITE ( test_serialization_utils_and_unicode )
24 
25 BOOST_AUTO_TEST_CASE( utils_join_test )
26 {
27  std::vector<std::string> fruit;
28 
29  BOOST_CHECK( utils::join(fruit) == "" );
30 
31  fruit.push_back("apples");
32 
33  BOOST_CHECK( utils::join(fruit) == "apples" );
34 
35  fruit.push_back("oranges");
36  fruit.push_back("lemons");
37 
38  BOOST_CHECK( utils::join(fruit) == "apples,oranges,lemons" );
39 }
40 
41 BOOST_AUTO_TEST_CASE( utils_unicode_test )
42 {
43  utf8::string unicode = "ünicod€ check";
44  BOOST_CHECK( utf8::size(unicode) == 13 );
45 
46  int euro = utf8::index(unicode,6);
47  BOOST_CHECK( unicode.substr(euro,utf8::index(unicode,7)-euro) == "€" );
48 
49  BOOST_CHECK( utf8::truncate(unicode,3) == "üni");
50 
51  utf8::string apple_u8("apple");
52  ucs4::string apple_u4 = unicode_cast<ucs4::string>(apple_u8);
53  utf16::string apple_u16 = unicode_cast<utf16::string>(apple_u4);
54 
55  BOOST_CHECK( apple_u4.size() == 5 );
56  BOOST_CHECK_EQUAL( apple_u8, unicode_cast<utf8::string>(apple_u4) );
57  BOOST_CHECK_EQUAL( apple_u8, unicode_cast<utf8::string>(apple_u16) );
58  BOOST_CHECK( apple_u4 == unicode_cast<ucs4::string>(apple_u16) );
59  BOOST_CHECK( apple_u16 == unicode_cast<utf16::string>(apple_u4) );
60  BOOST_CHECK_EQUAL( apple_u8.size(), apple_u16.size() );
61 
62  ucs4::string water_u4;
63  water_u4.push_back(0x6C34);
64  utf8::string water_u8 = unicode_cast<utf8::string>(water_u4);
65  utf16::string water_u16 = unicode_cast<utf16::string>(water_u4);
66 
67  BOOST_CHECK_EQUAL(water_u4[0], water_u16[0]);
68  BOOST_CHECK_EQUAL(water_u8, "\u6C34");
69 
70  utf8::string nonbmp_u8("\U00010000");
71  ucs4::string nonbmp_u4 = unicode_cast<ucs4::string>(nonbmp_u8);
72  utf16::string nonbmp_u16 = unicode_cast<utf16::string>(nonbmp_u4);
73 
74  BOOST_CHECK_EQUAL(nonbmp_u8.size(), 4);
75  BOOST_CHECK_EQUAL(nonbmp_u4[0], 0x10000);
76  BOOST_CHECK_EQUAL(nonbmp_u16[0], 0xD800);
77  BOOST_CHECK_EQUAL(nonbmp_u16[1], 0xDC00);
78  BOOST_CHECK_EQUAL(nonbmp_u8, unicode_cast<utf8::string>(nonbmp_u4));
79  BOOST_CHECK_EQUAL(nonbmp_u8, unicode_cast<utf8::string>(nonbmp_u16));
80  BOOST_CHECK(nonbmp_u16 == unicode_cast<utf16::string>(nonbmp_u4));
81  BOOST_CHECK(nonbmp_u4 == unicode_cast<ucs4::string>(nonbmp_u16));
82 }
83 
84 BOOST_AUTO_TEST_CASE( test_lowercase )
85 {
86  BOOST_CHECK_EQUAL ( utf8::lowercase("FOO") , "foo" );
87  BOOST_CHECK_EQUAL ( utf8::lowercase("foo") , "foo" );
88  BOOST_CHECK_EQUAL ( utf8::lowercase("FoO") , "foo" );
89  BOOST_CHECK_EQUAL ( utf8::lowercase("fO0") , "fo0" );
90 }
91 
92 BOOST_AUTO_TEST_CASE( test_wildcard_string_match )
93 {
94  const std::string str = "foo bar baz";
95 
96  BOOST_CHECK(utils::wildcard_string_match(str, "*bar*"));
97 
98  BOOST_CHECK(!utils::wildcard_string_match(str, "*BAR*"));
99  BOOST_CHECK(!utils::wildcard_string_match(str, "bar"));
100 
101  BOOST_CHECK(utils::wildcard_string_match(str, "*ba? b*"));
102  BOOST_CHECK(utils::wildcard_string_match(str, "*?a?*"));
103 
104  BOOST_CHECK(!utils::wildcard_string_match(str, "foo? "));
105  BOOST_CHECK(!utils::wildcard_string_match(str, "?foo"));
106 
107  std::string superfluous_mask;
108 
109  superfluous_mask = std::string(str.length(), '?');
110  BOOST_CHECK(utils::wildcard_string_match(str, superfluous_mask));
111  BOOST_CHECK(utils::wildcard_string_match(str, superfluous_mask + '?'));
112 
113  superfluous_mask = std::string(str.length(), '*');
114  BOOST_CHECK(utils::wildcard_string_match(str, superfluous_mask));
115  BOOST_CHECK(utils::wildcard_string_match(str, superfluous_mask + '*'));
116 
117  BOOST_CHECK(utils::wildcard_string_match("", ""));
118  BOOST_CHECK(!utils::wildcard_string_match(str, ""));
119 
120  BOOST_CHECK(utils::wildcard_string_match("", "*"));
121  BOOST_CHECK(utils::wildcard_string_match("", "***?**"));
122  BOOST_CHECK(!utils::wildcard_string_match("", "?"));
123  BOOST_CHECK(!utils::wildcard_string_match("", "???"));
124 }
125 
126 BOOST_AUTO_TEST_SUITE_END()
std::vector< char_t > string
size_t index(const utf8::string &str, const size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:73
std::vector< char_t > string
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 '*' as any number of characters (including none), and '?' as any one character.
utf8::string lowercase(const utf8::string &s)
Returns a lowercased version of the string.
Definition: unicode.cpp:53
BOOST_AUTO_TEST_SUITE(test_map_location)
utf8::string & truncate(utf8::string &str, const size_t size)
Truncates a UTF-8 string to the specified number of characters.
Definition: unicode.cpp:119
std::string join(T const &v, const std::string &s=",")
Generates a new string joining container items in a list.
BOOST_AUTO_TEST_CASE(utils_join_test)
size_t size(const utf8::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:88
GLsizei const GLcharARB ** string
Definition: glew.h:4503
std::string string