20 #include <type_traits> 24 #include "gtest/gtest.h" 29 #if defined(_MSC_VER) && _MSC_VER == 1900 32 #pragma warning(disable:4146) 38 class Uint128IntegerTraitsTest :
public ::testing::Test {};
39 typedef ::testing::Types<
bool, char,
signed char,
unsigned char, char16_t,
51 class Uint128FloatTraitsTest :
public ::testing::Test {};
52 typedef ::testing::Types<float, double, long double> FloatingPointTypes;
54 TYPED_TEST_SUITE(Uint128IntegerTraitsTest, IntegerTypes);
56 TYPED_TEST(Uint128IntegerTraitsTest, ConstructAssignTest) {
58 "absl::uint128 must be constructible from TypeParam");
60 "absl::uint128 must be assignable from TypeParam");
62 "TypeParam must not be assignable from absl::uint128");
65 TYPED_TEST_SUITE(Uint128FloatTraitsTest, FloatingPointTypes);
67 TYPED_TEST(Uint128FloatTraitsTest, ConstructAssignTest) {
69 "absl::uint128 must be constructible from TypeParam");
71 "absl::uint128 must not be assignable from TypeParam");
73 "TypeParam must not be assignable from absl::uint128");
76 #ifdef ABSL_HAVE_INTRINSIC_INT128 79 TEST(Uint128, IntrinsicTypeTraitsTest) {
81 "absl::uint128 must be constructible from __int128");
83 "absl::uint128 must be assignable from __int128");
85 "__int128 must not be assignable from absl::uint128");
88 "absl::uint128 must be constructible from unsigned __int128");
90 "absl::uint128 must be assignable from unsigned __int128");
92 "unsigned __int128 must not be assignable from absl::uint128");
94 #endif // ABSL_HAVE_INTRINSIC_INT128 96 TEST(Uint128, TrivialTraitsTest) {
105 TEST(Uint128, AllTests) {
122 EXPECT_EQ(one, one_2arg);
126 EXPECT_GE(big, big_minus_one);
127 EXPECT_GT(big, big_minus_one);
128 EXPECT_LT(big_minus_one, big);
129 EXPECT_LE(big_minus_one, big);
130 EXPECT_NE(big_minus_one, big);
131 EXPECT_LT(big, biggest);
132 EXPECT_LE(big, biggest);
133 EXPECT_GT(biggest, big);
134 EXPECT_GE(biggest, big);
135 EXPECT_EQ(big, ~~big);
136 EXPECT_EQ(one, one | one);
137 EXPECT_EQ(big, big | big);
138 EXPECT_EQ(one, one | zero);
139 EXPECT_EQ(one, one & one);
140 EXPECT_EQ(big, big & big);
141 EXPECT_EQ(zero, one & zero);
142 EXPECT_EQ(zero, big & ~big);
143 EXPECT_EQ(zero, one ^ one);
144 EXPECT_EQ(zero, big ^ big);
145 EXPECT_EQ(one, one ^ zero);
148 EXPECT_EQ(big, big << 0);
149 EXPECT_EQ(big, big >> 0);
150 EXPECT_GT(big << 1, big);
151 EXPECT_LT(big >> 1, big);
152 EXPECT_EQ(big, (big << 10) >> 10);
153 EXPECT_EQ(big, (big >> 1) << 1);
154 EXPECT_EQ(one, (one << 80) >> 80);
155 EXPECT_EQ(zero, (one >> 80) << 80);
159 EXPECT_EQ(big << 0, big_copy <<= 0);
161 EXPECT_EQ(big >> 0, big_copy >>= 0);
163 EXPECT_EQ(big << 1, big_copy <<= 1);
165 EXPECT_EQ(big >> 1, big_copy >>= 1);
167 EXPECT_EQ(big << 10, big_copy <<= 10);
169 EXPECT_EQ(big >> 10, big_copy >>= 10);
171 EXPECT_EQ(big << 64, big_copy <<= 64);
173 EXPECT_EQ(big >> 64, big_copy >>= 64);
175 EXPECT_EQ(big << 73, big_copy <<= 73);
177 EXPECT_EQ(big >> 73, big_copy >>= 73);
181 EXPECT_EQ(zero + one, one);
182 EXPECT_EQ(one + one, two);
183 EXPECT_EQ(big_minus_one + one, big);
184 EXPECT_EQ(one - one, zero);
185 EXPECT_EQ(one - zero, one);
186 EXPECT_EQ(zero - one, biggest);
187 EXPECT_EQ(big - big, zero);
188 EXPECT_EQ(big - one, big_minus_one);
189 EXPECT_EQ(big + std::numeric_limits<uint64_t>::max(), bigger);
190 EXPECT_EQ(biggest + 1, zero);
191 EXPECT_EQ(zero - 1, biggest);
192 EXPECT_EQ(high_low - one, low_high);
193 EXPECT_EQ(low_high + one, high_low);
196 std::numeric_limits<uint64_t>::max());
198 EXPECT_TRUE(!!high_low);
199 EXPECT_FALSE(!!zero);
201 EXPECT_FALSE(!high_low);
203 EXPECT_TRUE(zero == 0);
204 EXPECT_FALSE(zero != 0);
205 EXPECT_FALSE(one == 0);
206 EXPECT_TRUE(one != 0);
207 EXPECT_FALSE(high_low == 0);
208 EXPECT_TRUE(high_low != 0);
211 EXPECT_EQ(++test, one);
212 EXPECT_EQ(test, one);
213 EXPECT_EQ(test++, one);
214 EXPECT_EQ(test, two);
215 EXPECT_EQ(test -= 2, zero);
216 EXPECT_EQ(test, zero);
217 EXPECT_EQ(test += 2, two);
218 EXPECT_EQ(test, two);
219 EXPECT_EQ(--test, one);
220 EXPECT_EQ(test, one);
221 EXPECT_EQ(test--, one);
222 EXPECT_EQ(test, zero);
223 EXPECT_EQ(test |= three, three);
224 EXPECT_EQ(test &= one, one);
225 EXPECT_EQ(test ^= three, two);
226 EXPECT_EQ(test >>= 1, one);
227 EXPECT_EQ(test <<= 1, two);
229 EXPECT_EQ(big, -(-big));
230 EXPECT_EQ(two, -((-one) - 1));
232 EXPECT_EQ(zero, -zero);
237 TEST(Uint128, ConversionTests) {
240 #ifdef ABSL_HAVE_INTRINSIC_INT128 241 unsigned __int128 intrinsic =
242 (
static_cast<unsigned __int128
>(0x3a5b76c209de76f6) << 64) +
248 EXPECT_EQ(custom,
absl::uint128(static_cast<__int128>(intrinsic)));
249 EXPECT_EQ(intrinsic, static_cast<unsigned __int128>(custom));
250 EXPECT_EQ(intrinsic, static_cast<__int128>(custom));
251 #endif // ABSL_HAVE_INTRINSIC_INT128 256 double precise_double = 0x530e * std::pow(2.0, 64.0) + 0xda74000000000000;
260 EXPECT_EQ(from_precise_double, from_precise_ints);
261 EXPECT_DOUBLE_EQ(static_cast<double>(from_precise_ints), precise_double);
263 double approx_double = 0xffffeeeeddddcccc * std::pow(2.0, 64.0) +
266 EXPECT_DOUBLE_EQ(static_cast<double>(from_approx_double), approx_double);
268 double round_to_zero = 0.7;
269 double round_to_five = 5.8;
270 double round_to_nine = 9.3;
271 EXPECT_EQ(static_cast<absl::uint128>(round_to_zero), 0);
272 EXPECT_EQ(static_cast<absl::uint128>(round_to_five), 5);
273 EXPECT_EQ(static_cast<absl::uint128>(round_to_nine), 9);
276 ~
absl::uint128{} >> (128 - std::numeric_limits<long double>::digits);
277 EXPECT_EQ(highest_precision_in_long_double,
278 static_cast<absl::uint128>(
279 static_cast<long double>(highest_precision_in_long_double)));
283 EXPECT_EQ(highest_precision_in_long_double & arbitrary_mask,
284 static_cast<absl::uint128>(static_cast<long double>(
285 highest_precision_in_long_double & arbitrary_mask)));
287 EXPECT_EQ(static_cast<absl::uint128>(-0.1L), 0);
290 TEST(Uint128, OperatorAssignReturnRef) {
296 TEST(Uint128, Multiply) {
317 for (
int i = 0;
i < 64; ++
i) {
318 for (
int j = 0; j < 64; ++j) {
331 EXPECT_EQ(0, c - b * a);
332 EXPECT_EQ(a*a - b*b, (a+b) * (a-b));
339 EXPECT_EQ(0, c - b * a);
340 EXPECT_EQ(a*a - b*b, (a+b) * (a-b));
343 TEST(Uint128, AliasTests) {
355 TEST(Uint128, DivideAndMod) {
373 ASSERT_EQ(a, q * b + r);
378 EXPECT_EQ(q, result_q);
379 EXPECT_EQ(r, result_r);
385 EXPECT_EQ(q, result_q);
386 EXPECT_EQ(r, result_r);
394 EXPECT_EQ(0, result_q);
395 EXPECT_EQ(a, result_r);
400 EXPECT_EQ(0, result_q);
401 EXPECT_EQ(a, result_r);
411 ASSERT_EQ(a / 2 - 1, expected_r);
412 ASSERT_EQ(a, b + expected_r);
415 EXPECT_EQ(1, result_q);
416 EXPECT_EQ(expected_r, result_r);
419 TEST(Uint128, DivideAndModRandomInputs) {
420 const int kNumIters = 1 << 18;
421 std::minstd_rand random(testing::UnitTest::GetInstance()->random_seed());
422 std::uniform_int_distribution<uint64_t> uniform_uint64;
423 for (
int i = 0;
i < kNumIters; ++
i) {
433 ASSERT_EQ(a, b * q + r);
437 TEST(Uint128, ConstexprTest) {
446 TEST(Uint128, NumericLimitsTest) {
450 EXPECT_EQ(static_cast<int>(128 * std::log10(2)),
constexpr uint128 Uint128Max()
static constexpr int digits10
static constexpr bool is_specialized
constexpr uint64_t Uint128High64(uint128 v)
static constexpr absl::uint128 lowest()
constexpr uint64_t Uint128Low64(uint128 v)
std::pair< uint64_t, uint64_t > uint128
static constexpr bool is_integer
static constexpr absl::uint128() min()
static constexpr bool is_signed
static constexpr absl::uint128() max()
void swap(absl::InlinedVector< T, N, A > &a, absl::InlinedVector< T, N, A > &b) noexcept(noexcept(a.swap(b)))
ABSL_MUST_USE_RESULT testing::AssertionResult VerifyTypeImplementsAbslHashCorrectly(const Container &values)
const uint128 kuint128max
constexpr uint128 MakeUint128(uint64_t high, uint64_t low)
TEST(Symbolize, Unimplemented)