Skip to content

Commit

Permalink
test: add bigint value test
Browse files Browse the repository at this point in the history
PR-URL: #1096
Reviewed-By: Chengzhong Wu <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
  • Loading branch information
WenheLI authored and mhdawson committed Oct 25, 2021
1 parent 0dfa89f commit e439222
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
9 changes: 9 additions & 0 deletions test/bigint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ Value IsLossless(const CallbackInfo& info) {
return Boolean::New(env, lossless);
}

Value IsBigInt(const CallbackInfo& info) {
Env env = info.Env();

BigInt big = info[0].As<BigInt>();

return Boolean::New(env, big.IsBigInt());
}

Value TestInt64(const CallbackInfo& info) {
bool lossless;
int64_t input = info[0].As<BigInt>().Int64Value(&lossless);
Expand Down Expand Up @@ -71,6 +79,7 @@ Value TestTooBigBigInt(const CallbackInfo& info) {
Object InitBigInt(Env env) {
Object exports = Object::New(env);
exports["IsLossless"] = Function::New(env, IsLossless);
exports["IsBigInt"] = Function::New(env, IsBigInt);
exports["TestInt64"] = Function::New(env, TestInt64);
exports["TestUint64"] = Function::New(env, TestUint64);
exports["TestWords"] = Function::New(env, TestWords);
Expand Down
11 changes: 7 additions & 4 deletions test/bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ const assert = require('assert');

module.exports = require('./common').runTest(test);

function test(binding) {
function test (binding) {
const {
TestInt64,
TestUint64,
TestWords,
IsLossless,
TestTooBigBigInt,
IsBigInt,
TestTooBigBigInt
} = binding.bigint;

[
Expand All @@ -24,7 +25,7 @@ function test(binding) {
986583n,
-976675n,
98765432213456789876546896323445679887645323232436587988766545658n,
-4350987086545760976737453646576078997096876957864353245245769809n,
-4350987086545760976737453646576078997096876957864353245245769809n
].forEach((num) => {
if (num > -(2n ** 63n) && num < 2n ** 63n) {
assert.strictEqual(TestInt64(num), num);
Expand All @@ -40,11 +41,13 @@ function test(binding) {
assert.strictEqual(IsLossless(num, false), false);
}

assert.strictEqual(IsBigInt(num), true);

assert.strictEqual(num, TestWords(num));
});

assert.throws(TestTooBigBigInt, {
name: /^(RangeError|Error)$/,
message: /^(Maximum BigInt size exceeded|Invalid argument)$/,
message: /^(Maximum BigInt size exceeded|Invalid argument)$/
});
}

0 comments on commit e439222

Please sign in to comment.