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.9KB

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