@@ -5,50 +5,113 @@ const test_number = require(`./build/${common.buildType}/test_number`); |
5 | 5 |
|
6 | 6 |
|
7 | 7 | // testing api calls for number |
8 | | -assert.strictEqual(0, test_number.Test(0)); |
9 | | -assert.strictEqual(1, test_number.Test(1)); |
10 | | -assert.strictEqual(-1, test_number.Test(-1)); |
11 | | -assert.strictEqual(100, test_number.Test(100)); |
12 | | -assert.strictEqual(2121, test_number.Test(2121)); |
13 | | -assert.strictEqual(-1233, test_number.Test(-1233)); |
14 | | -assert.strictEqual(986583, test_number.Test(986583)); |
15 | | -assert.strictEqual(-976675, test_number.Test(-976675)); |
| 8 | +function testNumber(num) { |
| 9 | + assert.strictEqual(num, test_number.Test(num)); |
| 10 | +} |
16 | 11 |
|
17 | | -const num1 = 98765432213456789876546896323445679887645323232436587988766545658; |
18 | | -assert.strictEqual(num1, test_number.Test(num1)); |
| 12 | +testNumber(0); |
| 13 | +testNumber(-0); |
| 14 | +testNumber(1); |
| 15 | +testNumber(-1); |
| 16 | +testNumber(100); |
| 17 | +testNumber(2121); |
| 18 | +testNumber(-1233); |
| 19 | +testNumber(986583); |
| 20 | +testNumber(-976675); |
19 | 21 |
|
20 | | -const num2 = -4350987086545760976737453646576078997096876957864353245245769809; |
21 | | -assert.strictEqual(num2, test_number.Test(num2)); |
| 22 | +testNumber( |
| 23 | + 98765432213456789876546896323445679887645323232436587988766545658); |
| 24 | +testNumber( |
| 25 | + -4350987086545760976737453646576078997096876957864353245245769809); |
| 26 | +testNumber(Number.MIN_SAFE_INTEGER); |
| 27 | +testNumber(Number.MAX_SAFE_INTEGER); |
| 28 | +testNumber(Number.MAX_SAFE_INTEGER + 10); |
22 | 29 |
|
23 | | -const num3 = Number.MAX_SAFE_INTEGER; |
24 | | -assert.strictEqual(num3, test_number.Test(num3)); |
| 30 | +testNumber(Number.MIN_VALUE); |
| 31 | +testNumber(Number.MAX_VALUE); |
| 32 | +testNumber(Number.MAX_VALUE + 10); |
25 | 33 |
|
26 | | -const num4 = Number.MAX_SAFE_INTEGER + 10; |
27 | | -assert.strictEqual(num4, test_number.Test(num4)); |
| 34 | +testNumber(Number.POSITIVE_INFINITY); |
| 35 | +testNumber(Number.NEGATIVE_INFINITY); |
| 36 | +assert(Object.is(NaN, test_number.Test(NaN))); |
28 | 37 |
|
29 | | -const num5 = Number.MAX_VALUE; |
30 | | -assert.strictEqual(num5, test_number.Test(num5)); |
| 38 | +// validate documented behavior when value is retrieved as 32-bit integer with |
| 39 | +// `napi_get_value_int32` |
| 40 | +function testInt32(input, expected = input) { |
| 41 | + assert.strictEqual(expected, test_number.TestInt32Truncation(input)); |
| 42 | +} |
31 | 43 |
|
32 | | -const num6 = Number.MAX_VALUE + 10; |
33 | | -assert.strictEqual(num6, test_number.Test(num6)); |
| 44 | +// Test zero |
| 45 | +testInt32(0.0, 0); |
| 46 | +testInt32(-0.0, 0); |
34 | 47 |
|
35 | | -const num7 = Number.POSITIVE_INFINITY; |
36 | | -assert.strictEqual(num7, test_number.Test(num7)); |
| 48 | +// Test min/max int32 range |
| 49 | +testInt32(-Math.pow(2, 31)); |
| 50 | +testInt32(Math.pow(2, 31) - 1); |
37 | 51 |
|
38 | | -const num8 = Number.NEGATIVE_INFINITY; |
39 | | -assert.strictEqual(num8, test_number.Test(num8)); |
| 52 | +// Test overflow scenarios |
| 53 | +testInt32(4294967297, 1); |
| 54 | +testInt32(4294967296, 0); |
| 55 | +testInt32(4294967295, -1); |
| 56 | +testInt32(4294967296 * 5 + 3, 3); |
40 | 57 |
|
| 58 | +// Test min/max safe integer range |
| 59 | +testInt32(Number.MIN_SAFE_INTEGER, 1); |
| 60 | +testInt32(Number.MAX_SAFE_INTEGER, -1); |
41 | 61 |
|
42 | | -// validate documented behaviour when value is retrieved |
43 | | -// as 32 bit integer with napi_get_value_int32 |
44 | | -assert.strictEqual(1, test_number.TestInt32Truncation(4294967297)); |
45 | | -assert.strictEqual(0, test_number.TestInt32Truncation(4294967296)); |
46 | | -assert.strictEqual(-1, test_number.TestInt32Truncation(4294967295)); |
47 | | -assert.strictEqual(3, test_number.TestInt32Truncation(4294967296 * 5 + 3)); |
| 62 | +// Test within int64_t range (with precision loss) |
| 63 | +testInt32(-Math.pow(2, 63) + (Math.pow(2, 9) + 1), 1024); |
| 64 | +testInt32(Math.pow(2, 63) - (Math.pow(2, 9) + 1), -1024); |
48 | 65 |
|
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)); |
| 66 | +// Test min/max double value |
| 67 | +testInt32(-Number.MIN_VALUE, 0); |
| 68 | +testInt32(Number.MIN_VALUE, 0); |
| 69 | +testInt32(-Number.MAX_VALUE, 0); |
| 70 | +testInt32(Number.MAX_VALUE, 0); |
| 71 | + |
| 72 | +// Test outside int64_t range |
| 73 | +testInt32(-Math.pow(2, 63) + (Math.pow(2, 9)), 0); |
| 74 | +testInt32(Math.pow(2, 63) - (Math.pow(2, 9)), 0); |
| 75 | + |
| 76 | +// Test non-finite numbers |
| 77 | +testInt32(Number.POSITIVE_INFINITY, 0); |
| 78 | +testInt32(Number.NEGATIVE_INFINITY, 0); |
| 79 | +testInt32(Number.NaN, 0); |
| 80 | + |
| 81 | +// validate documented behavior when value is retrieved as 64-bit integer with |
| 82 | +// `napi_get_value_int64` |
| 83 | +function testInt64(input, expected = input) { |
| 84 | + assert.strictEqual(expected, test_number.TestInt64Truncation(input)); |
| 85 | +} |
| 86 | + |
| 87 | +// Both V8 and ChakraCore return a sentinel value of `0x8000000000000000` when |
| 88 | +// the conversion goes out of range, but V8 treats it as unsigned in some cases. |
| 89 | +const RANGEERROR_POSITIVE = Math.pow(2, 63); |
| 90 | +const RANGEERROR_NEGATIVE = -Math.pow(2, 63); |
| 91 | + |
| 92 | +// Test zero |
| 93 | +testInt64(0.0, 0); |
| 94 | +testInt64(-0.0, 0); |
| 95 | + |
| 96 | +// Test min/max safe integer range |
| 97 | +testInt64(Number.MIN_SAFE_INTEGER); |
| 98 | +testInt64(Number.MAX_SAFE_INTEGER); |
| 99 | + |
| 100 | +// Test within int64_t range (with precision loss) |
| 101 | +testInt64(-Math.pow(2, 63) + (Math.pow(2, 9) + 1)); |
| 102 | +testInt64(Math.pow(2, 63) - (Math.pow(2, 9) + 1)); |
| 103 | + |
| 104 | +// Test min/max double value |
| 105 | +testInt64(-Number.MIN_VALUE, 0); |
| 106 | +testInt64(Number.MIN_VALUE, 0); |
| 107 | +testInt64(-Number.MAX_VALUE, RANGEERROR_NEGATIVE); |
| 108 | +testInt64(Number.MAX_VALUE, RANGEERROR_POSITIVE); |
| 109 | + |
| 110 | +// Test outside int64_t range |
| 111 | +testInt64(-Math.pow(2, 63) + (Math.pow(2, 9)), RANGEERROR_NEGATIVE); |
| 112 | +testInt64(Math.pow(2, 63) - (Math.pow(2, 9)), RANGEERROR_POSITIVE); |
| 113 | + |
| 114 | +// Test non-finite numbers |
| 115 | +testInt64(Number.POSITIVE_INFINITY, 0); |
| 116 | +testInt64(Number.NEGATIVE_INFINITY, 0); |
| 117 | +testInt64(Number.NaN, 0); |