Merge pull request #1224 from thejoshwolfe/bigint-asuintn

BigInt.asUintN tests
This commit is contained in:
Daniel Ehrenberg 2017-09-25 08:06:55 -04:00 committed by GitHub
commit 554d1fe147
10 changed files with 385 additions and 43 deletions

View File

@ -6,6 +6,20 @@ description: |
operations like ToNumber.
---*/
function testCoercibleToIndexZero(test) {
testCoercibleToIntegerZero(test);
}
function testCoercibleToIndexOne(test) {
testCoercibleToIntegerOne(test);
}
function testCoercibleToIndexFromIndex(nominalIndex, test) {
assert(Number.isInteger(nominalIndex));
assert(0 <= nominalIndex && nominalIndex <= 2**53 - 1);
testCoercibleToIntegerFromInteger(nominalIndex, test);
}
function testCoercibleToIntegerZero(test) {
testCoercibleToNumberZero(test);
@ -176,10 +190,35 @@ function testCoercibleToPrimitiveWithMethod(hint, method, test) {
});
}
function testNotCoercibleToIndex(test) {
function testPrimitiveValue(value) {
test(RangeError, value);
// ToPrimitive
testPrimitiveWrappers(value, "number", function(value) {
test(RangeError, value);
});
}
// Let integerIndex be ? ToInteger(value).
testNotCoercibleToInteger(test);
// If integerIndex < 0, throw a RangeError exception.
testPrimitiveValue(-1);
testPrimitiveValue(-2.5);
testPrimitiveValue("-2.5");
testPrimitiveValue(-Infinity);
// Let index be ! ToLength(integerIndex).
// If SameValueZero(integerIndex, index) is false, throw a RangeError exception.
testPrimitiveValue(2 ** 53);
testPrimitiveValue(Infinity);
}
function testNotCoercibleToInteger(test) {
// ToInteger only throws from ToNumber.
return testNotCoercibleToNumber(test);
testNotCoercibleToNumber(test);
}
function testNotCoercibleToNumber(test) {
function testPrimitiveValue(value) {
test(TypeError, value);
@ -283,3 +322,88 @@ function testNotCoercibleToString(test) {
// ToPrimitive
testNotCoercibleToPrimitive("string", test);
}
function testCoercibleToBigIntZero(test) {
function testPrimitiveValue(value) {
test(value);
// ToPrimitive
testPrimitiveWrappers(value, "number", test);
}
testCoercibleToBigIntFromBigInt(0n, test);
testPrimitiveValue(-0n);
testPrimitiveValue("-0");
testPrimitiveValue(false);
testPrimitiveValue("");
testPrimitiveValue(" ");
// toString() returns ""
test([]);
// toString() returns "0"
test([0]);
}
function testCoercibleToBigIntOne(test) {
function testPrimitiveValue(value) {
test(value);
// ToPrimitive
testPrimitiveWrappers(value, "number", test);
}
testCoercibleToBigIntFromBigInt(1n, test);
testPrimitiveValue(true);
// toString() returns "1"
test([1]);
}
function testCoercibleToBigIntFromBigInt(nominalBigInt, test) {
function testPrimitiveValue(value) {
test(value);
// ToPrimitive
testPrimitiveWrappers(value, "number", test);
}
testPrimitiveValue(nominalBigInt);
testPrimitiveValue(nominalBigInt.toString());
testPrimitiveValue("0b" + nominalBigInt.toString(2));
testPrimitiveValue("0o" + nominalBigInt.toString(8));
testPrimitiveValue("0x" + nominalBigInt.toString(16));
testPrimitiveValue(" " + nominalBigInt.toString() + " ");
// toString() returns the decimal string representation
test([nominalBigInt]);
test([nominalBigInt.toString()]);
}
function testNotCoercibleToBigInt(test) {
function testPrimitiveValue(error, value) {
test(error, value);
// ToPrimitive
testPrimitiveWrappers(value, "number", function(value) {
test(error, value);
});
}
// Undefined, Null, Number, Symbol -> TypeError
testPrimitiveValue(TypeError, undefined);
testPrimitiveValue(TypeError, null);
testPrimitiveValue(TypeError, 0);
testPrimitiveValue(TypeError, NaN);
testPrimitiveValue(TypeError, Infinity);
testPrimitiveValue(TypeError, Symbol("1"));
// when a String parses to NaN -> SyntaxError
function testStringValue(string) {
testPrimitiveValue(SyntaxError, string);
testPrimitiveValue(SyntaxError, " " + string);
testPrimitiveValue(SyntaxError, string + " ");
testPrimitiveValue(SyntaxError, " " + string + " ");
}
testStringValue("a");
testStringValue("0b2");
testStringValue("0o8");
testStringValue("0xg");
testStringValue("1n");
}

View File

@ -8,24 +8,26 @@ info: >
2. Let bigint ? ToBigInt(bigint).
features: [BigInt, Symbol, Symbol.toPrimitive, arrow-function]
features: [BigInt, Symbol, Symbol.toPrimitive]
includes: [typeCoercion.js]
---*/
function MyError() {}
testCoercibleToBigIntZero(function(zero) {
assert.sameValue(BigInt.asIntN(2, zero), 0n);
});
assert.sameValue(BigInt.asIntN(3, Object(10n)), 2n);
assert.sameValue(BigInt.asIntN(3, {[Symbol.toPrimitive]:()=>10n, valueOf(){throw new MyError();}, toString(){throw new MyError();}}), 2n);
assert.sameValue(BigInt.asIntN(3, {valueOf:()=>10n, toString(){throw new MyError();}}), 2n);
assert.sameValue(BigInt.asIntN(3, {toString:()=>"10"}), 2n);
assert.throws(MyError, () => BigInt.asIntN(0, {[Symbol.toPrimitive](){throw new MyError();}, valueOf:()=>10n, toString:()=>"10"}));
assert.throws(MyError, () => BigInt.asIntN(0, {valueOf(){throw new MyError();}, toString:()=>"10"}));
assert.throws(MyError, () => BigInt.asIntN(0, {toString(){throw new MyError();}}));
assert.throws(TypeError, () => BigInt.asIntN(0, undefined));
assert.throws(TypeError, () => BigInt.asIntN(0, null));
assert.sameValue(BigInt.asIntN(2, true), 1n);
assert.sameValue(BigInt.asIntN(2, false), 0n);
assert.throws(TypeError, () => BigInt.asIntN(0, 0));
assert.throws(SyntaxError, () => BigInt.asIntN(0, "foo"));
assert.sameValue(BigInt.asIntN(3, "10"), 2n);
assert.sameValue(BigInt.asIntN(4, "12345678901234567890003"), 3n);
assert.throws(TypeError, () => BigInt.asIntN(0, Symbol("1")));
testCoercibleToBigIntOne(function(one) {
assert.sameValue(BigInt.asIntN(2, one), 1n);
});
testCoercibleToBigIntFromBigInt(10n, function(ten) {
assert.sameValue(BigInt.asIntN(3, ten), 2n);
});
testCoercibleToBigIntFromBigInt(12345678901234567890003n, function(value) {
assert.sameValue(BigInt.asIntN(4, value), 3n);
});
testNotCoercibleToBigInt(function(error, value) {
assert.throws(error, function() { BigInt.asIntN(0, value); });
});

View File

@ -8,30 +8,22 @@ info: >
1. Let bits be ? ToIndex(bits).
features: [BigInt, Symbol, Symbol.toPrimitive, arrow-function]
features: [BigInt, Symbol, Symbol.toPrimitive]
includes: [typeCoercion.js]
---*/
function MyError() {}
testCoercibleToIndexZero(function(zero) {
assert.sameValue(BigInt.asIntN(zero, 1n), 0n);
});
assert.sameValue(BigInt.asIntN(undefined, 1n), 0n);
assert.sameValue(BigInt.asIntN(null, 1n), 0n);
assert.sameValue(BigInt.asIntN(true, 1n), -1n);
assert.sameValue(BigInt.asIntN(false, 1n), 0n);
assert.sameValue(BigInt.asIntN("foo", 1n), 0n);
assert.sameValue(BigInt.asIntN("3", 10n), 2n);
assert.throws(TypeError, () => BigInt.asIntN(Symbol(0), 0n));
assert.throws(TypeError, () => BigInt.asIntN(1n, 0n));
assert.sameValue(BigInt.asIntN(Object(3), 10n), 2n);
assert.sameValue(BigInt.asIntN({[Symbol.toPrimitive]:()=>3, valueOf(){throw new MyError();}, toString(){throw new MyError();}}, 10n), 2n);
assert.sameValue(BigInt.asIntN({valueOf:()=>3, toString(){throw new MyError();}}, 10n), 2n);
assert.sameValue(BigInt.asIntN({toString:()=>"3"}, 10n), 2n);
assert.throws(MyError, () => BigInt.asIntN({[Symbol.toPrimitive](){throw new MyError();}, valueOf:()=>3, toString:()=>"3"}, 0n));
assert.throws(MyError, () => BigInt.asIntN({valueOf(){throw new MyError();}, toString:()=>"3"}, 0n));
assert.throws(MyError, () => BigInt.asIntN({toString(){throw new MyError();}}, 0n));
assert.sameValue(BigInt.asIntN(NaN, 1n), 0n);
assert.sameValue(BigInt.asIntN(3.9, 10n), 2n);
assert.sameValue(BigInt.asIntN(-0.9, 1n), 0n);
assert.throws(RangeError, () => BigInt.asIntN(-1, 0n));
assert.throws(RangeError, () => BigInt.asIntN("-2.5", 0n));
assert.throws(RangeError, () => BigInt.asIntN(2 ** 53, 0n));
assert.throws(RangeError, () => BigInt.asIntN(Infinity, 0n));
testCoercibleToIndexOne(function(one) {
assert.sameValue(BigInt.asIntN(one, 1n), -1n);
});
testCoercibleToIndexFromIndex(3, function(three) {
assert.sameValue(BigInt.asIntN(three, 10n), 2n);
});
testNotCoercibleToIndex(function(error, value) {
assert.throws(error, function() { BigInt.asIntN(value, 0n); });
});

View File

@ -0,0 +1,69 @@
// Copyright (C) 2017 Josh Wolfe. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: pending
description: BigInt.asUintN arithmetic test cases
info: >
BigInt.asUintN ( bits, bigint )
3. Return a BigInt representing bigint modulo 2**bits.
features: [BigInt]
---*/
assert.sameValue(BigInt.asUintN(0, -2n), 0n);
assert.sameValue(BigInt.asUintN(0, -1n), 0n);
assert.sameValue(BigInt.asUintN(0, 0n), 0n);
assert.sameValue(BigInt.asUintN(0, 1n), 0n);
assert.sameValue(BigInt.asUintN(0, 2n), 0n);
assert.sameValue(BigInt.asUintN(1, -3n), 1n);
assert.sameValue(BigInt.asUintN(1, -2n), 0n);
assert.sameValue(BigInt.asUintN(1, -1n), 1n);
assert.sameValue(BigInt.asUintN(1, 0n), 0n);
assert.sameValue(BigInt.asUintN(1, 1n), 1n);
assert.sameValue(BigInt.asUintN(1, 2n), 0n);
assert.sameValue(BigInt.asUintN(1, 3n), 1n);
assert.sameValue(BigInt.asUintN(1, -123456789012345678901n), 1n);
assert.sameValue(BigInt.asUintN(1, -123456789012345678900n), 0n);
assert.sameValue(BigInt.asUintN(1, 123456789012345678900n), 0n);
assert.sameValue(BigInt.asUintN(1, 123456789012345678901n), 1n);
assert.sameValue(BigInt.asUintN(2, -3n), 1n);
assert.sameValue(BigInt.asUintN(2, -2n), 2n);
assert.sameValue(BigInt.asUintN(2, -1n), 3n);
assert.sameValue(BigInt.asUintN(2, 0n), 0n);
assert.sameValue(BigInt.asUintN(2, 1n), 1n);
assert.sameValue(BigInt.asUintN(2, 2n), 2n);
assert.sameValue(BigInt.asUintN(2, 3n), 3n);
assert.sameValue(BigInt.asUintN(2, -123456789012345678901n), 3n);
assert.sameValue(BigInt.asUintN(2, -123456789012345678900n), 0n);
assert.sameValue(BigInt.asUintN(2, 123456789012345678900n), 0n);
assert.sameValue(BigInt.asUintN(2, 123456789012345678901n), 1n);
assert.sameValue(BigInt.asUintN(8, 0xabn), 0xabn);
assert.sameValue(BigInt.asUintN(8, 0xabcdn), 0xcdn);
assert.sameValue(BigInt.asUintN(8, 0xabcdef01n), 0x01n);
assert.sameValue(BigInt.asUintN(8, 0xabcdef0123456789abcdef0123n), 0x23n);
assert.sameValue(BigInt.asUintN(8, 0xabcdef0123456789abcdef0183n), 0x83n);
assert.sameValue(BigInt.asUintN(64, 0xabcdef0123456789abcdefn), 0x0123456789abcdefn);
assert.sameValue(BigInt.asUintN(65, 0xabcdef0123456789abcdefn), 0x10123456789abcdefn);
assert.sameValue(BigInt.asUintN(200,
0xbffffffffffffffffffffffffffffffffffffffffffffffffffn),
0x0ffffffffffffffffffffffffffffffffffffffffffffffffffn
);
assert.sameValue(BigInt.asUintN(201,
0xbffffffffffffffffffffffffffffffffffffffffffffffffffn),
0x1ffffffffffffffffffffffffffffffffffffffffffffffffffn
);
assert.sameValue(BigInt.asUintN(200,
0xb89e081df68b65fedb32cffea660e55df9605650a603ad5fc54n),
0x089e081df68b65fedb32cffea660e55df9605650a603ad5fc54n
);
assert.sameValue(BigInt.asUintN(201,
0xb89e081df68b65fedb32cffea660e55df9605650a603ad5fc54n),
0x189e081df68b65fedb32cffea660e55df9605650a603ad5fc54n
);

View File

@ -0,0 +1,21 @@
// Copyright (C) 2017 Josh Wolfe. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: pending
description: BigInt.asUintN property descriptor
info: >
BigInt.asUintN ( bits, bigint )
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
features: [BigInt]
---*/
assert.sameValue(typeof BigInt.asUintN, 'function');
verifyProperty(BigInt, "asUintN", {
enumerable: false,
writable: true,
configurable: true
});

View File

@ -0,0 +1,33 @@
// Copyright (C) 2017 Josh Wolfe. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: pending
description: BigInt.asUintN type coercion for bigint parameter
info: >
BigInt.asUintN ( bits, bigint )
2. Let bigint ? ToBigInt(bigint).
features: [BigInt, Symbol, Symbol.toPrimitive]
includes: [typeCoercion.js]
---*/
testCoercibleToBigIntZero(function(zero) {
assert.sameValue(BigInt.asUintN(2, zero), 0n);
});
testCoercibleToBigIntOne(function(one) {
assert.sameValue(BigInt.asUintN(2, one), 1n);
});
testCoercibleToBigIntFromBigInt(10n, function(ten) {
assert.sameValue(BigInt.asUintN(3, ten), 2n);
});
testCoercibleToBigIntFromBigInt(12345678901234567890003n, function(value) {
assert.sameValue(BigInt.asUintN(4, value), 3n);
});
testNotCoercibleToBigInt(function(error, value) {
assert.throws(error, function() { BigInt.asUintN(0, value); });
});

View File

@ -0,0 +1,29 @@
// Copyright (C) 2017 Josh Wolfe. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: pending
description: BigInt.asUintN type coercion for bits parameter
info: >
BigInt.asUintN ( bits, bigint )
1. Let bits be ? ToIndex(bits).
features: [BigInt, Symbol, Symbol.toPrimitive]
includes: [typeCoercion.js]
---*/
testCoercibleToIndexZero(function(zero) {
assert.sameValue(BigInt.asUintN(zero, 1n), 0n);
});
testCoercibleToIndexOne(function(one) {
assert.sameValue(BigInt.asUintN(one, 1n), 1n);
});
testCoercibleToIndexFromIndex(3, function(three) {
assert.sameValue(BigInt.asUintN(three, 10n), 2n);
});
testNotCoercibleToIndex(function(error, value) {
assert.throws(error, function() { BigInt.asUintN(value, 0n); });
});

View File

@ -0,0 +1,20 @@
// Copyright (C) 2017 Josh Wolfe. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: pending
description: BigInt.asUintN.length descriptor
info: >
BigInt.asUintN ( bits, bigint )
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
features: [BigInt]
---*/
verifyProperty(BigInt.asUintN, "length", {
value: 2,
enumerable: false,
writable: false,
configurable: true
});

View File

@ -0,0 +1,20 @@
// Copyright (C) 2017 Josh Wolfe. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: pending
description: BigInt.asUintN.name descriptor
info: >
BigInt.asUintN ( bits, bigint )
17 ECMAScript Standard Built-in Objects
includes: [propertyHelper.js]
features: [BigInt]
---*/
verifyProperty(BigInt.asUintN, "name", {
value: "asUintN",
enumerable: false,
writable: false,
configurable: true
});

View File

@ -0,0 +1,32 @@
// Copyright (C) 2017 Josh Wolfe. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: pending
description: BigInt.asUintN order of parameter type coercion
info: >
BigInt.asUintN ( bits, bigint )
1. Let bits be ? ToIndex(bits).
2. Let bigint ? ToBigInt(bigint).
features: [BigInt]
---*/
var i = 0;
var bits = {
valueOf() {
assert.sameValue(i, 0);
i++;
return 0;
}
};
var bigint = {
valueOf() {
assert.sameValue(i, 1);
i++;
return 0n;
}
};
BigInt.asUintN(bits, bigint);
assert.sameValue(i, 2);