PageRenderTime 21ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/tests/encodings.c

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C | 223 lines | 155 code | 38 blank | 30 comment | 49 complexity | 819ee37f90c38a15a7b6ea4ec60f7bd4 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /*
  2. encodings.c - test handling different character encodings
  3. Copyright (C) 2008 siliconforks.com
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program 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. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include <config.h>
  17. #include <assert.h>
  18. #include <string.h>
  19. #include "encoding.h"
  20. #include "stream.h"
  21. int main(void) {
  22. jschar * characters;
  23. size_t num_characters;
  24. int result;
  25. /* e, e grave, e acute, e circumflex */
  26. uint8_t utf8[] = {
  27. 'e',
  28. 0xc3,
  29. 0xa8,
  30. 0xc3,
  31. 0xa9,
  32. 0xc3,
  33. 0xaa,
  34. };
  35. result = jscoverage_bytes_to_characters("UTF-8", utf8, 7, &characters, &num_characters);
  36. #if HAVE_ICONV || HAVE_MULTIBYTETOWIDECHAR
  37. assert(result == 0);
  38. assert(num_characters == 4);
  39. assert(characters[0] == 'e');
  40. assert(characters[1] == 0xe8);
  41. assert(characters[2] == 0xe9);
  42. assert(characters[3] == 0xea);
  43. free(characters);
  44. #else
  45. assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED);
  46. #endif
  47. /*
  48. BOM is 0xfeff
  49. = 1111 1110 1111 1111
  50. UTF: 1110---- 10------ 10------
  51. = 11101111 10111011 10111111
  52. = EF BB BF
  53. */
  54. uint8_t utf8_with_bom[] = {
  55. 0xef,
  56. 0xbb,
  57. 0xbf,
  58. 'e',
  59. 0xc3,
  60. 0xa8,
  61. 0xc3,
  62. 0xa9,
  63. 0xc3,
  64. 0xaa,
  65. };
  66. result = jscoverage_bytes_to_characters("UTF-8", utf8_with_bom, 10, &characters, &num_characters);
  67. #if HAVE_ICONV || HAVE_MULTIBYTETOWIDECHAR
  68. assert(result == 0);
  69. assert(num_characters == 4);
  70. assert(characters[0] == 'e');
  71. assert(characters[1] == 0xe8);
  72. assert(characters[2] == 0xe9);
  73. assert(characters[3] == 0xea);
  74. free(characters);
  75. #else
  76. assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED);
  77. #endif
  78. uint8_t utf16be[] = {
  79. 0, 'e',
  80. 0, 0xe8,
  81. 0, 0xe9,
  82. 0, 0xea,
  83. };
  84. result = jscoverage_bytes_to_characters("UTF-16BE", utf16be, 8, &characters, &num_characters);
  85. #ifdef HAVE_ICONV
  86. assert(result == 0);
  87. assert(num_characters == 4);
  88. assert(characters[0] == 'e');
  89. assert(characters[1] == 0xe8);
  90. assert(characters[2] == 0xe9);
  91. assert(characters[3] == 0xea);
  92. free(characters);
  93. #else
  94. assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED);
  95. #endif
  96. uint8_t utf16be_with_bom[] = {
  97. 0xfe, 0xff,
  98. 0, 'e',
  99. 0, 0xe8,
  100. 0, 0xe9,
  101. 0, 0xea,
  102. };
  103. result = jscoverage_bytes_to_characters("UTF-16BE", utf16be_with_bom, 10, &characters, &num_characters);
  104. #ifdef HAVE_ICONV
  105. assert(result == 0);
  106. assert(num_characters == 4);
  107. assert(characters[0] == 'e');
  108. assert(characters[1] == 0xe8);
  109. assert(characters[2] == 0xe9);
  110. assert(characters[3] == 0xea);
  111. free(characters);
  112. #else
  113. assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED);
  114. #endif
  115. uint8_t utf16le[] = {
  116. 'e', 0,
  117. 0xe8, 0,
  118. 0xe9, 0,
  119. 0xea, 0,
  120. };
  121. result = jscoverage_bytes_to_characters("UTF-16LE", utf16le, 8, &characters, &num_characters);
  122. #ifdef HAVE_ICONV
  123. assert(result == 0);
  124. assert(num_characters == 4);
  125. assert(characters[0] == 'e');
  126. assert(characters[1] == 0xe8);
  127. assert(characters[2] == 0xe9);
  128. assert(characters[3] == 0xea);
  129. free(characters);
  130. #else
  131. assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED);
  132. #endif
  133. uint8_t utf16le_with_bom[] = {
  134. 0xff, 0xfe,
  135. 'e', 0,
  136. 0xe8, 0,
  137. 0xe9, 0,
  138. 0xea, 0,
  139. };
  140. result = jscoverage_bytes_to_characters("UTF-16LE", utf16le_with_bom, 10, &characters, &num_characters);
  141. #ifdef HAVE_ICONV
  142. assert(result == 0);
  143. assert(num_characters == 4);
  144. assert(characters[0] == 'e');
  145. assert(characters[1] == 0xe8);
  146. assert(characters[2] == 0xe9);
  147. assert(characters[3] == 0xea);
  148. free(characters);
  149. #else
  150. assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED);
  151. #endif
  152. /* bogus encoding */
  153. uint8_t bogus[] = {'b', 'o', 'g', 'u', 's'};
  154. result = jscoverage_bytes_to_characters("BOGUS", bogus, 5, &characters, &num_characters);
  155. assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED);
  156. #ifdef HAVE_ICONV
  157. /* malformed US-ASCII */
  158. /* NOTE: Windows simply discards the high bit */
  159. uint8_t malformed_ascii[] = {
  160. 'e',
  161. 0xe8,
  162. 0xe9,
  163. 0xea,
  164. };
  165. result = jscoverage_bytes_to_characters("US-ASCII", malformed_ascii, 4, &characters, &num_characters);
  166. assert(result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE);
  167. #endif
  168. /* malformed UTF-8 */
  169. uint8_t malformed_utf8[] = {
  170. 'e',
  171. 0xe8,
  172. 0xe9,
  173. 0xea,
  174. };
  175. result = jscoverage_bytes_to_characters("UTF-8", malformed_utf8, 4, &characters, &num_characters);
  176. #if HAVE_ICONV || HAVE_MULTIBYTETOWIDECHAR
  177. assert(result == JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE);
  178. #else
  179. assert(result == JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED);
  180. #endif
  181. return 0;
  182. }