From 26f6de376000d0ba41142a88ea04346242b50067 Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Wed, 22 Feb 2023 04:34:25 +0700 Subject: [PATCH] add some tests --- tests/unit-global/web.structured-clone.js | 34 ++++++++++++++++++++++- tests/unit-pure/web.structured-clone.js | 34 ++++++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/tests/unit-global/web.structured-clone.js b/tests/unit-global/web.structured-clone.js index b2fc12fafa26..978349681577 100644 --- a/tests/unit-global/web.structured-clone.js +++ b/tests/unit-global/web.structured-clone.js @@ -1,7 +1,7 @@ // Originally from: https://github.com/web-platform-tests/wpt/blob/4b35e758e2fc4225368304b02bcec9133965fd1a/IndexedDB/structured-clone.any.js // Copyright © web-platform-tests contributors. Available under the 3-Clause BSD License. import { GLOBAL, NODE } from '../helpers/constants'; -import { fromSource } from '../helpers/helpers'; +import { bufferToArray, fromSource } from '../helpers/helpers'; const { from } = Array; const { assign, getPrototypeOf, keys } = Object; @@ -178,6 +178,38 @@ QUnit.module('structuredClone', () => { }); }); } + + if ('resizable' in ArrayBuffer.prototype) { + QUnit.test('Resizable ArrayBuffer', assert => { + const array = [1, 2, 3, 4, 5, 6, 7, 8]; + + let buffer = new ArrayBuffer(8, { maxByteLength: 16 }); + new Int8Array(buffer).set(array); + let copy = structuredClone(buffer); + assert.arrayEqual(bufferToArray(copy), array, 'resizable-ab-1'); + assert.true(copy.resizable, 'resizable-ab-1'); + + buffer = new ArrayBuffer(8); + new Int8Array(buffer).set(array); + copy = structuredClone(buffer); + assert.arrayEqual(bufferToArray(copy), array, 'non-resizable-ab-1'); + assert.false(copy.resizable, 'non-resizable-ab-1'); + + buffer = new ArrayBuffer(8, { maxByteLength: 16 }); + let tarray = new Int8Array(buffer); + tarray.set(array); + copy = structuredClone(tarray).buffer; + assert.arrayEqual(bufferToArray(copy), array, 'resizable-ab-2'); + assert.true(copy.resizable, 'resizable-ab-2'); + + buffer = new ArrayBuffer(8); + tarray = new Int8Array(buffer); + tarray.set(array); + copy = structuredClone(tarray).buffer; + assert.arrayEqual(bufferToArray(copy), array, 'non-resizable-ab-2'); + assert.false(copy.resizable, 'non-resizable-ab-2'); + }); + } } // Map diff --git a/tests/unit-pure/web.structured-clone.js b/tests/unit-pure/web.structured-clone.js index e9dc11c7b70c..36640251670c 100644 --- a/tests/unit-pure/web.structured-clone.js +++ b/tests/unit-pure/web.structured-clone.js @@ -2,7 +2,7 @@ // Copyright © web-platform-tests contributors. Available under the 3-Clause BSD License. /* eslint-disable es/no-error-cause, es/no-typed-arrays -- safe */ import { GLOBAL, NODE } from '../helpers/constants'; -import { fromSource } from '../helpers/helpers'; +import { bufferToArray, fromSource } from '../helpers/helpers'; import structuredClone from 'core-js-pure/stable/structured-clone'; import from from 'core-js-pure/es/array/from'; @@ -186,6 +186,38 @@ QUnit.module('structuredClone', () => { }); }); } + + if ('resizable' in ArrayBuffer.prototype) { + QUnit.test('Resizable ArrayBuffer', assert => { + const array = [1, 2, 3, 4, 5, 6, 7, 8]; + + let buffer = new ArrayBuffer(8, { maxByteLength: 16 }); + new Int8Array(buffer).set(array); + let copy = structuredClone(buffer); + assert.arrayEqual(bufferToArray(copy), array, 'resizable-ab-1'); + assert.true(copy.resizable, 'resizable-ab-1'); + + buffer = new ArrayBuffer(8); + new Int8Array(buffer).set(array); + copy = structuredClone(buffer); + assert.arrayEqual(bufferToArray(copy), array, 'non-resizable-ab-1'); + assert.false(copy.resizable, 'non-resizable-ab-1'); + + buffer = new ArrayBuffer(8, { maxByteLength: 16 }); + let tarray = new Int8Array(buffer); + tarray.set(array); + copy = structuredClone(tarray).buffer; + assert.arrayEqual(bufferToArray(copy), array, 'resizable-ab-2'); + assert.true(copy.resizable, 'resizable-ab-2'); + + buffer = new ArrayBuffer(8); + tarray = new Int8Array(buffer); + tarray.set(array); + copy = structuredClone(tarray).buffer; + assert.arrayEqual(bufferToArray(copy), array, 'non-resizable-ab-2'); + assert.false(copy.resizable, 'non-resizable-ab-2'); + }); + } } // Map