summaryrefslogtreecommitdiffhomepage
path: root/src/test-unicode.cpp
blob: 692dfac042ecc608be65a30a10d3c742c3636495 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#define BOOST_TEST_MODULE unicode_test

#include <boost/test/included/unit_test.hpp>
#include <boost/test/data/dataset.hpp>
#include <boost/test/data/monomorphic.hpp>
#include <boost/test/data/test_case.hpp>

#include <chrono>
#include <exception>
#include <limits>
#include <random>
#include <string>
#include <tuple>
#include <type_traits>
#include <vector>

#include <unicode.h>

using namespace std::chrono_literals;

typedef std::tuple<std::basic_string<utf8_t>, std::basic_string<char16_t>, std::basic_string<char32_t>> types_collection_type;

// create tuple of the same string, in UTF-8, UTF-16 and UTF-32
#define SUCCESS_TUPLE(x) {u8 ## x, u ## x, U ## x}

// Success cases: convert string to all other types, respectively
std::vector<types_collection_type> success_sets {
 SUCCESS_TUPLE(""),
 SUCCESS_TUPLE("ASCII string1"),
 SUCCESS_TUPLE("Täst just looks like German"),
 SUCCESS_TUPLE("\u732b is chinese for cat"),
 SUCCESS_TUPLE("\U0001F63A"),
 SUCCESS_TUPLE("\U0001F63A is a smiling cat"),
};

// Error cases: throwing upon convert to all other types
std::vector<std::basic_string<utf8_t>> failure_strings_char8_t {
 u8"\x80", // utf-8 continuation byte
 u8"\x81", // utf-8 continuation byte
 u8"\xc3\xc3\xa4", // initial byte of utf-8 "ä", followed by valid utf-8 "ä"
 u8"\xF8\x80\x80\x80\x80", // overlong encoding
 u8"\xF7\xBF\xBF\xBF", // valid encoding of invalid code point
};

std::vector<std::basic_string<char16_t>> failure_strings_char16_t {
 u"\xD801", // single high surrogate
 u"\xDFFF", // single low surrogate
 u"\xDFFF\xD801", // bad surrogate pair order
};

std::vector<std::basic_string<char32_t>> failure_strings_char32_t {
 U"blabla \xD801", // invalid unicode (surrogate half)
 U"\x10000000", // invalid unicode (number too big)
};

// output operators must be in same namespace as the type itself
namespace std {

#ifdef __cpp_char8_t
std::ostream& operator<<(std::ostream& os, std::basic_string<utf8_t> const& s)
{
 os << "[";
 for (auto& c: s)
  os << " " << std::to_string(static_cast<uint8_t>(c));
 os << "]";

 return os;
}
#endif

std::ostream& operator<<(std::ostream& os, std::basic_string<char16_t> const& s)
{
 os << "[";
 for (auto& c: s)
  os << " " << std::to_string(static_cast<uint16_t>(c));
 os << "]";

 return os;
}

std::ostream& operator<<(std::ostream& os, std::basic_string<char32_t> const& s)
{
 os << "[";
 for (auto& c: s)
  os << " " << std::to_string(static_cast<uint32_t>(c));
 os << "]";

 return os;
}

}

template<size_t i = 0, size_t j = 0, typename... Ts>
void test_utf_to_utf(std::tuple<Ts...>& t)
{
 typedef typename std::tuple_element<i,typename std::remove_reference<decltype(t)>::type>::type From;
 typedef typename std::tuple_element<j,typename std::remove_reference<decltype(t)>::type>::type To;

 // test base type interface
 To result { unicode::convert<typename From::value_type, typename To::value_type>(std::get<i>(t)) };

 BOOST_CHECK_MESSAGE(std::get<j>(t) == result, "Base: From " << typeid(From).name() << "(" << i << ", " << std::get<i>(t) << ") to " << typeid(To).name() << "(" << j << ", " << std::get<j>(t) << "), got " << result);

 //std::cout << std::to_string(std::tuple_size<typename std::remove_reference<decltype(t)>::type>::value) << "," << std::to_string(i) << "," << std::to_string(j) << std::endl;
 
 // test facet interface
 result = unicode::convert<typename unicode::Encoding<typename From::value_type>::Facet, typename unicode::Encoding<typename To::value_type>::Facet>(std::get<i>(t));

 BOOST_CHECK_MESSAGE(std::get<j>(t) == result, "Facet: From " << typeid(From).name() << "(" << i << ", " << std::get<i>(t) << ") to " << typeid(To).name() << "(" << j << ", " << std::get<j>(t) << "), got " << result);
 
 // iterate over other combinations
 if constexpr (i + 1 < std::tuple_size<typename std::remove_reference<decltype(t)>::type>::value)
  test_utf_to_utf<i + 1, j>(t);
 else if constexpr (j + 1 < std::tuple_size<typename std::remove_reference<decltype(t)>::type>::value)
  test_utf_to_utf<0, j + 1>(t);
}

