Skip to content

Commit 0b201b0

Browse files
committed
test(typedarray): string arrays propagrate view
1 parent eee6a0e commit 0b201b0

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

packages/typedarray/__tests__/index.test.ts

+22-11
Original file line numberDiff line numberDiff line change
@@ -278,18 +278,16 @@ describe("UnicodeStringArray", () => {
278278
}).toStrictEqual({
279279
length: 2,
280280
BYTES_PER_ELEMENT: chars * 4,
281-
byteOffset: 1 * chars * 4,
282-
byteLength: 2 * chars * 4,
281+
byteOffset: chars * 4,
282+
byteLength: chars * 4 * 2,
283283
data: ["Hej Världen!", "Xin chào thế giới"],
284284
});
285285
});
286286

287287
test("new (values: Iterable<string>) -> UnicodeStringArray", () => {
288-
let arr = new UnicodeStringArray(20, [
289-
"¡Hola mundo!",
290-
"Hej Världen!",
291-
"Xin chào thế giới",
292-
]);
288+
let chars = 20;
289+
let data = ["¡Hola mundo!", "Hej Världen!", "Xin chào thế giới"];
290+
let arr = new UnicodeStringArray(20, data);
293291
expect({
294292
length: arr.length,
295293
BYTES_PER_ELEMENT: arr.BYTES_PER_ELEMENT,
@@ -300,13 +298,26 @@ describe("UnicodeStringArray", () => {
300298
arr.byteOffset,
301299
arr.byteLength / Int32Array.BYTES_PER_ELEMENT,
302300
),
301+
encoded_sub_view: new Int32Array(
302+
arr.buffer,
303+
arr.byteOffset + arr.BYTES_PER_ELEMENT,
304+
chars,
305+
),
303306
}).toStrictEqual({
304-
length: 3,
305-
BYTES_PER_ELEMENT: 20 * 4,
307+
length: data.length,
308+
BYTES_PER_ELEMENT: chars * Int32Array.BYTES_PER_ELEMENT,
306309
byteOffset: 0,
307-
byteLength: 3 * 20 * 4,
310+
byteLength: data.length * chars * Int32Array.BYTES_PER_ELEMENT,
311+
// deno-fmt-ignore
312+
encoded: new Int32Array([
313+
161, 72, 111, 108, 97, 32, 109, 117, 110, 100, 111, 33, 0, 0, 0, 0, 0, 0, 0, 0,
314+
72, 101, 106, 32, 86, 228, 114, 108, 100, 101, 110, 33, 0, 0, 0, 0, 0, 0, 0, 0,
315+
88, 105, 110, 32, 99, 104, 224, 111, 32, 116, 104, 7871, 32, 103, 105, 7899, 105, 0, 0, 0,
316+
]),
308317
// deno-fmt-ignore
309-
encoded: new Int32Array([161, 72, 111, 108, 97, 32, 109, 117, 110, 100, 111, 33, 0, 0, 0, 0, 0, 0, 0, 0, 72, 101, 106, 32, 86, 228, 114, 108, 100, 101, 110, 33, 0, 0, 0, 0, 0, 0, 0, 0, 88, 105, 110, 32, 99, 104, 224, 111, 32, 116, 104, 7871, 32, 103, 105, 7899, 105, 0, 0, 0]),
318+
encoded_sub_view: new Int32Array([
319+
72, 101, 106, 32, 86, 228, 114, 108, 100, 101, 110, 33, 0, 0, 0, 0, 0, 0, 0, 0,
320+
]),
310321
});
311322
});
312323

packages/typedarray/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -261,17 +261,17 @@ export class UnicodeStringArray {
261261
}
262262

263263
get BYTES_PER_ELEMENT() {
264-
return this.chars * this._data.BYTES_PER_ELEMENT;
265-
}
266-
267-
get byteOffset() {
268-
return this._data.byteOffset;
264+
return this._data.BYTES_PER_ELEMENT * this.chars;
269265
}
270266

271267
get byteLength() {
272268
return this._data.byteLength;
273269
}
274270

271+
get byteOffset() {
272+
return this._data.byteOffset;
273+
}
274+
275275
/** @type {ArrayBuffer} */
276276
get buffer() {
277277
return this._data.buffer;

0 commit comments

Comments
 (0)