test_TextEncoder.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * test_TextEncoder.js
  3. * bug 764234 tests
  4. */
  5. function runTextEncoderTests()
  6. {
  7. test(testEncoderEncode, "testEncoderEncode");
  8. test(testEncoderGetEncoding, "testEncoderGetEncoding");
  9. test(testInvalidSequence, "testInvalidSequence");
  10. test(testInputString, "testInputString");
  11. test(testStreamingOptions, "testStreamingOptions");
  12. }
  13. function testEncoderEncode()
  14. {
  15. var data = "\u00a0\u0e01\u0e02\u0e03\u0e04\u0e05\u0e06\u0e07\u0e08\u0e09"
  16. + "\u0e0a\u0e0b\u0e0c\u0e0d\u0e0e\u0e0f\u0e10\u0e11\u0e12\u0e13\u0e14"
  17. + "\u0e15\u0e16\u0e17\u0e18\u0e19\u0e1a\u0e1b\u0e1c\u0e1d\u0e1e\u0e1f"
  18. + "\u0e20\u0e21\u0e22\u0e23\u0e24\u0e25\u0e26\u0e27\u0e28\u0e29\u0e2a"
  19. + "\u0e2b\u0e2c\u0e2d\u0e2e\u0e2f\u0e30\u0e31\u0e32\u0e33\u0e34\u0e35"
  20. + "\u0e36\u0e37\u0e38\u0e39\u0e3a\u0e3f\u0e40\u0e41\u0e42\u0e43\u0e44"
  21. + "\u0e45\u0e46\u0e47\u0e48\u0e49\u0e4a\u0e4b\u0e4c\u0e4d\u0e4e\u0e4f"
  22. + "\u0e50\u0e51\u0e52\u0e53\u0e54\u0e55\u0e56\u0e57\u0e58\u0e59\u0e5a"
  23. + "\u0e5b";
  24. var expectedString = [0xC2, 0xA0, 0xE0, 0xB8, 0x81, 0xE0, 0xB8, 0x82, 0xE0,
  25. 0xB8, 0x83, 0xE0,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xE0, 0xB8, 0xBA, 0xE0, 0xB8, 0xBF, 0xE0,
  44. 0xB9, 0x80, 0xE0, 0xB9, 0x81, 0xE0, 0xB9, 0x82, 0xE0,
  45. 0xB9, 0x83, 0xE0, 0xB9, 0x84, 0xE0, 0xB9, 0x85, 0xE0,
  46. 0xB9, 0x86, 0xE0, 0xB9, 0x87, 0xE0, 0xB9, 0x88, 0xE0,
  47. 0xB9, 0x89, 0xE0, 0xB9, 0x8A, 0xE0, 0xB9, 0x8B, 0xE0,
  48. 0xB9, 0x8C, 0xE0, 0xB9, 0x8D, 0xE0, 0xB9, 0x8E, 0xE0,
  49. 0xB9, 0x8F, 0xE0, 0xB9, 0x90, 0xE0, 0xB9, 0x91, 0xE0,
  50. 0xB9, 0x92, 0xE0, 0xB9, 0x93, 0xE0, 0xB9, 0x94, 0xE0,
  51. 0xB9, 0x95, 0xE0, 0xB9, 0x96, 0xE0, 0xB9, 0x97, 0xE0,
  52. 0xB9, 0x98, 0xE0, 0xB9, 0x99, 0xE0, 0xB9, 0x9A, 0xE0,
  53. 0xB9, 0x9B];
  54. // valid encoding passed
  55. testSingleString({input: data, expected: expectedString,
  56. msg: "testing encoding with valid utf-8 encoding."});
  57. }
  58. function testInvalidSequence()
  59. {
  60. var data = "\u0e43\u0e44\ufffd\u0e45";
  61. var expectedString = [0xE0, 0xB9, 0x83, 0xE0, 0xB9, 0x84, 0xEF, 0xBF, 0xBD,
  62. 0xE0, 0xB9, 0x85];
  63. //Test null input string
  64. testSingleString({input: data, expected: expectedString,
  65. msg: "encoder with replacement character test."});
  66. }
  67. function testInputString()
  68. {
  69. //Test null input string
  70. testSingleString({input: "", expected: [],
  71. msg: "encoder null input string test."});
  72. //Test spaces as input string
  73. testSingleString({input: " ", expected: [32, 32],
  74. msg: "spaces as input string."});
  75. }
  76. function testSingleString(test)
  77. {
  78. var outText;
  79. try {
  80. var stream = test.stream ? {stream: true} : null;
  81. outText = (new TextEncoder()).encode(test.input, stream);
  82. } catch (e) {
  83. assert_equals(e.name, test.error, test.msg + " error thrown from the constructor.");
  84. if (test.errorMessage) {
  85. assert_equals(e.message, test.errorMessage, test.msg + " error thrown from the constructor.");
  86. }
  87. return;
  88. }
  89. assert_true(!test.error, test.msg);
  90. if (outText.length !== test.expected.length) {
  91. assert_equals(outText.length, test.expected.length, test.msg + " length mismatch");
  92. return;
  93. }
  94. for (var i = 0; i < outText.length; i++) {
  95. if (outText[i] != test.expected[i]) {
  96. assert_equals(escape(stringFromArray(outText.buffer)), escape(stringFromArray(test.expected)),
  97. test.msg + " Bytes do not match expected bytes.");
  98. return;
  99. }
  100. }
  101. }
  102. function stringFromArray(a) {
  103. return Array.map.call(a, function(v){return String.fromCharCode(v)}).join('');
  104. }
  105. function testStreamingOptions()
  106. {
  107. var data = [
  108. "\u00a0\u0e01\u0e02\u0e03\u0e04\u0e05\u0e06\u0e07\u0e08\u0e09\u0e0a"
  109. + "\u0e0b\u0e0c\u0e0d\u0e0e\u0e0f\u0e10\u0e11\u0e12\u0e13\u0e14"
  110. + "\u0e15\u0e16\u0e17\u0e18\u0e19\u0e1a\u0e1b\u0e1c\u0e1d",
  111. "\u0e1e\u0e1f\u0e20\u0e21\u0e22\u0e23\u0e24\u0e25\u0e26\u0e27\u0e28"
  112. + "\u0e29\u0e2a\u0e2b\u0e2c\u0e2d\u0e2e\u0e2f\u0e30\u0e31\u0e32"
  113. + "\u0e33\u0e34\u0e35\u0e36\u0e37\u0e38\u0e39\u0e3a\u0e3f\u0e40"
  114. + "\u0e41\u0e42",
  115. "\u0e43\u0e44\u0e45\u0e46\u0e47\u0e48\u0e49\u0e4a\u0e4b\u0e4c\u0e4d"
  116. + "\u0e4e\u0e4f\u0e50\u0e51\u0e52\u0e53\u0e54\u0e55\u0e56\u0e57"
  117. + "\u0e58\u0e59\u0e5a\u0e5b"
  118. ];
  119. var expected = [[0xC2, 0xA0, 0xE0, 0xB8, 0x81, 0xE0, 0xB8, 0x82, 0xE0,
  120. 0xB8, 0x83, 0xE0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0xE0, 0xB8, 0xBA, 0xE0, 0xB8, 0xBF,
  139. 0xE0, 0xB9, 0x80, 0xE0, 0xB9, 0x81, 0xE0, 0xB9, 0x82],
  140. [0xE0, 0xB9, 0x83, 0xE0, 0xB9, 0x84, 0xE0, 0xB9, 0x85,
  141. 0xE0, 0xB9, 0x86, 0xE0, 0xB9, 0x87, 0xE0, 0xB9, 0x88,
  142. 0xE0, 0xB9, 0x89, 0xE0, 0xB9, 0x8A, 0xE0, 0xB9, 0x8B,
  143. 0xE0, 0xB9, 0x8C, 0xE0, 0xB9, 0x8D, 0xE0, 0xB9, 0x8E,
  144. 0xE0, 0xB9, 0x8F, 0xE0, 0xB9, 0x90, 0xE0, 0xB9, 0x91,
  145. 0xE0, 0xB9, 0x92, 0xE0, 0xB9, 0x93, 0xE0, 0xB9, 0x94,
  146. 0xE0, 0xB9, 0x95, 0xE0, 0xB9, 0x96, 0xE0, 0xB9, 0x97,
  147. 0xE0, 0xB9, 0x98, 0xE0, 0xB9, 0x99, 0xE0, 0xB9, 0x9A,
  148. 0xE0, 0xB9, 0x9B]];
  149. // STREAMING TEST ONE: test streaming three valid strings with stream option
  150. // set to true for all three.
  151. testArrayOfStrings({array: [
  152. {input: data[0], stream: true, expected: expected[0]},
  153. {input: data[1], stream: true, expected: expected[1]},
  154. {input: data[2], stream: true, expected: expected[2]},
  155. ], msg: "streaming test one."});
  156. // STREAMING TEST TWO: test streaming valid strings with stream option
  157. // streaming option: false from constructor, string 1 stream: true,
  158. // string 2 stream: false, string 3 stream: false
  159. testArrayOfStrings({array: [
  160. {input: data[0], stream: true, expected: expected[0]},
  161. {input: data[1], expected: expected[1]},
  162. {input: data[2], expected: expected[2]},
  163. ], msg: "streaming test two."});
  164. }
  165. function arrayFromString(s) {
  166. return s.split('').map(function(c){return String.charCodeAt(c)});
  167. }
  168. function testArrayOfStrings(test)
  169. {
  170. var encoder;
  171. try {
  172. encoder = new TextEncoder();
  173. } catch (e) {
  174. assert_equals(e.name, test.error, test.msg);
  175. return;
  176. }
  177. assert_true(!test.error, test.msg);
  178. var array = test.array;
  179. for (var i = 0; i < array.length; i += 1) {
  180. var stream = array[i].stream ? {stream: true} : null;
  181. var view = encoder.encode(array[i].input, stream);
  182. var stringLen = view.length;
  183. var expected = array[i].expected;
  184. for (var j = 0; j < stringLen; j++) {
  185. if (view[j] !== expected[j]) {
  186. assert_equals(view[j], expected[j], msg + " Bytes do not match expected bytes.");
  187. return;
  188. }
  189. }
  190. }
  191. }
  192. function testEncoderGetEncoding()
  193. {
  194. var encoder = new TextEncoder();
  195. assert_equals(encoder.encoding, "utf-8", "TextEncoder encoding test.");
  196. }