Skip to content

Commit

Permalink
TextEncoder/TextDecoder SharedArrayBuffer tests
Browse files Browse the repository at this point in the history
Add tests for TextEncoder/TextDecoder with SharedArrayBuffer
  • Loading branch information
Bnaya committed Oct 7, 2019
1 parent e0289a9 commit 1df43ba
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 101 deletions.
150 changes: 82 additions & 68 deletions encoding/encodeInto.any.js
Expand Up @@ -73,77 +73,91 @@
"destinationOffset": 4,
"filler": "random"
}
].forEach(destinationData => {
test(() => {
// Setup
const bufferLength = testData.destinationLength + destinationData.bufferIncrease,
destinationOffset = destinationData.destinationOffset,
destinationLength = testData.destinationLength,
destinationFiller = destinationData.filler,
encoder = new TextEncoder(),
buffer = new ArrayBuffer(bufferLength),
view = new Uint8Array(buffer, destinationOffset, destinationLength),
fullView = new Uint8Array(buffer),
control = new Array(bufferLength);
let byte = destinationFiller;
for (let i = 0; i < bufferLength; i++) {
if (destinationFiller === "random") {
byte = Math.floor(Math.random() * 256);
].forEach((destinationData) => {
[ArrayBuffer, self["SharedArrayBuffer"]].forEach((ArrayBufferOrSharedArrayBuffer) => {
test(() => {
// Setup
const bufferLength = testData.destinationLength + destinationData.bufferIncrease,
destinationOffset = destinationData.destinationOffset,
destinationLength = testData.destinationLength,
destinationFiller = destinationData.filler,
encoder = new TextEncoder(),
buffer = new ArrayBufferOrSharedArrayBuffer(bufferLength),
view = new Uint8Array(buffer, destinationOffset, destinationLength),
fullView = new Uint8Array(buffer),
control = new Array(bufferLength);
let byte = destinationFiller;
for (let i = 0; i < bufferLength; i++) {
if (destinationFiller === "random") {
byte = Math.floor(Math.random() * 256);
}
control[i] = byte;
fullView[i] = byte;
}
control[i] = byte;
fullView[i] = byte;
}

// It's happening
const result = encoder.encodeInto(testData.input, view);

// Basics
assert_equals(view.byteLength, destinationLength);
assert_equals(view.length, destinationLength);

// Remainder
assert_equals(result.read, testData.read);
assert_equals(result.written, testData.written.length);
for (let i = 0; i < bufferLength; i++) {
if (i < destinationOffset || i >= (destinationOffset + testData.written.length)) {
assert_equals(fullView[i], control[i]);
} else {
assert_equals(fullView[i], testData.written[i - destinationOffset]);

// It's happening
const result = encoder.encodeInto(testData.input, view);

// Basics
assert_equals(view.byteLength, destinationLength);
assert_equals(view.length, destinationLength);

// Remainder
assert_equals(result.read, testData.read);
assert_equals(result.written, testData.written.length);
for (let i = 0; i < bufferLength; i++) {
if (i < destinationOffset || i >= (destinationOffset + testData.written.length)) {
assert_equals(fullView[i], control[i]);
} else {
assert_equals(fullView[i], testData.written[i - destinationOffset]);
}
}
}
}, "encodeInto() with " + testData.input + " and destination length " + testData.destinationLength + ", offset " + destinationData.destinationOffset + ", filler " + destinationData.filler);
}, "encodeInto() into " + ArrayBufferOrSharedArrayBuffer.name + " with " + testData.input + " and destination length " + testData.destinationLength + ", offset " + destinationData.destinationOffset + ", filler " + destinationData.filler);
})
});
});

[DataView,
Int8Array,
Int16Array,
Int32Array,
Uint16Array,
Uint32Array,
Uint8ClampedArray,
Float32Array,
Float64Array].forEach(view => {
test(() => {
assert_throws(new TypeError(), () => new TextEncoder().encodeInto("", new view(new ArrayBuffer(0))));
}, "Invalid encodeInto() destination: " + view.name);
});

test(() => {
assert_throws(new TypeError(), () => new TextEncoder().encodeInto("", new ArrayBuffer(10)));
}, "Invalid encodeInto() destination: ArrayBuffer");
[ArrayBuffer, self["SharedArrayBuffer"]].forEach((ArrayBufferOrSharedArrayBuffer) => {
[DataView,
Int8Array,
Int16Array,
Int32Array,
Uint16Array,
Uint32Array,
Uint8ClampedArray,
Float32Array,
Float64Array].forEach(view => {
test(() => {
assert_throws(new TypeError(), () => new TextEncoder().encodeInto("", new view(new ArrayBufferOrSharedArrayBuffer(0))));
}, "Invalid encodeInto() destination: " + view.name + ", backed by: " + ArrayBufferOrSharedArrayBuffer.name);
});

test(() => {
assert_throws(new TypeError(), () => new TextEncoder().encodeInto("", new ArrayBufferOrSharedArrayBuffer(10)));
}, "Invalid encodeInto() destination: " + ArrayBufferOrSharedArrayBuffer.name);
});

test(() => {
const buffer = new ArrayBuffer(10),
view = new Uint8Array(buffer);
let { read, written } = new TextEncoder().encodeInto("", view);
assert_equals(read, 0);
assert_equals(written, 0);
new MessageChannel().port1.postMessage(buffer, [buffer]);
({ read, written } = new TextEncoder().encodeInto("", view));
assert_equals(read, 0);
assert_equals(written, 0);
({ read, written } = new TextEncoder().encodeInto("test", view));
assert_equals(read, 0);
assert_equals(written, 0);
}, "encodeInto() and a detached output buffer");
[DataView,
Int8Array,
Int16Array,
Int32Array,
Uint16Array,
Uint32Array,
Uint8ClampedArray,
Float32Array,
Float64Array].forEach(view => {
test(() => {
const buffer = new ArrayBuffer(10),
view = new Uint8Array(buffer);
let { read, written } = new TextEncoder().encodeInto("", view);
assert_equals(read, 0);
assert_equals(written, 0);
new MessageChannel().port1.postMessage(buffer, [buffer]);
({ read, written } = new TextEncoder().encodeInto("", view));
assert_equals(read, 0);
assert_equals(written, 0);
({ read, written } = new TextEncoder().encodeInto("test", view));
assert_equals(read, 0);
assert_equals(written, 0);
}, "encodeInto() and a detached output buffer");
});
32 changes: 17 additions & 15 deletions encoding/textdecoder-copy.any.js
@@ -1,15 +1,17 @@
test(() => {
const buf = new ArrayBuffer(2),
view = new Uint8Array(buf),
buf2 = new ArrayBuffer(2),
view2 = new Uint8Array(buf2),
decoder = new TextDecoder("utf-8")
view[0] = 0xEF
view[1] = 0xBB
view2[0] = 0xBF
view2[1] = 0x40
assert_equals(decoder.decode(buf, {stream:true}), "")
view[0] = 0x01
view[1] = 0x02
assert_equals(decoder.decode(buf2), "@")
}, "Modify buffer after passing it in")
[ArrayBuffer, self["SharedArrayBuffer"]].forEach((ArrayBufferOrSharedArrayBuffer) => {
test(() => {
const buf = new ArrayBufferOrSharedArrayBuffer(2),
view = new Uint8Array(buf),
buf2 = new ArrayBufferOrSharedArrayBuffer(2),
view2 = new Uint8Array(buf2),
decoder = new TextDecoder("utf-8")
view[0] = 0xEF
view[1] = 0xBB
view2[0] = 0xBF
view2[1] = 0x40
assert_equals(decoder.decode(buf, {stream:true}), "")
view[0] = 0x01
view[1] = 0x02
assert_equals(decoder.decode(buf2), "@")
}, "Modify buffer after passing it in (" + ArrayBufferOrSharedArrayBuffer.name + ")")
});
40 changes: 22 additions & 18 deletions encoding/textdecoder-streaming.any.js
Expand Up @@ -16,21 +16,25 @@ var octets = {
0xDF,0xFF]
};

