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>
This commit is contained in:
Kyle Farnung 2018-03-12 13:36:29 -07:00
parent 3c61b87e59
commit 5fb6f7f22f
2 changed files with 30 additions and 0 deletions

View File

@ -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));

View File

@ -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(