Skip to content

Commit 8e31e24

Browse files
romandevmhdawson
authored andcommitted
test: Write tests for Number class
The new tests cover a part of the basic type conversion from JavaScript type to native type. PR-URL: #195 Reviewed-By: Michael Dawson <[email protected]>
1 parent 5543c01 commit 8e31e24

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed

test/basic_types/number.cc

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <cfloat>
2+
3+
#include "napi.h"
4+
5+
using namespace Napi;
6+
7+
Value ToInt32(const CallbackInfo& info) {
8+
return Number::New(info.Env(), info[0].As<Number>().Int32Value());
9+
}
10+
11+
Value ToUint32(const CallbackInfo& info) {
12+
return Number::New(info.Env(), info[0].As<Number>().Uint32Value());
13+
}
14+
15+
Value ToInt64(const CallbackInfo& info) {
16+
return Number::New(info.Env(), info[0].As<Number>().Int64Value());
17+
}
18+
19+
Value ToFloat(const CallbackInfo& info) {
20+
return Number::New(info.Env(), info[0].As<Number>().FloatValue());
21+
}
22+
23+
Value ToDouble(const CallbackInfo& info) {
24+
return Number::New(info.Env(), info[0].As<Number>().DoubleValue());
25+
}
26+
27+
Value MinFloat(const CallbackInfo& info) {
28+
return Number::New(info.Env(), FLT_MIN);
29+
}
30+
31+
Value MaxFloat(const CallbackInfo& info) {
32+
return Number::New(info.Env(), FLT_MAX);
33+
}
34+
35+
Value MinDouble(const CallbackInfo& info) {
36+
return Number::New(info.Env(), DBL_MIN);
37+
}
38+
39+
Value MaxDouble(const CallbackInfo& info) {
40+
return Number::New(info.Env(), DBL_MAX);
41+
}
42+
43+
Object InitBasicTypesNumber(Env env) {
44+
Object exports = Object::New(env);
45+
46+
exports["toInt32"] = Function::New(env, ToInt32);
47+
exports["toUint32"] = Function::New(env, ToUint32);
48+
exports["toInt64"] = Function::New(env, ToInt64);
49+
exports["toFloat"] = Function::New(env, ToFloat);
50+
exports["toDouble"] = Function::New(env, ToDouble);
51+
exports["minFloat"] = Function::New(env, MinFloat);
52+
exports["maxFloat"] = Function::New(env, MaxFloat);
53+
exports["minDouble"] = Function::New(env, MinDouble);
54+
exports["maxDouble"] = Function::New(env, MaxDouble);
55+
56+
return exports;
57+
}

