1 //===-- Unittests for user defined integer literals -----------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "src/__support/integer_literals.h"
10 #include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
11 #include "test/UnitTest/Test.h"
13 using LIBC_NAMESPACE::operator""_u8
;
14 using LIBC_NAMESPACE::operator""_u16
;
15 using LIBC_NAMESPACE::operator""_u32
;
16 using LIBC_NAMESPACE::operator""_u64
;
17 using LIBC_NAMESPACE::operator""_u128
;
18 using LIBC_NAMESPACE::operator""_u256
;
20 TEST(LlvmLibcIntegerLiteralTest
, u8
) {
21 EXPECT_EQ(uint8_t(0), 0_u8
);
22 EXPECT_EQ(uint8_t(UINT8_MAX
), 255_u8
);
23 EXPECT_EQ(uint8_t(UINT8_MAX
), 0xFF_u
8);
24 EXPECT_EQ(uint8_t(UINT8_MAX
), 0b11111111_u
8);
27 TEST(LlvmLibcIntegerLiteralTest
, u16
) {
28 EXPECT_EQ(uint16_t(0), 0_u16
);
29 EXPECT_EQ(uint16_t(UINT8_MAX
), 255_u16
);
30 EXPECT_EQ(uint16_t(UINT8_MAX
), 0xFF_u
16);
31 EXPECT_EQ(uint16_t(UINT8_MAX
), 0b11111111_u
16);
32 EXPECT_EQ(uint16_t(UINT16_MAX
), 65535_u16
);
33 EXPECT_EQ(uint16_t(UINT16_MAX
), 0xFFFF_u
16);
34 EXPECT_EQ(uint16_t(UINT16_MAX
), 0b11111111'11111111_u16
);
37 TEST(LlvmLibcIntegerLiteralTest
, u32
) {
38 EXPECT_EQ(uint32_t(0), 0_u32
);
39 EXPECT_EQ(uint32_t(UINT8_MAX
), 255_u32
);
40 EXPECT_EQ(uint32_t(UINT8_MAX
), 0xFF_u
32);
41 EXPECT_EQ(uint32_t(UINT8_MAX
), 0b11111111_u
32);
42 EXPECT_EQ(uint32_t(UINT16_MAX
), 65535_u32
);
43 EXPECT_EQ(uint32_t(UINT16_MAX
), 0xFFFF_u
32);
44 EXPECT_EQ(uint32_t(UINT16_MAX
), 0b11111111'11111111_u32
);
45 EXPECT_EQ(uint32_t(UINT32_MAX
), 4294967295_u32
);
46 EXPECT_EQ(uint32_t(UINT32_MAX
), 0xFFFFFFFF_u
32);
47 EXPECT_EQ(uint32_t(UINT32_MAX
), 0b1111111111111111'1111111111111111_u32
);
50 TEST(LlvmLibcIntegerLiteralTest
, u64
) {
51 EXPECT_EQ(uint64_t(0), 0_u64
);
52 EXPECT_EQ(uint64_t(UINT8_MAX
), 255_u64
);
53 EXPECT_EQ(uint64_t(UINT8_MAX
), 0xFF_u
64);
54 EXPECT_EQ(uint64_t(UINT8_MAX
), 0b11111111_u
64);
55 EXPECT_EQ(uint64_t(UINT16_MAX
), 65535_u64
);
56 EXPECT_EQ(uint64_t(UINT16_MAX
), 0xFFFF_u
64);
57 EXPECT_EQ(uint64_t(UINT16_MAX
), 0b11111111'11111111_u64
);
58 EXPECT_EQ(uint64_t(UINT32_MAX
), 4294967295_u64
);
59 EXPECT_EQ(uint64_t(UINT32_MAX
), 0xFFFFFFFF_u
64);
60 EXPECT_EQ(uint64_t(UINT32_MAX
), 0b1111111111111111'1111111111111111_u64
);
61 EXPECT_EQ(uint64_t(UINT64_MAX
), 18446744073709551615_u64
);
62 EXPECT_EQ(uint64_t(UINT64_MAX
), 0xFFFFFFFF'FFFFFFFF_u
64);
65 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111_u64
);
68 TEST(LlvmLibcIntegerLiteralTest
, u128
) {
69 #ifdef LIBC_TYPES_HAS_INT128
70 const __uint128_t ZERO
= 0;
71 const __uint128_t U8_MAX
= UINT8_MAX
;
72 const __uint128_t U16_MAX
= UINT16_MAX
;
73 const __uint128_t U32_MAX
= UINT32_MAX
;
74 const __uint128_t U64_MAX
= UINT64_MAX
;
75 const __uint128_t U128_MAX
= (U64_MAX
<< 64) | U64_MAX
;
77 const UInt128 ZERO
= 0;
78 const UInt128 U8_MAX
= UINT8_MAX
;
79 const UInt128 U16_MAX
= UINT16_MAX
;
80 const UInt128 U32_MAX
= UINT32_MAX
;
81 const UInt128 U64_MAX
= UINT64_MAX
;
82 const UInt128 U128_MAX
= (U64_MAX
<< 64) | U64_MAX
;
83 #endif // LIBC_TYPES_HAS_INT128
84 EXPECT_EQ(ZERO
, 0_u128
);
85 EXPECT_EQ(U8_MAX
, 255_u128
);
86 EXPECT_EQ(U8_MAX
, 0xFF_u
128);
87 EXPECT_EQ(U8_MAX
, 0b11111111_u
128);
88 EXPECT_EQ(U16_MAX
, 65535_u128
);
89 EXPECT_EQ(U16_MAX
, 0xFFFF_u
128);
90 EXPECT_EQ(U16_MAX
, 0b11111111'11111111_u128
);
91 EXPECT_EQ(U32_MAX
, 4294967295_u128
);
92 EXPECT_EQ(U32_MAX
, 0xFFFFFFFF_u
128);
93 EXPECT_EQ(U32_MAX
, 0b1111111111111111'1111111111111111_u128
);
94 EXPECT_EQ(U64_MAX
, 18446744073709551615_u128
);
95 EXPECT_EQ(U64_MAX
, 0xFFFFFFFF'FFFFFFFF_u
128);
98 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111_u128
);
99 EXPECT_EQ(U128_MAX
, 340282366920938463463374607431768211455_u128
);
100 EXPECT_EQ(U128_MAX
, 0xFFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF_u
128);
103 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111'1111111111111111_u128
);
106 TEST(LlvmLibcIntegerLiteralTest
, u256
) {
107 using UInt256
= LIBC_NAMESPACE::UInt
<256>;
108 const UInt256 ZERO
= 0;
109 const UInt256 U8_MAX
= UINT8_MAX
;
110 const UInt256 U16_MAX
= UINT16_MAX
;
111 const UInt256 U32_MAX
= UINT32_MAX
;
112 const UInt256 U64_MAX
= UINT64_MAX
;
113 const UInt256 U128_MAX
= (U64_MAX
<< 64) | U64_MAX
;
114 const UInt256 U256_MAX
= (U128_MAX
<< 128) | U128_MAX
;
115 EXPECT_EQ(ZERO
, 0_u256
);
116 EXPECT_EQ(U8_MAX
, 255_u256
);
117 EXPECT_EQ(U8_MAX
, 0xFF_u
256);
118 EXPECT_EQ(U8_MAX
, 0b11111111_u
256);
119 EXPECT_EQ(U16_MAX
, 65535_u256
);
120 EXPECT_EQ(U16_MAX
, 0xFFFF_u
256);
121 EXPECT_EQ(U16_MAX
, 0b11111111'11111111_u256
);
122 EXPECT_EQ(U32_MAX
, 4294967295_u256
);
123 EXPECT_EQ(U32_MAX
, 0xFFFFFFFF_u
256);
124 EXPECT_EQ(U32_MAX
, 0b1111111111111111'1111111111111111_u256
);
125 EXPECT_EQ(U64_MAX
, 18446744073709551615_u256
);
126 EXPECT_EQ(U64_MAX
, 0xFFFFFFFF'FFFFFFFF_u
256);
129 0b1111111111111111'1111111111111111'1111111111111111'1111111111111111_u256
);
130 EXPECT_EQ(U128_MAX
, 0xFFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF_u
256);
133 0xFFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF'FFFFFFFF_u
256);
136 TEST(LlvmLibcIntegerLiteralTest
, parse_bigint
) {
137 using T
= LIBC_NAMESPACE::Int
<128>;
141 } constexpr TEST_CASES
[] = {
142 {"0", 0}, {"-1", -1}, {"+1", 1}, {"-0xFF", -255}, {"-0b11", -3},
144 for (auto tc
: TEST_CASES
) {
145 T actual
= LIBC_NAMESPACE::parse_bigint
<T
>(tc
.str
);
146 EXPECT_EQ(actual
, tc
.expected
);
150 TEST(LlvmLibcIntegerLiteralTest
, parse_bigint_invalid
) {
151 using T
= LIBC_NAMESPACE::Int
<128>;
152 const T expected
; // default construction
153 EXPECT_EQ(LIBC_NAMESPACE::parse_bigint
<T
>(nullptr), expected
);
154 EXPECT_EQ(LIBC_NAMESPACE::parse_bigint
<T
>(""), expected
);