testUTF8.cxx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 <stdio.h>
  4. #include <cm_utf8.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. #ifndef __clang_analyzer__ // somehow thinks arguments are not initialized
  10. printf("[0x%02X,0x%02X,0x%02X,0x%02X]", static_cast<int>(d[0]),
  11. static_cast<int>(d[1]), static_cast<int>(d[2]),
  12. static_cast<int>(d[3]));
  13. #endif
  14. }
  15. static void byte_array_print(char const* s)
  16. {
  17. unsigned char const* d = reinterpret_cast<unsigned char const*>(s);
  18. bool started = false;
  19. printf("[");
  20. for (; *d; ++d) {
  21. if (started) {
  22. printf(",");
  23. }
  24. started = true;
  25. printf("0x%02X", static_cast<int>(*d));
  26. }
  27. printf("]");
  28. }
  29. struct test_utf8_entry
  30. {
  31. int n;
  32. test_utf8_char str;
  33. unsigned int chr;
  34. };
  35. static test_utf8_entry const good_entry[] = {
  36. { 1, "\x20\x00\x00\x00", 0x0020 }, /* Space. */
  37. { 2, "\xC2\xA9\x00\x00", 0x00A9 }, /* Copyright. */
  38. { 3, "\xE2\x80\x98\x00", 0x2018 }, /* Open-single-quote. */
  39. { 3, "\xE2\x80\x99\x00", 0x2019 }, /* Close-single-quote. */
  40. { 4, "\xF0\xA3\x8E\xB4", 0x233B4 }, /* Example from RFC 3629. */
  41. { 3, "\xED\x80\x80\x00", 0xD000 }, /* Valid 0xED prefixed codepoint. */
  42. { 4, "\xF4\x8F\xBF\xBF", 0x10FFFF }, /* Highest valid RFC codepoint. */
  43. { 0, { 0, 0, 0, 0, 0 }, 0 }
  44. };
  45. static test_utf8_char const bad_chars[] = {
  46. "\x80\x00\x00\x00", /* Leading continuation byte. */
  47. "\xC0\x80\x00\x00", /* Overlong encoding. */
  48. "\xC1\x80\x00\x00", /* Overlong encoding. */
  49. "\xC2\x00\x00\x00", /* Missing continuation byte. */
  50. "\xE0\x00\x00\x00", /* Missing continuation bytes. */
  51. "\xE0\x80\x80\x00", /* Overlong encoding. */
  52. "\xF0\x80\x80\x80", /* Overlong encoding. */
  53. "\xED\xA0\x80\x00", /* UTF-16 surrogate half. */
  54. "\xED\xBF\xBF\x00", /* UTF-16 surrogate half. */
  55. "\xF4\x90\x80\x80", /* Lowest out-of-range codepoint. */
  56. "\xF5\x80\x80\x80", /* Prefix forces out-of-range codepoints. */
  57. { 0, 0, 0, 0, 0 }
  58. };
  59. static char const* good_strings[] = { "", "ASCII", "\xC2\xA9 Kitware", 0 };
  60. static char const* bad_strings[] = {
  61. "\xC0\x80", /* Modified UTF-8 for embedded 0-byte. */
  62. 0
  63. };
  64. static void report_good(bool passed, test_utf8_char const c)
  65. {
  66. printf("%s: decoding good ", passed ? "pass" : "FAIL");
  67. test_utf8_char_print(c);
  68. printf(" (%s) ", c);
  69. }
  70. static void report_bad(bool passed, test_utf8_char const c)
  71. {
  72. printf("%s: decoding bad ", passed ? "pass" : "FAIL");
  73. test_utf8_char_print(c);
  74. printf(" ");
  75. }
  76. static bool decode_good(test_utf8_entry const entry)
  77. {
  78. unsigned int uc;
  79. if (const char* e =
  80. cm_utf8_decode_character(entry.str, entry.str + 4, &uc)) {
  81. int used = static_cast<int>(e - entry.str);
  82. if (uc != entry.chr) {
  83. report_good(false, entry.str);
  84. printf("expected 0x%04X, got 0x%04X\n", entry.chr, uc);
  85. return false;
  86. }
  87. if (used != entry.n) {
  88. report_good(false, entry.str);
  89. printf("had %d bytes, used %d\n", entry.n, used);
  90. return false;
  91. }
  92. report_good(true, entry.str);
  93. printf("got 0x%04X\n", uc);
  94. return true;
  95. }
  96. report_good(false, entry.str);
  97. printf("failed\n");
  98. return false;
  99. }
  100. static bool decode_bad(test_utf8_char const s)
  101. {
  102. unsigned int uc = 0xFFFFu;
  103. const char* e = cm_utf8_decode_character(s, s + 4, &uc);
  104. if (e) {
  105. report_bad(false, s);
  106. printf("expected failure, got 0x%04X\n", uc);
  107. return false;
  108. }
  109. report_bad(true, s);
  110. printf("failed as expected\n");
  111. return true;
  112. }
  113. static void report_valid(bool passed, char const* s)
  114. {
  115. printf("%s: validity good ", passed ? "pass" : "FAIL");
  116. byte_array_print(s);
  117. printf(" (%s) ", s);
  118. }
  119. static void report_invalid(bool passed, char const* s)
  120. {
  121. printf("%s: validity bad ", passed ? "pass" : "FAIL");
  122. byte_array_print(s);
  123. printf(" ");
  124. }
  125. static bool is_valid(const char* s)
  126. {
  127. bool valid = cm_utf8_is_valid(s) != 0;
  128. if (!valid) {
  129. report_valid(false, s);
  130. printf("expected valid, reported as invalid\n");
  131. return false;
  132. }
  133. report_valid(true, s);
  134. printf("valid as expected\n");
  135. return true;
  136. }
  137. static bool is_invalid(const char* s)
  138. {
  139. bool valid = cm_utf8_is_valid(s) != 0;
  140. if (valid) {
  141. report_invalid(false, s);
  142. printf("expected invalid, reported as valid\n");
  143. return false;
  144. }
  145. report_invalid(true, s);
  146. printf("invalid as expected\n");
  147. return true;
  148. }
  149. int testUTF8(int /*unused*/, char* /*unused*/ [])
  150. {
  151. int result = 0;
  152. for (test_utf8_entry const* e = good_entry; e->n; ++e) {
  153. if (!decode_good(*e)) {
  154. result = 1;
  155. }
  156. if (!is_valid(e->str)) {
  157. result = 1;
  158. }
  159. }
  160. for (test_utf8_char const* c = bad_chars; (*c)[0]; ++c) {
  161. if (!decode_bad(*c)) {
  162. result = 1;
  163. }
  164. if (!is_invalid(*c)) {
  165. result = 1;
  166. }
  167. }
  168. for (char const** s = good_strings; *s; ++s) {
  169. if (!is_valid(*s)) {
  170. result = 1;
  171. }
  172. }
  173. for (char const** s = bad_strings; *s; ++s) {
  174. if (!is_invalid(*s)) {
  175. result = 1;
  176. }
  177. }
  178. return result;
  179. }