test/basic_types/number.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
'use strict';
2+
3+
const buildType = process.config.target_defaults.default_configuration;
4+
const assert = require('assert');
5+
6+
test(require(`../build/${buildType}/binding.node`));
7+
test(require(`../build/${buildType}/binding_noexcept.node`));
8+
9+
function test(binding) {
10+
const MIN_INT32 = -2147483648;
11+
const MAX_INT32 = 2147483647;
12+
const MIN_UINT32 = 0;
13+
const MAX_UINT32 = 4294967295;
14+
const MIN_INT64 = Number.MIN_SAFE_INTEGER;
15+
const MAX_INT64 = Number.MAX_SAFE_INTEGER;
16+
const MIN_FLOAT = binding.basic_types_number.minFloat();
17+
const MAX_FLOAT = binding.basic_types_number.maxFloat();
18+
const MIN_DOUBLE = binding.basic_types_number.minDouble();
19+
const MAX_DOUBLE = binding.basic_types_number.maxDouble();
20+
21+
function randomRangeTestForInteger(min, max, converter) {
22+
for (let i = min; i < max; i+= Math.floor(Math.random() * max / 100)) {
23+
assert.strictEqual(i, converter(i));
24+
}
25+
}
26+
27+
// Test for 32bit signed integer [-2147483648, 2147483647]
28+
{
29+
// Range tests
30+
randomRangeTestForInteger(MIN_INT32, MAX_INT32, binding.basic_types_number.toInt32);
31+
assert.strictEqual(MIN_INT32, binding.basic_types_number.toInt32(MIN_INT32));
32+
assert.strictEqual(MAX_INT32, binding.basic_types_number.toInt32(MAX_INT32));
33+
34+
// Overflow tests
35+
assert.notStrictEqual(MAX_INT32 + 1, binding.basic_types_number.toInt32(MAX_INT32 + 1));
36+
assert.notStrictEqual(MIN_INT32 - 1, binding.basic_types_number.toInt32(MIN_INT32 - 1));
37+
}
38+
39+
// Test for 32bit unsigned integer [0, 4294967295]
40+
{
41+
// Range tests
42+
randomRangeTestForInteger(MIN_UINT32, MAX_UINT32, binding.basic_types_number.toUint32);
43+
assert.strictEqual(MIN_UINT32, binding.basic_types_number.toUint32(MIN_UINT32));
44+
assert.strictEqual(MAX_UINT32, binding.basic_types_number.toUint32(MAX_UINT32));
45+
46+
// Overflow tests
47+
assert.notStrictEqual(MAX_UINT32 + 1, binding.basic_types_number.toUint32(MAX_UINT32 + 1));
48+
assert.notStrictEqual(MIN_UINT32 - 1, binding.basic_types_number.toUint32(MIN_UINT32 - 1));
49+
}
50+
51+
// Test for 64bit signed integer
52+
{
53+
// Range tests
54+
randomRangeTestForInteger(MIN_INT64, MAX_INT64, binding.basic_types_number.toInt64);
55+
assert.strictEqual(MIN_INT64, binding.basic_types_number.toInt64(MIN_INT64));
56+
assert.strictEqual(MAX_INT64, binding.basic_types_number.toInt64(MAX_INT64));
57+
58+
// The int64 type can't be represented with full precision in JavaScript.
59+
// So, we are not able to do overflow test here.
60+
// Please see https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type.
61+
}
62+
63+
// Test for float type (might be single-precision 32bit IEEE 754 floating point number)
64+
{
65+
// Range test
66+
assert.strictEqual(MIN_FLOAT, binding.basic_types_number.toFloat(MIN_FLOAT));
67+
assert.strictEqual(MAX_FLOAT, binding.basic_types_number.toFloat(MAX_FLOAT));
68+
69+
// Overflow test
70+
assert.strictEqual(0, binding.basic_types_number.toFloat(MIN_FLOAT * MIN_FLOAT));
71+
assert.strictEqual(Infinity, binding.basic_types_number.toFloat(MAX_FLOAT * MAX_FLOAT));
72+
}
73+
74+
// Test for double type (is double-precision 64 bit IEEE 754 floating point number)
75+
{
76+
assert.strictEqual(MIN_DOUBLE, binding.basic_types_number.toDouble(MIN_DOUBLE));
77+
assert.strictEqual(MAX_DOUBLE, binding.basic_types_number.toDouble(MAX_DOUBLE));
78+
79+
// Overflow test
80+
assert.strictEqual(0, binding.basic_types_number.toDouble(MIN_DOUBLE * MIN_DOUBLE));
81+
assert.strictEqual(Infinity, binding.basic_types_number.toDouble(MAX_DOUBLE * MAX_DOUBLE));
82+
}
83+
}

test/binding.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ using namespace Napi;
44

55
Object InitArrayBuffer(Env env);
66
Object InitAsyncWorker(Env env);
7+
Object InitBasicTypesNumber(Env env);
78
Object InitBuffer(Env env);
89
Object InitError(Env env);
910
Object InitExternal(Env env);
@@ -17,6 +18,7 @@ Object InitObjectWrap(Env env);
1718
Object Init(Env env, Object exports) {
1819
exports.Set("arraybuffer", InitArrayBuffer(env));
1920
exports.Set("asyncworker", InitAsyncWorker(env));
21+
exports.Set("basic_types_number", InitBasicTypesNumber(env));
2022
exports.Set("buffer", InitBuffer(env));
2123
exports.Set("error", InitError(env));
2224
exports.Set("external", InitExternal(env));

test/binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
'sources': [
44
'arraybuffer.cc',
55
'asyncworker.cc',
6+
'basic_types/number.cc',
67
'binding.cc',
78
'buffer.cc',
89
'error.cc',

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ process.config.target_defaults.default_configuration =
1010
let testModules = [
1111
'arraybuffer',
1212
'asyncworker',
13+
'basic_types/number',
1314
'buffer',
1415
'error',
1516
'external',

0 commit comments

Comments
 (0)