// We don't use BOOST_DATA_TEST_CASE here because boost::test tries to assign
// a new variable to each tuple element which we don't want
// https://lists.boost.org/boost-bugs/2016/05/45214.php

BOOST_AUTO_TEST_CASE(utf_to_utf_success)
{
 for (auto& t: success_sets)
  test_utf_to_utf(t);
}

template<size_t i = 0, typename... Ts>
void test_is_valid_utf(std::tuple<Ts...>& t)
{
 typedef typename std::tuple_element<i,typename std::remove_reference<decltype(t)>::type>::type T;

 // test via basic type
 bool result { unicode::is_valid_utf<typename T::value_type>(std::get<i>(t)) };
 BOOST_CHECK_MESSAGE(result == true, "is_valid_utf w/ " << typeid(T).name() << "(" << i << ", " << std::get<i>(t) << "), got " << result);

 // test via Facet
 result = unicode::is_valid_utf<typename unicode::Encoding<typename T::value_type>::Facet>(std::get<i>(t));
 BOOST_CHECK_MESSAGE(result == true, "is_valid_utf w/ " << typeid(typename unicode::Encoding<typename T::value_type>::Facet).name() << "(" << i << ", " << std::get<i>(t) << "), got " << result);

 // iterate over other combinations
 if constexpr (i + 1 < std::tuple_size<typename std::remove_reference<decltype(t)>::type>::value)
  test_is_valid_utf<i + 1>(t);
}

BOOST_AUTO_TEST_CASE(is_valid_utf_success)
{
 for (auto& t: success_sets)
  test_is_valid_utf(t);
}

// iterate over std::tuple T types
template<typename From, typename Collection, size_t index = 0>
void test_utf_to_utf_failure(std::basic_string<From>& s)
{
 typedef typename std::tuple_element<index, Collection>::type::value_type To;

 // via base type
 try {
  (void) unicode::convert<From,To>(s);
  BOOST_ERROR("Base: Expected exception at index: " << index << ", " << typeid(From).name() << " -> " << typeid(To).name());
 } catch (const std::invalid_argument&) {
  // OK: this is an expected exception for convert() on bad input
 } catch (const std::exception& ex) {
  BOOST_ERROR("Unexpected error on convert(): " << ex.what());
 };

 // via facet
 try {
  (void) unicode::convert<typename unicode::Encoding<From>::Facet,typename unicode::Encoding<To>::Facet>(s);
  BOOST_ERROR("Facet: Expected exception at index: " << index << ", " << typeid(From).name() << " -> " << typeid(To).name());
 } catch (const std::invalid_argument&) {
  // OK: this is an expected exception for convert() on bad input
 } catch (const std::exception& ex) {
  BOOST_ERROR("Unexpected error on convert(): " << ex.what());
 };

 // iterate over remaining types 
 if constexpr (index + 1 < std::tuple_size<Collection>::value)
  test_utf_to_utf_failure<From, Collection, index + 1>(s);
}

BOOST_AUTO_TEST_CASE(utf_to_utf_failure)
{
 for (auto& s: failure_strings_char8_t)
  test_utf_to_utf_failure<typename std::remove_reference<decltype(s)>::type::value_type, types_collection_type>(s);
 
 for (auto& s: failure_strings_char16_t)
  test_utf_to_utf_failure<typename std::remove_reference<decltype(s)>::type::value_type, types_collection_type>(s);

 for (auto& s: failure_strings_char32_t)
  test_utf_to_utf_failure<typename std::remove_reference<decltype(s)>::type::value_type, types_collection_type>(s);
}

