testUTF8.cxx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include <cm_utf8.h>
  4. #include <stdio.h>
  5. typedef char test_utf8_char[5];
  6. static void test_utf8_char_print(test_utf8_char const c)
  7. {
  8. unsigned char const* d = reinterpret_cast<unsigned char const*>(c);
  9. printf("[0x%02X,0x%02X,0x%02X,0x%02X]", static_cast<int>(d[0]),
  10. static_cast<int>(d[1]), static_cast<int>(d[2]),
  11. static_cast<int>(d[3]));
  12. }
  13. struct test_utf8_entry
  14. {
  15. int n;
  16. test_utf8_char str;
  17. unsigned int chr;
  18. };
  19. static test_utf8_entry const good_entry[] = {
  20. { 1, "\x20\x00\x00\x00", 0x0020 }, /* Space. */
  21. { 2, "\xC2\xA9\x00\x00", 0x00A9 }, /* Copyright. */
  22. { 3, "\xE2\x80\x98\x00", 0x2018 }, /* Open-single-quote. */
  23. { 3, "\xE2\x80\x99\x00", 0x2019 }, /* Close-single-quote. */
  24. { 4, "\xF0\xA3\x8E\xB4", 0x233B4 }, /* Example from RFC 3629. */
  25. { 0, { 0, 0, 0, 0, 0 }, 0 }
  26. };
  27. static test_utf8_char const bad_chars[] = {
  28. "\x80\x00\x00\x00", "\xC0\x00\x00\x00", "\xE0\x00\x00\x00",
  29. "\xE0\x80\x80\x00", "\xF0\x80\x80\x80", { 0, 0, 0, 0, 0 }
  30. };
  31. static void report_good(bool passed, test_utf8_char const c)
  32. {
  33. printf("%s: decoding good ", passed ? "pass" : "FAIL");
  34. test_utf8_char_print(c);
  35. printf(" (%s) ", c);
  36. }
  37. static void report_bad(bool passed, test_utf8_char const c)
  38. {
  39. printf("%s: decoding bad ", passed ? "pass" : "FAIL");
  40. test_utf8_char_print(c);
  41. printf(" ");
  42. }
  43. static bool decode_good(test_utf8_entry const entry)
  44. {
  45. unsigned int uc;
  46. if (const char* e =
  47. cm_utf8_decode_character(entry.str, entry.str + 4, &uc)) {
  48. int used = static_cast<int>(e - entry.str);
  49. if (uc != entry.chr) {
  50. report_good(false, entry.str);
  51. printf("expected 0x%04X, got 0x%04X\n", entry.chr, uc);
  52. return false;
  53. }
  54. if (used != entry.n) {
  55. report_good(false, entry.str);
  56. printf("had %d bytes, used %d\n", entry.n, used);
  57. return false;
  58. }
  59. report_good(true, entry.str);
  60. printf("got 0x%04X\n", uc);
  61. return true;
  62. }
  63. report_good(false, entry.str);
  64. printf("failed\n");
  65. return false;
  66. }
  67. static bool decode_bad(test_utf8_char const s)
  68. {
  69. unsigned int uc = 0xFFFFu;
  70. const char* e = cm_utf8_decode_character(s, s + 4, &uc);
  71. if (e) {
  72. report_bad(false, s);
  73. printf("expected failure, got 0x%04X\n", uc);
  74. return false;
  75. }
  76. report_bad(true, s);
  77. printf("failed as expected\n");
  78. return true;
  79. }
  80. int testUTF8(int /*unused*/, char* /*unused*/ [])
  81. {
  82. int result = 0;
  83. for (test_utf8_entry const* e = good_entry; e->n; ++e) {
  84. if (!decode_good(*e)) {
  85. result = 1;
  86. }
  87. }
  88. for (test_utf8_char const* c = bad_chars; (*c)[0]; ++c) {
  89. if (!decode_bad(*c)) {
  90. result = 1;
  91. }
  92. }
  93. return result;
  94. }