Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* |
michael@0 | 2 | * test_TextDecoderOptions.js |
michael@0 | 3 | * bug 764234 tests |
michael@0 | 4 | */ |
michael@0 | 5 | |
michael@0 | 6 | function runTextDecoderOptions() |
michael@0 | 7 | { |
michael@0 | 8 | const data = [0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, |
michael@0 | 9 | 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, |
michael@0 | 10 | 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, |
michael@0 | 11 | 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, |
michael@0 | 12 | 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, |
michael@0 | 13 | 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, |
michael@0 | 14 | 0xda, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, |
michael@0 | 15 | 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, |
michael@0 | 16 | 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb]; |
michael@0 | 17 | |
michael@0 | 18 | const expectedString = "\u00a0\u0e01\u0e02\u0e03\u0e04\u0e05\u0e06\u0e07" |
michael@0 | 19 | + "\u0e08\u0e09\u0e0a\u0e0b\u0e0c\u0e0d\u0e0e\u0e0f" |
michael@0 | 20 | + "\u0e10\u0e11\u0e12\u0e13\u0e14\u0e15\u0e16\u0e17" |
michael@0 | 21 | + "\u0e18\u0e19\u0e1a\u0e1b\u0e1c\u0e1d\u0e1e\u0e1f" |
michael@0 | 22 | + "\u0e20\u0e21\u0e22\u0e23\u0e24\u0e25\u0e26\u0e27" |
michael@0 | 23 | + "\u0e28\u0e29\u0e2a\u0e2b\u0e2c\u0e2d\u0e2e\u0e2f" |
michael@0 | 24 | + "\u0e30\u0e31\u0e32\u0e33\u0e34\u0e35\u0e36\u0e37" |
michael@0 | 25 | + "\u0e38\u0e39\u0e3a\u0e3f\u0e40\u0e41\u0e42\u0e43" |
michael@0 | 26 | + "\u0e44\u0e45\u0e46\u0e47\u0e48\u0e49\u0e4a\u0e4b" |
michael@0 | 27 | + "\u0e4c\u0e4d\u0e4e\u0e4f\u0e50\u0e51\u0e52\u0e53" |
michael@0 | 28 | + "\u0e54\u0e55\u0e56\u0e57\u0e58\u0e59\u0e5a\u0e5b"; |
michael@0 | 29 | |
michael@0 | 30 | test(testDecoderGetEncoding, "testDecoderGetEncoding"); |
michael@0 | 31 | test(testDecodeGreek, "testDecodeGreek"); |
michael@0 | 32 | test(function() { |
michael@0 | 33 | testConstructorFatalOption(data, expectedString); |
michael@0 | 34 | }, "testConstructorFatalOption"); |
michael@0 | 35 | test(function() { |
michael@0 | 36 | testConstructorEncodingOption(data, expectedString); |
michael@0 | 37 | }, "testConstructorEncodingOption"); |
michael@0 | 38 | test(function() { |
michael@0 | 39 | testDecodeStreamOption(data, expectedString); |
michael@0 | 40 | }, "testDecodeStreamOption"); |
michael@0 | 41 | test(testDecodeStreamCompositions, "testDecodeStreamCompositions"); |
michael@0 | 42 | test(function() { |
michael@0 | 43 | testDecodeABVOption(data, expectedString); |
michael@0 | 44 | }, "testDecodeABVOption"); |
michael@0 | 45 | test(testDecoderForThaiEncoding, "testDecoderForThaiEncoding"); |
michael@0 | 46 | test(testInvalid2022JP, "testInvalid2022JP"); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | /* |
michael@0 | 50 | * function testConstructor() |
michael@0 | 51 | * |
michael@0 | 52 | * - This function tests the constructor optional arguments. |
michael@0 | 53 | * - Stream option remains null for this test. |
michael@0 | 54 | * - The stream option is passed to the decode function. |
michael@0 | 55 | * - This function is not testing the decode function. |
michael@0 | 56 | * |
michael@0 | 57 | */ |
michael@0 | 58 | function testConstructorFatalOption(data, expectedString) |
michael@0 | 59 | { |
michael@0 | 60 | //invalid string to decode passed, fatal = false |
michael@0 | 61 | testCharset({fatal: false, encoding: "iso-8859-11", input: [], expected: "", |
michael@0 | 62 | msg: "constructor fatal option set to false test."}); |
michael@0 | 63 | |
michael@0 | 64 | //invalid string to decode passed, fatal = true |
michael@0 | 65 | testCharset({fatal: true, encoding: "iso-8859-11", input: [], expected: "", |
michael@0 | 66 | msg: "constructor fatal option set to true test."}); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | function testConstructorEncodingOption(aData, aExpectedString) |
michael@0 | 70 | { |
michael@0 | 71 | // valid encoding passed |
michael@0 | 72 | testCharset({encoding: "iso-8859-11", input: aData, expected: aExpectedString, |
michael@0 | 73 | msg: "decoder testing constructor valid encoding."}); |
michael@0 | 74 | |
michael@0 | 75 | // invalid encoding passed |
michael@0 | 76 | testCharset({encoding: "asdfasdf", input: aData, error: "TypeError", |
michael@0 | 77 | msg: "constructor encoding, invalid encoding test."}); |
michael@0 | 78 | |
michael@0 | 79 | // passing spaces for encoding |
michael@0 | 80 | testCharset({encoding: " ", input: aData, error: "TypeError", |
michael@0 | 81 | msg: "constructor encoding, spaces encoding test."}); |
michael@0 | 82 | |
michael@0 | 83 | // passing null for encoding |
michael@0 | 84 | testCharset({encoding: null, input: aData, error: "TypeError", |
michael@0 | 85 | msg: "constructor encoding, \"null\" encoding test."}); |
michael@0 | 86 | |
michael@0 | 87 | // empty encoding passed |
michael@0 | 88 | testCharset({encoding: "", input: aData, error: "TypeError", |
michael@0 | 89 | msg: "constuctor encoding, empty encoding test."}); |
michael@0 | 90 | |
michael@0 | 91 | // replacement character test |
michael@0 | 92 | aExpectedString = "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd" |
michael@0 | 93 | + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd" |
michael@0 | 94 | + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd" |
michael@0 | 95 | + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd" |
michael@0 | 96 | + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd" |
michael@0 | 97 | + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd" |
michael@0 | 98 | + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd" |
michael@0 | 99 | + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd" |
michael@0 | 100 | + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd" |
michael@0 | 101 | + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd" |
michael@0 | 102 | + "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"; |
michael@0 | 103 | testCharset({encoding: "utf-8", input: aData, expected: aExpectedString, |
michael@0 | 104 | msg: "constuctor encoding, utf-8 test."}); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | /* |
michael@0 | 108 | * function testDecodeStreamOption() |
michael@0 | 109 | * |
michael@0 | 110 | * - fatal remains null for the entire test |
michael@0 | 111 | * - encoding remains as "iso-8859-11" |
michael@0 | 112 | * - The stream option is modified for this test. |
michael@0 | 113 | * - ArrayBufferView is modified for this test. |
michael@0 | 114 | */ |
michael@0 | 115 | function testDecodeStreamOption(data, expectedString) |
michael@0 | 116 | { |
michael@0 | 117 | const streamData = [[0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, |
michael@0 | 118 | 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, |
michael@0 | 119 | 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, |
michael@0 | 120 | 0xb9, 0xba, 0xbb, 0xbc, 0xbd], |
michael@0 | 121 | [0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, |
michael@0 | 122 | 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, |
michael@0 | 123 | 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, |
michael@0 | 124 | 0xd8, 0xd9, 0xda, 0xdf, 0xe0, 0xe1, 0xe2], |
michael@0 | 125 | [0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, |
michael@0 | 126 | 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, |
michael@0 | 127 | 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb]]; |
michael@0 | 128 | |
michael@0 | 129 | const expectedStringOne = "\u00a0\u0e01\u0e02\u0e03\u0e04\u0e05\u0e06\u0e07" |
michael@0 | 130 | + "\u0e08\u0e09\u0e0a\u0e0b\u0e0c\u0e0d\u0e0e\u0e0f" |
michael@0 | 131 | + "\u0e10\u0e11\u0e12\u0e13\u0e14\u0e15\u0e16\u0e17" |
michael@0 | 132 | + "\u0e18\u0e19\u0e1a\u0e1b\u0e1c\u0e1d"; |
michael@0 | 133 | const expectedStringTwo = "\u0e1e\u0e1f\u0e20\u0e21\u0e22\u0e23\u0e24\u0e25" |
michael@0 | 134 | + "\u0e26\u0e27\u0e28\u0e29\u0e2a\u0e2b\u0e2c\u0e2d" |
michael@0 | 135 | + "\u0e2e\u0e2f\u0e30\u0e31\u0e32\u0e33\u0e34\u0e35" |
michael@0 | 136 | + "\u0e36\u0e37\u0e38\u0e39\u0e3a\u0e3f\u0e40\u0e41" |
michael@0 | 137 | + "\u0e42"; |
michael@0 | 138 | const expectedStringThree = "\u0e43\u0e44\u0e45\u0e46\u0e47\u0e48\u0e49\u0e4a" |
michael@0 | 139 | + "\u0e4b\u0e4c\u0e4d\u0e4e\u0e4f\u0e50\u0e51" |
michael@0 | 140 | + "\u0e52\u0e53\u0e54\u0e55\u0e56\u0e57\u0e58" |
michael@0 | 141 | + "\u0e59\u0e5a\u0e5b"; |
michael@0 | 142 | expectedString = [expectedStringOne, expectedStringTwo, expectedStringThree]; |
michael@0 | 143 | |
michael@0 | 144 | // streaming test |
michael@0 | 145 | |
michael@0 | 146 | /* - the streaming is null |
michael@0 | 147 | * - streaming is not set in the decode function |
michael@0 | 148 | */ |
michael@0 | 149 | testCharset({encoding: "iso-8859-11", array: [ |
michael@0 | 150 | {input: streamData[0], expected: expectedStringOne}, |
michael@0 | 151 | {input: streamData[1], expected: expectedStringTwo}, |
michael@0 | 152 | {input: streamData[2], expected: expectedStringThree}, |
michael@0 | 153 | ], msg: "decode() stream test zero."}); |
michael@0 | 154 | |
michael@0 | 155 | testCharset({encoding: "iso-8859-11", array: [ |
michael@0 | 156 | {input: streamData[0], expected: expectedStringOne, stream: true}, |
michael@0 | 157 | {input: streamData[1], expected: expectedStringTwo, stream: true}, |
michael@0 | 158 | {input: streamData[2], expected: expectedStringThree, stream: true}, |
michael@0 | 159 | ], msg: "decode() stream test one."}); |
michael@0 | 160 | |
michael@0 | 161 | testCharset({encoding: "iso-8859-11", array: [ |
michael@0 | 162 | {input: streamData[0], expected: expectedStringOne, stream: true}, |
michael@0 | 163 | {input: streamData[1], expected: expectedStringTwo}, |
michael@0 | 164 | {input: streamData[2], expected: expectedStringThree}, |
michael@0 | 165 | ], msg: "decode() stream test two."}); |
michael@0 | 166 | |
michael@0 | 167 | testCharset({encoding: "utf-8", array: [ |
michael@0 | 168 | {input: [0xC2], expected: "\uFFFD"}, |
michael@0 | 169 | {input: [0x80], expected: "\uFFFD"}, |
michael@0 | 170 | ], msg: "decode() stream test utf-8."}); |
michael@0 | 171 | |
michael@0 | 172 | testCharset({encoding: "utf-8", fatal: true, array: [ |
michael@0 | 173 | {input: [0xC2], error: "EncodingError"}, |
michael@0 | 174 | {input: [0x80], error: "EncodingError"}, |
michael@0 | 175 | ], msg: "decode() stream test utf-8 fatal."}); |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | function testDecodeStreamCompositions() { |
michael@0 | 179 | var tests = [ |
michael@0 | 180 | {encoding: "utf-8", input: [0xC2,0x80], expected: ["","\x80"]}, |
michael@0 | 181 | {encoding: "utf-8", input: [0xEF,0xBB,0xBF,0xC2,0x80], expected: ["","","","","\x80"]}, |
michael@0 | 182 | {encoding: "utf-16", input: [0x01,0x00], expected: ["","\x01"]}, |
michael@0 | 183 | {encoding: "utf-16", input: [0x01,0x00,0x03,0x02], expected: ["","\x01","","\u0203"]}, |
michael@0 | 184 | {encoding: "utf-16", input: [0xFF,0xFD], expected: ["","\uFDFF"]}, |
michael@0 | 185 | {encoding: "utf-16", input: [0xFF,0xFE], expected: ["",""]}, |
michael@0 | 186 | {encoding: "utf-16", input: [0xFF,0xFF], expected: ["","\uFFFF"]}, |
michael@0 | 187 | {encoding: "utf-16", input: [0xFF,0xFE,0x01,0x00], expected: ["","","","\x01"]}, |
michael@0 | 188 | {encoding: "utf-16", input: [0xFF,0xFE,0xFF,0xFE], expected: ["","","","\uFEFF"]}, |
michael@0 | 189 | {encoding: "utf-16", input: [0xFF,0xFE,0xFE,0xFF], expected: ["","","","\uFFFE"]}, |
michael@0 | 190 | {encoding: "utf-16", input: [0xFD,0xFE], expected: ["","\uFEFD"]}, |
michael@0 | 191 | {encoding: "utf-16", input: [0xFD,0xFF], expected: ["","\uFFFD"]}, |
michael@0 | 192 | {encoding: "utf-16", input: [0xFE,0xFD], expected: ["","\uFDFE"]}, |
michael@0 | 193 | {encoding: "utf-16", input: [0xFE,0xFE], expected: ["","\uFEFE"]}, |
michael@0 | 194 | {encoding: "utf-16", input: [0xFE,0xFF], expected: ["","\uFFFE"]}, |
michael@0 | 195 | {encoding: "utf-16", input: [0xFE,0xFF,0x01,0x00], expected: ["","\uFFFE","","\x01"]}, |
michael@0 | 196 | {encoding: "utf-16", input: [0xFE,0xFF,0xFF,0xFE], expected: ["","\uFFFE","","\uFEFF"]}, |
michael@0 | 197 | {encoding: "utf-16", input: [0xFE,0xFF,0xFE,0xFF], expected: ["","\uFFFE","","\uFFFE"]}, |
michael@0 | 198 | {encoding: "utf-16le", input: [0x01,0x00], expected: ["","\x01"]}, |
michael@0 | 199 | {encoding: "utf-16le", input: [0x01,0x00,0x03,0x02], expected: ["","\x01","","\u0203"]}, |
michael@0 | 200 | {encoding: "utf-16le", input: [0xFF,0xFE,0x01,0x00], expected: ["","","","\x01"]}, |
michael@0 | 201 | {encoding: "utf-16le", input: [0xFE,0xFF,0x01,0x00], expected: ["","\uFFFE","","\x01"]}, |
michael@0 | 202 | {encoding: "utf-16be", input: [0x01,0x00], expected: ["","\u0100"]}, |
michael@0 | 203 | {encoding: "utf-16be", input: [0x01,0x00,0x03,0x02], expected: ["","\u0100","","\u0302"]}, |
michael@0 | 204 | {encoding: "utf-16be", input: [0xFD,0xFE], expected: ["","\uFDFE"]}, |
michael@0 | 205 | {encoding: "utf-16be", input: [0xFD,0xFF], expected: ["","\uFDFF"]}, |
michael@0 | 206 | {encoding: "utf-16be", input: [0xFE,0xFD], expected: ["","\uFEFD"]}, |
michael@0 | 207 | {encoding: "utf-16be", input: [0xFE,0xFE], expected: ["","\uFEFE"]}, |
michael@0 | 208 | {encoding: "utf-16be", input: [0xFE,0xFF], expected: ["",""]}, |
michael@0 | 209 | {encoding: "utf-16be", input: [0xFE,0xFF,0x01,0x00], expected: ["","","","\u0100"]}, |
michael@0 | 210 | {encoding: "utf-16be", input: [0xFF,0xFD], expected: ["","\uFFFD"]}, |
michael@0 | 211 | {encoding: "utf-16be", input: [0xFF,0xFE], expected: ["","\uFFFE"]}, |
michael@0 | 212 | {encoding: "utf-16be", input: [0xFF,0xFF], expected: ["","\uFFFF"]}, |
michael@0 | 213 | {encoding: "utf-16be", input: [0xFF,0xFE,0x01,0x00], expected: ["","\uFFFE","","\u0100"]}, |
michael@0 | 214 | {encoding: "shift_jis", input: [0x81,0x40], expected: ["","\u3000"]}, |
michael@0 | 215 | ]; |
michael@0 | 216 | tests.forEach(function(t) { |
michael@0 | 217 | (function generateCompositions(a, n) { |
michael@0 | 218 | a.push(n); |
michael@0 | 219 | var l = a.length - 1; |
michael@0 | 220 | var array=[]; |
michael@0 | 221 | for (var i = 0, o = 0; i <= l; i++) { |
michael@0 | 222 | array.push({ |
michael@0 | 223 | input: t.input.slice(o, o+a[i]), |
michael@0 | 224 | expected: t.expected.slice(o, o+=a[i]).join(""), |
michael@0 | 225 | stream: i < l |
michael@0 | 226 | }); |
michael@0 | 227 | } |
michael@0 | 228 | testCharset({encoding: t.encoding, array: array, |
michael@0 | 229 | msg: "decode() stream test " + t.encoding + " " + a.join("-") + "."}); |
michael@0 | 230 | while (a[l] > 1) { |
michael@0 | 231 | a[l]--; |
michael@0 | 232 | generateCompositions(a.slice(0), n - a[l]); |
michael@0 | 233 | } |
michael@0 | 234 | })([], t.input.length); |
michael@0 | 235 | }); |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | /* |
michael@0 | 239 | * function testDecodeABVOption() |
michael@0 | 240 | * |
michael@0 | 241 | * - ABV for ArrayBufferView |
michael@0 | 242 | * - fatal remains null for the entire test |
michael@0 | 243 | * - encoding remains as "iso-8859-11" |
michael@0 | 244 | * - The stream option is modified for this test. |
michael@0 | 245 | * - ArrayBufferView is modified for this test. |
michael@0 | 246 | */ |
michael@0 | 247 | function testDecodeABVOption(data, expectedString) |
michael@0 | 248 | { |
michael@0 | 249 | // valid data |
michael@0 | 250 | testCharset({encoding: "iso-8859-11", input: data, expected: expectedString, |
michael@0 | 251 | msg: "decode test ABV valid data."}); |
michael@0 | 252 | |
michael@0 | 253 | // invalid empty data |
michael@0 | 254 | testCharset({encoding: "iso-8859-11", input: [], expected: "", |
michael@0 | 255 | msg: "decode test ABV empty data."}); |
michael@0 | 256 | |
michael@0 | 257 | // spaces |
michael@0 | 258 | testCharset({encoding: "iso-8859-11", input: ["\u0020\u0020"], expected: "\0", |
michael@0 | 259 | msg: "text decoding ABV string test."}); |
michael@0 | 260 | |
michael@0 | 261 | testCharset({encoding: "iso-8859-11", input: [""], expected: "\0", |
michael@0 | 262 | msg: "text decoding ABV empty string test."}); |
michael@0 | 263 | |
michael@0 | 264 | // null for Array Buffer |
michael@0 | 265 | testCharset({encoding: "iso-8859-11", input: null, error: "TypeError", |
michael@0 | 266 | msg: "text decoding ABV null test."}); |
michael@0 | 267 | } |
michael@0 | 268 | |
michael@0 | 269 | function testDecodeGreek() |
michael@0 | 270 | { |
michael@0 | 271 | var data = [0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, |
michael@0 | 272 | 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, |
michael@0 | 273 | 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, |
michael@0 | 274 | 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, |
michael@0 | 275 | 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd3, 0xd4, 0xd5, 0xd6, |
michael@0 | 276 | 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, |
michael@0 | 277 | 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, |
michael@0 | 278 | 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, |
michael@0 | 279 | 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe]; |
michael@0 | 280 | |
michael@0 | 281 | var expectedString = "\u00a0\u2018\u2019\u00a3\u20ac\u20af\u00a6\u00a7\u00a8" |
michael@0 | 282 | + "\u00a9\u037a\u00ab\u00ac\u00ad\u2015\u00b0\u00b1" |
michael@0 | 283 | + "\u00b2\u00b3\u0384\u0385\u0386\u00b7\u0388\u0389" |
michael@0 | 284 | + "\u038a\u00bb\u038c\u00bd\u038e\u038f\u0390\u0391" |
michael@0 | 285 | + "\u0392\u0393\u0394\u0395\u0396\u0397\u0398\u0399" |
michael@0 | 286 | + "\u039a\u039b\u039c\u039d\u039e\u039f\u03a0\u03a1" |
michael@0 | 287 | + "\u03a3\u03a4\u03a5\u03a6\u03a7\u03a8\u03a9\u03aa" |
michael@0 | 288 | + "\u03ab\u03ac\u03ad\u03ae\u03af\u03b0\u03b1\u03b2" |
michael@0 | 289 | + "\u03b3\u03b4\u03b5\u03b6\u03b7\u03b8\u03b9\u03ba" |
michael@0 | 290 | + "\u03bb\u03bc\u03bd\u03be\u03bf\u03c0\u03c1\u03c2" |
michael@0 | 291 | + "\u03c3\u03c4\u03c5\u03c6\u03c7\u03c8\u03c9\u03ca" |
michael@0 | 292 | + "\u03cb\u03cc\u03cd\u03ce"; |
michael@0 | 293 | |
michael@0 | 294 | testCharset({encoding: "greek", input: data, expected: expectedString, |
michael@0 | 295 | msg: "decode greek test."}); |
michael@0 | 296 | } |
michael@0 | 297 | |
michael@0 | 298 | function testDecoderForThaiEncoding() |
michael@0 | 299 | { |
michael@0 | 300 | // TEST One |
michael@0 | 301 | const data = [0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb]; |
michael@0 | 302 | |
michael@0 | 303 | const expectedString = "\u00a0\u0e01\u0e02\u0e03\u0e04\u0e05\u0e06\u0e07\u0e08\u0e09\u0e0a\u0e0b\u0e0c\u0e0d\u0e0e\u0e0f\u0e10\u0e11\u0e12\u0e13\u0e14\u0e15\u0e16\u0e17\u0e18\u0e19\u0e1a\u0e1b\u0e1c\u0e1d\u0e1e\u0e1f\u0e20\u0e21\u0e22\u0e23\u0e24\u0e25\u0e26\u0e27\u0e28\u0e29\u0e2a\u0e2b\u0e2c\u0e2d\u0e2e\u0e2f\u0e30\u0e31\u0e32\u0e33\u0e34\u0e35\u0e36\u0e37\u0e38\u0e39\u0e3a\u0e3f\u0e40\u0e41\u0e42\u0e43\u0e44\u0e45\u0e46\u0e47\u0e48\u0e49\u0e4a\u0e4b\u0e4c\u0e4d\u0e4e\u0e4f\u0e50\u0e51\u0e52\u0e53\u0e54\u0e55\u0e56\u0e57\u0e58\u0e59\u0e5a\u0e5b"; |
michael@0 | 304 | |
michael@0 | 305 | const aliases = [ "ISO-8859-11", "iso-8859-11", "iso8859-11", "iso885911" ]; |
michael@0 | 306 | |
michael@0 | 307 | testCharset({encoding: "iso-8859-11", input: data, expected: expectedString, |
michael@0 | 308 | msg: "decoder testing valid ISO-8859-11 encoding."}); |
michael@0 | 309 | } |
michael@0 | 310 | |
michael@0 | 311 | function testDecoderGetEncoding() |
michael@0 | 312 | { |
michael@0 | 313 | var labelEncodings = [ |
michael@0 | 314 | {encoding: "utf-8", labels: ["unicode-1-1-utf-8", "utf-8", "utf8"]}, |
michael@0 | 315 | {encoding: "ibm866", labels: ["866", "cp866", "csibm866", "ibm866"]}, |
michael@0 | 316 | {encoding: "iso-8859-2", labels: ["csisolatin2", "iso-8859-2", "iso-ir-101", "iso8859-2", "iso88592", "iso_8859-2", "iso_8859-2:1987", "l2", "latin2"]}, |
michael@0 | 317 | {encoding: "iso-8859-3", labels: ["csisolatin3", "iso-8859-3", "iso-ir-109", "iso8859-3", "iso88593", "iso_8859-3", "iso_8859-3:1988", "l3", "latin3"]}, |
michael@0 | 318 | {encoding: "iso-8859-4", labels: ["csisolatin4", "iso-8859-4", "iso-ir-110", "iso8859-4", "iso88594", "iso_8859-4", "iso_8859-4:1988", "l4", "latin4"]}, |
michael@0 | 319 | {encoding: "iso-8859-5", labels: ["csisolatincyrillic", "cyrillic", "iso-8859-5", "iso-ir-144", "iso8859-5", "iso88595", "iso_8859-5", "iso_8859-5:1988"]}, |
michael@0 | 320 | {encoding: "iso-8859-6", labels: ["arabic", "asmo-708", "csiso88596e", "csiso88596i", "csisolatinarabic", "ecma-114", "iso-8859-6", "iso-8859-6-e", "iso-8859-6-i", "iso-ir-127", "iso8859-6", "iso88596", "iso_8859-6", "iso_8859-6:1987"]}, |
michael@0 | 321 | {encoding: "iso-8859-7", labels: ["csisolatingreek", "ecma-118", "elot_928", "greek", "greek8", "iso-8859-7", "iso-ir-126", "iso8859-7", "iso88597", "iso_8859-7", "iso_8859-7:1987", "sun_eu_greek"]}, |
michael@0 | 322 | {encoding: "iso-8859-8", labels: ["csiso88598e", "csisolatinhebrew", "hebrew", "iso-8859-8", "iso-8859-8-e", "iso-ir-138", "iso8859-8", "iso88598", "iso_8859-8", "iso_8859-8:1988", "visual"]}, |
michael@0 | 323 | {encoding: "iso-8859-8-i", labels: ["csiso88598i", "iso-8859-8-i", "logical"]}, |
michael@0 | 324 | {encoding: "iso-8859-10", labels: ["csisolatin6", "iso-8859-10", "iso-ir-157", "iso8859-10", "iso885910", "l6", "latin6"]}, |
michael@0 | 325 | {encoding: "iso-8859-13", labels: ["iso-8859-13", "iso8859-13", "iso885913"]}, |
michael@0 | 326 | {encoding: "iso-8859-14", labels: ["iso-8859-14", "iso8859-14", "iso885914"]}, |
michael@0 | 327 | {encoding: "iso-8859-15", labels: ["csisolatin9", "iso-8859-15", "iso8859-15", "iso885915", "iso_8859-15", "l9"]}, |
michael@0 | 328 | {encoding: "iso-8859-16", labels: ["iso-8859-16"]}, |
michael@0 | 329 | {encoding: "koi8-r", labels: ["cskoi8r", "koi", "koi8", "koi8-r", "koi8_r"]}, |
michael@0 | 330 | {encoding: "koi8-u", labels: ["koi8-u"]}, |
michael@0 | 331 | {encoding: "macintosh", labels: ["csmacintosh", "mac", "macintosh", "x-mac-roman"]}, |
michael@0 | 332 | {encoding: "windows-874", labels: ["dos-874", "iso-8859-11", "iso8859-11", "iso885911", "tis-620", "windows-874"]}, |
michael@0 | 333 | {encoding: "windows-1250", labels: ["cp1250", "windows-1250", "x-cp1250"]}, |
michael@0 | 334 | {encoding: "windows-1251", labels: ["cp1251", "windows-1251", "x-cp1251"]}, |
michael@0 | 335 | {encoding: "windows-1252", labels: ["ansi_x3.4-1968", "ascii", "cp1252", "cp819", "csisolatin1", "ibm819", "iso-8859-1", "iso-ir-100", "iso8859-1", "iso88591", "iso_8859-1", "iso_8859-1:1987", "l1", "latin1", "us-ascii", "windows-1252", "x-cp1252"]}, |
michael@0 | 336 | {encoding: "windows-1253", labels: ["cp1253", "windows-1253", "x-cp1253"]}, |
michael@0 | 337 | {encoding: "windows-1254", labels: ["cp1254", "csisolatin5", "iso-8859-9", "iso-ir-148", "iso8859-9", "iso88599", "iso_8859-9", "iso_8859-9:1989", "l5", "latin5", "windows-1254", "x-cp1254"]}, |
michael@0 | 338 | {encoding: "windows-1255", labels: ["cp1255", "windows-1255", "x-cp1255"]}, |
michael@0 | 339 | {encoding: "windows-1256", labels: ["cp1256", "windows-1256", "x-cp1256"]}, |
michael@0 | 340 | {encoding: "windows-1257", labels: ["cp1257", "windows-1257", "x-cp1257"]}, |
michael@0 | 341 | {encoding: "windows-1258", labels: ["cp1258", "windows-1258", "x-cp1258"]}, |
michael@0 | 342 | {encoding: "x-mac-cyrillic", labels: ["x-mac-cyrillic", "x-mac-ukrainian"]}, |
michael@0 | 343 | {encoding: "gbk", labels: ["chinese", "csgb2312", "csiso58gb231280", "gb2312", "gb_2312", "gb_2312-80", "gbk", "iso-ir-58", "x-gbk"]}, |
michael@0 | 344 | {encoding: "gb18030", labels: ["gb18030"]}, |
michael@0 | 345 | {encoding: "hz-gb-2312", labels: ["hz-gb-2312"]}, |
michael@0 | 346 | {encoding: "big5", labels: ["big5", "cn-big5", "csbig5", "x-x-big5"]}, |
michael@0 | 347 | {encoding: "big5-hkscs", labels: ["big5-hkscs"]}, |
michael@0 | 348 | {encoding: "euc-jp", labels: ["cseucpkdfmtjapanese", "euc-jp", "x-euc-jp"]}, |
michael@0 | 349 | {encoding: "iso-2022-jp", labels: ["csiso2022jp", "iso-2022-jp"]}, |
michael@0 | 350 | {encoding: "shift_jis", labels: ["csshiftjis", "ms_kanji", "shift-jis", "shift_jis", "sjis", "windows-31j", "x-sjis"]}, |
michael@0 | 351 | {encoding: "euc-kr", labels: ["cseuckr", "csksc56011987", "euc-kr", "iso-ir-149", "korean", "ks_c_5601-1987", "ks_c_5601-1989", "ksc5601", "ksc_5601", "windows-949"]}, |
michael@0 | 352 | {encoding: "utf-16le", labels: ["utf-16", "utf-16le"]}, |
michael@0 | 353 | {encoding: "utf-16be", labels: ["utf-16be"]}, |
michael@0 | 354 | {encoding: "x-user-defined", labels: ["x-user-defined"]}, |
michael@0 | 355 | {error: "TypeError", labels: ["x-windows-949", "\u0130SO-8859-1", "csiso2022kr", "iso-2022-kr", "iso-2022-cn", "iso-2022-cn-ext", "replacement"]}, |
michael@0 | 356 | ]; |
michael@0 | 357 | |
michael@0 | 358 | for (var le of labelEncodings) { |
michael@0 | 359 | for (var label of le.labels) { |
michael@0 | 360 | try { |
michael@0 | 361 | var decoder = new TextDecoder(label); |
michael@0 | 362 | } catch (e) { |
michael@0 | 363 | assert_true(!!le.error, label + " shoud not throw " + e.name); |
michael@0 | 364 | assert_equals(e.name, le.error, label + " label encoding unsupported test."); |
michael@0 | 365 | continue; |
michael@0 | 366 | } |
michael@0 | 367 | assert_true(!le.error, label + " shoud throw " + le.error); |
michael@0 | 368 | assert_equals(decoder.encoding, le.encoding, label + " label encoding test."); |
michael@0 | 369 | } |
michael@0 | 370 | } |
michael@0 | 371 | } |
michael@0 | 372 | |
michael@0 | 373 | function testCharset(test) |
michael@0 | 374 | { |
michael@0 | 375 | try { |
michael@0 | 376 | var fatal = test.fatal ? {fatal: test.fatal} : null; |
michael@0 | 377 | var decoder = new TextDecoder(test.encoding, fatal); |
michael@0 | 378 | } catch (e) { |
michael@0 | 379 | assert_equals(e.name, test.error, test.msg + " error thrown from the constructor."); |
michael@0 | 380 | return; |
michael@0 | 381 | } |
michael@0 | 382 | |
michael@0 | 383 | var array = test.array || [test]; |
michael@0 | 384 | var num_strings = array.length; |
michael@0 | 385 | for (var i = 0; i < num_strings; i++) { |
michael@0 | 386 | var decodeView = array[i].input !== null ? new Uint8Array(array[i].input) : null; |
michael@0 | 387 | var stream = array[i].stream ? {stream: array[i].stream} : null; |
michael@0 | 388 | var outText; |
michael@0 | 389 | try { |
michael@0 | 390 | outText = decoder.decode(decodeView, stream); |
michael@0 | 391 | } catch (e) { |
michael@0 | 392 | assert_equals(e.name, array[i].error, test.msg + " error thrown from decode()."); |
michael@0 | 393 | return; |
michael@0 | 394 | } |
michael@0 | 395 | |
michael@0 | 396 | var expected = array[i].expected; |
michael@0 | 397 | if (outText !== expected) { |
michael@0 | 398 | assert_equals(escape(outText), escape(expected), test.msg + " Code points do not match expected code points."); |
michael@0 | 399 | break; |
michael@0 | 400 | } |
michael@0 | 401 | } |
michael@0 | 402 | assert_true(!test.error, test.msg); |
michael@0 | 403 | } |
michael@0 | 404 | |
michael@0 | 405 | function testInvalid2022JP() |
michael@0 | 406 | { |
michael@0 | 407 | var inputs = [ |
michael@0 | 408 | [0x80], |
michael@0 | 409 | [0x1b, 0xFF], |
michael@0 | 410 | [0x1b, 0x28, 0xFF], |
michael@0 | 411 | [0x1b, 0x24, 0x80], |
michael@0 | 412 | [0x1b, 0x24, 0x28, 0x80], |
michael@0 | 413 | [0x1b, 0x28, 0x4a, 0xFF], |
michael@0 | 414 | [0x1b, 0x28, 0x49, 0xFF], |
michael@0 | 415 | [0x1b, 0x24, 0x40, 0x20], |
michael@0 | 416 | [0x1b, 0x24, 0x41, 0x20], |
michael@0 | 417 | [0x1b, 0x24, 0x42, 0x20], |
michael@0 | 418 | [0x1b, 0x24, 0x28, 0x43, 0x20], |
michael@0 | 419 | [0x1b, 0x24, 0x28, 0x44, 0x20], |
michael@0 | 420 | [0x1b, 0x24, 0x40, 0x80, 0x21], |
michael@0 | 421 | [0x1b, 0x24, 0x41, 0xFF, 0x21], |
michael@0 | 422 | [0x1b, 0x24, 0x42, 0x80, 0x21], |
michael@0 | 423 | [0x1b, 0x24, 0x28, 0x43, 0xFF, 0x21], |
michael@0 | 424 | [0x1b, 0x24, 0x28, 0x44, 0x80, 0x21], |
michael@0 | 425 | [0x1b, 0x24, 0x40, 0x21, 0x20], |
michael@0 | 426 | [0x1b, 0x24, 0x41, 0x21, 0x20], |
michael@0 | 427 | [0x1b, 0x24, 0x42, 0x21, 0x20], |
michael@0 | 428 | [0x1b, 0x24, 0x28, 0x43, 0x21, 0x20], |
michael@0 | 429 | [0x1b, 0x24, 0x28, 0x44, 0x21, 0x20], |
michael@0 | 430 | [0x1b, 0x2e, 0xFF], |
michael@0 | 431 | [0x1b, 0x4e, 0x20], |
michael@0 | 432 | [0x1b, 0x4e, 0x7F], |
michael@0 | 433 | [0x1b, 0x2e, 0x41, 0x1b, 0x4e, 0x80], |
michael@0 | 434 | [0x1b, 0x2e, 0x41, 0x1b, 0x4e, 0xFF], |
michael@0 | 435 | ]; |
michael@0 | 436 | |
michael@0 | 437 | var failureCount = 0; |
michael@0 | 438 | inputs.forEach(function(input) { |
michael@0 | 439 | try { |
michael@0 | 440 | // decode() should never throw unless {fatal: true} is specified |
michael@0 | 441 | (new TextDecoder("iso-2022-jp")).decode(new Uint8Array(input)); |
michael@0 | 442 | } catch (e) { |
michael@0 | 443 | if (e.name !== "EncodingError") { |
michael@0 | 444 | throw e; |
michael@0 | 445 | } |
michael@0 | 446 | failureCount++; |
michael@0 | 447 | } |
michael@0 | 448 | }); |
michael@0 | 449 | assert_equals(failureCount, 0, failureCount + " of " + inputs.length + " tests failed"); |
michael@0 | 450 | } |