Add at changes

This commit is contained in:
Sarah GHP 2022-06-24 13:46:34 +02:00
parent 75a3be5403
commit 71ed19cefe
7 changed files with 173 additions and 54 deletions

View File

@ -125,7 +125,7 @@ function testTypedArrayConversions(byteConversionValues, fn) {
function createTypedArrayVariations(TA, values) { function createTypedArrayVariations(TA, values) {
const rab = () => { const rab = () => {
let buffer = new ArrayBuffer(4 * TA.BYTES_PER_ELEMENT, { maxByteLength: 8 * TA.BYTES_PER_ELEMENT }); let buffer = new ArrayBuffer(values.length * TA.BYTES_PER_ELEMENT, { maxByteLength: values.length * 2 * TA.BYTES_PER_ELEMENT });
let ta_write = new TA(buffer); let ta_write = new TA(buffer);
for (let i = 0; i < values.length; ++i) { for (let i = 0; i < values.length; ++i) {
@ -152,7 +152,9 @@ function createTypedArrayVariations(TA, values) {
const fixedLengthWithOffset = { const fixedLengthWithOffset = {
name: 'fixed length with offset', name: 'fixed length with offset',
contents: new TA(rab(), 2 * TA.BYTES_PER_ELEMENT, (values.length / 2)) // Using Math.ceil ensures both offset buffers are the same length when
// values.length is odd
contents: new TA(rab(), 2 * TA.BYTES_PER_ELEMENT, Math.ceil(values.length / 2))
}; };
const lengthTrackingWithOffset = { const lengthTrackingWithOffset = {

View File

@ -20,6 +20,21 @@ assert.sameValue(
testWithTypedArrayConstructors(TA => { testWithTypedArrayConstructors(TA => {
assert.sameValue(typeof TA.prototype.at, 'function', 'The value of `typeof TA.prototype.at` is "function"'); assert.sameValue(typeof TA.prototype.at, 'function', 'The value of `typeof TA.prototype.at` is "function"');
const {
nonresizable,
fixedLength,
lengthTracking,
fixedLengthWithOffset,
lengthTrackingWithOffset
} = createTypedArrayVariations(TA, [0, 1, 2, 3]);
[
nonresizable,
fixedLength,
lengthTracking,
].forEach(({ name, contents }) => {
let valueOfCallCount = 0; let valueOfCallCount = 0;
let index = { let index = {
valueOf() { valueOf() {
@ -28,8 +43,24 @@ testWithTypedArrayConstructors(TA => {
} }
}; };
let a = new TA([0,1,2,3]); assert.sameValue(contents.at(index), 1, `contents.at({valueOf() {valueOfCallCount++; return 1;}}) must return 1 in ${name}`);
assert.sameValue(valueOfCallCount, 1, `The value of 'alueOfCallCount' is 1 in ${name}`);
assert.sameValue(a.at(index), 1, 'a.at({valueOf() {valueOfCallCount++; return 1;}}) must return 1'); });
assert.sameValue(valueOfCallCount, 1, 'The value of `valueOfCallCount` is 1');
[
fixedLengthWithOffset,
lengthTrackingWithOffset
].forEach(({ name, contents }) => {
let valueOfCallCount = 0;
let index = {
valueOf() {
valueOfCallCount++;
return 1;
}
};
assert.sameValue(contents.at(index), 3, `contents.at({valueOf() {valueOfCallCount++; return 1;}}) must return 1 in ${name}`);
assert.sameValue(valueOfCallCount, 1, `The value of 'valueOfCallCount' is 1 in ${name}`);
});
}); });

View File

@ -20,9 +20,25 @@ assert.sameValue(
testWithTypedArrayConstructors(TA => { testWithTypedArrayConstructors(TA => {
assert.sameValue(typeof TA.prototype.at, 'function', 'The value of `typeof TA.prototype.at` is "function"'); assert.sameValue(typeof TA.prototype.at, 'function', 'The value of `typeof TA.prototype.at` is "function"');
let a = new TA([0,1,2,3]);
const {
nonresizable,
fixedLength,
lengthTracking,
fixedLengthWithOffset,
lengthTrackingWithOffset
} = createTypedArrayVariations(TA, [0, 1, 2, 3]);
[
nonresizable,
fixedLength,
lengthTracking,
fixedLengthWithOffset,
lengthTrackingWithOffset
].forEach(({ name, contents }) => {
assert.throws(TypeError, () => { assert.throws(TypeError, () => {
a.at(Symbol()); contents.at(Symbol());
}, '`a.at(Symbol())` throws TypeError'); }, `'contents.at(Symbol())' throws TypeError in ${name}`);
});
}); });

View File

@ -21,15 +21,31 @@ assert.sameValue(
testWithTypedArrayConstructors(TA => { testWithTypedArrayConstructors(TA => {
assert.sameValue(typeof TA.prototype.at, 'function', 'The value of `typeof TA.prototype.at` is "function"'); assert.sameValue(typeof TA.prototype.at, 'function', 'The value of `typeof TA.prototype.at` is "function"');
let a = new TA([0,1,2,3]); const {
nonresizable,
fixedLength,
lengthTracking,
fixedLengthWithOffset,
lengthTrackingWithOffset
} = createTypedArrayVariations(TA, [0, 1, 2, 3]);
[
nonresizable,
fixedLength,
lengthTracking,
fixedLengthWithOffset,
lengthTrackingWithOffset
].forEach(({ name, contents }) => {
assert.sameValue(contents.at(false), 0, `contents.at(false) must return 0 in ${name}`);
assert.sameValue(contents.at(null), 0, `contents.at(null) must return 0 in ${name}`);
assert.sameValue(contents.at(undefined), 0, `contents.at(undefined) must return 0 in ${name}`);
assert.sameValue(contents.at(""), 0, `contents.at("") must return 0 in ${name}`);
assert.sameValue(contents.at(function() {}), 0, `contents.at(function() {}) must return 0 in ${name}`);
assert.sameValue(contents.at([]), 0, `contents.at([]) must return 0 in ${name}`);
assert.sameValue(contents.at(true), 1, `contents.at(true) must return 1 in ${name}`);
assert.sameValue(contents.at("1"), 1, `contents.at("1") must return 1 in ${name}`);
});
assert.sameValue(a.at(false), 0, 'a.at(false) must return 0');
assert.sameValue(a.at(null), 0, 'a.at(null) must return 0');
assert.sameValue(a.at(undefined), 0, 'a.at(undefined) must return 0');
assert.sameValue(a.at(""), 0, 'a.at("") must return 0');
assert.sameValue(a.at(function() {}), 0, 'a.at(function() {}) must return 0');
assert.sameValue(a.at([]), 0, 'a.at([]) must return 0');
assert.sameValue(a.at(true), 1, 'a.at(true) must return 1');
assert.sameValue(a.at("1"), 1, 'a.at("1") must return 1');
}); });

View File

@ -28,10 +28,33 @@ assert.sameValue(
); );
testWithTypedArrayConstructors(TA => { testWithTypedArrayConstructors(TA => {
let a = new TA([1, 2, 3, 4, 5]); const {
assert.sameValue(a.at(0), 1, 'a.at(0) must return 1'); nonresizable,
assert.sameValue(a.at(-1), 5, 'a.at(-1) must return 5'); fixedLength,
assert.sameValue(a.at(-2), 4, 'a.at(-2) must return 4'); lengthTracking,
assert.sameValue(a.at(-3), 3, 'a.at(-3) must return 3'); fixedLengthWithOffset,
assert.sameValue(a.at(-4), 2, 'a.at(-4) must return 2'); lengthTrackingWithOffset
} = createTypedArrayVariations(TA, [1, 2, 3, 4, 5]);
[
nonresizable,
fixedLength,
lengthTracking,
].forEach(({ name, contents }) => {
assert.sameValue(contents.at(0), 1, `contents.at(0) must return 1 in ${name}`);
assert.sameValue(contents.at(-1), 5, `contents.at(-1) must return 5 in ${name}`);
assert.sameValue(contents.at(-2), 4, `contents.at(-2) must return 4 in ${name}`);
assert.sameValue(contents.at(-3), 3, `contents.at(-3) must return 3 in ${name}`);
assert.sameValue(contents.at(-4), 2, `contents.at(-4) must return 2 in ${name}`);
assert.sameValue(contents.at(-5), 1, `contents.at(-5) must return 1 in ${name}`);
});
[
fixedLengthWithOffset,
lengthTrackingWithOffset
].forEach(({ name, contents }) => {
assert.sameValue(contents.at(0), 3, `contents.at(0) must return 1 in ${name}`);
assert.sameValue(contents.at(-1), 5, `contents.at(-1) must return 5 in ${name}`);
assert.sameValue(contents.at(-2), 4, `contents.at(-2) must return 4 in ${name}`);
});
}); });

View File

@ -28,23 +28,40 @@ assert.sameValue(
); );
testWithTypedArrayConstructors(TA => { testWithTypedArrayConstructors(TA => {
let a = new TA([0, 1, , 3, 4, , 6]);
let filler = 0; const {
if (TA.name.startsWith('Float')) { nonresizable,
filler = NaN; fixedLength,
} lengthTracking,
assert.sameValue(a.at(0), 0, 'a.at(0) must return 0'); fixedLengthWithOffset,
assert.sameValue(a.at(1), 1, 'a.at(1) must return 1'); lengthTrackingWithOffset
assert.sameValue(a.at(2), filler, 'a.at(2) must return the value of filler'); } = createTypedArrayVariations(TA, [0, 1, , 3, 4, , 6]);
assert.sameValue(a.at(3), 3, 'a.at(3) must return 3');
assert.sameValue(a.at(4), 4, 'a.at(4) must return 4'); const filler = TA.name.startsWith('Float') ? 0 : NaN;
assert.sameValue(a.at(5), filler, 'a.at(5) must return the value of filler');
assert.sameValue(a.at(6), 6, 'a.at(6) must return 6'); [
assert.sameValue(a.at(-0), 0, 'a.at(-0) must return 0'); nonresizable,
assert.sameValue(a.at(-1), 6, 'a.at(-1) must return 6'); fixedLength,
assert.sameValue(a.at(-2), filler, 'a.at(-2) must return the value of filler'); lengthTracking,
assert.sameValue(a.at(-3), 4, 'a.at(-3) must return 4'); fixedLengthWithOffset,
assert.sameValue(a.at(-4), 3, 'a.at(-4) must return 3'); lengthTrackingWithOffset
assert.sameValue(a.at(-5), filler, 'a.at(-5) must return the value of filler'); ].forEach(({ contents, name }) => {
assert.sameValue(a.at(-6), 1, 'a.at(-6) must return 1');
assert.sameValue(contents.at(0), 0, 'contents.at(0) must return 0');
assert.sameValue(contents.at(1), 1, 'contents.at(1) must return 1');
assert.sameValue(contents.at(2), filler, 'contents.at(2) must return the value of filler');
assert.sameValue(contents.at(3), 3, 'contents.at(3) must return 3');
assert.sameValue(contents.at(4), 4, 'contents.at(4) must return 4');
assert.sameValue(contents.at(5), filler, 'contents.at(5) must return the value of filler');
assert.sameValue(contents.at(6), 6, 'contents.at(6) must return 6');
assert.sameValue(contents.at(-0), 0, 'contents.at(-0) must return 0');
assert.sameValue(contents.at(-1), 6, 'contents.at(-1) must return 6');
assert.sameValue(contents.at(-2), filler, 'contents.at(-2) must return the value of filler');
assert.sameValue(contents.at(-3), 4, 'contents.at(-3) must return 4');
assert.sameValue(contents.at(-4), 3, 'contents.at(-4) must return 3');
assert.sameValue(contents.at(-5), filler, 'contents.at(-5) must return the value of filler');
assert.sameValue(contents.at(-6), 1, 'contents.at(-6) must return 1');
});
}); });

View File

@ -20,9 +20,23 @@ assert.sameValue(
testWithTypedArrayConstructors(TA => { testWithTypedArrayConstructors(TA => {
assert.sameValue(typeof TA.prototype.at, 'function', 'The value of `typeof TA.prototype.at` is "function"'); assert.sameValue(typeof TA.prototype.at, 'function', 'The value of `typeof TA.prototype.at` is "function"');
let a = new TA([]);
assert.sameValue(a.at(-2), undefined, 'a.at(-2) must return undefined'); // wrap around the end const {
assert.sameValue(a.at(0), undefined, 'a.at(0) must return undefined'); nonresizable,
assert.sameValue(a.at(1), undefined, 'a.at(1) must return undefined'); fixedLength,
lengthTracking,
fixedLengthWithOffset,
lengthTrackingWithOffset
} = createTypedArrayVariations(TA, [1, 2, 3, 4]);
[
nonresizable,
fixedLength,
lengthTracking,
fixedLengthWithOffset,
lengthTrackingWithOffset
].forEach(({ contents, name }) => {
assert.sameValue(contents.at(-5), undefined, `contents.at(-5) must return undefined in ${name}`); // go past the start
assert.sameValue(contents.at(4), undefined, `contents.at(4) must return undefined in ${name}`);
});
}); });