1 // META: global=window,worker
2 // META: script=/common/sab.js
8 "destinationLength": 0,
14 "destinationLength": 10,
18 "input": "\u{1D306}", // "\uD834\uDF06"
20 "destinationLength": 4,
21 "written": [0xF0, 0x9D, 0x8C, 0x86]
24 "input": "\u{1D306}A",
26 "destinationLength": 3,
30 "input": "\uD834A\uDF06A¥Hi",
32 "destinationLength": 10,
33 "written": [0xEF, 0xBF, 0xBD, 0x41, 0xEF, 0xBF, 0xBD, 0x41, 0xC2, 0xA5]
38 "destinationLength": 4,
39 "written": [0x41, 0xEF, 0xBF, 0xBD]
44 "destinationLength": 4,
45 "written": [0xC2, 0xA5, 0xC2, 0xA5]
47 ].forEach(testData => {
51 "destinationOffset": 0,
56 "destinationOffset": 4,
61 "destinationOffset": 0,
66 "destinationOffset": 4,
71 "destinationOffset": 0,
76 "destinationOffset": 4,
79 ].forEach(destinationData => {
80 ["ArrayBuffer", "SharedArrayBuffer"].forEach(arrayBufferOrSharedArrayBuffer => {
83 const bufferLength = testData.destinationLength + destinationData.bufferIncrease;
84 const destinationOffset = destinationData.destinationOffset;
85 const destinationLength = testData.destinationLength;
86 const destinationFiller = destinationData.filler;
87 const encoder = new TextEncoder();
88 const buffer = createBuffer(arrayBufferOrSharedArrayBuffer, bufferLength);
89 const view = new Uint8Array(buffer, destinationOffset, destinationLength);
90 const fullView = new Uint8Array(buffer);
91 const control = new Array(bufferLength);
92 let byte = destinationFiller;
93 for (let i = 0; i < bufferLength; i++) {
94 if (destinationFiller === "random") {
95 byte = Math.floor(Math.random() * 256);
102 const result = encoder.encodeInto(testData.input, view);
105 assert_equals(view.byteLength, destinationLength);
106 assert_equals(view.length, destinationLength);
109 assert_equals(result.read, testData.read);
110 assert_equals(result.written, testData.written.length);
111 for (let i = 0; i < bufferLength; i++) {
112 if (i < destinationOffset || i >= (destinationOffset + testData.written.length)) {
113 assert_equals(fullView[i], control[i]);
115 assert_equals(fullView[i], testData.written[i - destinationOffset]);
118 }, "encodeInto() into " + arrayBufferOrSharedArrayBuffer + " with " + testData.input + " and destination length " + testData.destinationLength + ", offset " + destinationData.destinationOffset + ", filler " + destinationData.filler);
133 "Float64Array"].forEach(type => {
134 ["ArrayBuffer", "SharedArrayBuffer"].forEach((arrayBufferOrSharedArrayBuffer) => {
136 const viewInstance = new self[type](createBuffer(arrayBufferOrSharedArrayBuffer, 0));
137 assert_throws_js(TypeError, () => new TextEncoder().encodeInto("", viewInstance));
138 }, "Invalid encodeInto() destination: " + type + ", backed by: " + arrayBufferOrSharedArrayBuffer);
142 ["ArrayBuffer", "SharedArrayBuffer"].forEach((arrayBufferOrSharedArrayBuffer) => {
144 assert_throws_js(TypeError, () => new TextEncoder().encodeInto("", createBuffer(arrayBufferOrSharedArrayBuffer, 10)));
145 }, "Invalid encodeInto() destination: " + arrayBufferOrSharedArrayBuffer);
149 const buffer = new ArrayBuffer(10),
150 view = new Uint8Array(buffer);
151 let { read, written } = new TextEncoder().encodeInto("", view);
152 assert_equals(read, 0);
153 assert_equals(written, 0);
154 new MessageChannel().port1.postMessage(buffer, [buffer]);
155 ({ read, written } = new TextEncoder().encodeInto("", view));
156 assert_equals(read, 0);
157 assert_equals(written, 0);
158 ({ read, written } = new TextEncoder().encodeInto("test", view));
159 assert_equals(read, 0);
160 assert_equals(written, 0);
161 }, "encodeInto() and a detached output buffer");