Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | * test_TextEncoder.js |
michael@0 | 3 | * bug 764234 tests |
michael@0 | 4 | */ |
michael@0 | 5 | |
michael@0 | 6 | function runTextEncoderTests() |
michael@0 | 7 | { |
michael@0 | 8 | var data = "\u00a0\u0e01\u0e02\u0e03\u0e04\u0e05\u0e06\u0e07\u0e08\u0e09" |
michael@0 | 9 | + "\u0e0a\u0e0b\u0e0c\u0e0d\u0e0e\u0e0f\u0e10\u0e11\u0e12\u0e13\u0e14" |
michael@0 | 10 | + "\u0e15\u0e16\u0e17\u0e18\u0e19\u0e1a\u0e1b\u0e1c\u0e1d\u0e1e\u0e1f" |
michael@0 | 11 | + "\u0e20\u0e21\u0e22\u0e23\u0e24\u0e25\u0e26\u0e27\u0e28\u0e29\u0e2a" |
michael@0 | 12 | + "\u0e2b\u0e2c\u0e2d\u0e2e\u0e2f\u0e30\u0e31\u0e32\u0e33\u0e34\u0e35" |
michael@0 | 13 | + "\u0e36\u0e37\u0e38\u0e39\u0e3a\u0e3f\u0e40\u0e41\u0e42\u0e43\u0e44" |
michael@0 | 14 | + "\u0e45\u0e46\u0e47\u0e48\u0e49\u0e4a\u0e4b\u0e4c\u0e4d\u0e4e\u0e4f" |
michael@0 | 15 | + "\u0e50\u0e51\u0e52\u0e53\u0e54\u0e55\u0e56\u0e57\u0e58\u0e59\u0e5a" |
michael@0 | 16 | + "\u0e5b"; |
michael@0 | 17 | |
michael@0 | 18 | var expectedString = [0xC2, 0xA0, 0xE0, 0xB8, 0x81, 0xE0, 0xB8, 0x82, 0xE0, |
michael@0 | 19 | 0xB8, 0x83, 0xE0, 0xB8, 0x84, 0xE0, 0xB8, 0x85, 0xE0, |
michael@0 | 20 | 0xB8, 0x86, 0xE0, 0xB8, 0x87, 0xE0, 0xB8, 0x88, 0xE0, |
michael@0 | 21 | 0xB8, 0x89, 0xE0, 0xB8, 0x8A, 0xE0, 0xB8, 0x8B, 0xE0, |
michael@0 | 22 | 0xB8, 0x8C, 0xE0, 0xB8, 0x8D, 0xE0, 0xB8, 0x8E, 0xE0, |
michael@0 | 23 | 0xB8, 0x8F, 0xE0, 0xB8, 0x90, 0xE0, 0xB8, 0x91, 0xE0, |
michael@0 | 24 | 0xB8, 0x92, 0xE0, 0xB8, 0x93, 0xE0, 0xB8, 0x94, 0xE0, |
michael@0 | 25 | 0xB8, 0x95, 0xE0, 0xB8, 0x96, 0xE0, 0xB8, 0x97, 0xE0, |
michael@0 | 26 | 0xB8, 0x98, 0xE0, 0xB8, 0x99, 0xE0, 0xB8, 0x9A, 0xE0, |
michael@0 | 27 | 0xB8, 0x9B, 0xE0, 0xB8, 0x9C, 0xE0, 0xB8, 0x9D, 0xE0, |
michael@0 | 28 | 0xB8, 0x9E, 0xE0, 0xB8, 0x9F, 0xE0, 0xB8, 0xA0, 0xE0, |
michael@0 | 29 | 0xB8, 0xA1, 0xE0, 0xB8, 0xA2, 0xE0, 0xB8, 0xA3, 0xE0, |
michael@0 | 30 | 0xB8, 0xA4, 0xE0, 0xB8, 0xA5, 0xE0, 0xB8, 0xA6, 0xE0, |
michael@0 | 31 | 0xB8, 0xA7, 0xE0, 0xB8, 0xA8, 0xE0, 0xB8, 0xA9, 0xE0, |
michael@0 | 32 | 0xB8, 0xAA, 0xE0, 0xB8, 0xAB, 0xE0, 0xB8, 0xAC, 0xE0, |
michael@0 | 33 | 0xB8, 0xAD, 0xE0, 0xB8, 0xAE, 0xE0, 0xB8, 0xAF, 0xE0, |
michael@0 | 34 | 0xB8, 0xB0, 0xE0, 0xB8, 0xB1, 0xE0, 0xB8, 0xB2, 0xE0, |
michael@0 | 35 | 0xB8, 0xB3, 0xE0, 0xB8, 0xB4, 0xE0, 0xB8, 0xB5, 0xE0, |
michael@0 | 36 | 0xB8, 0xB6, 0xE0, 0xB8, 0xB7, 0xE0, 0xB8, 0xB8, 0xE0, |
michael@0 | 37 | 0xB8, 0xB9, 0xE0, 0xB8, 0xBA, 0xE0, 0xB8, 0xBF, 0xE0, |
michael@0 | 38 | 0xB9, 0x80, 0xE0, 0xB9, 0x81, 0xE0, 0xB9, 0x82, 0xE0, |
michael@0 | 39 | 0xB9, 0x83, 0xE0, 0xB9, 0x84, 0xE0, 0xB9, 0x85, 0xE0, |
michael@0 | 40 | 0xB9, 0x86, 0xE0, 0xB9, 0x87, 0xE0, 0xB9, 0x88, 0xE0, |
michael@0 | 41 | 0xB9, 0x89, 0xE0, 0xB9, 0x8A, 0xE0, 0xB9, 0x8B, 0xE0, |
michael@0 | 42 | 0xB9, 0x8C, 0xE0, 0xB9, 0x8D, 0xE0, 0xB9, 0x8E, 0xE0, |
michael@0 | 43 | 0xB9, 0x8F, 0xE0, 0xB9, 0x90, 0xE0, 0xB9, 0x91, 0xE0, |
michael@0 | 44 | 0xB9, 0x92, 0xE0, 0xB9, 0x93, 0xE0, 0xB9, 0x94, 0xE0, |
michael@0 | 45 | 0xB9, 0x95, 0xE0, 0xB9, 0x96, 0xE0, 0xB9, 0x97, 0xE0, |
michael@0 | 46 | 0xB9, 0x98, 0xE0, 0xB9, 0x99, 0xE0, 0xB9, 0x9A, 0xE0, |
michael@0 | 47 | 0xB9, 0x9B]; |
michael@0 | 48 | |
michael@0 | 49 | test(testEncoderGetEncoding, "testEncoderGetEncoding"); |
michael@0 | 50 | test(testInvalidSequence, "testInvalidSequence"); |
michael@0 | 51 | test(testEncodeUTF16ToUTF16, "testEncodeUTF16ToUTF16"); |
michael@0 | 52 | test(function() { |
michael@0 | 53 | testConstructorEncodingOption(data, expectedString) |
michael@0 | 54 | }, "testConstructorEncodingOption"); |
michael@0 | 55 | test(function() { |
michael@0 | 56 | testEncodingValues(data, expectedString) |
michael@0 | 57 | }, "testEncodingValues"); |
michael@0 | 58 | test(function() { |
michael@0 | 59 | testInputString(data, expectedString) |
michael@0 | 60 | }, "testInputString"); |
michael@0 | 61 | test(testStreamingOptions, "testStreamingOptions"); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | function testInvalidSequence() |
michael@0 | 65 | { |
michael@0 | 66 | var data = "\u0e43\u0e44\ufffd\u0e45"; |
michael@0 | 67 | var expectedString = [0xE0, 0xB9, 0x83, 0xE0, 0xB9, 0x84, 0xEF, 0xBF, 0xBD, |
michael@0 | 68 | 0xE0, 0xB9, 0x85]; |
michael@0 | 69 | |
michael@0 | 70 | //Test null input string |
michael@0 | 71 | testSingleString({encoding: "utf-8", input: data, expected: expectedString, |
michael@0 | 72 | msg: "encoder with replacement character test."}); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | function testEncodeUTF16ToUTF16() |
michael@0 | 76 | { |
michael@0 | 77 | var data = "\u0e43\u0e44\u0e45\u0e46\u0e47\u0e48\u0e49\u0e4a\u0e4b\u0e4c" |
michael@0 | 78 | + "\u0e4d\u0e4e\u0e4f\u0e50\u0e51\u0e52\u0e53\u0e54\u0e55\u0e56" |
michael@0 | 79 | + "\u0e57\u0e58\u0e59\u0e5a\u0e5b"; |
michael@0 | 80 | |
michael@0 | 81 | var expected = [0x43, 0x0E, 0x44, 0x0E, 0x45, 0x0E, 0x46, 0x0E, 0x47, 0x0E, |
michael@0 | 82 | 0x48, 0x0E, 0x49, 0x0E, 0x4A, 0x0E, 0x4B, 0x0E, 0x4C, 0x0E, |
michael@0 | 83 | 0x4D, 0x0E, 0x4E, 0x0E, 0x4F, 0x0E, 0x50, 0x0E, 0x51, 0x0E, |
michael@0 | 84 | 0x52, 0x0E, 0x53, 0x0E, 0x54, 0x0E, 0x55, 0x0E, 0x56, 0x0E, |
michael@0 | 85 | 0x57, 0x0E, 0x58, 0x0E, 0x59, 0x0E, 0x5A, 0x0E, 0x5B, 0x0E]; |
michael@0 | 86 | |
michael@0 | 87 | testSingleString({encoding: "Utf-16", input: data, expected: expected, |
michael@0 | 88 | msg: "testing encoding from utf-16 to utf-16 zero."}); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | function testConstructorEncodingOption(aData, aExpectedString) |
michael@0 | 92 | { |
michael@0 | 93 | // valid encoding passed |
michael@0 | 94 | testSingleString({encoding: "UTF-8", input: aData, expected: aExpectedString, |
michael@0 | 95 | msg: "testing encoding with valid utf-8 encoding."}); |
michael@0 | 96 | |
michael@0 | 97 | // passing spaces for encoding |
michael@0 | 98 | testSingleString({encoding: " ", input: aData, error: "TypeError", |
michael@0 | 99 | msg: "constructor encoding, spaces encoding test."}); |
michael@0 | 100 | |
michael@0 | 101 | // invalid encoding passed |
michael@0 | 102 | testSingleString({encoding: "asdfasdf", input: aData, error: "TypeError", |
michael@0 | 103 | msg: "constructor encoding, invalid encoding test."}); |
michael@0 | 104 | |
michael@0 | 105 | // null encoding passed |
michael@0 | 106 | testSingleString({encoding: null, input: aData, error: "TypeError", |
michael@0 | 107 | msg: "constructor encoding, \"null\" encoding test."}); |
michael@0 | 108 | |
michael@0 | 109 | // null encoding passed |
michael@0 | 110 | testSingleString({encoding: "", input: aData, error: "TypeError", |
michael@0 | 111 | msg: "constructor encoding, empty encoding test."}); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | function testEncodingValues(aData, aExpectedString) |
michael@0 | 115 | { |
michael@0 | 116 | var encoding = "ISO-8859-11"; |
michael@0 | 117 | testSingleString({encoding: aData, input: encoding, error: "TypeError", |
michael@0 | 118 | msg: "encoder encoding values test."}); |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | function testInputString(aData, aExpectedString) |
michael@0 | 122 | { |
michael@0 | 123 | //Test null input string |
michael@0 | 124 | testSingleString({encoding: "utf-8", input: "", expected: [], |
michael@0 | 125 | msg: "encoder null input string test."}); |
michael@0 | 126 | |
michael@0 | 127 | //Test spaces as input string |
michael@0 | 128 | testSingleString({encoding: "utf-8", input: " ", expected: [32, 32], |
michael@0 | 129 | msg: "spaces as input string."}); |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | function testSingleString(test) |
michael@0 | 133 | { |
michael@0 | 134 | var outText; |
michael@0 | 135 | try { |
michael@0 | 136 | var stream = test.stream ? {stream: true} : null; |
michael@0 | 137 | outText = (new TextEncoder(test.encoding)).encode(test.input, stream); |
michael@0 | 138 | } catch (e) { |
michael@0 | 139 | assert_equals(e.name, test.error, test.msg); |
michael@0 | 140 | return; |
michael@0 | 141 | } |
michael@0 | 142 | assert_true(!test.error, test.msg); |
michael@0 | 143 | |
michael@0 | 144 | if (outText.length !== test.expected.length) { |
michael@0 | 145 | assert_equals(outText.length, test.expected.length, test.msg + " length mismatch"); |
michael@0 | 146 | return; |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | for (var i = 0; i < outText.length; i++) { |
michael@0 | 150 | if (outText[i] != test.expected[i]) { |
michael@0 | 151 | assert_equals(escape(stringFromArray(outText.buffer)), escape(stringFromArray(test.expected)), |
michael@0 | 152 | test.msg + " Bytes do not match expected bytes."); |
michael@0 | 153 | return; |
michael@0 | 154 | } |
michael@0 | 155 | } |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | function stringFromArray(a) { |
michael@0 | 159 | return Array.map.call(a, function(v){return String.fromCharCode(v)}).join(''); |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | function testStreamingOptions() |
michael@0 | 163 | { |
michael@0 | 164 | var data = [ |
michael@0 | 165 | "\u00a0\u0e01\u0e02\u0e03\u0e04\u0e05\u0e06\u0e07\u0e08\u0e09\u0e0a" |
michael@0 | 166 | + "\u0e0b\u0e0c\u0e0d\u0e0e\u0e0f\u0e10\u0e11\u0e12\u0e13\u0e14" |
michael@0 | 167 | + "\u0e15\u0e16\u0e17\u0e18\u0e19\u0e1a\u0e1b\u0e1c\u0e1d", |
michael@0 | 168 | "\u0e1e\u0e1f\u0e20\u0e21\u0e22\u0e23\u0e24\u0e25\u0e26\u0e27\u0e28" |
michael@0 | 169 | + "\u0e29\u0e2a\u0e2b\u0e2c\u0e2d\u0e2e\u0e2f\u0e30\u0e31\u0e32" |
michael@0 | 170 | + "\u0e33\u0e34\u0e35\u0e36\u0e37\u0e38\u0e39\u0e3a\u0e3f\u0e40" |
michael@0 | 171 | + "\u0e41\u0e42", |
michael@0 | 172 | "\u0e43\u0e44\u0e45\u0e46\u0e47\u0e48\u0e49\u0e4a\u0e4b\u0e4c\u0e4d" |
michael@0 | 173 | + "\u0e4e\u0e4f\u0e50\u0e51\u0e52\u0e53\u0e54\u0e55\u0e56\u0e57" |
michael@0 | 174 | + "\u0e58\u0e59\u0e5a\u0e5b" |
michael@0 | 175 | ]; |
michael@0 | 176 | |
michael@0 | 177 | var expected = [[0xC2, 0xA0, 0xE0, 0xB8, 0x81, 0xE0, 0xB8, 0x82, 0xE0, |
michael@0 | 178 | 0xB8, 0x83, 0xE0, 0xB8, 0x84, 0xE0, 0xB8, 0x85, 0xE0, |
michael@0 | 179 | 0xB8, 0x86, 0xE0, 0xB8, 0x87, 0xE0, 0xB8, 0x88, 0xE0, |
michael@0 | 180 | 0xB8, 0x89, 0xE0, 0xB8, 0x8A, 0xE0, 0xB8, 0x8B, 0xE0, |
michael@0 | 181 | 0xB8, 0x8C, 0xE0, 0xB8, 0x8D, 0xE0, 0xB8, 0x8E, 0xE0, |
michael@0 | 182 | 0xB8, 0x8F, 0xE0, 0xB8, 0x90, 0xE0, 0xB8, 0x91, 0xE0, |
michael@0 | 183 | 0xB8, 0x92, 0xE0, 0xB8, 0x93, 0xE0, 0xB8, 0x94, 0xE0, |
michael@0 | 184 | 0xB8, 0x95, 0xE0, 0xB8, 0x96, 0xE0, 0xB8, 0x97, 0xE0, |
michael@0 | 185 | 0xB8, 0x98, 0xE0, 0xB8, 0x99, 0xE0, 0xB8, 0x9A, 0xE0, |
michael@0 | 186 | 0xB8, 0x9B, 0xE0, 0xB8, 0x9C, 0xE0, 0xB8, 0x9D], |
michael@0 | 187 | |
michael@0 | 188 | [0xE0, 0xB8, 0x9E, 0xE0, 0xB8, 0x9F, 0xE0, 0xB8, 0xA0, |
michael@0 | 189 | 0xE0, 0xB8, 0xA1, 0xE0, 0xB8, 0xA2, 0xE0, 0xB8, 0xA3, |
michael@0 | 190 | 0xE0, 0xB8, 0xA4, 0xE0, 0xB8, 0xA5, 0xE0, 0xB8, 0xA6, |
michael@0 | 191 | 0xE0, 0xB8, 0xA7, 0xE0, 0xB8, 0xA8, 0xE0, 0xB8, 0xA9, |
michael@0 | 192 | 0xE0, 0xB8, 0xAA, 0xE0, 0xB8, 0xAB, 0xE0, 0xB8, 0xAC, |
michael@0 | 193 | 0xE0, 0xB8, 0xAD, 0xE0, 0xB8, 0xAE, 0xE0, 0xB8, 0xAF, |
michael@0 | 194 | 0xE0, 0xB8, 0xB0, 0xE0, 0xB8, 0xB1, 0xE0, 0xB8, 0xB2, |
michael@0 | 195 | 0xE0, 0xB8, 0xB3, 0xE0, 0xB8, 0xB4, 0xE0, 0xB8, 0xB5, |
michael@0 | 196 | 0xE0, 0xB8, 0xB6, 0xE0, 0xB8, 0xB7, 0xE0, 0xB8, 0xB8, |
michael@0 | 197 | 0xE0, 0xB8, 0xB9, 0xE0, 0xB8, 0xBA, 0xE0, 0xB8, 0xBF, |
michael@0 | 198 | 0xE0, 0xB9, 0x80, 0xE0, 0xB9, 0x81, 0xE0, 0xB9, 0x82], |
michael@0 | 199 | |
michael@0 | 200 | [0xE0, 0xB9, 0x83, 0xE0, 0xB9, 0x84, 0xE0, 0xB9, 0x85, |
michael@0 | 201 | 0xE0, 0xB9, 0x86, 0xE0, 0xB9, 0x87, 0xE0, 0xB9, 0x88, |
michael@0 | 202 | 0xE0, 0xB9, 0x89, 0xE0, 0xB9, 0x8A, 0xE0, 0xB9, 0x8B, |
michael@0 | 203 | 0xE0, 0xB9, 0x8C, 0xE0, 0xB9, 0x8D, 0xE0, 0xB9, 0x8E, |
michael@0 | 204 | 0xE0, 0xB9, 0x8F, 0xE0, 0xB9, 0x90, 0xE0, 0xB9, 0x91, |
michael@0 | 205 | 0xE0, 0xB9, 0x92, 0xE0, 0xB9, 0x93, 0xE0, 0xB9, 0x94, |
michael@0 | 206 | 0xE0, 0xB9, 0x95, 0xE0, 0xB9, 0x96, 0xE0, 0xB9, 0x97, |
michael@0 | 207 | 0xE0, 0xB9, 0x98, 0xE0, 0xB9, 0x99, 0xE0, 0xB9, 0x9A, |
michael@0 | 208 | 0xE0, 0xB9, 0x9B]]; |
michael@0 | 209 | |
michael@0 | 210 | var expectedUTF16 = data.map(function(d) { |
michael@0 | 211 | return new Uint8Array(new Uint16Array(arrayFromString(d)).buffer); |
michael@0 | 212 | }); |
michael@0 | 213 | |
michael@0 | 214 | // STREAMING TEST ONE: test streaming three valid strings with stream option |
michael@0 | 215 | // set to true for all three. |
michael@0 | 216 | testArrayOfStrings({encoding: "utf-8", array: [ |
michael@0 | 217 | {input: data[0], stream: true, expected: expected[0]}, |
michael@0 | 218 | {input: data[1], stream: true, expected: expected[1]}, |
michael@0 | 219 | {input: data[2], stream: true, expected: expected[2]}, |
michael@0 | 220 | ], msg: "streaming test one."}); |
michael@0 | 221 | |
michael@0 | 222 | // STREAMING TEST TWO: test streaming valid strings with stream option |
michael@0 | 223 | // streaming option: false from constructor, string 1 stream: true, |
michael@0 | 224 | // string 2 stream: false, string 3 stream: false |
michael@0 | 225 | testArrayOfStrings({encoding: "utf-16", array: [ |
michael@0 | 226 | {input: data[0], stream: true, expected: expectedUTF16[0]}, |
michael@0 | 227 | {input: data[1], expected: expectedUTF16[1]}, |
michael@0 | 228 | {input: data[2], expected: expectedUTF16[2]}, |
michael@0 | 229 | ], msg: "streaming test two."}); |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | function arrayFromString(s) { |
michael@0 | 233 | return s.split('').map(function(c){return String.charCodeAt(c)}); |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | function testArrayOfStrings(test) |
michael@0 | 237 | { |
michael@0 | 238 | var encoder; |
michael@0 | 239 | try { |
michael@0 | 240 | encoder = new TextEncoder(test.encoding); |
michael@0 | 241 | } catch (e) { |
michael@0 | 242 | assert_equals(e.name, test.error, test.msg); |
michael@0 | 243 | return; |
michael@0 | 244 | } |
michael@0 | 245 | assert_true(!test.error, test.msg); |
michael@0 | 246 | |
michael@0 | 247 | var array = test.array; |
michael@0 | 248 | for (var i = 0; i < array.length; i += 1) { |
michael@0 | 249 | var stream = array[i].stream ? {stream: true} : null; |
michael@0 | 250 | var view = encoder.encode(array[i].input, stream); |
michael@0 | 251 | |
michael@0 | 252 | var stringLen = view.length; |
michael@0 | 253 | var expected = array[i].expected; |
michael@0 | 254 | for (var j = 0; j < stringLen; j++) { |
michael@0 | 255 | if (view[j] !== expected[j]) { |
michael@0 | 256 | assert_equals(view[j], expected[j], msg + " Bytes do not match expected bytes."); |
michael@0 | 257 | return; |
michael@0 | 258 | } |
michael@0 | 259 | } |
michael@0 | 260 | } |
michael@0 | 261 | } |
michael@0 | 262 | |
michael@0 | 263 | function testEncoderGetEncoding() |
michael@0 | 264 | { |
michael@0 | 265 | var labelEncodings = [ |
michael@0 | 266 | {encoding: "utf-8", labels: ["unicode-1-1-utf-8", "utf-8", "utf8"]}, |
michael@0 | 267 | {encoding: "utf-16le", labels: ["utf-16", "utf-16"]}, |
michael@0 | 268 | {encoding: "utf-16be", labels: ["utf-16be"]}, |
michael@0 | 269 | ]; |
michael@0 | 270 | |
michael@0 | 271 | for (var le of labelEncodings) { |
michael@0 | 272 | for (var label of le.labels) { |
michael@0 | 273 | var encoder = new TextEncoder(label); |
michael@0 | 274 | assert_equals(encoder.encoding, le.encoding, label + " label encoding test."); |
michael@0 | 275 | } |
michael@0 | 276 | } |
michael@0 | 277 | } |