Line data Source code
1 : // Copyright 2017 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include <cmath>
6 :
7 : #include "src/conversions.h"
8 : #include "src/heap/factory.h"
9 : #include "src/isolate.h"
10 : #include "src/objects-inl.h"
11 : #include "src/objects/bigint.h"
12 : #include "test/unittests/test-utils.h"
13 :
14 : #include "testing/gtest/include/gtest/gtest.h"
15 :
16 : namespace v8 {
17 : namespace internal {
18 :
19 : typedef TestWithIsolate BigIntWithIsolate;
20 :
21 39 : void Compare(Handle<BigInt> x, double value, ComparisonResult expected) {
22 39 : CHECK_EQ(expected, BigInt::CompareToDouble(x, value));
23 39 : }
24 :
25 6 : Handle<BigInt> NewFromInt(Isolate* isolate, int value) {
26 : Handle<Smi> smi_value = handle(Smi::FromInt(value), isolate);
27 12 : return BigInt::FromNumber(isolate, smi_value).ToHandleChecked();
28 : }
29 :
30 15444 : TEST_F(BigIntWithIsolate, CompareToDouble) {
31 1 : Handle<BigInt> zero = NewFromInt(isolate(), 0);
32 1 : Handle<BigInt> one = NewFromInt(isolate(), 1);
33 1 : Handle<BigInt> minus_one = NewFromInt(isolate(), -1);
34 :
35 : // Non-finite doubles.
36 1 : Compare(zero, std::nan(""), ComparisonResult::kUndefined);
37 1 : Compare(one, INFINITY, ComparisonResult::kLessThan);
38 1 : Compare(one, -INFINITY, ComparisonResult::kGreaterThan);
39 :
40 : // Unequal sign.
41 1 : Compare(one, -1, ComparisonResult::kGreaterThan);
42 1 : Compare(minus_one, 1, ComparisonResult::kLessThan);
43 :
44 : // Cases involving zero.
45 1 : Compare(zero, 0, ComparisonResult::kEqual);
46 1 : Compare(zero, -0, ComparisonResult::kEqual);
47 1 : Compare(one, 0, ComparisonResult::kGreaterThan);
48 1 : Compare(minus_one, 0, ComparisonResult::kLessThan);
49 1 : Compare(zero, 1, ComparisonResult::kLessThan);
50 1 : Compare(zero, -1, ComparisonResult::kGreaterThan);
51 :
52 : // Small doubles.
53 1 : Compare(zero, 0.25, ComparisonResult::kLessThan);
54 1 : Compare(one, 0.5, ComparisonResult::kGreaterThan);
55 1 : Compare(one, -0.5, ComparisonResult::kGreaterThan);
56 1 : Compare(zero, -0.25, ComparisonResult::kGreaterThan);
57 1 : Compare(minus_one, -0.5, ComparisonResult::kLessThan);
58 :
59 : // Different bit lengths.
60 1 : Handle<BigInt> four = NewFromInt(isolate(), 4);
61 1 : Handle<BigInt> minus_five = NewFromInt(isolate(), -5);
62 1 : Compare(four, 3.9, ComparisonResult::kGreaterThan);
63 1 : Compare(four, 1.5, ComparisonResult::kGreaterThan);
64 1 : Compare(four, 8, ComparisonResult::kLessThan);
65 1 : Compare(four, 16, ComparisonResult::kLessThan);
66 1 : Compare(minus_five, -4.9, ComparisonResult::kLessThan);
67 1 : Compare(minus_five, -4, ComparisonResult::kLessThan);
68 1 : Compare(minus_five, -25, ComparisonResult::kGreaterThan);
69 :
70 : // Same bit length, difference in first digit.
71 : double big_double = 4428155326412785451008.0;
72 : Handle<BigInt> big =
73 2 : BigIntLiteral(isolate(), "0xF10D00000000000000").ToHandleChecked();
74 1 : Compare(big, big_double, ComparisonResult::kGreaterThan);
75 2 : big = BigIntLiteral(isolate(), "0xE00D00000000000000").ToHandleChecked();
76 1 : Compare(big, big_double, ComparisonResult::kLessThan);
77 :
78 : double other_double = -13758438578910658560.0;
79 : Handle<BigInt> other =
80 2 : BigIntLiteral(isolate(), "-0xBEEFC1FE00000000").ToHandleChecked();
81 1 : Compare(other, other_double, ComparisonResult::kGreaterThan);
82 2 : other = BigIntLiteral(isolate(), "-0xBEEFCBFE00000000").ToHandleChecked();
83 1 : Compare(other, other_double, ComparisonResult::kLessThan);
84 :
85 : // Same bit length, difference in non-first digit.
86 2 : big = BigIntLiteral(isolate(), "0xF00D00000000000001").ToHandleChecked();
87 1 : Compare(big, big_double, ComparisonResult::kGreaterThan);
88 2 : big = BigIntLiteral(isolate(), "0xF00A00000000000000").ToHandleChecked();
89 1 : Compare(big, big_double, ComparisonResult::kLessThan);
90 :
91 2 : other = BigIntLiteral(isolate(), "-0xBEEFCAFE00000001").ToHandleChecked();
92 1 : Compare(other, other_double, ComparisonResult::kLessThan);
93 :
94 : // Same bit length, difference in fractional part.
95 1 : Compare(one, 1.5, ComparisonResult::kLessThan);
96 1 : Compare(minus_one, -1.25, ComparisonResult::kGreaterThan);
97 1 : big = NewFromInt(isolate(), 0xF00D00);
98 1 : Compare(big, 15731968.125, ComparisonResult::kLessThan);
99 1 : Compare(big, 15731967.875, ComparisonResult::kGreaterThan);
100 2 : big = BigIntLiteral(isolate(), "0x123456789AB").ToHandleChecked();
101 1 : Compare(big, 1250999896491.125, ComparisonResult::kLessThan);
102 :
103 : // Equality!
104 1 : Compare(one, 1, ComparisonResult::kEqual);
105 1 : Compare(minus_one, -1, ComparisonResult::kEqual);
106 2 : big = BigIntLiteral(isolate(), "0xF00D00000000000000").ToHandleChecked();
107 1 : Compare(big, big_double, ComparisonResult::kEqual);
108 :
109 : Handle<BigInt> two_52 =
110 2 : BigIntLiteral(isolate(), "0x10000000000000").ToHandleChecked();
111 1 : Compare(two_52, 4503599627370496.0, ComparisonResult::kEqual);
112 1 : }
113 :
114 : } // namespace internal
115 9264 : } // namespace v8
|