| 283 | //} |
| 284 | |
| 285 | TEST(EncodingsTest, UTF8) { |
| 286 | StringBuffer os, os2; |
| 287 | for (const unsigned* range = kCodepointRanges; *range != 0xFFFFFFFF; range += 2) { |
| 288 | for (unsigned codepoint = range[0]; codepoint <= range[1]; ++codepoint) { |
| 289 | os.Clear(); |
| 290 | UTF8<>::Encode(os, codepoint); |
| 291 | const char* encodedStr = os.GetString(); |
| 292 | |
| 293 | // Decode with Hoehrmann |
| 294 | { |
| 295 | unsigned decodedCodepoint = 0; |
| 296 | unsigned state = 0; |
| 297 | |
| 298 | unsigned decodedCount = 0; |
| 299 | for (const char* s = encodedStr; *s; ++s) |
| 300 | if (!decode(&state, &decodedCodepoint, static_cast<unsigned char>(*s))) { |
| 301 | EXPECT_EQ(codepoint, decodedCodepoint); |
| 302 | decodedCount++; |
| 303 | } |
| 304 | |
| 305 | if (*encodedStr) { // This decoder cannot handle U+0000 |
| 306 | EXPECT_EQ(1u, decodedCount); // Should only contain one code point |
| 307 | } |
| 308 | |
| 309 | EXPECT_EQ(UTF8_ACCEPT, state); |
| 310 | if (UTF8_ACCEPT != state) |
| 311 | std::cout << std::hex << codepoint << " " << decodedCodepoint << std::endl; |
| 312 | } |
| 313 | |
| 314 | // Decode |
| 315 | { |
| 316 | StringStream is(encodedStr); |
| 317 | unsigned decodedCodepoint; |
| 318 | bool result = UTF8<>::Decode(is, &decodedCodepoint); |
| 319 | EXPECT_TRUE(result); |
| 320 | EXPECT_EQ(codepoint, decodedCodepoint); |
| 321 | if (!result || codepoint != decodedCodepoint) |
| 322 | std::cout << std::hex << codepoint << " " << decodedCodepoint << std::endl; |
| 323 | } |
| 324 | |
| 325 | // Validate |
| 326 | { |
| 327 | StringStream is(encodedStr); |
| 328 | os2.Clear(); |
| 329 | bool result = UTF8<>::Validate(is, os2); |
| 330 | EXPECT_TRUE(result); |
| 331 | EXPECT_EQ(0, StrCmp(encodedStr, os2.GetString())); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | TEST(EncodingsTest, UTF16) { |
| 338 | GenericStringBuffer<UTF16<> > os, os2; |