diff options
author | Gabriel Schulhof <gabriel.schulhof@intel.com> | 2018-07-09 18:41:58 +0300 |
---|---|---|
committer | Michaƫl Zasso <targos@protonmail.com> | 2018-07-12 10:09:01 +0300 |
commit | 4f3bbfaaca9817fce8d2282207719e4745ab4c63 (patch) | |
tree | 5960558f6a949b60e83dbebda28eaf0c9c854924 /test/addons-napi | |
parent | 277077853fa062f8a101b9af86e62b23168c87ae (diff) |
n-api: test uint32 truncation
Re: https://github.com/nodejs/abi-stable-node/issues/55#issuecomment-403382424
PR-URL: https://github.com/nodejs/node/pull/21722
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Diffstat (limited to 'test/addons-napi')
-rw-r--r-- | test/addons-napi/test_number/test.js | 14 | ||||
-rw-r--r-- | test/addons-napi/test_number/test_number.c | 23 |
2 files changed, 37 insertions, 0 deletions
diff --git a/test/addons-napi/test_number/test.js b/test/addons-napi/test_number/test.js index 6c04a222cb4..34f48aee578 100644 --- a/test/addons-napi/test_number/test.js +++ b/test/addons-napi/test_number/test.js @@ -35,6 +35,20 @@ testNumber(Number.POSITIVE_INFINITY); testNumber(Number.NEGATIVE_INFINITY); testNumber(Number.NaN); +function testUint32(input, expected = input) { + assert.strictEqual(expected, test_number.TestUint32Truncation(input)); +} + +// Test zero +testUint32(0.0, 0); +testUint32(-0.0, 0); + +// Test overflow scenarios +testUint32(4294967295); +testUint32(4294967296, 0); +testUint32(4294967297, 1); +testUint32(17 * 4294967296 + 1, 1); + // validate documented behavior when value is retrieved as 32-bit integer with // `napi_get_value_int32` function testInt32(input, expected = input) { diff --git a/test/addons-napi/test_number/test_number.c b/test/addons-napi/test_number/test_number.c index 19b0ae83f05..63290bf6d64 100644 --- a/test/addons-napi/test_number/test_number.c +++ b/test/addons-napi/test_number/test_number.c @@ -23,6 +23,28 @@ static napi_value Test(napi_env env, napi_callback_info info) { return output; } +static napi_value TestUint32Truncation(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."); + + uint32_t input; + NAPI_CALL(env, napi_get_value_uint32(env, args[0], &input)); + + napi_value output; + NAPI_CALL(env, napi_create_uint32(env, input, &output)); + + return output; +} + static napi_value TestInt32Truncation(napi_env env, napi_callback_info info) { size_t argc = 1; napi_value args[1]; @@ -71,6 +93,7 @@ static 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("TestUint32Truncation", TestUint32Truncation), DECLARE_NAPI_PROPERTY("TestInt64Truncation", TestInt64Truncation), }; |