utf8.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright © 2014 Ran Benita <ran234@gmail.com>
  3. * SPDX-License-Identifier: MIT
  4. */
  5. #include "config.h"
  6. #include "test-config.h"
  7. #include <assert.h>
  8. #include <inttypes.h>
  9. #include <stdbool.h>
  10. #include <stddef.h>
  11. #include <string.h>
  12. #include "src/keysym.h"
  13. #include "test.h"
  14. #include "utf8.h"
  15. #include "utf8-decoding.h"
  16. #include "utils.h"
  17. #define VALID(lit) assert(is_valid_utf8(lit, sizeof(lit)-1))
  18. #define INVALID(lit) assert(!is_valid_utf8(lit, sizeof(lit)-1))
  19. static void
  20. test_is_valid_utf8(void)
  21. {
  22. /*
  23. * Mostly taken from:
  24. * https://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
  25. */
  26. VALID("ascii");
  27. VALID("\xCE\xBA\xE1\xBD\xB9\xCF\x83\xCE\xBC\xCE\xB5");
  28. VALID("");
  29. VALID("\x00");
  30. VALID("\x00\x00");
  31. VALID("\x50");
  32. VALID("\xC2\x80");
  33. VALID("\xE0\xA0\x80");
  34. VALID("\xF0\x90\x80\x80");
  35. /* 5/6-byte continuations aren't allowed (unlike UTF-8-test). */
  36. INVALID("\xF8\x88\x80\x80\x80");
  37. INVALID("\xFC\x84\x80\x80\x80\x80");
  38. VALID("\x7F");
  39. VALID("\xDF\xBF");
  40. VALID("\xEF\xBF\xBF");
  41. /* VALID("\xF7\xBF\xBF\xBF"); */
  42. /* 5/6-byte continuations aren't allowed (unlike UTF-8-test). */
  43. INVALID("\xFB\xBF\xBF\xBF\xBF");
  44. INVALID("\xFD\xBFxBF\xBF\xBF");
  45. VALID("\xED\x9F\xBF");
  46. VALID("\xEE\x80\x80");
  47. VALID("\xEF\xBF\xBD");
  48. VALID("\xF4\x8F\xBF\xBF");
  49. /* VALID("\xF4\x90\x80\x80"); */
  50. INVALID("\x80");
  51. INVALID("\xBF");
  52. INVALID("\x80\xBF");
  53. INVALID("\x80\xBF\x80");
  54. INVALID("\x80\xBF\x80\xBF");
  55. INVALID("\x80\xBF\x80\xBF\x80");
  56. INVALID("\x80\xBF\x80\xBF\x80\xBF");
  57. INVALID("\x80\xBF\x80\xBF\x80\xBF\x80");
  58. INVALID("\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F"
  59. "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F"
  60. "\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF"
  61. "\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF");
  62. INVALID("\xC0 \xC1 \xC2 \xC3 \xC4 \xC5 \xC6 \xC7 \xC8 \xC9 \xCA \xCB \xCC "
  63. "\xCD \xCE \xCF "
  64. "\xD0 \xD1 \xD2 \xD3 \xD4 \xD5 \xD6 \xD7 \xD8 \xD9 \xDA \xDB \xDD "
  65. "\xDD \xDE \xDF ");
  66. INVALID("\xF0 \xF1 \xF2 \xF3 \xF4 \xF5 \xF6 \xF7 ");
  67. INVALID("\xF8 \xF9 \xFA \xFB ");
  68. INVALID("\xFC \xFD ");
  69. INVALID("\xC0");
  70. INVALID("\xE0\x80");
  71. INVALID("\xF0\x80\x80");
  72. INVALID("\xF8\x80\x80\x80");
  73. INVALID("\xFC\x80\x80\x80\x80");
  74. INVALID("\xDF");
  75. INVALID("\xEF\xBF");
  76. INVALID("\xF7\xBF\xBF");
  77. INVALID("\xFB\xBF\xBF\xBF");
  78. INVALID("\xFD\xBF\xBF\xBF\xBF");
  79. INVALID("\xC0\xE0\x80\xF0\x80\x80\xF8\x80\x80\x80\xFC\x80\x80\x80\x80"
  80. "\xDF\xEF\xBF\xF7\xBF\xBF\xFB\xBF\xBF\xBF\xFD\xBF\xBF\xBF\xBF");
  81. INVALID("\xFE");
  82. INVALID("\xFF");
  83. INVALID("\xFE\xFE\xFF\xFF");
  84. INVALID("\xC0\xAF");
  85. INVALID("\xE0\x80\xAF");
  86. INVALID("\xF0\x80\x80\xAF");
  87. INVALID("\xF8\x80\x80\x80\xAF");
  88. INVALID("\xFC\x80\x80\x80\x80\xAF");
  89. INVALID("\xC1\xBF");
  90. INVALID("\xE0\x9F\xBF");
  91. INVALID("\xF0\x8F\xBF\xBF");
  92. INVALID("\xF8\x87\xBF\xBF\xBF");
  93. INVALID("\xFC\x83\xBF\xBF\xBF\xBF");
  94. INVALID("\xC0\x80");
  95. INVALID("\xE0\x80\x80");
  96. INVALID("\xF0\x80\x80\x80");
  97. INVALID("\xF8\x80\x80\x80\x80");
  98. INVALID("\xFC\x80\x80\x80\x80\x80");
  99. INVALID("\xED\xA0\x80");
  100. INVALID("\xED\xAD\xBF");
  101. INVALID("\xED\xAE\x80");
  102. INVALID("\xED\xAF\xBF");
  103. INVALID("\xED\xB0\x80");
  104. INVALID("\xED\xBE\x80");
  105. INVALID("\xED\xBF\xBF");
  106. INVALID("\xED\xA0\x80\xED\xB0\x80");
  107. INVALID("\xED\xA0\x80\xED\xBF\xBF");
  108. INVALID("\xED\xAD\xBF\xED\xB0\x80");
  109. INVALID("\xED\xAD\xBF\xED\xBF\xBF");
  110. INVALID("\xED\xAE\x80\xED\xB0\x80");
  111. INVALID("\xED\xAE\x80\xED\xBF\xBF");
  112. INVALID("\xED\xAF\xBF\xED\xB0\x80");
  113. INVALID("\xED\xAF\xBF\xED\xBF\xBF");
  114. /* INVALID("\xEF\xBF\xBE"); */
  115. /* INVALID("\xEF\xBF\xBF"); */
  116. }
  117. static void
  118. check_utf32_to_utf8(uint32_t unichar, int expected_length, const char *expected) {
  119. char buffer[XKB_KEYSYM_UTF8_MAX_SIZE] = {0};
  120. const uint8_t length = utf32_to_utf8(unichar, buffer);
  121. assert(length == expected_length);
  122. assert(streq(buffer, expected));
  123. }
  124. static void
  125. test_utf32_to_utf8(void)
  126. {
  127. check_utf32_to_utf8(0x0, 2, "");
  128. check_utf32_to_utf8(0x40, 2, "\x40");
  129. check_utf32_to_utf8(0xA1, 3, "\xc2\xa1");
  130. check_utf32_to_utf8(0x2701, 4, "\xe2\x9c\x81");
  131. check_utf32_to_utf8(0xd800, 0, ""); // Unicode surrogates
  132. check_utf32_to_utf8(0xdfff, 0, ""); // Unicode surrogates
  133. check_utf32_to_utf8(0x1f004, 5, "\xf0\x9f\x80\x84");
  134. check_utf32_to_utf8(0x110000, 0, "");
  135. check_utf32_to_utf8(0xffffffff, 0, "");
  136. }
  137. static void
  138. /* Check roundtrip UTF-32 → UTF-8 → UTF-32 */
  139. test_utf8_to_utf32(void)
  140. {
  141. char buffer[XKB_KEYSYM_UTF8_MAX_SIZE];
  142. for (uint32_t cp = 0; cp < 0x10ffff; cp++) {
  143. const int8_t length = (int8_t)((int)utf32_to_utf8(cp, buffer) - 1);
  144. /* Check surrogates */
  145. if (is_surrogate(cp)) {
  146. assert(length == -1);
  147. } else {
  148. assert(length > 0);
  149. size_t length2 = 0;
  150. uint32_t cp2 = utf8_next_code_point(buffer, (size_t)length, &length2);
  151. assert(cp2 != INVALID_UTF8_CODE_POINT && cp2 == cp &&
  152. length2 == (size_t)length);
  153. }
  154. }
  155. }
  156. int
  157. main(void)
  158. {
  159. test_init();
  160. test_is_valid_utf8();
  161. test_utf32_to_utf8();
  162. test_utf8_to_utf32();
  163. return 0;
  164. }