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