// iterate over std::tuple T types
template<typename T, typename Collection, size_t index = 0>
void test_is_valid_utf_failure(std::basic_string<T>& s)
{
 BOOST_CHECK_MESSAGE(unicode::is_valid_utf<T>(s) == false, "Expected bad UTF at index: " << index << ", " << typeid(T).name());
 
 BOOST_CHECK_MESSAGE(unicode::is_valid_utf<typename unicode::Encoding<T>::Facet>(s) == false, "Expected bad UTF at index: " << index << ", " << typeid(typename unicode::Encoding<T>::Facet).name());

 // iterate over remaining types 
 if constexpr (index + 1 < std::tuple_size<Collection>::value)
  test_is_valid_utf_failure<T, Collection, index + 1>(s);
}

BOOST_AUTO_TEST_CASE(is_valid_utf_failure)
{
 for (auto& s: failure_strings_char8_t)
  test_is_valid_utf_failure<typename std::remove_reference<decltype(s)>::type::value_type, types_collection_type>(s);
 
 for (auto& s: failure_strings_char16_t)
  test_is_valid_utf_failure<typename std::remove_reference<decltype(s)>::type::value_type, types_collection_type>(s);

 for (auto& s: failure_strings_char32_t)
  test_is_valid_utf_failure<typename std::remove_reference<decltype(s)>::type::value_type, types_collection_type>(s);
}

BOOST_AUTO_TEST_CASE(is_valid_unicode)
{
 BOOST_CHECK(unicode::is_valid_unicode('\0'));
 BOOST_CHECK(unicode::is_valid_unicode(U'a'));
 BOOST_CHECK(unicode::is_valid_unicode(U'ä'));
 BOOST_CHECK(unicode::is_valid_unicode(U'\u732b')); // cat chinese
 BOOST_CHECK(unicode::is_valid_unicode(U'\U0001F63A')); // cat chinese
 BOOST_CHECK(unicode::is_valid_unicode(0x0001F63A)); // cat smiley

 BOOST_CHECK(!unicode::is_valid_unicode(0x00110000));
 BOOST_CHECK(!unicode::is_valid_unicode(0xFFFFFFFF)); // U"\UFFFFFFFF" is invalid C++
 BOOST_CHECK(!unicode::is_valid_unicode(0x01234567));
 BOOST_CHECK(!unicode::is_valid_unicode(0x12345678));
 BOOST_CHECK(!unicode::is_valid_unicode(0xD800));
 BOOST_CHECK(!unicode::is_valid_unicode(0xD987));
 BOOST_CHECK(!unicode::is_valid_unicode(0xDFFF));
}

struct random_context {
 std::random_device rd;  // OS random number engine to seed RNG (below)
 std::mt19937 gen{rd()};
 std::uniform_int_distribution<size_t> sequence_length{0, 100000}; // length of sequence: 0 ... 100000 code units
};

template<typename T>
T generate_random(random_context& rc, size_t length)
{
 // Using unsigned long for std::uniform_int_distribution<> because it needs to be basic type according to MSVC
 std::uniform_int_distribution<unsigned long> code_unit(std::numeric_limits<typename T::value_type>::max()); // code unit value
 T result;
 std::generate_n(std::back_inserter(result), length, [&](){return static_cast<typename T::value_type>(code_unit(rc.gen));});

 return result;
}

