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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 <wchar.h>
  23. #include <rfb/util.h>
  24. struct _ucs4utf8 {
  25. unsigned ucs4;
  26. const char *utf8;
  27. };
  28. struct _ucs4utf16 {
  29. unsigned ucs4;
  30. const wchar_t *utf16;
  31. };
  32. struct _latin1utf8 {
  33. const char *latin1;
  34. const char *utf8;
  35. };
  36. struct _utf8utf16 {
  37. const char *utf8;
  38. const wchar_t *utf16;
  39. };
  40. struct _ucs4utf8 ucs4utf8[] = {
  41. { 0x0061, "a" },
  42. { 0x00f6, "\xc3\xb6" },
  43. { 0x263a, "\xe2\x98\xba" },
  44. { 0x1f638, "\xf0\x9f\x98\xb8" },
  45. { 0x2d006, "\xf0\xad\x80\x86" },
  46. { 0xfffd, "\xe5\xe4" },
  47. { 0x110200, "\xef\xbf\xbd" },
  48. };
  49. struct _ucs4utf16 ucs4utf16[] = {
  50. { 0x0061, L"a" },
  51. { 0x00f6, L"\xf6" },
  52. { 0x263a, L"\x263a" },
  53. { 0x1f638, L"\xd83d\xde38" },
  54. { 0x2d006, L"\xd874\xdc06" },
  55. { 0xfffd, L"\xdc40\xdc12" },
  56. { 0x110200, L"\xfffd" },
  57. { 0xd87f, L"\xfffd" },
  58. };
  59. struct _latin1utf8 latin1utf8[] = {
  60. { "abc", "abc" },
  61. { "\xe5\xe4\xf6", "\xc3\xa5\xc3\xa4\xc3\xb6" },
  62. { "???", "\xe2\x98\xb9\xe2\x98\xba\xe2\x98\xbb" },
  63. { "?", "\xe5\xe4" },
  64. };
  65. struct _utf8utf16 utf8utf16[] = {
  66. { "abc", L"abc" },
  67. { "\xc3\xa5\xc3\xa4\xc3\xb6", L"\xe5\xe4\xf6" },
  68. { "\xe2\x98\xb9\xe2\x98\xba\xe2\x98\xbb", L"\x2639\x263a\x263b" },
  69. { "\xf0\x9f\x98\xb8\xf0\x9f\x99\x81\xf0\x9f\x99\x82", L"\xd83d\xde38\xd83d\xde41\xd83d\xde42" },
  70. { "\xf0\xad\x80\x86\xf0\xad\x80\x88", L"\xd874\xdc06\xd874\xdc08" },
  71. { "\xef\xbf\xbd\xc3\xa5", L"\xd840\xe5" },
  72. { "\xed\xa1\xbf", L"\xfffd" },
  73. };
  74. #define ARRAY_SIZE(a) (sizeof(a)/sizeof(*a))
  75. int main(int /*argc*/, char** /*argv*/)
  76. {
  77. int failures;
  78. size_t i;
  79. unsigned ucs4;
  80. char utf8[5];
  81. wchar_t utf16[3];
  82. std::string out;
  83. std::wstring wout;
  84. size_t len;
  85. failures = 0;
  86. for (i = 0;i < ARRAY_SIZE(ucs4utf8);i++) {
  87. /* Expected failure? */
  88. if (ucs4utf8[i].ucs4 == 0xfffd)
  89. continue;
  90. len = rfb::ucs4ToUTF8(ucs4utf8[i].ucs4, utf8);
  91. if ((len != strlen(utf8)) ||
  92. (strcmp(utf8, ucs4utf8[i].utf8) != 0)) {
  93. printf("FAILED: ucs4ToUTF8() #%d\n", (int)i+1);
  94. failures++;
  95. }
  96. }
  97. for (i = 0;i < ARRAY_SIZE(ucs4utf8);i++) {
  98. /* Expected failure? */
  99. if (strcmp(ucs4utf8[i].utf8, "\xef\xbf\xbd") == 0)
  100. continue;
  101. len = rfb::utf8ToUCS4(ucs4utf8[i].utf8, strlen(ucs4utf8[i].utf8), &ucs4);
  102. if ((len != strlen(ucs4utf8[i].utf8)) ||
  103. (ucs4 != ucs4utf8[i].ucs4)) {
  104. printf("FAILED: utf8ToUCS4() #%d\n", (int)i+1);
  105. failures++;
  106. }
  107. }
  108. for (i = 0;i < ARRAY_SIZE(ucs4utf16);i++) {
  109. /* Expected failure? */
  110. if (ucs4utf16[i].ucs4 == 0xfffd)
  111. continue;
  112. len = rfb::ucs4ToUTF16(ucs4utf16[i].ucs4, utf16);
  113. if ((len != wcslen(utf16)) ||
  114. (wcscmp(utf16, ucs4utf16[i].utf16) != 0)) {
  115. printf("FAILED: ucs4ToUTF16() #%d\n", (int)i+1);
  116. failures++;
  117. }
  118. }
  119. for (i = 0;i < ARRAY_SIZE(ucs4utf16);i++) {
  120. /* Expected failure? */
  121. if (wcscmp(ucs4utf16[i].utf16, L"\xfffd") == 0)
  122. continue;
  123. len = rfb::utf16ToUCS4(ucs4utf16[i].utf16, wcslen(ucs4utf16[i].utf16), &ucs4);
  124. if ((len != wcslen(ucs4utf16[i].utf16)) ||
  125. (ucs4 != ucs4utf16[i].ucs4)) {
  126. printf("FAILED: utf16ToUCS4() #%d\n", (int)i+1);
  127. failures++;
  128. }
  129. }
  130. for (i = 0;i < ARRAY_SIZE(latin1utf8);i++) {
  131. /* Expected failure? */
  132. if (strchr(latin1utf8[i].latin1, '?') != NULL)
  133. continue;
  134. out = rfb::latin1ToUTF8(latin1utf8[i].latin1);
  135. if (out != latin1utf8[i].utf8) {
  136. printf("FAILED: latin1ToUTF8() #%d\n", (int)i+1);
  137. failures++;
  138. }
  139. }
  140. for (i = 0;i < ARRAY_SIZE(latin1utf8);i++) {
  141. out = rfb::utf8ToLatin1(latin1utf8[i].utf8);
  142. if (out != latin1utf8[i].latin1) {
  143. printf("FAILED: utf8ToLatin1() #%d\n", (int)i+1);
  144. failures++;
  145. }
  146. }
  147. for (i = 0;i < ARRAY_SIZE(utf8utf16);i++) {
  148. /* Expected failure? */
  149. if (wcscmp(utf8utf16[i].utf16, L"\xfffd") == 0)
  150. continue;
  151. out = rfb::utf16ToUTF8(utf8utf16[i].utf16);
  152. if (out != utf8utf16[i].utf8) {
  153. printf("FAILED: utf16ToUTF8() #%d\n", (int)i+1);
  154. failures++;
  155. }
  156. }
  157. for (i = 0;i < ARRAY_SIZE(utf8utf16);i++) {
  158. /* Expected failure? */
  159. if (strstr(utf8utf16[i].utf8, "\xef\xbf\xbd") != NULL)
  160. continue;
  161. wout = rfb::utf8ToUTF16(utf8utf16[i].utf8);
  162. if (wout != utf8utf16[i].utf16) {
  163. printf("FAILED: utf8ToUTF16() #%d\n", (int)i+1);
  164. failures++;
  165. }
  166. }
  167. if (failures == 0) {
  168. printf("OK\n");
  169. } else {
  170. printf("FAIL: %d failures\n", failures);
  171. }
  172. return 0;
  173. }