1 | |
2 | //===-- Unittests for user defined integer literals -----------------------===// |
3 | // |
4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
5 | // See https://llvm.org/LICENSE.txt for license information. |
6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
7 | // |
8 | //===----------------------------------------------------------------------===// |
9 | |
10 | #include "src/__support/integer_literals.h" |
11 | #include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128 |
12 | #include "test/UnitTest/Test.h" |
13 | |
14 | using LIBC_NAMESPACE::operator""_u8 ; |
15 | using LIBC_NAMESPACE::operator""_u16 ; |
16 | using LIBC_NAMESPACE::operator""_u32 ; |
17 | using LIBC_NAMESPACE::operator""_u64 ; |
18 | using LIBC_NAMESPACE::operator""_u128 ; |
19 | using LIBC_NAMESPACE::operator""_u256 ; |
20 | |
21 | TEST(LlvmLibcIntegerLiteralTest, u8) { |
22 | EXPECT_EQ(uint8_t(0), 0_u8); |
23 | EXPECT_EQ(uint8_t(UINT8_MAX), 255_u8); |
24 | EXPECT_EQ(uint8_t(UINT8_MAX), 0xFF_u8); |
25 | EXPECT_EQ(uint8_t(UINT8_MAX), 0b11111111_u8); |
26 | } |
27 | |
28 | TEST(LlvmLibcIntegerLiteralTest, u16) { |
29 | EXPECT_EQ(uint16_t(0), 0_u16); |
30 | EXPECT_EQ(uint16_t(UINT8_MAX), 255_u16); |
31 | EXPECT_EQ(uint16_t(UINT8_MAX), 0xFF_u16); |
32 | EXPECT_EQ(uint16_t(UINT8_MAX), 0b11111111_u16); |
33 | EXPECT_EQ(uint16_t(UINT16_MAX), 65535_u16); |
34 | EXPECT_EQ(uint16_t(UINT16_MAX), 0xFFFF_u16); |
35 | EXPECT_EQ(uint16_t(UINT16_MAX), 0b11111111'11111111_u16); |
36 | } |
37 | |
38 | TEST(LlvmLibcIntegerLiteralTest, u32) { |
39 | EXPECT_EQ(uint32_t(0), 0_u32); |
40 | EXPECT_EQ(uint32_t(UINT8_MAX), 255_u32); |
41 | EXPECT_EQ(uint32_t(UINT8_MAX), 0xFF_u32); |
42 | EXPECT_EQ(uint32_t(UINT8_MAX), 0b11111111_u32); |
43 | EXPECT_EQ(uint32_t(UINT16_MAX), 65535_u32); |
44 | EXPECT_EQ(uint32_t(UINT16_MAX), 0xFFFF_u32); |
45 | EXPECT_EQ(uint32_t(UINT16_MAX), 0b11111111'11111111_u32); |
46 | EXPECT_EQ(uint32_t(UINT32_MAX), 4294967295_u32); |
47 | EXPECT_EQ(uint32_t(UINT32_MAX), 0xFFFFFFFF_u32); |
48 | EXPECT_EQ(uint32_t(UINT32_MAX), 0b1111111111111111'1111111111111111_u32); |
49 | } |
50 | |
51 | TEST(LlvmLibcIntegerLiteralTest, u64) { |
52 | EXPECT_EQ(uint64_t(0), 0_u64); |
53 | EXPECT_EQ(uint64_t(UINT8_MAX), 255_u64); |
54 | EXPECT_EQ(uint64_t(UINT8_MAX), 0xFF_u64); |
55 | EXPECT_EQ(uint64_t(UINT8_MAX), 0b11111111_u64); |
56 | EXPECT_EQ(uint64_t(UINT16_MAX), 65535_u64); |
57 | EXPECT_EQ(uint64_t(UINT16_MAX), 0xFFFF_u64); |
58 | EXPECT_EQ(uint64_t(UINT16_MAX), 0b11111111'11111111_u64); |
59 | EXPECT_EQ(uint64_t(UINT32_MAX), 4294967295_u64); |
60 | EXPECT_EQ(uint64_t(UINT32_MAX), 0xFFFFFFFF_u64); |
61 | EXPECT_EQ(uint64_t(UINT32_MAX), 0b1111111111111111'1111111111111111_u64); |
62 | EXPECT_EQ(uint64_t(UINT64_MAX), 18446744073709551615_u64); |
63 | EXPECT_EQ(uint64_t(UINT64_MAX), 0xFFFFFFFF'FFFFFFFF_u64); |
64 | EXPECT_EQ( |
65 | uint64_t(UINT64_MAX), |
66 | 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111_u64); |
67 | } |
68 | |
69 | TEST(LlvmLibcIntegerLiteralTest, u128) { |
70 | #ifdef LIBC_TYPES_HAS_INT128 |
71 | const __uint128_t ZERO = 0; |
72 | const __uint128_t U8_MAX = UINT8_MAX; |
73 | const __uint128_t U16_MAX = UINT16_MAX; |
74 | const __uint128_t U32_MAX = UINT32_MAX; |
75 | const __uint128_t U64_MAX = UINT64_MAX; |
76 | const __uint128_t U128_MAX = (U64_MAX << 64) | U64_MAX; |
77 | #else |
78 | const UInt128 ZERO = 0; |
79 | const UInt128 U8_MAX = UINT8_MAX; |
80 | const UInt128 U16_MAX = UINT16_MAX; |
81 | const UInt128 U32_MAX = UINT32_MAX; |
82 | const UInt128 U64_MAX = UINT64_MAX; |
83 | const UInt128 U128_MAX = (U64_MAX << 64) | U64_MAX; |
84 | #endif // LIBC_TYPES_HAS_INT128 |
85 | EXPECT_EQ(ZERO, 0_u128); |
86 | EXPECT_EQ(U8_MAX, 255_u128); |
87 | EXPECT_EQ(U8_MAX, 0xFF_u128); |
88 | EXPECT_EQ(U8_MAX, 0b11111111_u128); |
89 | EXPECT_EQ(U16_MAX, 65535_u128); |
90 | EXPECT_EQ(U16_MAX, 0xFFFF_u128); |
91 | EXPECT_EQ(U16_MAX, 0b11111111'11111111_u128); |
92 | EXPECT_EQ(U32_MAX, 4294967295_u128); |
93 | EXPECT_EQ(U32_MAX, 0xFFFFFFFF_u128); |
94 | EXPECT_EQ(U32_MAX, 0b1111111111111111'1111111111111111_u128); |
95 | EXPECT_EQ(U64_MAX, 18446744073709551615_u128); |
96 | EXPECT_EQ(U64_MAX, 0xFFFFFFFF'FFFFFFFF_u128); |
97 | EXPECT_EQ( |
98 | U64_MAX, |
99 | 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111_u128); |
100 | EXPECT_EQ(U128_MAX, 340282366920938463463374607431768211455_u128); |
101 | EXPECT_EQ(U128_MAX, 0xFFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF_u128); |
102 | EXPECT_EQ( |
103 | U128_MAX, |
104 | 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111_u128); |
105 | } |
106 | |
107 | TEST(LlvmLibcIntegerLiteralTest, u256) { |
108 | using UInt256 = LIBC_NAMESPACE::UInt<256>; |
109 | const UInt256 ZERO = 0; |
110 | const UInt256 U8_MAX = UINT8_MAX; |
111 | const UInt256 U16_MAX = UINT16_MAX; |
112 | const UInt256 U32_MAX = UINT32_MAX; |
113 | const UInt256 U64_MAX = UINT64_MAX; |
114 | const UInt256 U128_MAX = (U64_MAX << 64) | U64_MAX; |
115 | const UInt256 U256_MAX = (U128_MAX << 128) | U128_MAX; |
116 | EXPECT_EQ(ZERO, 0_u256); |
117 | EXPECT_EQ(U8_MAX, 255_u256); |
118 | EXPECT_EQ(U8_MAX, 0xFF_u256); |
119 | EXPECT_EQ(U8_MAX, 0b11111111_u256); |
120 | EXPECT_EQ(U16_MAX, 65535_u256); |
121 | EXPECT_EQ(U16_MAX, 0xFFFF_u256); |
122 | EXPECT_EQ(U16_MAX, 0b11111111'11111111_u256); |
123 | EXPECT_EQ(U32_MAX, 4294967295_u256); |
124 | EXPECT_EQ(U32_MAX, 0xFFFFFFFF_u256); |
125 | EXPECT_EQ(U32_MAX, 0b1111111111111111'1111111111111111_u256); |
126 | EXPECT_EQ(U64_MAX, 18446744073709551615_u256); |
127 | EXPECT_EQ(U64_MAX, 0xFFFFFFFF'FFFFFFFF_u256); |
128 | EXPECT_EQ( |
129 | U64_MAX, |
130 | 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111_u256); |
131 | EXPECT_EQ(U128_MAX, 0xFFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF_u256); |
132 | EXPECT_EQ( |
133 | U256_MAX, |
134 | 0xFFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF_u256); |
135 | } |
136 | |
137 | TEST(LlvmLibcIntegerLiteralTest, parse_bigint) { |
138 | using T = LIBC_NAMESPACE::Int<128>; |
139 | struct { |
140 | const char *str; |
141 | T expected; |
142 | } constexpr TEST_CASES[] = { |
143 | {.str: "0" , .expected: 0}, {.str: "-1" , .expected: -1}, {.str: "+1" , .expected: 1}, {.str: "-0xFF" , .expected: -255}, {.str: "-0b11" , .expected: -3}, |
144 | }; |
145 | for (auto tc : TEST_CASES) { |
146 | T actual = LIBC_NAMESPACE::parse_bigint<T>(ptr: tc.str); |
147 | EXPECT_EQ(actual, tc.expected); |
148 | } |
149 | } |
150 | |
151 | TEST(LlvmLibcIntegerLiteralTest, parse_bigint_invalid) { |
152 | using T = LIBC_NAMESPACE::Int<128>; |
153 | const T expected; // default construction |
154 | EXPECT_EQ(LIBC_NAMESPACE::parse_bigint<T>(nullptr), expected); |
155 | EXPECT_EQ(LIBC_NAMESPACE::parse_bigint<T>("" ), expected); |
156 | } |
157 | |