2017-10-19 00:38:51 +00:00
|
|
|
// Copyright 2017 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
2019-05-22 07:55:37 +00:00
|
|
|
#include "src/execution/isolate.h"
|
2018-04-09 19:11:22 +00:00
|
|
|
#include "src/heap/factory.h"
|
2019-05-15 18:28:45 +00:00
|
|
|
#include "src/numbers/conversions.h"
|
2017-11-17 20:53:16 +00:00
|
|
|
#include "src/objects/bigint.h"
|
2019-05-23 08:51:46 +00:00
|
|
|
#include "src/objects/objects-inl.h"
|
2017-10-19 00:38:51 +00:00
|
|
|
#include "test/unittests/test-utils.h"
|
|
|
|
|
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2019-05-27 11:31:49 +00:00
|
|
|
using BigIntWithIsolate = TestWithIsolate;
|
2017-10-19 00:38:51 +00:00
|
|
|
|
|
|
|
void Compare(Handle<BigInt> x, double value, ComparisonResult expected) {
|
|
|
|
CHECK_EQ(expected, BigInt::CompareToDouble(x, value));
|
|
|
|
}
|
|
|
|
|
2017-11-17 20:53:16 +00:00
|
|
|
Handle<BigInt> NewFromInt(Isolate* isolate, int value) {
|
|
|
|
Handle<Smi> smi_value = handle(Smi::FromInt(value), isolate);
|
|
|
|
return BigInt::FromNumber(isolate, smi_value).ToHandleChecked();
|
|
|
|
}
|
|
|
|
|
2017-10-19 00:38:51 +00:00
|
|
|
TEST_F(BigIntWithIsolate, CompareToDouble) {
|
2017-11-17 20:53:16 +00:00
|
|
|
Handle<BigInt> zero = NewFromInt(isolate(), 0);
|
|
|
|
Handle<BigInt> one = NewFromInt(isolate(), 1);
|
|
|
|
Handle<BigInt> minus_one = NewFromInt(isolate(), -1);
|
2017-10-19 00:38:51 +00:00
|
|
|
|
|
|
|
// Non-finite doubles.
|
|
|
|
Compare(zero, std::nan(""), ComparisonResult::kUndefined);
|
|
|
|
Compare(one, INFINITY, ComparisonResult::kLessThan);
|
|
|
|
Compare(one, -INFINITY, ComparisonResult::kGreaterThan);
|
|
|
|
|
|
|
|
// Unequal sign.
|
|
|
|
Compare(one, -1, ComparisonResult::kGreaterThan);
|
|
|
|
Compare(minus_one, 1, ComparisonResult::kLessThan);
|
|
|
|
|
|
|
|
// Cases involving zero.
|
|
|
|
Compare(zero, 0, ComparisonResult::kEqual);
|
|
|
|
Compare(zero, -0, ComparisonResult::kEqual);
|
|
|
|
Compare(one, 0, ComparisonResult::kGreaterThan);
|
|
|
|
Compare(minus_one, 0, ComparisonResult::kLessThan);
|
|
|
|
Compare(zero, 1, ComparisonResult::kLessThan);
|
|
|
|
Compare(zero, -1, ComparisonResult::kGreaterThan);
|
|
|
|
|
|
|
|
// Small doubles.
|
|
|
|
Compare(zero, 0.25, ComparisonResult::kLessThan);
|
|
|
|
Compare(one, 0.5, ComparisonResult::kGreaterThan);
|
|
|
|
Compare(one, -0.5, ComparisonResult::kGreaterThan);
|
|
|
|
Compare(zero, -0.25, ComparisonResult::kGreaterThan);
|
|
|
|
Compare(minus_one, -0.5, ComparisonResult::kLessThan);
|
|
|
|
|
|
|
|
// Different bit lengths.
|
2017-11-17 20:53:16 +00:00
|
|
|
Handle<BigInt> four = NewFromInt(isolate(), 4);
|
|
|
|
Handle<BigInt> minus_five = NewFromInt(isolate(), -5);
|
2017-10-19 00:38:51 +00:00
|
|
|
Compare(four, 3.9, ComparisonResult::kGreaterThan);
|
|
|
|
Compare(four, 1.5, ComparisonResult::kGreaterThan);
|
|
|
|
Compare(four, 8, ComparisonResult::kLessThan);
|
|
|
|
Compare(four, 16, ComparisonResult::kLessThan);
|
|
|
|
Compare(minus_five, -4.9, ComparisonResult::kLessThan);
|
|
|
|
Compare(minus_five, -4, ComparisonResult::kLessThan);
|
|
|
|
Compare(minus_five, -25, ComparisonResult::kGreaterThan);
|
|
|
|
|
|
|
|
// Same bit length, difference in first digit.
|
|
|
|
double big_double = 4428155326412785451008.0;
|
|
|
|
Handle<BigInt> big =
|
2017-10-19 17:39:28 +00:00
|
|
|
BigIntLiteral(isolate(), "0xF10D00000000000000").ToHandleChecked();
|
2017-10-19 00:38:51 +00:00
|
|
|
Compare(big, big_double, ComparisonResult::kGreaterThan);
|
2017-10-19 17:39:28 +00:00
|
|
|
big = BigIntLiteral(isolate(), "0xE00D00000000000000").ToHandleChecked();
|
2017-10-19 00:38:51 +00:00
|
|
|
Compare(big, big_double, ComparisonResult::kLessThan);
|
|
|
|
|
|
|
|
double other_double = -13758438578910658560.0;
|
|
|
|
Handle<BigInt> other =
|
2017-10-19 17:39:28 +00:00
|
|
|
BigIntLiteral(isolate(), "-0xBEEFC1FE00000000").ToHandleChecked();
|
2017-10-19 00:38:51 +00:00
|
|
|
Compare(other, other_double, ComparisonResult::kGreaterThan);
|
2017-10-19 17:39:28 +00:00
|
|
|
other = BigIntLiteral(isolate(), "-0xBEEFCBFE00000000").ToHandleChecked();
|
2017-10-19 00:38:51 +00:00
|
|
|
Compare(other, other_double, ComparisonResult::kLessThan);
|
|
|
|
|
|
|
|
// Same bit length, difference in non-first digit.
|
2017-10-19 17:39:28 +00:00
|
|
|
big = BigIntLiteral(isolate(), "0xF00D00000000000001").ToHandleChecked();
|
2017-10-19 00:38:51 +00:00
|
|
|
Compare(big, big_double, ComparisonResult::kGreaterThan);
|
2017-10-19 17:39:28 +00:00
|
|
|
big = BigIntLiteral(isolate(), "0xF00A00000000000000").ToHandleChecked();
|
2017-10-19 00:38:51 +00:00
|
|
|
Compare(big, big_double, ComparisonResult::kLessThan);
|
|
|
|
|
2017-10-19 17:39:28 +00:00
|
|
|
other = BigIntLiteral(isolate(), "-0xBEEFCAFE00000001").ToHandleChecked();
|
2017-10-19 00:38:51 +00:00
|
|
|
Compare(other, other_double, ComparisonResult::kLessThan);
|
|
|
|
|
|
|
|
// Same bit length, difference in fractional part.
|
|
|
|
Compare(one, 1.5, ComparisonResult::kLessThan);
|
|
|
|
Compare(minus_one, -1.25, ComparisonResult::kGreaterThan);
|
2017-11-17 20:53:16 +00:00
|
|
|
big = NewFromInt(isolate(), 0xF00D00);
|
2017-10-19 00:38:51 +00:00
|
|
|
Compare(big, 15731968.125, ComparisonResult::kLessThan);
|
|
|
|
Compare(big, 15731967.875, ComparisonResult::kGreaterThan);
|
2017-12-02 00:30:37 +00:00
|
|
|
big = BigIntLiteral(isolate(), "0x123456789AB").ToHandleChecked();
|
2017-10-19 00:38:51 +00:00
|
|
|
Compare(big, 1250999896491.125, ComparisonResult::kLessThan);
|
|
|
|
|
|
|
|
// Equality!
|
|
|
|
Compare(one, 1, ComparisonResult::kEqual);
|
|
|
|
Compare(minus_one, -1, ComparisonResult::kEqual);
|
2017-10-19 17:39:28 +00:00
|
|
|
big = BigIntLiteral(isolate(), "0xF00D00000000000000").ToHandleChecked();
|
2017-10-19 00:38:51 +00:00
|
|
|
Compare(big, big_double, ComparisonResult::kEqual);
|
|
|
|
|
|
|
|
Handle<BigInt> two_52 =
|
2017-10-19 17:39:28 +00:00
|
|
|
BigIntLiteral(isolate(), "0x10000000000000").ToHandleChecked();
|
2017-10-19 00:38:51 +00:00
|
|
|
Compare(two_52, 4503599627370496.0, ComparisonResult::kEqual);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|