From 0c9577edfccc1ef24d34cde7803fed046e989a2d Mon Sep 17 00:00:00 2001 From: Kyle Farnung Date: Mon, 12 Mar 2018 13:36:29 -0700 Subject: [PATCH] n-api,test: add int64 bounds tests Added some simple tests to verify that the int64 API is correctly handling numbers greater than 32-bits. This is a basic test, but verifies that an implementer hasn't truncated back to 32-bits. Refs: https://github.com/nodejs/node-chakracore/pull/496 PR-URL: https://github.com/nodejs/node/pull/19309 Reviewed-By: Colin Ihrig Reviewed-By: Gabriel Schulhof Reviewed-By: James M Snell --- test/addons-napi/test_number/test.js | 7 +++++++ test/addons-napi/test_number/test_number.c | 23 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/test/addons-napi/test_number/test.js b/test/addons-napi/test_number/test.js index e92c6d0cd29951..11cf6f6b7478a6 100644 --- a/test/addons-napi/test_number/test.js +++ b/test/addons-napi/test_number/test.js @@ -45,3 +45,10 @@ assert.strictEqual(1, test_number.TestInt32Truncation(4294967297)); assert.strictEqual(0, test_number.TestInt32Truncation(4294967296)); assert.strictEqual(-1, test_number.TestInt32Truncation(4294967295)); assert.strictEqual(3, test_number.TestInt32Truncation(4294967296 * 5 + 3)); + +// validate that the boundaries of safe integer can be passed through +// successfully +assert.strictEqual(Number.MAX_SAFE_INTEGER, + test_number.TestInt64Truncation(Number.MAX_SAFE_INTEGER)); +assert.strictEqual(Number.MIN_SAFE_INTEGER, + test_number.TestInt64Truncation(Number.MIN_SAFE_INTEGER)); diff --git a/test/addons-napi/test_number/test_number.c b/test/addons-napi/test_number/test_number.c index 3707f1ee57d713..a1a70950083324 100644 --- a/test/addons-napi/test_number/test_number.c +++ b/test/addons-napi/test_number/test_number.c @@ -45,10 +45,33 @@ napi_value TestInt32Truncation(napi_env env, napi_callback_info info) { return output; } +napi_value TestInt64Truncation(napi_env env, napi_callback_info info) { + size_t argc = 1; + napi_value args[1]; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); + + NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments"); + + napi_valuetype valuetype0; + NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0)); + + NAPI_ASSERT(env, valuetype0 == napi_number, + "Wrong type of arguments. Expects a number as first argument."); + + int64_t input; + NAPI_CALL(env, napi_get_value_int64(env, args[0], &input)); + + napi_value output; + NAPI_CALL(env, napi_create_int64(env, input, &output)); + + return output; +} + napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor descriptors[] = { DECLARE_NAPI_PROPERTY("Test", Test), DECLARE_NAPI_PROPERTY("TestInt32Truncation", TestInt32Truncation), + DECLARE_NAPI_PROPERTY("TestInt64Truncation", TestInt64Truncation), }; NAPI_CALL(env, napi_define_properties(