Object.keys(octets).forEach(function(encoding) {
for (var len = 1; len <= 5; ++len) {
test(function() {
var encoded = octets[encoding];

var out = '';
var decoder = new TextDecoder(encoding);
for (var i = 0; i < encoded.length; i += len) {
var sub = [];
for (var j = i; j < encoded.length && j < i + len; ++j)
sub.push(encoded[j]);
out += decoder.decode(new Uint8Array(sub), {stream: true});
}
out += decoder.decode();
assert_equals(out, string);
}, 'Streaming decode: ' + encoding + ', ' + len + ' byte window');
}
});
[ArrayBuffer, self["SharedArrayBuffer"]].forEach((ArrayBufferOrSharedArrayBuffer) => {
Object.keys(octets).forEach(function(encoding) {
for (var len = 1; len <= 5; ++len) {
test(function() {
var encoded = octets[encoding];

var out = '';
var decoder = new TextDecoder(encoding);
for (var i = 0; i < encoded.length; i += len) {
var sub = [];
for (var j = i; j < encoded.length && j < i + len; ++j)
sub.push(encoded[j]);
var uintArray = new Uint8Array(new ArrayBufferOrSharedArrayBuffer(sub.length));
uintArray.set(sub);
out += decoder.decode(uintArray, {stream: true});
}
out += decoder.decode();
assert_equals(out, string);
}, 'Streaming decode: ' + encoding + ', ' + len + ' byte window (' + ArrayBufferOrSharedArrayBuffer.name + ')');
}
});
})

0 comments on commit 1df43ba

Please sign in to comment.