summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyle Farnung <kfarnung@microsoft.com>2018-03-12 13:36:29 -0700
committerKyle Farnung <kfarnung@microsoft.com>2018-03-15 14:17:11 -0700
commit5fb6f7f22f8fa7284cceb9d6ad7f96a603aca6d2 (patch)
tree581b3b2df4d65df7d0c1d41d6fa03c7d4974a454
parent3c61b87e5918441c9d14641e328584e3ce184c98 (diff)
downloadnode-new-5fb6f7f22f8fa7284cceb9d6ad7f96a603aca6d2.tar.gz
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 <cjihrig@gmail.com> Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com> Reviewed-By: James M Snell <jasnell@gmail.com>
-rw-r--r--test/addons-napi/test_number/test.js7
-rw-r--r--test/addons-napi/test_number/test_number.c23
2 files changed, 30 insertions, 0 deletions
diff --git a/test/addons-napi/test_number/test.js b/test/addons-napi/test_number/test.js
index e92c6d0cd2..11cf6f6b74 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 3707f1ee57..a1a7095008 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(