ClickHouse/src/Common/tests/gtest_wide_integer.cpp

56 lines
2.9 KiB
C++
Raw Normal View History

2021-05-05 20:56:44 +00:00
#include <gtest/gtest.h>
#include <common/extended_types.h>
#include <IO/WriteHelpers.h>
using namespace DB;
2021-05-05 21:20:55 +00:00
GTEST_TEST(WideInteger, Conversions)
2021-05-05 20:56:44 +00:00
{
ASSERT_EQ(toString(UInt128(12345678901234567890ULL)), "12345678901234567890");
ASSERT_EQ(toString(UInt256(12345678901234567890ULL)), "12345678901234567890");
ASSERT_EQ(toString(Int128(-1234567890123456789LL)), "-1234567890123456789");
ASSERT_EQ(toString(Int256(-1234567890123456789LL)), "-1234567890123456789");
ASSERT_EQ(UInt64(UInt128(12345678901234567890ULL)), 12345678901234567890ULL);
ASSERT_EQ(UInt64(UInt256(12345678901234567890ULL)), 12345678901234567890ULL);
ASSERT_EQ(__uint128_t(UInt128(12345678901234567890ULL)), 12345678901234567890ULL);
ASSERT_EQ(__uint128_t(UInt256(12345678901234567890ULL)), 12345678901234567890ULL);
2021-05-05 21:20:55 +00:00
ASSERT_EQ(__int128_t(Int128(-1234567890123456789LL)), -1234567890123456789LL);
ASSERT_EQ(__int128_t(Int256(-1234567890123456789LL)), -1234567890123456789LL);
ASSERT_EQ(toString(Int128(-1)), "-1");
2021-05-05 20:56:44 +00:00
ASSERT_EQ(toString(Int256(-1)), "-1");
2021-05-05 21:20:55 +00:00
ASSERT_EQ(toString(UInt128(123.456)), "123");
ASSERT_EQ(toString(UInt256(123.456)), "123");
ASSERT_EQ(toString(Int128(-123.456)), "-123");
ASSERT_EQ(toString(Int256(-123.456)), "-123");
ASSERT_EQ(toString(UInt128(123.456f)), "123");
ASSERT_EQ(toString(UInt256(123.456f)), "123");
ASSERT_EQ(toString(Int128(-123.456f)), "-123");
ASSERT_EQ(toString(Int256(-123.456f)), "-123");
ASSERT_EQ(toString(UInt128(1) * 1000000000 * 1000000000 * 1000000000 * 1000000000), "1000000000000000000000000000000000000");
ASSERT_EQ(Float64(UInt128(1) * 1000000000 * 1000000000 * 1000000000 * 1000000000), 1e36);
ASSERT_EQ(toString(UInt256(1) * 1000000000 * 1000000000 * 1000000000 * 1000000000 * 1000000000 * 1000000000 * 1000000000 * 1000000000),
"1000000000000000000000000000000000000000000000000000000000000000000000000");
ASSERT_EQ(Float64(UInt256(1) * 1000000000 * 1000000000 * 1000000000 * 1000000000 * 1000000000 * 1000000000 * 1000000000 * 1000000000), 1e72);
2021-05-05 20:56:44 +00:00
}
2021-05-05 21:20:55 +00:00
GTEST_TEST(WideInteger, Arithmetic)
{
ASSERT_EQ(UInt256(12345678901234567890ULL) * 12345678901234567890ULL / 12345678901234567890ULL, 12345678901234567890ULL);
ASSERT_EQ(UInt256(12345678901234567890ULL) * UInt256(12345678901234567890ULL) / 12345678901234567890ULL, 12345678901234567890ULL);
ASSERT_EQ(UInt256(12345678901234567890ULL) * 12345678901234567890ULL / UInt256(12345678901234567890ULL), 12345678901234567890ULL);
ASSERT_EQ(UInt256(12345678901234567890ULL) * 12345678901234567890ULL / 12345678901234567890ULL, UInt256(12345678901234567890ULL));
ASSERT_EQ(UInt128(12345678901234567890ULL) * 12345678901234567890ULL / UInt128(12345678901234567890ULL), 12345678901234567890ULL);
ASSERT_EQ(UInt256(12345678901234567890ULL) * UInt128(12345678901234567890ULL) / 12345678901234567890ULL, 12345678901234567890ULL);
}