Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: add operator overloading tests for Number class #355

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}