comparison UnitTestsSources/UnitTestsMain.cpp @ 3322:b32b7c44a223

Toolbox::Utf8ToUnicodeCharacter()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 15 Mar 2019 17:37:51 +0100
parents 6f9398eb902d
children b21d4cc8e5d1
comparison
equal deleted inserted replaced
3321:e54ca78059bd 3322:b32b7c44a223
480 ASSERT_EQ(s, Toolbox::ConvertFromUtf8(Toolbox::ConvertToUtf8(s, Encoding_Latin1, false), Encoding_Latin1)); 480 ASSERT_EQ(s, Toolbox::ConvertFromUtf8(Toolbox::ConvertToUtf8(s, Encoding_Latin1, false), Encoding_Latin1));
481 ASSERT_EQ("cre", Toolbox::ConvertToUtf8(s, Encoding_Utf8, false)); 481 ASSERT_EQ("cre", Toolbox::ConvertToUtf8(s, Encoding_Utf8, false));
482 } 482 }
483 483
484 484
485 static int32_t GetUnicode(const uint8_t* data,
486 size_t size,
487 size_t expectedLength)
488 {
489 std::string s((char*) &data[0], size);
490 uint32_t unicode;
491 size_t length;
492 Toolbox::Utf8ToUnicodeCharacter(unicode, length, s, 0);
493 if (length != expectedLength)
494 {
495 return -1; // Error case
496 }
497 else
498 {
499 return unicode;
500 }
501 }
502
503
504 TEST(Toolbox, Utf8ToUnicode)
505 {
506 // https://en.wikipedia.org/wiki/UTF-8
507
508 ASSERT_EQ(1, sizeof(char));
509 ASSERT_EQ(1, sizeof(uint8_t));
510
511 {
512 const uint8_t data[] = { 0x24 };
513 ASSERT_EQ(0x24, GetUnicode(data, 1, 1));
514 ASSERT_THROW(GetUnicode(data, 0, 1), OrthancException);
515 }
516
517 {
518 const uint8_t data[] = { 0xc2, 0xa2 };
519 ASSERT_EQ(0xa2, GetUnicode(data, 2, 2));
520 ASSERT_THROW(GetUnicode(data, 1, 2), OrthancException);
521 }
522
523 {
524 const uint8_t data[] = { 0xe0, 0xa4, 0xb9 };
525 ASSERT_EQ(0x0939, GetUnicode(data, 3, 3));
526 ASSERT_THROW(GetUnicode(data, 2, 3), OrthancException);
527 }
528
529 {
530 const uint8_t data[] = { 0xe2, 0x82, 0xac };
531 ASSERT_EQ(0x20ac, GetUnicode(data, 3, 3));
532 ASSERT_THROW(GetUnicode(data, 2, 3), OrthancException);
533 }
534
535 {
536 const uint8_t data[] = { 0xf0, 0x90, 0x8d, 0x88 };
537 ASSERT_EQ(0x010348, GetUnicode(data, 4, 4));
538 ASSERT_THROW(GetUnicode(data, 3, 4), OrthancException);
539 }
540
541 {
542 const uint8_t data[] = { 0xe0 };
543 ASSERT_THROW(GetUnicode(data, 1, 1), OrthancException);
544 }
545 }
546
547
485 TEST(Toolbox, UrlDecode) 548 TEST(Toolbox, UrlDecode)
486 { 549 {
487 std::string s; 550 std::string s;
488 551
489 s = "Hello%20World"; 552 s = "Hello%20World";