Skip to content

Commit

Permalink
n-api,test: add int64 bounds tests
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
kfarnung authored and MylesBorins committed Mar 20, 2018
1 parent 99e6734 commit 0c9577e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions test/addons-napi/test_number/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
23 changes: 23 additions & 0 deletions test/addons-napi/test_number/test_number.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 0c9577e

Please sign in to comment.