Skip to content

Commit a910ad1

Browse files
Bnayaannevk
authored andcommitted
Encoding: TextEncoder/TextDecoder SharedArrayBuffer tests
For whatwg/encoding#182.
1 parent 2d84e4d commit a910ad1

File tree

5 files changed

+123
-116
lines changed

5 files changed

+123
-116
lines changed

encoding/encodeInto.any.js

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -74,44 +74,46 @@
7474
"filler": "random"
7575
}
7676
].forEach(destinationData => {
77-
test(() => {
78-
// Setup
79-
const bufferLength = testData.destinationLength + destinationData.bufferIncrease,
80-
destinationOffset = destinationData.destinationOffset,
81-
destinationLength = testData.destinationLength,
82-
destinationFiller = destinationData.filler,
83-
encoder = new TextEncoder(),
84-
buffer = new ArrayBuffer(bufferLength),
85-
view = new Uint8Array(buffer, destinationOffset, destinationLength),
86-
fullView = new Uint8Array(buffer),
87-
control = new Array(bufferLength);
88-
let byte = destinationFiller;
89-
for (let i = 0; i < bufferLength; i++) {
90-
if (destinationFiller === "random") {
91-
byte = Math.floor(Math.random() * 256);
77+
["ArrayBuffer", "SharedArrayBuffer"].forEach(arrayBufferOrSharedArrayBuffer => {
78+
test(() => {
79+
// Setup
80+
const bufferLength = testData.destinationLength + destinationData.bufferIncrease,
81+
destinationOffset = destinationData.destinationOffset,
82+
destinationLength = testData.destinationLength,
83+
destinationFiller = destinationData.filler,
84+
encoder = new TextEncoder(),
85+
buffer = new self[arrayBufferOrSharedArrayBuffer](bufferLength),
86+
view = new Uint8Array(buffer, destinationOffset, destinationLength),
87+
fullView = new Uint8Array(buffer),
88+
control = new Array(bufferLength);
89+
let byte = destinationFiller;
90+
for (let i = 0; i < bufferLength; i++) {
91+
if (destinationFiller === "random") {
92+
byte = Math.floor(Math.random() * 256);
93+
}
94+
control[i] = byte;
95+
fullView[i] = byte;
9296
}
93-
control[i] = byte;
94-
fullView[i] = byte;
95-
}
9697

97-
// It's happening
98-
const result = encoder.encodeInto(testData.input, view);
98+
// It's happening
99+
const result = encoder.encodeInto(testData.input, view);
99100

100-
// Basics
101-
assert_equals(view.byteLength, destinationLength);
102-
assert_equals(view.length, destinationLength);
101+
// Basics
102+
assert_equals(view.byteLength, destinationLength);
103+
assert_equals(view.length, destinationLength);
103104

104-
// Remainder
105-
assert_equals(result.read, testData.read);
106-
assert_equals(result.written, testData.written.length);
107-
for (let i = 0; i < bufferLength; i++) {
108-
if (i < destinationOffset || i >= (destinationOffset + testData.written.length)) {
109-
assert_equals(fullView[i], control[i]);
110-
} else {
111-
assert_equals(fullView[i], testData.written[i - destinationOffset]);
105+
// Remainder
106+
assert_equals(result.read, testData.read);
107+
assert_equals(result.written, testData.written.length);
108+
for (let i = 0; i < bufferLength; i++) {
109+
if (i < destinationOffset || i >= (destinationOffset + testData.written.length)) {
110+
assert_equals(fullView[i], control[i]);
111+
} else {
112+
assert_equals(fullView[i], testData.written[i - destinationOffset]);
113+
}
112114
}
113-
}
114-
}, "encodeInto() with " + testData.input + " and destination length " + testData.destinationLength + ", offset " + destinationData.destinationOffset + ", filler " + destinationData.filler);
115+
}, "encodeInto() into " + arrayBufferOrSharedArrayBuffer + " with " + testData.input + " and destination length " + testData.destinationLength + ", offset " + destinationData.destinationOffset + ", filler " + destinationData.filler);
116+
})
115117
});
116118
});
117119

