You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

unicode.cxx 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* Copyright 2020 Pierre Ossman <ossman@cendio.se> for Cendio AB
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <wchar.h>
  24. #include <rfb/util.h>
  25. struct _ucs4utf8 {
  26. unsigned ucs4;
  27. const char *utf8;
  28. };
  29. struct _ucs4utf16 {
  30. unsigned ucs4;
  31. const wchar_t *utf16;
  32. };
  33. struct _latin1utf8 {
  34. const char *latin1;
  35. const char *utf8;
  36. };
  37. struct _utf8utf16 {
  38. const char *utf8;
  39. const wchar_t *utf16;
  40. };
  41. struct _ucs4utf8 ucs4utf8[] = {
  42. { 0x0061, "a" },
  43. { 0x00f6, "\xc3\xb6" },
  44. { 0x263a, "\xe2\x98\xba" },
  45. { 0x1f638, "\xf0\x9f\x98\xb8" },
  46. { 0x2d006, "\xf0\xad\x80\x86" },
  47. { 0xfffd, "\xe5\xe4" },
  48. { 0xfffd, "\xed\xa2\x80" },
  49. { 0xfffd, "\xed\xbb\xbf" },
  50. { 0xd880, "\xef\xbf\xbd" },
  51. { 0xdeff, "\xef\xbf\xbd" },
  52. { 0x110200, "\xef\xbf\xbd" },
  53. };
  54. struct _ucs4utf16 ucs4utf16[] = {
  55. { 0x0061, L"a" },
  56. { 0x00f6, L"\xf6" },
  57. { 0x263a, L"\x263a" },
  58. { 0x1f638, L"\xd83d\xde38" },
  59. { 0x2d006, L"\xd874\xdc06" },
  60. { 0xfffd, L"\xdc40\xdc12" },
  61. { 0x110200, L"\xfffd" },
  62. { 0xd87f, L"\xfffd" },
  63. };
  64. struct _latin1utf8 latin1utf8[] = {
  65. { "abc", "abc" },
  66. { "\xe5\xe4\xf6", "\xc3\xa5\xc3\xa4\xc3\xb6" },
  67. { "???", "\xe2\x98\xb9\xe2\x98\xba\xe2\x98\xbb" },
  68. { "?", "\xe5\xe4" },
  69. };
  70. struct _utf8utf16 utf8utf16[] = {
  71. { "abc", L"abc" },
  72. { "\xc3\xa5\xc3\xa4\xc3\xb6", L"\xe5\xe4\xf6" },
  73. { "\xe2\x98\xb9\xe2\x98\xba\xe2\x98\xbb", L"\x2639\x263a\x263b" },
  74. { "\xf0\x9f\x98\xb8\xf0\x9f\x99\x81\xf0\x9f\x99\x82", L"\xd83d\xde38\xd83d\xde41\xd83d\xde42" },
  75. { "\xf0\xad\x80\x86\xf0\xad\x80\x88", L"\xd874\xdc06\xd874\xdc08" },
  76. { "\xef\xbf\xbd\xc3\xa5", L"\xd840\xe5" },
  77. { "\xed\xa1\xbf", L"\xfffd" },
  78. };
  79. const char *validutf8[] = {
  80. "abc",
  81. "\xc3\xa5\xc3\xa4\xc3\xb6",
  82. "\xf0\xad\x80\x86",
  83. };
  84. const char *invalidutf8[] = {
  85. "\xe5\xe4\xf6",
  86. "\xf8\xa1\xa1\xa1\xa1",
  87. "\xed\xa2\x80",
  88. };
  89. const wchar_t *validutf16[] = {
  90. L"abc",
  91. L"\xe5\xe4\xf6",
  92. L"\xd83d\xde38\xd83d\xde41\xd83d\xde42",
  93. };
  94. const wchar_t *invalidutf16[] = {
  95. L"\xdc40\xdc12",
  96. };
  97. #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*a))
  98. int main(int /*argc*/, char** /*argv*/)
  99. {
  100. int failures;
  101. size_t i;
  102. unsigned ucs4;
  103. char utf8[5];
  104. wchar_t utf16[3];
  105. std::string out;
  106. std::wstring wout;
  107. size_t len;
  108. failures = 0;
  109. for (i = 0;i < ARRAY_SIZE(ucs4utf8);i++) {
  110. /* Expected failure? */
  111. if (ucs4utf8[i].ucs4 == 0xfffd)
  112. continue;
  113. len = rfb::ucs4ToUTF8(ucs4utf8[i].ucs4, utf8);
  114. if ((len != strlen(utf8)) ||
  115. (strcmp(utf8, ucs4utf8[i].utf8) != 0)) {
  116. printf("FAILED: ucs4ToUTF8() #%d\n", (int)i+1);
  117. failures++;
  118. }
  119. }
  120. for (i = 0;i < ARRAY_SIZE(ucs4utf8);i++) {
  121. /* Expected failure? */
  122. if (strcmp(ucs4utf8[i].utf8, "\xef\xbf\xbd") == 0)
  123. continue;
  124. len = rfb::utf8ToUCS4(ucs4utf8[i].utf8, strlen(ucs4utf8[i].utf8), &ucs4);
  125. if ((len != strlen(ucs4utf8[i].utf8)) ||
  126. (ucs4 != ucs4utf8[i].ucs4)) {
  127. printf("FAILED: utf8ToUCS4() #%d\n", (int)i+1);
  128. failures++;
  129. }
  130. }
  131. for (i = 0;i < ARRAY_SIZE(ucs4utf16);i++) {
  132. /* Expected failure? */
  133. if (ucs4utf16[i].ucs4 == 0xfffd)
  134. continue;
  135. len = rfb::ucs4ToUTF16(ucs4utf16[i].ucs4, utf16);
  136. if ((len != wcslen(utf16)) ||
  137. (wcscmp(utf16, ucs4utf16[i].utf16) != 0)) {
  138. printf("FAILED: ucs4ToUTF16() #%d\n", (int)i+1);
  139. failures++;
  140. }
  141. }
  142. for (i = 0;i < ARRAY_SIZE(ucs4utf16);i++) {
  143. /* Expected failure? */
  144. if (wcscmp(ucs4utf16[i].utf16, L"\xfffd") == 0)
  145. continue;
  146. len = rfb::utf16ToUCS4(ucs4utf16[i].utf16, wcslen(ucs4utf16[i].utf16), &ucs4);
  147. if ((len != wcslen(ucs4utf16[i].utf16)) ||
  148. (ucs4 != ucs4utf16[i].ucs4)) {
  149. printf("FAILED: utf16ToUCS4() #%d\n", (int)i+1);
  150. failures++;
  151. }
  152. }
  153. for (i = 0;i < ARRAY_SIZE(latin1utf8);i++) {
  154. /* Expected failure? */
  155. if (strchr(latin1utf8[i].latin1, '?') != NULL)
  156. continue;
  157. out = rfb::latin1ToUTF8(latin1utf8[i].latin1);
  158. if (out != latin1utf8[i].utf8) {
  159. printf("FAILED: latin1ToUTF8() #%d\n", (int)i+1);
  160. failures++;
  161. }
  162. }
  163. for (i = 0;i < ARRAY_SIZE(latin1utf8);i++) {
  164. out = rfb::utf8ToLatin1(latin1utf8[i].utf8);
  165. if (out != latin1utf8[i].latin1) {
  166. printf("FAILED: utf8ToLatin1() #%d\n", (int)i+1);
  167. failures++;
  168. }
  169. }
  170. for (i = 0;i < ARRAY_SIZE(utf8utf16);i++) {
  171. /* Expected failure? */
  172. if (wcscmp(utf8utf16[i].utf16, L"\xfffd") == 0)
  173. continue;
  174. out = rfb::utf16ToUTF8(utf8utf16[i].utf16);
  175. if (out != utf8utf16[i].utf8) {
  176. printf("FAILED: utf16ToUTF8() #%d\n", (int)i+1);
  177. failures++;
  178. }
  179. }
  180. for (i = 0;i < ARRAY_SIZE(utf8utf16);i++) {
  181. /* Expected failure? */
  182. if (strstr(utf8utf16[i].utf8, "\xef\xbf\xbd") != NULL)
  183. continue;
  184. wout = rfb::utf8ToUTF16(utf8utf16[i].utf8);
  185. if (wout != utf8utf16[i].utf16) {
  186. printf("FAILED: utf8ToUTF16() #%d\n", (int)i+1);
  187. failures++;
  188. }
  189. }
  190. for (i = 0;i < ARRAY_SIZE(validutf8);i++) {
  191. if (!rfb::isValidUTF8(validutf8[i])) {
  192. printf("FAILED: isValidUTF8() #%d\n", (int)i+1);
  193. failures++;
  194. }
  195. }
  196. for (i = 0;i < ARRAY_SIZE(invalidutf8);i++) {
  197. if (rfb::isValidUTF8(invalidutf8[i])) {
  198. printf("FAILED: ! isValidUTF8() #%d\n", (int)i+1);
  199. failures++;
  200. }
  201. }
  202. for (i = 0;i < ARRAY_SIZE(validutf16);i++) {
  203. if (!rfb::isValidUTF16(validutf16[i])) {
  204. printf("FAILED: isValidUTF16() #%d\n", (int)i+1);
  205. failures++;
  206. }
  207. }
  208. for (i = 0;i < ARRAY_SIZE(invalidutf16);i++) {
  209. if (rfb::isValidUTF16(invalidutf16[i])) {
  210. printf("FAILED: ! isValidUTF16() #%d\n", (int)i+1);
  211. failures++;
  212. }
  213. }
  214. if (failures == 0) {
  215. printf("OK\n");
  216. } else {
  217. printf("FAIL: %d failures\n", failures);
  218. }
  219. return 0;
  220. }