Skip to content

Commit

Permalink
test: add operator overloading tests in Number
Browse files Browse the repository at this point in the history
PR-URL: #355
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Nicola Del Gobbo <nicoladelgobbo@NickNaso.local>
  • Loading branch information
Your Name authored and mhdawson committed Sep 28, 2018
1 parent 51ffe45 commit 779560f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
41 changes: 41 additions & 0 deletions test/basic_types/number.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,40 @@ Value MaxDouble(const CallbackInfo& info) {
return Number::New(info.Env(), DBL_MAX);
}

Value OperatorInt32(const CallbackInfo& info) {
Number number = info[0].As<Number>();
return Boolean::New(info.Env(), number.Int32Value() == static_cast<int32_t>(number));
}

Value OperatorUint32(const CallbackInfo& info) {
Number number = info[0].As<Number>();
return Boolean::New(info.Env(), number.Uint32Value() == static_cast<uint32_t>(number));
}

Value OperatorInt64(const CallbackInfo& info) {
Number number = info[0].As<Number>();
return Boolean::New(info.Env(), number.Int64Value() == static_cast<int64_t>(number));
}

Value OperatorFloat(const CallbackInfo& info) {
Number number = info[0].As<Number>();
return Boolean::New(info.Env(), number.FloatValue() == static_cast<float>(number));
}

Value OperatorDouble(const CallbackInfo& info) {
Number number = info[0].As<Number>();
return Boolean::New(info.Env(), number.DoubleValue() == static_cast<double>(number));
}

Value CreateEmptyNumber(const CallbackInfo& info) {
Number* number = new Number();
return Boolean::New(info.Env(), number->IsEmpty());
}

Value CreateNumberFromExistingValue(const CallbackInfo& info) {
return info[0].As<Number>();
}

Object InitBasicTypesNumber(Env env) {
Object exports = Object::New(env);

Expand All @@ -53,6 +87,13 @@ Object InitBasicTypesNumber(Env env) {
exports["maxFloat"] = Function::New(env, MaxFloat);
exports["minDouble"] = Function::New(env, MinDouble);
exports["maxDouble"] = Function::New(env, MaxDouble);
exports["operatorInt32"] = Function::New(env, OperatorInt32);
exports["operatorUint32"] = Function::New(env, OperatorUint32);
exports["operatorInt64"] = Function::New(env, OperatorInt64);
exports["operatorFloat"] = Function::New(env, OperatorFloat);
exports["operatorDouble"] = Function::New(env, OperatorDouble);
exports["createEmptyNumber"] = Function::New(env, CreateEmptyNumber);
exports["createNumberFromExistingValue"] = Function::New(env, CreateNumberFromExistingValue);

return exports;
}
33 changes: 33 additions & 0 deletions test/basic_types/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,37 @@ function test(binding) {
assert.strictEqual(0, binding.basic_types_number.toDouble(MIN_DOUBLE * MIN_DOUBLE));
assert.strictEqual(Infinity, binding.basic_types_number.toDouble(MAX_DOUBLE * MAX_DOUBLE));
}

// Test for operator overloading
{
assert.strictEqual(binding.basic_types_number.operatorInt32(MIN_INT32), true);
assert.strictEqual(binding.basic_types_number.operatorInt32(MAX_INT32), true);
assert.strictEqual(binding.basic_types_number.operatorUint32(MIN_UINT32), true);
assert.strictEqual(binding.basic_types_number.operatorUint32(MAX_UINT32), true);
assert.strictEqual(binding.basic_types_number.operatorInt64(MIN_INT64), true);
assert.strictEqual(binding.basic_types_number.operatorInt64(MAX_INT64), true);
assert.strictEqual(binding.basic_types_number.operatorFloat(MIN_FLOAT), true);
assert.strictEqual(binding.basic_types_number.operatorFloat(MAX_FLOAT), true);
assert.strictEqual(binding.basic_types_number.operatorFloat(MAX_DOUBLE), true);
assert.strictEqual(binding.basic_types_number.operatorDouble(MIN_DOUBLE), true);
assert.strictEqual(binding.basic_types_number.operatorDouble(MAX_DOUBLE), true);
}

// Construction test
{
    assert.strictEqual(binding.basic_types_number.createEmptyNumber(), true);
   randomRangeTestForInteger(MIN_INT32, MAX_INT32, binding.basic_types_number.createNumberFromExistingValue);
   assert.strictEqual(MIN_INT32, binding.basic_types_number.createNumberFromExistingValue(MIN_INT32));
   assert.strictEqual(MAX_INT32, binding.basic_types_number.createNumberFromExistingValue(MAX_INT32));
   randomRangeTestForInteger(MIN_UINT32, MAX_UINT32, binding.basic_types_number.createNumberFromExistingValue);
   assert.strictEqual(MIN_UINT32, binding.basic_types_number.createNumberFromExistingValue(MIN_UINT32));
   assert.strictEqual(MAX_UINT32, binding.basic_types_number.createNumberFromExistingValue(MAX_UINT32));
   randomRangeTestForInteger(MIN_INT64, MAX_INT64, binding.basic_types_number.createNumberFromExistingValue);
   assert.strictEqual(MIN_INT64, binding.basic_types_number.createNumberFromExistingValue(MIN_INT64));
   assert.strictEqual(MAX_INT64, binding.basic_types_number.createNumberFromExistingValue(MAX_INT64));
    assert.strictEqual(MIN_FLOAT, binding.basic_types_number.createNumberFromExistingValue(MIN_FLOAT));
    assert.strictEqual(MAX_FLOAT, binding.basic_types_number.createNumberFromExistingValue(MAX_FLOAT));
    assert.strictEqual(MIN_DOUBLE, binding.basic_types_number.createNumberFromExistingValue(MIN_DOUBLE));
    assert.strictEqual(MAX_DOUBLE, binding.basic_types_number.createNumberFromExistingValue(MAX_DOUBLE));
}
}

0 comments on commit 779560f

Please sign in to comment.