1 // Copyright (c) 2012 The Chromium 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.
5 #include "base/basictypes.h"
6 #include "base/logging.h"
7 #include "net/base/int128.h"
8 #include "testing/gtest/include/gtest/gtest.h"
11 TEST(Int128
, AllTests
) {
14 uint128
one_2arg(0, 1);
18 uint128
big_minus_one(2000, 1);
19 uint128
bigger(2001, 1);
20 uint128
biggest(kuint128max
);
21 uint128
high_low(1, 0);
22 uint128
low_high(0, kuint64max
);
27 EXPECT_EQ(one
, one_2arg
);
31 EXPECT_GE(big
, big_minus_one
);
32 EXPECT_GT(big
, big_minus_one
);
33 EXPECT_LT(big_minus_one
, big
);
34 EXPECT_LE(big_minus_one
, big
);
35 EXPECT_NE(big_minus_one
, big
);
36 EXPECT_LT(big
, biggest
);
37 EXPECT_LE(big
, biggest
);
38 EXPECT_GT(biggest
, big
);
39 EXPECT_GE(biggest
, big
);
40 EXPECT_EQ(big
, ~~big
);
41 EXPECT_EQ(one
, one
| one
);
42 EXPECT_EQ(big
, big
| big
);
43 EXPECT_EQ(one
, one
| zero
);
44 EXPECT_EQ(one
, one
& one
);
45 EXPECT_EQ(big
, big
& big
);
46 EXPECT_EQ(zero
, one
& zero
);
47 EXPECT_EQ(zero
, big
& ~big
);
48 EXPECT_EQ(zero
, one
^ one
);
49 EXPECT_EQ(zero
, big
^ big
);
50 EXPECT_EQ(one
, one
^ zero
);
51 EXPECT_EQ(big
, big
<< 0);
52 EXPECT_EQ(big
, big
>> 0);
53 EXPECT_GT(big
<< 1, big
);
54 EXPECT_LT(big
>> 1, big
);
55 EXPECT_EQ(big
, (big
<< 10) >> 10);
56 EXPECT_EQ(big
, (big
>> 1) << 1);
57 EXPECT_EQ(one
, (one
<< 80) >> 80);
58 EXPECT_EQ(zero
, (one
>> 80) << 80);
59 EXPECT_EQ(zero
, big
>> 128);
60 EXPECT_EQ(zero
, big
<< 128);
61 EXPECT_EQ(Uint128High64(biggest
), kuint64max
);
62 EXPECT_EQ(Uint128Low64(biggest
), kuint64max
);
63 EXPECT_EQ(zero
+ one
, one
);
64 EXPECT_EQ(one
+ one
, two
);
65 EXPECT_EQ(big_minus_one
+ one
, big
);
66 EXPECT_EQ(one
- one
, zero
);
67 EXPECT_EQ(one
- zero
, one
);
68 EXPECT_EQ(zero
- one
, biggest
);
69 EXPECT_EQ(big
- big
, zero
);
70 EXPECT_EQ(big
- one
, big_minus_one
);
71 EXPECT_EQ(big
+ kuint64max
, bigger
);
72 EXPECT_EQ(biggest
+ 1, zero
);
73 EXPECT_EQ(zero
- 1, biggest
);
74 EXPECT_EQ(high_low
- one
, low_high
);
75 EXPECT_EQ(low_high
+ one
, high_low
);
76 EXPECT_EQ(Uint128High64((uint128(1) << 64) - 1), 0u);
77 EXPECT_EQ(Uint128Low64((uint128(1) << 64) - 1), kuint64max
);
79 EXPECT_TRUE(!!high_low
);
82 EXPECT_FALSE(!high_low
);
84 EXPECT_TRUE(zero
== 0);
85 EXPECT_FALSE(zero
!= 0);
86 EXPECT_FALSE(one
== 0);
87 EXPECT_TRUE(one
!= 0);
90 EXPECT_EQ(++test
, one
);
92 EXPECT_EQ(test
++, one
);
94 EXPECT_EQ(test
-= 2, zero
);
95 EXPECT_EQ(test
, zero
);
96 EXPECT_EQ(test
+= 2, two
);
98 EXPECT_EQ(--test
, one
);
100 EXPECT_EQ(test
--, one
);
101 EXPECT_EQ(test
, zero
);
102 EXPECT_EQ(test
|= three
, three
);
103 EXPECT_EQ(test
&= one
, one
);
104 EXPECT_EQ(test
^= three
, two
);
105 EXPECT_EQ(test
>>= 1, one
);
106 EXPECT_EQ(test
<<= 1, two
);
108 EXPECT_EQ(big
, -(-big
));
109 EXPECT_EQ(two
, -((-one
) - 1));
110 EXPECT_EQ(kuint128max
, -one
);
111 EXPECT_EQ(zero
, -zero
);
114 LOG(INFO
) << big_minus_one
;
117 TEST(Int128
, PodTests
) {
118 uint128_pod pod
= { 12345, 67890 };
119 uint128
from_pod(pod
);
120 EXPECT_EQ(12345u, Uint128High64(from_pod
));
121 EXPECT_EQ(67890u, Uint128Low64(from_pod
));
124 uint128_pod zero_pod
= {0, 0};
126 uint128_pod one_pod
= {0, 1};
128 uint128_pod two_pod
= {0, 2};
130 uint128_pod three_pod
= {0, 3};
132 uint128_pod big_pod
= {1, 0};
134 EXPECT_EQ(zero
, zero_pod
);
135 EXPECT_EQ(zero_pod
, zero
);
136 EXPECT_EQ(zero_pod
, zero_pod
);
137 EXPECT_EQ(one
, one_pod
);
138 EXPECT_EQ(one_pod
, one
);
139 EXPECT_EQ(one_pod
, one_pod
);
140 EXPECT_EQ(two
, two_pod
);
141 EXPECT_EQ(two_pod
, two
);
142 EXPECT_EQ(two_pod
, two_pod
);
144 EXPECT_NE(one
, two_pod
);
145 EXPECT_NE(one_pod
, two
);
146 EXPECT_NE(one_pod
, two_pod
);
148 EXPECT_LT(one
, two_pod
);
149 EXPECT_LT(one_pod
, two
);
150 EXPECT_LT(one_pod
, two_pod
);
151 EXPECT_LE(one
, one_pod
);
152 EXPECT_LE(one_pod
, one
);
153 EXPECT_LE(one_pod
, one_pod
);
154 EXPECT_LE(one
, two_pod
);
155 EXPECT_LE(one_pod
, two
);
156 EXPECT_LE(one_pod
, two_pod
);
158 EXPECT_GT(two
, one_pod
);
159 EXPECT_GT(two_pod
, one
);
160 EXPECT_GT(two_pod
, one_pod
);
161 EXPECT_GE(two
, two_pod
);
162 EXPECT_GE(two_pod
, two
);
163 EXPECT_GE(two_pod
, two_pod
);
164 EXPECT_GE(two
, one_pod
);
165 EXPECT_GE(two_pod
, one
);
166 EXPECT_GE(two_pod
, one_pod
);
168 EXPECT_EQ(three
, one
| two_pod
);
169 EXPECT_EQ(three
, one_pod
| two
);
170 EXPECT_EQ(three
, one_pod
| two_pod
);
171 EXPECT_EQ(one
, three
& one_pod
);
172 EXPECT_EQ(one
, three_pod
& one
);
173 EXPECT_EQ(one
, three_pod
& one_pod
);
174 EXPECT_EQ(two
, three
^ one_pod
);
175 EXPECT_EQ(two
, three_pod
^ one
);
176 EXPECT_EQ(two
, three_pod
^ one_pod
);
177 EXPECT_EQ(two
, three
& (~one
));
178 EXPECT_EQ(three
, ~~three
);
180 EXPECT_EQ(two
, two_pod
<< 0);
181 EXPECT_EQ(two
, one_pod
<< 1);
182 EXPECT_EQ(big
, one_pod
<< 64);
183 EXPECT_EQ(zero
, one_pod
<< 128);
184 EXPECT_EQ(two
, two_pod
>> 0);
185 EXPECT_EQ(one
, two_pod
>> 1);
186 EXPECT_EQ(one
, big_pod
>> 64);
188 EXPECT_EQ(one
, zero
+ one_pod
);
189 EXPECT_EQ(one
, zero_pod
+ one
);
190 EXPECT_EQ(one
, zero_pod
+ one_pod
);
191 EXPECT_EQ(one
, two
- one_pod
);
192 EXPECT_EQ(one
, two_pod
- one
);
193 EXPECT_EQ(one
, two_pod
- one_pod
);
196 TEST(Int128
, OperatorAssignReturnRef
) {
202 TEST(Int128
, Multiply
) {
217 // Self-operation with max carries.
223 for (int i
= 0; i
< 64; ++i
) {
224 for (int j
= 0; j
< 64; ++j
) {
228 EXPECT_EQ(uint128(1) << (i
+j
), c
);
233 a
= uint128(0xffffeeeeddddccccULL
, 0xbbbbaaaa99998888ULL
);
234 b
= uint128(0x7777666655554444ULL
, 0x3333222211110000ULL
);
236 EXPECT_EQ(uint128(0x530EDA741C71D4C3ULL
, 0xBF25975319080000ULL
), c
);
237 EXPECT_EQ(0, c
- b
* a
);
238 EXPECT_EQ(a
*a
- b
*b
, (a
+b
) * (a
-b
));
241 a
= uint128(0x0123456789abcdefULL
, 0xfedcba9876543210ULL
);
242 b
= uint128(0x02468ace13579bdfULL
, 0xfdb97531eca86420ULL
);
244 EXPECT_EQ(uint128(0x97a87f4f261ba3f2ULL
, 0x342d0bbf48948200ULL
), c
);
245 EXPECT_EQ(0, c
- b
* a
);
246 EXPECT_EQ(a
*a
- b
*b
, (a
+b
) * (a
-b
));
249 TEST(Int128
, AliasTests
) {
255 uint128
x3(1, 1ull << 63);