OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/logging.h" | 6 #include "base/logging.h" |
7 #include "net/base/int128.h" | 7 #include "net/base/int128.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 | 10 |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 for (int i = 0; i < 64; ++i) { | 223 for (int i = 0; i < 64; ++i) { |
224 for (int j = 0; j < 64; ++j) { | 224 for (int j = 0; j < 64; ++j) { |
225 a = uint128(1) << i; | 225 a = uint128(1) << i; |
226 b = uint128(1) << j; | 226 b = uint128(1) << j; |
227 c = a * b; | 227 c = a * b; |
228 EXPECT_EQ(uint128(1) << (i+j), c); | 228 EXPECT_EQ(uint128(1) << (i+j), c); |
229 } | 229 } |
230 } | 230 } |
231 | 231 |
232 // Verified with dc. | 232 // Verified with dc. |
233 a = uint128(GG_ULONGLONG(0xffffeeeeddddcccc), | 233 a = uint128(0xffffeeeeddddccccULL, 0xbbbbaaaa99998888ULL); |
234 GG_ULONGLONG(0xbbbbaaaa99998888)); | 234 b = uint128(0x7777666655554444ULL, 0x3333222211110000ULL); |
235 b = uint128(GG_ULONGLONG(0x7777666655554444), | |
236 GG_ULONGLONG(0x3333222211110000)); | |
237 c = a * b; | 235 c = a * b; |
238 EXPECT_EQ(uint128(GG_ULONGLONG(0x530EDA741C71D4C3), | 236 EXPECT_EQ(uint128(0x530EDA741C71D4C3ULL, 0xBF25975319080000ULL), c); |
239 GG_ULONGLONG(0xBF25975319080000)), c); | |
240 EXPECT_EQ(0, c - b * a); | 237 EXPECT_EQ(0, c - b * a); |
241 EXPECT_EQ(a*a - b*b, (a+b) * (a-b)); | 238 EXPECT_EQ(a*a - b*b, (a+b) * (a-b)); |
242 | 239 |
243 // Verified with dc. | 240 // Verified with dc. |
244 a = uint128(GG_ULONGLONG(0x0123456789abcdef), | 241 a = uint128(0x0123456789abcdefULL, 0xfedcba9876543210ULL); |
245 GG_ULONGLONG(0xfedcba9876543210)); | 242 b = uint128(0x02468ace13579bdfULL, 0xfdb97531eca86420ULL); |
246 b = uint128(GG_ULONGLONG(0x02468ace13579bdf), | |
247 GG_ULONGLONG(0xfdb97531eca86420)); | |
248 c = a * b; | 243 c = a * b; |
249 EXPECT_EQ(uint128(GG_ULONGLONG(0x97a87f4f261ba3f2), | 244 EXPECT_EQ(uint128(0x97a87f4f261ba3f2ULL, 0x342d0bbf48948200ULL), c); |
250 GG_ULONGLONG(0x342d0bbf48948200)), c); | |
251 EXPECT_EQ(0, c - b * a); | 245 EXPECT_EQ(0, c - b * a); |
252 EXPECT_EQ(a*a - b*b, (a+b) * (a-b)); | 246 EXPECT_EQ(a*a - b*b, (a+b) * (a-b)); |
253 } | 247 } |
254 | 248 |
255 TEST(Int128, AliasTests) { | 249 TEST(Int128, AliasTests) { |
256 uint128 x1(1, 2); | 250 uint128 x1(1, 2); |
257 uint128 x2(2, 4); | 251 uint128 x2(2, 4); |
258 x1 += x1; | 252 x1 += x1; |
259 EXPECT_EQ(x2, x1); | 253 EXPECT_EQ(x2, x1); |
260 | 254 |
261 uint128 x3(1, 1ull << 63); | 255 uint128 x3(1, 1ull << 63); |
262 uint128 x4(3, 0); | 256 uint128 x4(3, 0); |
263 x3 += x3; | 257 x3 += x3; |
264 EXPECT_EQ(x4, x3); | 258 EXPECT_EQ(x4, x3); |
265 } | 259 } |
OLD | NEW |