Skip to content

Commit 38c7fa1

Browse files
kfarnungMylesBorins
authored andcommitted
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: nodejs/node-chakracore#496 PR-URL: #19309 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Gabriel Schulhof <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent d3edb7f commit 38c7fa1

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

test/addons-napi/test_number/test.js

+7
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,10 @@ assert.strictEqual(1, test_number.TestInt32Truncation(4294967297));
4545
assert.strictEqual(0, test_number.TestInt32Truncation(4294967296));
4646
assert.strictEqual(-1, test_number.TestInt32Truncation(4294967295));
4747
assert.strictEqual(3, test_number.TestInt32Truncation(4294967296 * 5 + 3));
48+
49+
// validate that the boundaries of safe integer can be passed through
50+
// successfully
51+
assert.strictEqual(Number.MAX_SAFE_INTEGER,
52+
test_number.TestInt64Truncation(Number.MAX_SAFE_INTEGER));
53+
assert.strictEqual(Number.MIN_SAFE_INTEGER,
54+
test_number.TestInt64Truncation(Number.MIN_SAFE_INTEGER));

test/addons-napi/test_number/test_number.c

+23
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,33 @@ napi_value TestInt32Truncation(napi_env env, napi_callback_info info) {
4545
return output;
4646
}
4747

48+
napi_value TestInt64Truncation(napi_env env, napi_callback_info info) {
49+
size_t argc = 1;
50+
napi_value args[1];
51+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
52+
53+
NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
54+
55+
napi_valuetype valuetype0;
56+
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
57+
58+
NAPI_ASSERT(env, valuetype0 == napi_number,
59+
"Wrong type of arguments. Expects a number as first argument.");
60+
61+
int64_t input;
62+
NAPI_CALL(env, napi_get_value_int64(env, args[0], &input));
63+
64+
napi_value output;
65+
NAPI_CALL(env, napi_create_int64(env, input, &output));
66+
67+
return output;
68+
}
69+
4870
napi_value Init(napi_env env, napi_value exports) {
4971
napi_property_descriptor descriptors[] = {
5072
DECLARE_NAPI_PROPERTY("Test", Test),
5173
DECLARE_NAPI_PROPERTY("TestInt32Truncation", TestInt32Truncation),
74+
DECLARE_NAPI_PROPERTY("TestInt64Truncation", TestInt64Truncation),
5275
};
5376

5477
NAPI_CALL(env, napi_define_properties(

0 commit comments

Comments
 (0)