@@ -124,14 +126,20 @@
124126
Uint8ClampedArray,
125127
Float32Array,
126128
Float64Array].forEach(view => {
129+
["ArrayBuffer", "SharedArrayBuffer"].forEach((arrayBufferOrSharedArrayBuffer) => {
130+
test(() => {
131+
assert_throws(new TypeError(), () => new TextEncoder().encodeInto("", new view(new self[arrayBufferOrSharedArrayBuffer](0))));
132+
}, "Invalid encodeInto() destination: " + view.name + ", backed by: " + arrayBufferOrSharedArrayBuffer);
133+
});
134+
});
135+
136+
["ArrayBuffer", "SharedArrayBuffer"].forEach((arrayBufferOrSharedArrayBuffer) => {
127137
test(() => {
128-
assert_throws(new TypeError(), () => new TextEncoder().encodeInto("", new view(new ArrayBuffer(0))));
129-
}, "Invalid encodeInto() destination: " + view.name);
130-
});
138+
assert_throws(new TypeError(), () => new TextEncoder().encodeInto("", new self[arrayBufferOrSharedArrayBuffer](10)));
139+
}, "Invalid encodeInto() destination: " + arrayBufferOrSharedArrayBuffer);
140+
});
141+
131142

132-
test(() => {
133-
assert_throws(new TypeError(), () => new TextEncoder().encodeInto("", new ArrayBuffer(10)));
134-
}, "Invalid encodeInto() destination: ArrayBuffer");
135143