template<typename From, typename ToTypesCollectionType, size_t i = 0>
void test_random(random_context& rc, size_t length)
{
 //std::cerr << "LENGTH: " << length << std::endl;
 typedef typename std::tuple_element<i,ToTypesCollectionType>::type To;

 From r {static_cast<From>(generate_random<From>(rc, length))};

 // base type interface
 try {
  To result{unicode::convert<typename From::value_type,typename To::value_type>(r)};

  if (r.empty()) {
   BOOST_CHECK(result.empty());
  } else {
   BOOST_CHECK(!result.empty());
  }
 } catch (const std::invalid_argument&) {
  // OK: this is an expected exception for convert() on bad input
 } catch (const std::exception& ex) {
  BOOST_ERROR("Unexpected error on convert(): " << ex.what());
 }

 // facet interface
 try {
  To result{unicode::convert<typename unicode::Encoding<typename From::value_type>::Facet,typename unicode::Encoding<typename To::value_type>::Facet>(r)};

  if (r.empty()) {
   BOOST_CHECK(result.empty());
  } else {
   BOOST_CHECK(!result.empty());
  }
 } catch (const std::invalid_argument&) {
  // OK: this is an expected exception for convert() on bad input
 } catch (const std::exception& ex) {
  BOOST_ERROR("Unexpected error on convert(): " << ex.what());
 }

 // iterate over remaining To types
 if constexpr (i + 1 < std::tuple_size<ToTypesCollectionType>::value)
  test_random<From, ToTypesCollectionType, i + 1>(rc, length);
}

BOOST_AUTO_TEST_CASE_TEMPLATE(random_sequences, T, types_collection_type)
{
 random_context rc;
 int i{};

 // run for 1s (debug) 10s (release) = total time for all random_sequences types!
#ifdef _DEBUG
 const auto timeout{1.0s};
#else
 const auto timeout{10.0s};
#endif

 auto timeout_stamp { std::chrono::steady_clock::now() + (timeout / std::tuple_size<types_collection_type>::value)};

 while (!(std::chrono::steady_clock::now() > timeout_stamp)) {
  test_random<T,types_collection_type>(rc, rc.sequence_length(rc.gen));
  i++;
 }

 BOOST_CHECK_MESSAGE(i > 1, "Not enough iterations done!");
}

// Test ISO and UTF encodings
BOOST_AUTO_TEST_CASE(convert)
{
 BOOST_CHECK((std::string{unicode::convert<unicode::ISO_8859_1,unicode::ISO_8859_1>({})}) == std::string{});
 BOOST_CHECK((std::string{unicode::convert<unicode::ISO_8859_1,unicode::ISO_8859_1>("abc")}) == std::string{"abc"});
 BOOST_CHECK((std::string{unicode::convert<unicode::ISO_8859_1,unicode::ISO_8859_1>("äöü")}) == std::string{"äöü"});
 BOOST_CHECK((std::string{unicode::convert<unicode::ISO_8859_1,unicode::ISO_8859_1>("\xa4")}) == std::string{"\xa4"}); // €
 
 BOOST_CHECK((std::string{unicode::convert<unicode::ISO_8859_15,unicode::ISO_8859_15>("\xa4")}) == std::string{"\xa4"}); // €
 
 BOOST_CHECK_THROW(((void)std::string{unicode::convert<unicode::ISO_8859_15,unicode::ISO_8859_1>("\xa4")}), std::invalid_argument); // € not available in ISO-8859-1
 
 BOOST_CHECK((unicode::convert<unicode::UTF_8,unicode::UTF_16>("abc")) == std::u16string{u"abc"});
 BOOST_CHECK((unicode::convert<unicode::UTF_32,unicode::UTF_16>(U"abc")) == std::u16string{u"abc"});
 
 BOOST_CHECK((unicode::convert<utf8_t,char16_t>("abc")) == std::u16string{u"abc"});
 BOOST_CHECK((unicode::convert<char32_t,char16_t>(U"abc")) == std::u16string{u"abc"});
}

BOOST_AUTO_TEST_CASE(is_valid_utf)
{
 BOOST_CHECK(unicode::is_valid_utf<char16_t>(u"äöü"));

 BOOST_CHECK(unicode::is_valid_utf<unicode::UTF_8>(u8"äöü"));
}

BOOST_AUTO_TEST_CASE(string_u8string)
{
 std::string a{"\xc3\xa4"};

 std::basic_string<utf8_t> b{a.begin(), a.end()};

 BOOST_CHECK(b == std::basic_string<utf8_t>{u8"ä"});
 
 a = std::string{b.begin(), b.end()};
 
 BOOST_CHECK(a == std::string{"\xc3\xa4"});
}

// TODO:
//
// char8_t, char16_t, char32_t, char, wchar_t (UTF-16 on Windows, UTF-32 on Linux)
// string, vector?
// uint8_t, uint16_t, uint32_t?