From e439222fe64e941dfeeb9d865d05214ab8770a0c Mon Sep 17 00:00:00 2001 From: WenheLI Date: Sun, 24 Oct 2021 22:07:32 -0400 Subject: [PATCH] test: add bigint value test PR-URL: https://github.com/nodejs/node-addon-api/pull/1096 Reviewed-By: Chengzhong Wu Reviewed-By: Michael Dawson --- test/bigint.cc | 9 +++++++++ test/bigint.js | 11 +++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/test/bigint.cc b/test/bigint.cc index 300e5a498..e8e6e9cb4 100644 --- a/test/bigint.cc +++ b/test/bigint.cc @@ -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(); + + return Boolean::New(env, big.IsBigInt()); +} + Value TestInt64(const CallbackInfo& info) { bool lossless; int64_t input = info[0].As().Int64Value(&lossless); @@ -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); diff --git a/test/bigint.js b/test/bigint.js index bc27d9501..c30e525d4 100644 --- a/test/bigint.js +++ b/test/bigint.js @@ -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; [ @@ -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); @@ -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)$/ }); }