1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
/*---
includes: [runTsuite.js, compareArray.js]
flags: [async]
---*/
function p(args, default_opts) {
let params = merge({}, default_opts);
params = merge(params, args);
return params;
}
let stream_tsuite = {
name: "TextDecoder() stream tests",
T: async (params) => {
let td = new TextDecoder('utf-8');
if (td.encoding !== 'utf-8') {
throw Error(`unexpected encoding "${td.encoding}" != "utf-8"`);
}
if (td.fatal !== false) {
throw Error(`unexpected fatal "${td.fatal}" != "false"`);
}
if (td.ignoreBOM !== false) {
throw Error(`unexpected ignoreBOM "${td.ignoreBOM}" != "false"`);
}
let chunks = [];
for (var i = 0; i < params.chunks.length; i++) {
let r = td.decode(params.chunks[i], { stream: (i != params.chunks.length - 1) });
chunks.push(r);
}
if (!compareArray(chunks, params.expected)) {
throw Error(`unexpected output "${chunks.join('|')}" != "${params.expected.join('|')}"`);
}
return 'SUCCESS';
},
tests: [
{ chunks: [new Uint8Array([0xF0, 0x9F, 0x8C, 0x9F])],
expected: ['🌟'] },
// BOM is ignored
{ chunks: [new Uint8Array([0xEF, 0xBB, 0xBF, 0xF0, 0x9F, 0x8C, 0x9F])],
expected: ['🌟'] },
{ chunks: [(new Uint8Array([0xF0, 0x9F, 0x8C, 0x9F])).buffer],
expected: ['🌟'] },
{ chunks: [new Uint32Array((new Uint8Array([0xF0, 0x9F, 0x8C, 0x9F])).buffer)],
expected: ['🌟'] },
{ chunks: [new Uint8Array((new Uint8Array([0x00, 0xF0, 0x9F, 0x8C, 0x9F, 0x00])).buffer, 1, 4)],
expected: ['🌟'] },
{ chunks: [new Uint8Array([0xF0, 0x9F]), new Uint8Array([0x8C, 0x9F])],
expected: ['', '🌟'] },
{ chunks: [new Uint8Array([0xF0, 0xA0]), new Uint8Array([0xAE]), new Uint8Array([0xB7])],
expected: ['', '', 'ð ®·'] },
{ chunks: [new Uint8Array([0xF0, 0xA0]), new Uint8Array([])],
expected: ['', '�'] },
{ chunks: [''],
exception: 'TypeError: TypeError: not a TypedArray' },
],
};
let fatal_tsuite = {
name: "TextDecoder() fatal tests",
T: async (params) => {
let td = new TextDecoder('utf8', {fatal: true, ignoreBOM: true});
if (td.encoding !== 'utf-8') {
throw Error(`unexpected encoding "${td.encoding}" != "utf-8"`);
}
if (td.fatal !== true) {
throw Error(`unexpected fatal "${td.fatal}" != "true"`);
}
if (td.ignoreBOM !== true) {
throw Error(`unexpected ignoreBOM "${td.ignoreBOM}" != "true"`);
}
let chunks = [];
for (var i = 0; i < params.chunks.length; i++) {
let r = td.decode(params.chunks[i]);
chunks.push(r);
}
if (!compareArray(chunks, params.expected)) {
throw Error(`unexpected output "${chunks.join('|')}" != "${params.expected.join('|')}"`);
}
return 'SUCCESS';
},
tests: [
{ chunks: [new Uint8Array([0xF0, 0xA0, 0xAE, 0xB7])],
expected: ['ð ®·'] },
{ chunks: [new Uint8Array([0xF0, 0xA0, 0xAE])],
exception: 'Error: The encoded data was not valid' },
{ chunks: [new Uint8Array([0xF0, 0xA0])],
exception: 'Error: The encoded data was not valid' },
{ chunks: [new Uint8Array([0xF0])],
exception: 'Error: The encoded data was not valid' },
],
};
let ignoreBOM_tsuite = {
name: "TextDecoder() ignoreBOM tests",
T: async (params) => {
let td = new TextDecoder('utf8', params.opts);
let te = new TextEncoder();
let res = te.encode(td.decode(params.value));
if (!compareArray(res, params.expected)) {
throw Error(`unexpected output "${res}" != "${params.expected}"`);
}
return 'SUCCESS';
},
tests: [
{ value: new Uint8Array([239, 187, 191, 50]),
opts: {ignoreBOM: true},
expected: [239, 187, 191, 50] },
{ value: new Uint8Array([239, 187, 191, 50]),
opts: {ignoreBOM: false},
expected: [50] },
{ value: new Uint8Array([239, 187, 191, 50]),
opts: {},
expected: [50] },
],
};
run([
stream_tsuite,
fatal_tsuite,
ignoreBOM_tsuite,
])
.then($DONE, $DONE);
|