136144
test(() => {
137145
const buffer = new ArrayBuffer(10),

encoding/streams/decode-bad-chunks.any.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,6 @@ const badChunks = [
2222
{
2323
name: 'array',
2424
value: [65]
25-
},
26-
{
27-
name: 'SharedArrayBuffer',
28-
// Use a getter to postpone construction so that all tests don't fail where
29-
// SharedArrayBuffer is not yet implemented.
30-
get value() {
31-
return new SharedArrayBuffer();
32-
}
33-
},
34-
{
35-
name: 'shared Uint8Array',
36-
get value() {
37-
new Uint8Array(new SharedArrayBuffer())
38-
}
3925
}
4026
];
4127

encoding/streams/decode-utf8.any.js

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,48 @@
44

55
'use strict';
66

7-
const emptyChunk = new Uint8Array([]);
8-
const inputChunk = new Uint8Array([73, 32, 240, 159, 146, 153, 32, 115, 116,
9-
114, 101, 97, 109, 115]);
10-
const expectedOutputString = 'I \u{1F499} streams';
7+
["ArrayBuffer", "SharedArrayBuffer"].forEach((arrayBufferOrSharedArrayBuffer) => {
8+
const inputChunkData = [73, 32, 240, 159, 146, 153, 32, 115, 116,
9+
114, 101, 97, 109, 115]
1110

12-
promise_test(async () => {
13-
const input = readableStreamFromArray([inputChunk]);
14-
const output = input.pipeThrough(new TextDecoderStream());
15-
const array = await readableStreamToArray(output);
16-
assert_array_equals(array, [expectedOutputString],
17-
'the output should be in one chunk');
18-
}, 'decoding one UTF-8 chunk should give one output string');
11+
const emptyChunk = new Uint8Array(new self[arrayBufferOrSharedArrayBuffer](0));
12+
const inputChunk = new Uint8Array(new self[arrayBufferOrSharedArrayBuffer](inputChunkData.length));
1913

20-
promise_test(async () => {
21-
const input = readableStreamFromArray([emptyChunk]);
22-
const output = input.pipeThrough(new TextDecoderStream());
23-
const array = await readableStreamToArray(output);
24-
assert_array_equals(array, [], 'no chunks should be output');
25-
}, 'decoding an empty chunk should give no output chunks');
14+
inputChunk.set(inputChunkData);
2615

27-
promise_test(async () => {
28-
const input = readableStreamFromArray([emptyChunk, inputChunk]);
29-
const output = input.pipeThrough(new TextDecoderStream());
30-
const array = await readableStreamToArray(output);
31-
assert_array_equals(array, [expectedOutputString],
32-
'the output should be in one chunk');
33-
}, 'an initial empty chunk should be ignored');
16+
const expectedOutputString = 'I \u{1F499} streams';
3417

35-
promise_test(async () => {
36-
const input = readableStreamFromArray([inputChunk, emptyChunk]);
37-
const output = input.pipeThrough(new TextDecoderStream());
38-
const array = await readableStreamToArray(output);
39-
assert_array_equals(array, [expectedOutputString],
40-
'the output should be in one chunk');
41-
}, 'a trailing empty chunk should be ignored');
18+
promise_test(async () => {
19+
const input = readableStreamFromArray([inputChunk]);
20+
const output = input.pipeThrough(new TextDecoderStream());
21+
const array = await readableStreamToArray(output);
22+
assert_array_equals(array, [expectedOutputString],
23+
'the output should be in one chunk');
24+
}, 'decoding one UTF-8 chunk should give one output string - ' + arrayBufferOrSharedArrayBuffer);
25+
26+
promise_test(async () => {
27+
const input = readableStreamFromArray([emptyChunk]);
28+
const output = input.pipeThrough(new TextDecoderStream());
29+
const array = await readableStreamToArray(output);
30+
assert_array_equals(array, [], 'no chunks should be output');
31+
}, 'decoding an empty chunk should give no output chunks - ' + arrayBufferOrSharedArrayBuffer);
32+
33+
promise_test(async () => {
34+
const input = readableStreamFromArray([emptyChunk, inputChunk]);
35+
const output = input.pipeThrough(new TextDecoderStream());
36+
const array = await readableStreamToArray(output);
37+
assert_array_equals(array, [expectedOutputString],
38+
'the output should be in one chunk');
39+
}, 'an initial empty chunk should be ignored - ' + arrayBufferOrSharedArrayBuffer);
40+
41+
promise_test(async () => {
42+
const input = readableStreamFromArray([inputChunk, emptyChunk]);
43+
const output = input.pipeThrough(new TextDecoderStream());
44+
const array = await readableStreamToArray(output);
45+
assert_array_equals(array, [expectedOutputString],
46+
'the output should be in one chunk');
47+
}, 'a trailing empty chunk should be ignored- ' + arrayBufferOrSharedArrayBuffer);
48+
});
4249

4350
promise_test(async () => {
4451
const buffer = new ArrayBuffer(3);

encoding/textdecoder-copy.any.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
test(() => {
2-
const buf = new ArrayBuffer(2),
3-
view = new Uint8Array(buf),
4-
buf2 = new ArrayBuffer(2),
5-
view2 = new Uint8Array(buf2),
6-
decoder = new TextDecoder("utf-8")
7-
view[0] = 0xEF
8-
view[1] = 0xBB
9-
view2[0] = 0xBF
10-
view2[1] = 0x40
11-
assert_equals(decoder.decode(buf, {stream:true}), "")
12-
view[0] = 0x01
13-
view[1] = 0x02
14-
assert_equals(decoder.decode(buf2), "@")
15-
}, "Modify buffer after passing it in")
1+
["ArrayBuffer", "SharedArrayBuffer"].forEach(arrayBufferOrSharedArrayBuffer => {
2+
test(() => {
3+
const buf = new self[arrayBufferOrSharedArrayBuffer](2),
4+
view = new Uint8Array(buf),
5+
buf2 = new self[arrayBufferOrSharedArrayBuffer](2),
6+
view2 = new Uint8Array(buf2),
7+
decoder = new TextDecoder("utf-8");
8+
view[0] = 0xEF;
9+
view[1] = 0xBB;
10+
view2[0] = 0xBF;
11+
view2[1] = 0x40;
12+
assert_equals(decoder.decode(buf, {stream:true}), "");
13+
view[0] = 0x01;
14+
view[1] = 0x02;
15+
assert_equals(decoder.decode(buf2), "@");
16+
}, "Modify buffer after passing it in (" + arrayBufferOrSharedArrayBuffer + ")");
17+
});

encoding/textdecoder-streaming.any.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,25 @@ var octets = {
1616
0xDF,0xFF]
1717
};
1818

19-
Object.keys(octets).forEach(function(encoding) {
20-
for (var len = 1; len <= 5; ++len) {
21-
test(function() {
22-
var encoded = octets[encoding];
19+
["ArrayBuffer", "SharedArrayBuffer"].forEach((arrayBufferOrSharedArrayBuffer) => {
20+
Object.keys(octets).forEach(function(encoding) {
21+
for (var len = 1; len <= 5; ++len) {
22+
test(function() {
23+
var encoded = octets[encoding];
2324

24-
var out = '';
25-
var decoder = new TextDecoder(encoding);
26-
for (var i = 0; i < encoded.length; i += len) {
27-
var sub = [];
28-
for (var j = i; j < encoded.length && j < i + len; ++j)
29-
sub.push(encoded[j]);
30-
out += decoder.decode(new Uint8Array(sub), {stream: true});
31-
}
32-
out += decoder.decode();
33-
assert_equals(out, string);
34-
}, 'Streaming decode: ' + encoding + ', ' + len + ' byte window');
35-
}
36-
});
25+
var out = '';
26+
var decoder = new TextDecoder(encoding);
27+
for (var i = 0; i < encoded.length; i += len) {
28+
var sub = [];
29+
for (var j = i; j < encoded.length && j < i + len; ++j)
30+
sub.push(encoded[j]);
31+
var uintArray = new Uint8Array(new self[arrayBufferOrSharedArrayBuffer](sub.length));
32+
uintArray.set(sub);
33+
out += decoder.decode(uintArray, {stream: true});
34+
}
35+
out += decoder.decode();
36+
assert_equals(out, string);
37+
}, 'Streaming decode: ' + encoding + ', ' + len + ' byte window (' + arrayBufferOrSharedArrayBuffer + ')');
38+
}
39+
});
40+
})

0 commit comments

Comments
 (0)