Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / base / int128_unittest.cc
blob957038bffd3b8c047e1095d4752c092d67fac05d
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) {
12 uint128 zero(0);
13 uint128 one(1);
14 uint128 one_2arg(0, 1);
15 uint128 two(0, 2);
16 uint128 three(0, 3);
17 uint128 big(2000, 2);
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);
23 EXPECT_LT(one, two);
24 EXPECT_GT(two, one);
25 EXPECT_LT(one, big);
26 EXPECT_LT(one, big);
27 EXPECT_EQ(one, one_2arg);
28 EXPECT_NE(one, two);
29 EXPECT_GT(big, one);
30 EXPECT_GE(big, two);
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);
78 EXPECT_TRUE(!!one);
79 EXPECT_TRUE(!!high_low);
80 EXPECT_FALSE(!!zero);
81 EXPECT_FALSE(!one);
82 EXPECT_FALSE(!high_low);
83 EXPECT_TRUE(!zero);
84 EXPECT_TRUE(zero == 0);
85 EXPECT_FALSE(zero != 0);
86 EXPECT_FALSE(one == 0);
87 EXPECT_TRUE(one != 0);
89 uint128 test = zero;
90 EXPECT_EQ(++test, one);
91 EXPECT_EQ(test, one);
92 EXPECT_EQ(test++, one);
93 EXPECT_EQ(test, two);
94 EXPECT_EQ(test -= 2, zero);
95 EXPECT_EQ(test, zero);
96 EXPECT_EQ(test += 2, two);
97 EXPECT_EQ(test, two);
98 EXPECT_EQ(--test, one);
99 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);
113 LOG(INFO) << one;
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));
123 uint128 zero(0);
124 uint128_pod zero_pod = {0, 0};
125 uint128 one(1);
126 uint128_pod one_pod = {0, 1};
127 uint128 two(2);
128 uint128_pod two_pod = {0, 2};
129 uint128 three(3);
130 uint128_pod three_pod = {0, 3};
131 uint128 big(1, 0);
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) {
197 uint128 v(1);
198 (v += 4) -= 3;
199 EXPECT_EQ(2, v);
202 TEST(Int128, Multiply) {
203 uint128 a, b, c;
205 // Zero test.
206 a = 0;
207 b = 0;
208 c = a * b;
209 EXPECT_EQ(0, c);
211 // Max carries.
212 a = uint128(0) - 1;
213 b = uint128(0) - 1;
214 c = a * b;
215 EXPECT_EQ(1, c);
217 // Self-operation with max carries.
218 c = uint128(0) - 1;
219 c *= c;
220 EXPECT_EQ(1, c);
222 // 1-bit x 1-bit.
223 for (int i = 0; i < 64; ++i) {
224 for (int j = 0; j < 64; ++j) {
225 a = uint128(1) << i;
226 b = uint128(1) << j;
227 c = a * b;
228 EXPECT_EQ(uint128(1) << (i+j), c);
232 // Verified with dc.
233 a = uint128(0xffffeeeeddddccccULL, 0xbbbbaaaa99998888ULL);
234 b = uint128(0x7777666655554444ULL, 0x3333222211110000ULL);
235 c = a * b;
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));
240 // Verified with dc.
241 a = uint128(0x0123456789abcdefULL, 0xfedcba9876543210ULL);
242 b = uint128(0x02468ace13579bdfULL, 0xfdb97531eca86420ULL);
243 c = a * b;
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) {
250 uint128 x1(1, 2);
251 uint128 x2(2, 4);
252 x1 += x1;
253 EXPECT_EQ(x2, x1);
255 uint128 x3(1, 1ull << 63);
256 uint128 x4(3, 0);
257 x3 += x3;
258 EXPECT_EQ(x4, x3);