31 #include <google/protobuf/stubs/int128.h>
37 #include <google/protobuf/testing/googletest.h>
38 #include <gtest/gtest.h>
40 #include <google/protobuf/port_def.inc>
45 TEST(Int128, AllTests) {
48 uint128 one_2arg(0, 1);
52 uint128 big_minus_one(2000, 1);
53 uint128 bigger(2001, 1);
55 uint128 high_low(1, 0);
99 uint128 big_copy = big;
180 TEST(Int128, PodTests) {
181 uint128_pod pod = { 12345, 67890 };
182 uint128 from_pod(pod);
187 uint128_pod zero_pod = {0, 0};
189 uint128_pod one_pod = {0, 1};
191 uint128_pod two_pod = {0, 2};
193 uint128_pod three_pod = {0, 3};
195 uint128_pod big_pod = {1, 0};
259 TEST(Int128, OperatorAssignReturnRef) {
265 TEST(Int128, Multiply) {
286 for (
int i = 0;
i < 64; ++
i) {
287 for (
int j = 0;
j < 64; ++
j) {
314 TEST(Int128, AliasTests) {
320 uint128 x3(1,
static_cast<uint64>(1) << 63);
326 #ifdef PROTOBUF_HAS_DEATH_TEST
327 TEST(Int128, DivideByZeroCheckFails) {
330 EXPECT_DEATH(a /
b,
"Division or mod by zero:");
332 EXPECT_DEATH(a /
b,
"Division or mod by zero:");
335 TEST(Int128, ModByZeroCheckFails) {
338 EXPECT_DEATH(a %
b,
"Division or mod by zero:");
340 EXPECT_DEATH(a %
b,
"Division or mod by zero:");
342 #endif // PROTOBUF_HAS_DEATH_TEST
344 TEST(Int128, DivideAndMod) {
358 b = uint128(0x1110001);
359 r = uint128(0x3eb455);
362 uint128 result_q, result_r;
395 uint128 expected_r(
uint64_t{0x29876d3a0e38ea61
u},
413 TEST(Int128, DivideAndModRandomInputs) {
414 const int kNumIters = 1 << 18;
415 for (
int i = 0;
i < kNumIters; ++
i) {
421 const uint128 q =
a /
b;
422 const uint128
r =
a %
b;
427 #ifdef GOOGLE_PROTOBUF_HAS_CONSTEXPR
428 TEST(Int128, ConstexprTest) {
429 constexpr uint128 zero;
430 constexpr uint128 one = 1;
431 constexpr uint128_pod pod = {2, 3};
432 constexpr uint128 from_pod = pod;
433 constexpr uint128 minus_two = -2;
439 TEST(Int128, Traits) {
444 #endif // GOOGLE_PROTOBUF_HAS_CONSTEXPR
446 TEST(Int128, OStream) {
449 std::ios_base::fmtflags
flags;
450 std::streamsize
width;
455 {uint128(0), std::ios::dec, 0,
'_',
"0"},
456 {uint128(0), std::ios::oct, 0,
'_',
"0"},
457 {uint128(0), std::ios::hex, 0,
'_',
"0"},
459 {uint128(0, -1), std::ios::dec, 0,
'_',
"18446744073709551615"},
460 {uint128(0, -1), std::ios::oct, 0,
'_',
"1777777777777777777777"},
461 {uint128(0, -1), std::ios::hex, 0,
'_',
"ffffffffffffffff"},
462 {uint128(1, 0), std::ios::dec, 0,
'_',
"18446744073709551616"},
463 {uint128(1, 0), std::ios::oct, 0,
'_',
"2000000000000000000000"},
464 {uint128(1, 0), std::ios::hex, 0,
'_',
"10000000000000000"},
466 {uint128(
uint64_t{0x8000000000000000
u}, 0), std::ios::dec, 0,
'_',
467 "170141183460469231731687303715884105728"},
468 {uint128(
uint64_t{0x8000000000000000
u}, 0), std::ios::oct, 0,
'_',
469 "2000000000000000000000000000000000000000000"},
470 {uint128(
uint64_t{0x8000000000000000
u}, 0), std::ios::hex, 0,
'_',
471 "80000000000000000000000000000000"},
473 {uint128(-1, -1), std::ios::dec, 0,
'_',
474 "340282366920938463463374607431768211455"},
475 {uint128(-1, -1), std::ios::oct, 0,
'_',
476 "3777777777777777777777777777777777777777777"},
477 {uint128(-1, -1), std::ios::hex, 0,
'_',
478 "ffffffffffffffffffffffffffffffff"},
480 {uint128(-1, -1), std::ios::hex | std::ios::uppercase, 0,
'_',
481 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"},
483 {uint128(1), std::ios::dec | std::ios::showbase, 0,
'_',
"1"},
484 {uint128(1), std::ios::oct | std::ios::showbase, 0,
'_',
"01"},
485 {uint128(1), std::ios::hex | std::ios::showbase, 0,
'_',
"0x1"},
487 {uint128(0), std::ios::dec | std::ios::showbase, 0,
'_',
"0"},
488 {uint128(0), std::ios::oct | std::ios::showbase, 0,
'_',
"0"},
489 {uint128(0), std::ios::hex | std::ios::showbase, 0,
'_',
"0"},
491 {uint128(1), std::ios::dec | std::ios::showpos, 0,
'_',
"1"},
493 {uint128(9), std::ios::dec, 6,
'_',
"_____9"},
494 {uint128(12345), std::ios::dec, 6,
'_',
"_12345"},
496 {uint128(9), std::ios::dec | std::ios::left, 6,
'_',
"9_____"},
497 {uint128(12345), std::ios::dec | std::ios::left, 6,
'_',
"12345_"},
500 std::ostringstream os;
501 os.flags(cases[i].
flags);
502 os.width(cases[i].
width);
503 os.fill(cases[i].
fill);
511 #include <google/protobuf/port_undef.inc>