mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-29 02:52:13 +00:00
Fix error; add a test
This commit is contained in:
parent
0e39d3a589
commit
df0d6a8d24
@ -1,6 +1,35 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <common/extended_types.h>
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <limits>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <initializer_list>
|
||||||
|
|
||||||
|
#include <Core/Types.h>
|
||||||
#include <IO/WriteHelpers.h>
|
#include <IO/WriteHelpers.h>
|
||||||
|
#include <common/demangle.h>
|
||||||
|
|
||||||
|
|
||||||
|
static_assert(is_signed_v<Int128>);
|
||||||
|
static_assert(!is_unsigned_v<Int128>);
|
||||||
|
static_assert(is_integer_v<Int128>);
|
||||||
|
static_assert(sizeof(Int128) == 16);
|
||||||
|
|
||||||
|
static_assert(is_signed_v<Int256>);
|
||||||
|
static_assert(!is_unsigned_v<Int256>);
|
||||||
|
static_assert(is_integer_v<Int256>);
|
||||||
|
static_assert(sizeof(Int256) == 32);
|
||||||
|
|
||||||
|
static_assert(!is_signed_v<UInt128>);
|
||||||
|
static_assert(is_unsigned_v<UInt128>);
|
||||||
|
static_assert(is_integer_v<UInt128>);
|
||||||
|
static_assert(sizeof(UInt128) == 16);
|
||||||
|
|
||||||
|
static_assert(!is_signed_v<UInt256>);
|
||||||
|
static_assert(is_unsigned_v<UInt256>);
|
||||||
|
static_assert(is_integer_v<UInt256>);
|
||||||
|
static_assert(sizeof(UInt256) == 32);
|
||||||
|
|
||||||
|
|
||||||
using namespace DB;
|
using namespace DB;
|
||||||
|
|
||||||
@ -10,6 +39,38 @@ GTEST_TEST(WideInteger, Conversions)
|
|||||||
ASSERT_EQ(toString(UInt128(12345678901234567890ULL)), "12345678901234567890");
|
ASSERT_EQ(toString(UInt128(12345678901234567890ULL)), "12345678901234567890");
|
||||||
ASSERT_EQ(toString(UInt256(12345678901234567890ULL)), "12345678901234567890");
|
ASSERT_EQ(toString(UInt256(12345678901234567890ULL)), "12345678901234567890");
|
||||||
|
|
||||||
|
Int128 minus_one = -1;
|
||||||
|
ASSERT_EQ(minus_one.items[0], -1);
|
||||||
|
ASSERT_EQ(minus_one.items[1], -1);
|
||||||
|
|
||||||
|
ASSERT_EQ(0, memcmp(&minus_one, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", sizeof(minus_one)));
|
||||||
|
|
||||||
|
ASSERT_EQ(minus_one, -1);
|
||||||
|
ASSERT_EQ(minus_one, -1LL);
|
||||||
|
ASSERT_EQ(minus_one, Int8(-1));
|
||||||
|
ASSERT_EQ(minus_one, Int16(-1));
|
||||||
|
ASSERT_EQ(minus_one, Int32(-1));
|
||||||
|
ASSERT_EQ(minus_one, Int64(-1));
|
||||||
|
|
||||||
|
ASSERT_LT(minus_one, 0);
|
||||||
|
|
||||||
|
Int128 zero = 0;
|
||||||
|
zero += -1;
|
||||||
|
ASSERT_EQ(zero, -1);
|
||||||
|
ASSERT_EQ(zero, minus_one);
|
||||||
|
|
||||||
|
zero += minus_one;
|
||||||
|
ASSERT_EQ(0, memcmp(&zero, "\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", sizeof(zero)));
|
||||||
|
|
||||||
|
zero += 2;
|
||||||
|
ASSERT_EQ(zero, 0);
|
||||||
|
|
||||||
|
ASSERT_EQ(toString(Int128(-1)), "-1");
|
||||||
|
ASSERT_EQ(toString(Int256(-1)), "-1");
|
||||||
|
|
||||||
|
ASSERT_EQ(toString(Int128(-1LL)), "-1");
|
||||||
|
ASSERT_EQ(toString(Int256(-1LL)), "-1");
|
||||||
|
|
||||||
ASSERT_EQ(toString(Int128(-1234567890123456789LL)), "-1234567890123456789");
|
ASSERT_EQ(toString(Int128(-1234567890123456789LL)), "-1234567890123456789");
|
||||||
ASSERT_EQ(toString(Int256(-1234567890123456789LL)), "-1234567890123456789");
|
ASSERT_EQ(toString(Int256(-1234567890123456789LL)), "-1234567890123456789");
|
||||||
|
|
||||||
@ -46,10 +107,38 @@ GTEST_TEST(WideInteger, Conversions)
|
|||||||
|
|
||||||
GTEST_TEST(WideInteger, Arithmetic)
|
GTEST_TEST(WideInteger, Arithmetic)
|
||||||
{
|
{
|
||||||
|
Int128 minus_one = -1;
|
||||||
|
Int128 zero = 0;
|
||||||
|
|
||||||
|
zero += -1;
|
||||||
|
ASSERT_EQ(zero, -1);
|
||||||
|
ASSERT_EQ(zero, minus_one);
|
||||||
|
|
||||||
|
zero += minus_one;
|
||||||
|
ASSERT_EQ(0, memcmp(&zero, "\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", sizeof(zero)));
|
||||||
|
|
||||||
|
zero += 2;
|
||||||
|
ASSERT_EQ(zero, 0);
|
||||||
|
|
||||||
ASSERT_EQ(UInt256(12345678901234567890ULL) * 12345678901234567890ULL / 12345678901234567890ULL, 12345678901234567890ULL);
|
ASSERT_EQ(UInt256(12345678901234567890ULL) * 12345678901234567890ULL / 12345678901234567890ULL, 12345678901234567890ULL);
|
||||||
ASSERT_EQ(UInt256(12345678901234567890ULL) * UInt256(12345678901234567890ULL) / 12345678901234567890ULL, 12345678901234567890ULL);
|
ASSERT_EQ(UInt256(12345678901234567890ULL) * UInt256(12345678901234567890ULL) / 12345678901234567890ULL, 12345678901234567890ULL);
|
||||||
ASSERT_EQ(UInt256(12345678901234567890ULL) * 12345678901234567890ULL / UInt256(12345678901234567890ULL), 12345678901234567890ULL);
|
ASSERT_EQ(UInt256(12345678901234567890ULL) * 12345678901234567890ULL / UInt256(12345678901234567890ULL), 12345678901234567890ULL);
|
||||||
ASSERT_EQ(UInt256(12345678901234567890ULL) * 12345678901234567890ULL / 12345678901234567890ULL, UInt256(12345678901234567890ULL));
|
ASSERT_EQ(UInt256(12345678901234567890ULL) * 12345678901234567890ULL / 12345678901234567890ULL, UInt256(12345678901234567890ULL));
|
||||||
ASSERT_EQ(UInt128(12345678901234567890ULL) * 12345678901234567890ULL / UInt128(12345678901234567890ULL), 12345678901234567890ULL);
|
ASSERT_EQ(UInt128(12345678901234567890ULL) * 12345678901234567890ULL / UInt128(12345678901234567890ULL), 12345678901234567890ULL);
|
||||||
ASSERT_EQ(UInt256(12345678901234567890ULL) * UInt128(12345678901234567890ULL) / 12345678901234567890ULL, 12345678901234567890ULL);
|
ASSERT_EQ(UInt256(12345678901234567890ULL) * UInt128(12345678901234567890ULL) / 12345678901234567890ULL, 12345678901234567890ULL);
|
||||||
|
|
||||||
|
ASSERT_EQ(Int128(0) + Int32(-1), Int128(-1));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
GTEST_TEST(WideInteger, DecimalArithmetic)
|
||||||
|
{
|
||||||
|
Decimal128 zero{};
|
||||||
|
Decimal32 addend = -1000;
|
||||||
|
|
||||||
|
zero += Decimal128(addend);
|
||||||
|
ASSERT_EQ(zero.value, -1000);
|
||||||
|
|
||||||
|
zero += addend;
|
||||||
|
ASSERT_EQ(zero.value, -2000);
|
||||||
}
|
}
|
||||||
|
@ -179,6 +179,12 @@ struct Decimal
|
|||||||
const Decimal<T> & operator /= (const T & x) { value /= x; return *this; }
|
const Decimal<T> & operator /= (const T & x) { value /= x; return *this; }
|
||||||
const Decimal<T> & operator %= (const T & x) { value %= x; return *this; }
|
const Decimal<T> & operator %= (const T & x) { value %= x; return *this; }
|
||||||
|
|
||||||
|
template <typename U> const Decimal<T> & operator += (const Decimal<U> & x) { value += x.value; return *this; }
|
||||||
|
template <typename U> const Decimal<T> & operator -= (const Decimal<U> & x) { value -= x.value; return *this; }
|
||||||
|
template <typename U> const Decimal<T> & operator *= (const Decimal<U> & x) { value *= x.value; return *this; }
|
||||||
|
template <typename U> const Decimal<T> & operator /= (const Decimal<U> & x) { value /= x.value; return *this; }
|
||||||
|
template <typename U> const Decimal<T> & operator %= (const Decimal<U> & x) { value %= x.value; return *this; }
|
||||||
|
|
||||||
/// This is to avoid UB for sumWithOverflow()
|
/// This is to avoid UB for sumWithOverflow()
|
||||||
void NO_SANITIZE_UNDEFINED addOverflow(const T & x) { value += x; }
|
void NO_SANITIZE_UNDEFINED addOverflow(const T & x) { value += x; }
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user