MCPcopy Create free account
hub / github.com/LadybugDB/ladybug / TEST

Function TEST

test/common/uint128_test.cpp:11–123  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9using namespace lbug::common;
10
11TEST(Uint128Tests, Casting) {
12 { // conversion from int32_t
13 uint128_t value = 42;
14 EXPECT_EQ(value.low, 42);
15 EXPECT_EQ(value.high, 0);
16 }
17
18 { // conversion to int32_t
19 uint128_t value = {(1ULL << 31) - 2, 0};
20 int32_t int32_value = (int32_t)value;
21 EXPECT_EQ(int32_value, (int32_t)((1ULL << 31) - 2));
22 }
23
24 { // conversion from int32_t (error: negative value)
25 EXPECT_THROW({ [[maybe_unused]] uint128_t value = -64; }, OverflowException);
26 }
27
28 { // conversion to int64_t (narrow casting)
29 uint128_t value = {2718, 1};
30 int64_t int64_value = (int64_t)value;
31 EXPECT_EQ(int64_value, 2718);
32 }
33
34 { // conversion to uint16_t (narrow casting)
35 uint128_t value = {UINT16_MAX, UINT16_MAX};
36 uint16_t uint16_value = (uint16_t)value;
37 EXPECT_EQ(uint16_value, UINT16_MAX);
38 }
39
40 { // conversion from int8_t (error: negative value)
41 int8_t int8_value = -64;
42 EXPECT_THROW({ [[maybe_unused]] uint128_t value = int8_value; }, OverflowException);
43 }
44
45 { // conversion from int128_t 2^64 + 1 (no implicit casting from int128_t to uint128_t)
46 int128_t int128_value{(uint64_t)1, (int64_t)1};
47 uint128_t value = (uint128_t)int128_value;
48 EXPECT_EQ(value.low, 1);
49 EXPECT_EQ(value.high, 1);
50 }
51
52 { // conversion to int128_t
53 int128_t int128_value = uint128_t{35, 165};
54 EXPECT_EQ(int128_value.low, 35);
55 EXPECT_EQ(int128_value.high, 165);
56 }
57
58 { // conversion from int128_t (error: negative value)
59 int128_t negative_two = -2;
60 EXPECT_THROW(
61 { [[maybe_unused]] uint128_t value = (uint128_t)negative_two; }, OverflowException);
62 }
63
64 { // conversion to int128_t (error: overflow)
65 uint128_t large_uint128 = {335882, (1ULL << 63) + 9101028};
66 EXPECT_THROW(
67 { [[maybe_unused]] int128_t value1 = (int128_t)large_uint128; }, OverflowException);
68 uint128_t max_uint128 = {UINT64_MAX, UINT64_MAX};

Callers

nothing calls this directly

Calls 5

AddClass · 0.85
XorClass · 0.85
uint128_tClass · 0.50
int128_tClass · 0.50
toStringFunction · 0.50

Tested by

no test coverage detected