| 1 |
#include "../extern/ftest/ftest.h" |
| 2 |
#include "utf8.h" |
| 3 |
#include <string> |
| 4 |
using namespace utf8; |
| 5 |
using namespace std; |
| 6 |
|
| 7 |
#if __cplusplus >= 201103L // C++ 11 or later |
| 8 |
|
| 9 |
TEST(CPP11APITests, test_append) |
| 10 |
{ |
| 11 |
string u; |
| 12 |
append(0x0448, u); |
| 13 |
EXPECT_EQ (u[0], char(0xd1)); |
| 14 |
EXPECT_EQ (u[1], char(0x88)); |
| 15 |
EXPECT_EQ (u.length(), 2); |
| 16 |
|
| 17 |
u.clear(); |
| 18 |
append(0x65e5, u); |
| 19 |
EXPECT_EQ (u[0], char(0xe6)); |
| 20 |
EXPECT_EQ (u[1], char(0x97)); |
| 21 |
EXPECT_EQ (u[2], char(0xa5)); |
| 22 |
EXPECT_EQ (u.length(), 3); |
| 23 |
|
| 24 |
u.clear(); |
| 25 |
append(0x3044, u); |
| 26 |
EXPECT_EQ (u[0], char(0xe3)); |
| 27 |
EXPECT_EQ (u[1], char(0x81)); |
| 28 |
EXPECT_EQ (u[2], char(0x84)); |
| 29 |
EXPECT_EQ (u.length(), 3); |
| 30 |
|
| 31 |
u.clear(); |
| 32 |
append(0x10346, u); |
| 33 |
EXPECT_EQ (u[0], char(0xf0)); |
| 34 |
EXPECT_EQ (u[1], char(0x90)); |
| 35 |
EXPECT_EQ (u[2], char(0x8d)); |
| 36 |
EXPECT_EQ (u[3], char(0x86)); |
| 37 |
EXPECT_EQ (u.length(), 4); |
| 38 |
} |
| 39 |
|
| 40 |
TEST(CPP11APITests, test_utf16to8) |
| 41 |
{ |
| 42 |
u16string utf16string = {0x41, 0x0448, 0x65e5, 0xd834, 0xdd1e}; |
| 43 |
string u = utf16to8(utf16string); |
| 44 |
EXPECT_EQ (u.size(), 10); |
| 45 |
} |
| 46 |
|
| 47 |
TEST(CPP11APITests, test_utf8to16) |
| 48 |
{ |
| 49 |
string utf8_with_surrogates = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e"; |
| 50 |
u16string utf16result = utf8to16(utf8_with_surrogates); |
| 51 |
EXPECT_EQ (utf16result.size(), 4); |
| 52 |
EXPECT_EQ (utf16result[2], 0xd834); |
| 53 |
EXPECT_EQ (utf16result[3], 0xdd1e); |
| 54 |
// Just to make sure it compiles with string literals |
| 55 |
utf8to16(u8"simple"); |
| 56 |
utf8to16("simple"); |
| 57 |
} |
| 58 |
|
| 59 |
TEST(CPP11APITests, test_utf32to8) |
| 60 |
{ |
| 61 |
u32string utf32string = {0x448, 0x65E5, 0x10346}; |
| 62 |
string utf8result = utf32to8(utf32string); |
| 63 |
EXPECT_EQ (utf8result.size(), 9); |
| 64 |
} |
| 65 |
|
| 66 |
TEST(CPP11APITests, test_utf8to32) |
| 67 |
{ |
| 68 |
const char* twochars = "\xe6\x97\xa5\xd1\x88"; |
| 69 |
u32string utf32result = utf8to32(twochars); |
| 70 |
EXPECT_EQ (utf32result.size(), 2); |
| 71 |
} |
| 72 |
|
| 73 |
TEST(CPP11APITests, test_find_invalid) |
| 74 |
{ |
| 75 |
string utf_invalid = "\xe6\x97\xa5\xd1\x88\xfa"; |
| 76 |
auto invalid = find_invalid(utf_invalid); |
| 77 |
EXPECT_EQ (invalid, 5); |
| 78 |
} |
| 79 |
|
| 80 |
TEST(CPP11APITests, test_is_valid) |
| 81 |
{ |
| 82 |
string utf_invalid = "\xe6\x97\xa5\xd1\x88\xfa"; |
| 83 |
bool bvalid = is_valid(utf_invalid); |
| 84 |
EXPECT_FALSE (bvalid); |
| 85 |
string utf8_with_surrogates = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e"; |
| 86 |
bvalid = is_valid(utf8_with_surrogates); |
| 87 |
EXPECT_TRUE (bvalid); |
| 88 |
} |
| 89 |
|
| 90 |
TEST(CPP11APITests, test_replace_invalid) |
| 91 |
{ |
| 92 |
string invalid_sequence = "a\x80\xe0\xa0\xc0\xaf\xed\xa0\x80z"; |
| 93 |
string replace_invalid_result = replace_invalid(invalid_sequence, '?'); |
| 94 |
bool bvalid = is_valid(replace_invalid_result); |
| 95 |
EXPECT_TRUE (bvalid); |
| 96 |
const string fixed_invalid_sequence = "a????z"; |
| 97 |
EXPECT_EQ(fixed_invalid_sequence, replace_invalid_result); |
| 98 |
} |
| 99 |
|
| 100 |
TEST(CPP11APITests, test_starts_with_bom) |
| 101 |
{ |
| 102 |
string byte_order_mark = {char(0xef), char(0xbb), char(0xbf)}; |
| 103 |
bool bbom = starts_with_bom(byte_order_mark); |
| 104 |
EXPECT_TRUE (bbom); |
| 105 |
string threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; |
| 106 |
bool no_bbom = starts_with_bom(threechars); |
| 107 |
EXPECT_FALSE (no_bbom); |
| 108 |
} |
| 109 |
#endif // C++ 11 or later |