| 1 |
#ifndef UTF8_FOR_CPP_TEST_UNCHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 |
| 2 |
#define UTF8_FOR_CPP_TEST_UNCHECKED_H_2675DCD0_9480_4c0c_B92A_CC14C027B731 |
| 3 |
|
| 4 |
#include "utf8/unchecked.h" |
| 5 |
|
| 6 |
#include <string> |
| 7 |
#include <vector> |
| 8 |
|
| 9 |
using namespace std; |
| 10 |
|
| 11 |
TEST(UnCheckedAPITests, test_append) |
| 12 |
{ |
| 13 |
unsigned char u[5] = {0,0,0,0,0}; |
| 14 |
utf8::unchecked::append(0x0448, u); |
| 15 |
EXPECT_EQ (u[0], 0xd1); |
| 16 |
EXPECT_EQ (u[1], 0x88); |
| 17 |
EXPECT_EQ (u[2], 0); |
| 18 |
EXPECT_EQ (u[3], 0); |
| 19 |
EXPECT_EQ (u[4], 0); |
| 20 |
|
| 21 |
utf8::unchecked::append(0x65e5, u); |
| 22 |
EXPECT_EQ (u[0], 0xe6); |
| 23 |
EXPECT_EQ (u[1], 0x97); |
| 24 |
EXPECT_EQ (u[2], 0xa5); |
| 25 |
EXPECT_EQ (u[3], 0); |
| 26 |
EXPECT_EQ (u[4], 0); |
| 27 |
|
| 28 |
utf8::unchecked::append(0x3044, u); |
| 29 |
EXPECT_EQ (u[0], 0xe3); |
| 30 |
EXPECT_EQ (u[1], 0x81); |
| 31 |
EXPECT_EQ (u[2], 0x84); |
| 32 |
EXPECT_EQ (u[3], 0); |
| 33 |
EXPECT_EQ (u[4], 0); |
| 34 |
|
| 35 |
utf8::unchecked::append(0x10346, u); |
| 36 |
EXPECT_EQ (u[0], 0xf0); |
| 37 |
EXPECT_EQ (u[1], 0x90); |
| 38 |
EXPECT_EQ (u[2], 0x8d); |
| 39 |
EXPECT_EQ (u[3], 0x86); |
| 40 |
EXPECT_EQ (u[4], 0); |
| 41 |
} |
| 42 |
|
| 43 |
TEST(UnCheckedAPITests, test_next) |
| 44 |
{ |
| 45 |
const char* twochars = "\xe6\x97\xa5\xd1\x88"; |
| 46 |
const char* w = twochars; |
| 47 |
unsigned int cp = utf8::unchecked::next(w); |
| 48 |
EXPECT_EQ (cp, 0x65e5); |
| 49 |
EXPECT_EQ (w, twochars + 3); |
| 50 |
|
| 51 |
const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; |
| 52 |
w = threechars; |
| 53 |
|
| 54 |
cp = utf8::unchecked::next(w); |
| 55 |
EXPECT_EQ (cp, 0x10346); |
| 56 |
EXPECT_EQ (w, threechars + 4); |
| 57 |
|
| 58 |
cp = utf8::unchecked::next(w); |
| 59 |
EXPECT_EQ (cp, 0x65e5); |
| 60 |
EXPECT_EQ (w, threechars + 7); |
| 61 |
|
| 62 |
cp = utf8::unchecked::next(w); |
| 63 |
EXPECT_EQ (cp, 0x0448); |
| 64 |
EXPECT_EQ (w, threechars + 9); |
| 65 |
} |
| 66 |
|
| 67 |
TEST(UnCheckedAPITests, test_peek_next) |
| 68 |
{ |
| 69 |
const char* const cw = "\xe6\x97\xa5\xd1\x88"; |
| 70 |
unsigned int cp = utf8::unchecked::peek_next(cw); |
| 71 |
EXPECT_EQ (cp, 0x65e5); |
| 72 |
} |
| 73 |
|
| 74 |
TEST(UnCheckedAPITests, test_prior) |
| 75 |
{ |
| 76 |
const char* twochars = "\xe6\x97\xa5\xd1\x88"; |
| 77 |
const char* w = twochars + 3; |
| 78 |
unsigned int cp = utf8::unchecked::prior (w); |
| 79 |
EXPECT_EQ (cp, 0x65e5); |
| 80 |
EXPECT_EQ (w, twochars); |
| 81 |
|
| 82 |
const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; |
| 83 |
w = threechars + 9; |
| 84 |
cp = utf8::unchecked::prior(w); |
| 85 |
EXPECT_EQ (cp, 0x0448); |
| 86 |
EXPECT_EQ (w, threechars + 7); |
| 87 |
cp = utf8::unchecked::prior(w); |
| 88 |
EXPECT_EQ (cp, 0x65e5); |
| 89 |
EXPECT_EQ (w, threechars + 4); |
| 90 |
cp = utf8::unchecked::prior(w); |
| 91 |
EXPECT_EQ (cp, 0x10346); |
| 92 |
EXPECT_EQ (w, threechars); |
| 93 |
} |
| 94 |
|
| 95 |
TEST(UnCheckedAPITests, test_advance) |
| 96 |
{ |
| 97 |
const char* threechars = "\xf0\x90\x8d\x86\xe6\x97\xa5\xd1\x88"; |
| 98 |
const char* w = threechars; |
| 99 |
utf8::unchecked::advance(w, 2); |
| 100 |
EXPECT_EQ(w, threechars + 7); |
| 101 |
utf8::unchecked::advance(w, -2); |
| 102 |
EXPECT_EQ(w, threechars); |
| 103 |
utf8::unchecked::advance(w, 3); |
| 104 |
EXPECT_EQ(w, threechars + 9); |
| 105 |
utf8::unchecked::advance(w, -2); |
| 106 |
EXPECT_EQ(w, threechars + 4); |
| 107 |
utf8::unchecked::advance(w, -1); |
| 108 |
EXPECT_EQ(w, threechars); |
| 109 |
} |
| 110 |
|
| 111 |
TEST(UnCheckedAPITests, test_distance) |
| 112 |
{ |
| 113 |
const char* twochars = "\xe6\x97\xa5\xd1\x88"; |
| 114 |
size_t dist = static_cast<size_t>(utf8::unchecked::distance(twochars, twochars + 5)); |
| 115 |
EXPECT_EQ (dist, 2); |
| 116 |
} |
| 117 |
|
| 118 |
TEST(UnCheckedAPITests, test_utf32to8) |
| 119 |
{ |
| 120 |
unsigned int utf32string[] = {0x448, 0x65E5, 0x10346, 0}; |
| 121 |
string utf8result; |
| 122 |
utf8::unchecked::utf32to8(utf32string, utf32string + 3, back_inserter(utf8result)); |
| 123 |
EXPECT_EQ (utf8result.size(), 9); |
| 124 |
} |
| 125 |
|
| 126 |
TEST(UnCheckedAPITests, test_utf8to32) |
| 127 |
{ |
| 128 |
const char* twochars = "\xe6\x97\xa5\xd1\x88"; |
| 129 |
vector<unsigned int> utf32result; |
| 130 |
utf8::unchecked::utf8to32(twochars, twochars + 5, back_inserter(utf32result)); |
| 131 |
EXPECT_EQ (utf32result.size(), 2); |
| 132 |
} |
| 133 |
|
| 134 |
TEST(UnCheckedAPITests, test_utf16to8) |
| 135 |
{ |
| 136 |
unsigned short utf16string[] = {0x41, 0x0448, 0x65e5, 0xd834, 0xdd1e}; |
| 137 |
string utf8result; |
| 138 |
utf8::unchecked::utf16to8(utf16string, utf16string + 5, back_inserter(utf8result)); |
| 139 |
EXPECT_EQ (utf8result.size(), 10); |
| 140 |
} |
| 141 |
|
| 142 |
TEST(UnCheckedAPITests, test_utf8to16) |
| 143 |
{ |
| 144 |
char utf8_with_surrogates[] = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e"; |
| 145 |
vector <unsigned short> utf16result; |
| 146 |
utf8::unchecked::utf8to16(utf8_with_surrogates, utf8_with_surrogates + 9, back_inserter(utf16result)); |
| 147 |
EXPECT_EQ (utf16result.size(), 4); |
| 148 |
EXPECT_EQ (utf16result[2], 0xd834); |
| 149 |
EXPECT_EQ (utf16result[3], 0xdd1e); |
| 150 |
} |
| 151 |
|
| 152 |
TEST(UnCheckedAPITests, test_replace_invalid) |
| 153 |
{ |
| 154 |
char invalid_sequence[] = "a\x80\xe0\xa0\xc0\xaf\xed\xa0\x80z"; |
| 155 |
vector<char> replace_invalid_result; |
| 156 |
utf8::unchecked::replace_invalid (invalid_sequence, invalid_sequence + sizeof(invalid_sequence), std::back_inserter(replace_invalid_result), '?'); |
| 157 |
bool bvalid = utf8::is_valid(replace_invalid_result.begin(), replace_invalid_result.end()); |
| 158 |
EXPECT_TRUE (bvalid); |
| 159 |
const char fixed_invalid_sequence[] = "a????z"; |
| 160 |
EXPECT_EQ (sizeof(fixed_invalid_sequence), replace_invalid_result.size()); |
| 161 |
EXPECT_TRUE (std::equal(replace_invalid_result.begin(), replace_invalid_result.begin() + sizeof(fixed_invalid_sequence), fixed_invalid_sequence)); |
| 162 |
} |
| 163 |
|
| 164 |
#endif |