1 //===- llvm/unittest/ADT/APSIntTest.cpp - APSInt unit tests ---------------===//
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 "llvm/ADT/APSInt.h"
10 #include "gtest/gtest.h"
16 TEST(APSIntTest
, MoveTest
) {
18 EXPECT_TRUE(A
.isUnsigned());
22 EXPECT_FALSE(A
.isUnsigned());
25 EXPECT_FALSE(C
.isUnsigned());
28 const uint64_t *Bits
= Wide
.getRawData();
29 APSInt
D(std::move(Wide
));
30 EXPECT_TRUE(D
.isUnsigned());
31 EXPECT_EQ(Bits
, D
.getRawData()); // Verify that "Wide" was really moved.
34 EXPECT_TRUE(A
.isUnsigned());
37 Bits
= Wide
.getRawData();
39 EXPECT_TRUE(A
.isUnsigned());
40 EXPECT_EQ(Bits
, A
.getRawData()); // Verify that "Wide" was really moved.
43 TEST(APSIntTest
, get
) {
44 EXPECT_TRUE(APSInt::get(7).isSigned());
45 EXPECT_EQ(64u, APSInt::get(7).getBitWidth());
46 EXPECT_EQ(7u, APSInt::get(7).getZExtValue());
47 EXPECT_EQ(7, APSInt::get(7).getSExtValue());
48 EXPECT_TRUE(APSInt::get(-7).isSigned());
49 EXPECT_EQ(64u, APSInt::get(-7).getBitWidth());
50 EXPECT_EQ(-7, APSInt::get(-7).getSExtValue());
51 EXPECT_EQ(UINT64_C(0) - 7, APSInt::get(-7).getZExtValue());
54 TEST(APSIntTest
, getUnsigned
) {
55 EXPECT_TRUE(APSInt::getUnsigned(7).isUnsigned());
56 EXPECT_EQ(64u, APSInt::getUnsigned(7).getBitWidth());
57 EXPECT_EQ(7u, APSInt::getUnsigned(7).getZExtValue());
58 EXPECT_EQ(7, APSInt::getUnsigned(7).getSExtValue());
59 EXPECT_TRUE(APSInt::getUnsigned(-7).isUnsigned());
60 EXPECT_EQ(64u, APSInt::getUnsigned(-7).getBitWidth());
61 EXPECT_EQ(-7, APSInt::getUnsigned(-7).getSExtValue());
62 EXPECT_EQ(UINT64_C(0) - 7, APSInt::getUnsigned(-7).getZExtValue());
65 TEST(APSIntTest
, isRepresentableByInt64
) {
66 ASSERT_TRUE(APSInt(APInt(3, 7), true).isRepresentableByInt64());
67 ASSERT_TRUE(APSInt(APInt(128, 7), true).isRepresentableByInt64());
68 ASSERT_TRUE(APSInt(APInt(128, 7), false).isRepresentableByInt64());
69 ASSERT_TRUE(APSInt(APInt(64, -1), false).isRepresentableByInt64());
70 ASSERT_FALSE(APSInt(APInt(64, (uint64_t)-1), true).isRepresentableByInt64());
72 TEST(APSIntTest
, getExtValue
) {
73 EXPECT_TRUE(APSInt(APInt(3, 7), true).isUnsigned());
74 EXPECT_TRUE(APSInt(APInt(3, 7), false).isSigned());
75 EXPECT_TRUE(APSInt(APInt(4, 7), true).isUnsigned());
76 EXPECT_TRUE(APSInt(APInt(4, 7), false).isSigned());
77 EXPECT_TRUE(APSInt(APInt(4, -7, true), true).isUnsigned());
78 EXPECT_TRUE(APSInt(APInt(4, -7, true), false).isSigned());
79 EXPECT_EQ(7, APSInt(APInt(3, 7), true).getExtValue());
80 EXPECT_EQ(-1, APSInt(APInt(3, 7), false).getExtValue());
81 EXPECT_EQ(7, APSInt(APInt(4, 7), true).getExtValue());
82 EXPECT_EQ(7, APSInt(APInt(4, 7), false).getExtValue());
83 EXPECT_EQ(9, APSInt(APInt(4, -7, true), true).getExtValue());
84 EXPECT_EQ(-7, APSInt(APInt(4, -7, true), false).getExtValue());
86 TEST(APSIntTest
, tryExtValue
) {
87 ASSERT_EQ(-7, APSInt(APInt(64, -7), false).tryExtValue().value_or(42));
88 ASSERT_EQ(42, APSInt(APInt(128, -7), false).tryExtValue().value_or(42));
90 APSInt(APInt::getAllOnes(128), false).tryExtValue().value_or(42));
91 ASSERT_EQ(42, APSInt(APInt(64, -7), true).tryExtValue().value_or(42));
92 ASSERT_EQ(1, APSInt(APInt(128, 1), true).tryExtValue().value_or(42));
94 APSInt(APInt::getAllOnes(128), true).tryExtValue().value_or(42));
97 TEST(APSIntTest
, compareValues
) {
98 auto U
= [](uint64_t V
) { return APSInt::getUnsigned(V
); };
99 auto S
= [](int64_t V
) { return APSInt::get(V
); };
101 // Bit-width matches and is-signed.
102 EXPECT_TRUE(APSInt::compareValues(S(7), S(8)) < 0);
103 EXPECT_TRUE(APSInt::compareValues(S(8), S(7)) > 0);
104 EXPECT_TRUE(APSInt::compareValues(S(7), S(7)) == 0);
105 EXPECT_TRUE(APSInt::compareValues(S(-7), S(8)) < 0);
106 EXPECT_TRUE(APSInt::compareValues(S(8), S(-7)) > 0);
107 EXPECT_TRUE(APSInt::compareValues(S(-7), S(-7)) == 0);
108 EXPECT_TRUE(APSInt::compareValues(S(-7), S(-8)) > 0);
109 EXPECT_TRUE(APSInt::compareValues(S(-8), S(-7)) < 0);
110 EXPECT_TRUE(APSInt::compareValues(S(-7), S(-7)) == 0);
112 // Bit-width matches and not is-signed.
113 EXPECT_TRUE(APSInt::compareValues(U(7), U(8)) < 0);
114 EXPECT_TRUE(APSInt::compareValues(U(8), U(7)) > 0);
115 EXPECT_TRUE(APSInt::compareValues(U(7), U(7)) == 0);
117 // Bit-width matches and mixed signs.
118 EXPECT_TRUE(APSInt::compareValues(U(7), S(8)) < 0);
119 EXPECT_TRUE(APSInt::compareValues(U(8), S(7)) > 0);
120 EXPECT_TRUE(APSInt::compareValues(U(7), S(7)) == 0);
121 EXPECT_TRUE(APSInt::compareValues(U(8), S(-7)) > 0);
123 // Bit-width mismatch and is-signed.
124 EXPECT_TRUE(APSInt::compareValues(S(7).trunc(32), S(8)) < 0);
125 EXPECT_TRUE(APSInt::compareValues(S(8).trunc(32), S(7)) > 0);
126 EXPECT_TRUE(APSInt::compareValues(S(7).trunc(32), S(7)) == 0);
127 EXPECT_TRUE(APSInt::compareValues(S(-7).trunc(32), S(8)) < 0);
128 EXPECT_TRUE(APSInt::compareValues(S(8).trunc(32), S(-7)) > 0);
129 EXPECT_TRUE(APSInt::compareValues(S(-7).trunc(32), S(-7)) == 0);
130 EXPECT_TRUE(APSInt::compareValues(S(-7).trunc(32), S(-8)) > 0);
131 EXPECT_TRUE(APSInt::compareValues(S(-8).trunc(32), S(-7)) < 0);
132 EXPECT_TRUE(APSInt::compareValues(S(-7).trunc(32), S(-7)) == 0);
133 EXPECT_TRUE(APSInt::compareValues(S(7), S(8).trunc(32)) < 0);
134 EXPECT_TRUE(APSInt::compareValues(S(8), S(7).trunc(32)) > 0);
135 EXPECT_TRUE(APSInt::compareValues(S(7), S(7).trunc(32)) == 0);
136 EXPECT_TRUE(APSInt::compareValues(S(-7), S(8).trunc(32)) < 0);
137 EXPECT_TRUE(APSInt::compareValues(S(8), S(-7).trunc(32)) > 0);
138 EXPECT_TRUE(APSInt::compareValues(S(-7), S(-7).trunc(32)) == 0);
139 EXPECT_TRUE(APSInt::compareValues(S(-7), S(-8).trunc(32)) > 0);
140 EXPECT_TRUE(APSInt::compareValues(S(-8), S(-7).trunc(32)) < 0);
141 EXPECT_TRUE(APSInt::compareValues(S(-7), S(-7).trunc(32)) == 0);
143 // Bit-width mismatch and not is-signed.
144 EXPECT_TRUE(APSInt::compareValues(U(7), U(8).trunc(32)) < 0);
145 EXPECT_TRUE(APSInt::compareValues(U(8), U(7).trunc(32)) > 0);
146 EXPECT_TRUE(APSInt::compareValues(U(7), U(7).trunc(32)) == 0);
147 EXPECT_TRUE(APSInt::compareValues(U(7).trunc(32), U(8)) < 0);
148 EXPECT_TRUE(APSInt::compareValues(U(8).trunc(32), U(7)) > 0);
149 EXPECT_TRUE(APSInt::compareValues(U(7).trunc(32), U(7)) == 0);
151 // Bit-width mismatch and mixed signs.
152 EXPECT_TRUE(APSInt::compareValues(U(7).trunc(32), S(8)) < 0);
153 EXPECT_TRUE(APSInt::compareValues(U(8).trunc(32), S(7)) > 0);
154 EXPECT_TRUE(APSInt::compareValues(U(7).trunc(32), S(7)) == 0);
155 EXPECT_TRUE(APSInt::compareValues(U(8).trunc(32), S(-7)) > 0);
156 EXPECT_TRUE(APSInt::compareValues(U(7), S(8).trunc(32)) < 0);
157 EXPECT_TRUE(APSInt::compareValues(U(8), S(7).trunc(32)) > 0);
158 EXPECT_TRUE(APSInt::compareValues(U(7), S(7).trunc(32)) == 0);
159 EXPECT_TRUE(APSInt::compareValues(U(8), S(-7).trunc(32)) > 0);
162 TEST(APSIntTest
, FromString
) {
163 EXPECT_EQ(APSInt("1").getExtValue(), 1);
164 EXPECT_EQ(APSInt("-1").getExtValue(), -1);
165 EXPECT_EQ(APSInt("0").getExtValue(), 0);
166 EXPECT_EQ(APSInt("56789").getExtValue(), 56789);
167 EXPECT_EQ(APSInt("-1234").getExtValue(), -1234);
170 TEST(APSIntTest
, FromStringBitWidth
) {
171 EXPECT_EQ(APSInt("0").getBitWidth(), 1U);
172 EXPECT_EQ(APSInt("000").getBitWidth(), 1U);
173 EXPECT_EQ(APSInt("1").getBitWidth(), 1U);
174 EXPECT_EQ(APSInt("2").getBitWidth(), 2U);
175 EXPECT_EQ(APSInt("3").getBitWidth(), 2U);
176 EXPECT_EQ(APSInt("003").getBitWidth(), 2U);
177 EXPECT_EQ(APSInt("15").getBitWidth(), 4U);
178 EXPECT_EQ(APSInt("16").getBitWidth(), 5U);
179 EXPECT_EQ(APSInt("17").getBitWidth(), 5U);
181 EXPECT_EQ(APSInt("-0").getBitWidth(), 1U);
182 EXPECT_EQ(APSInt("-000").getBitWidth(), 1U);
183 EXPECT_EQ(APSInt("-1").getBitWidth(), 1U);
184 EXPECT_EQ(APSInt("-2").getBitWidth(), 2U);
185 EXPECT_EQ(APSInt("-3").getBitWidth(), 3U);
186 EXPECT_EQ(APSInt("-003").getBitWidth(), 3U);
187 EXPECT_EQ(APSInt("-5").getBitWidth(), 4U);
188 EXPECT_EQ(APSInt("-15").getBitWidth(), 5U);
189 EXPECT_EQ(APSInt("-16").getBitWidth(), 5U);
190 EXPECT_EQ(APSInt("-17").getBitWidth(), 6U);
193 #if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG)
195 TEST(APSIntTest
, StringDeath
) {
196 EXPECT_DEATH((void)APSInt(""), "Invalid string length");
197 EXPECT_DEATH((void)APSInt("1a"), "Invalid character in digit string");
202 TEST(APSIntTest
, SignedHighBit
) {
203 APSInt
False(APInt(1, 0), false);
204 APSInt
True(APInt(1, 1), false);
205 APSInt
CharMin(APInt(8, 0), false);
206 APSInt
CharSmall(APInt(8, 0x13), false);
207 APSInt
CharBoundaryUnder(APInt(8, 0x7f), false);
208 APSInt
CharBoundaryOver(APInt(8, 0x80), false);
209 APSInt
CharLarge(APInt(8, 0xd9), false);
210 APSInt
CharMax(APInt(8, 0xff), false);
212 EXPECT_FALSE(False
.isNegative());
213 EXPECT_TRUE(False
.isNonNegative());
214 EXPECT_FALSE(False
.isStrictlyPositive());
216 EXPECT_TRUE(True
.isNegative());
217 EXPECT_FALSE(True
.isNonNegative());
218 EXPECT_FALSE(True
.isStrictlyPositive());
220 EXPECT_FALSE(CharMin
.isNegative());
221 EXPECT_TRUE(CharMin
.isNonNegative());
222 EXPECT_FALSE(CharMin
.isStrictlyPositive());
224 EXPECT_FALSE(CharSmall
.isNegative());
225 EXPECT_TRUE(CharSmall
.isNonNegative());
226 EXPECT_TRUE(CharSmall
.isStrictlyPositive());
228 EXPECT_FALSE(CharBoundaryUnder
.isNegative());
229 EXPECT_TRUE(CharBoundaryUnder
.isNonNegative());
230 EXPECT_TRUE(CharBoundaryUnder
.isStrictlyPositive());
232 EXPECT_TRUE(CharBoundaryOver
.isNegative());
233 EXPECT_FALSE(CharBoundaryOver
.isNonNegative());
234 EXPECT_FALSE(CharBoundaryOver
.isStrictlyPositive());
236 EXPECT_TRUE(CharLarge
.isNegative());
237 EXPECT_FALSE(CharLarge
.isNonNegative());
238 EXPECT_FALSE(CharLarge
.isStrictlyPositive());
240 EXPECT_TRUE(CharMax
.isNegative());
241 EXPECT_FALSE(CharMax
.isNonNegative());
242 EXPECT_FALSE(CharMax
.isStrictlyPositive());
245 TEST(APSIntTest
, UnsignedHighBit
) {
246 APSInt
False(APInt(1, 0));
247 APSInt
True(APInt(1, 1));
248 APSInt
CharMin(APInt(8, 0));
249 APSInt
CharSmall(APInt(8, 0x13));
250 APSInt
CharBoundaryUnder(APInt(8, 0x7f));
251 APSInt
CharBoundaryOver(APInt(8, 0x80));
252 APSInt
CharLarge(APInt(8, 0xd9));
253 APSInt
CharMax(APInt(8, 0xff));
255 EXPECT_FALSE(False
.isNegative());
256 EXPECT_TRUE(False
.isNonNegative());
257 EXPECT_FALSE(False
.isStrictlyPositive());
259 EXPECT_FALSE(True
.isNegative());
260 EXPECT_TRUE(True
.isNonNegative());
261 EXPECT_TRUE(True
.isStrictlyPositive());
263 EXPECT_FALSE(CharMin
.isNegative());
264 EXPECT_TRUE(CharMin
.isNonNegative());
265 EXPECT_FALSE(CharMin
.isStrictlyPositive());
267 EXPECT_FALSE(CharSmall
.isNegative());
268 EXPECT_TRUE(CharSmall
.isNonNegative());
269 EXPECT_TRUE(CharSmall
.isStrictlyPositive());
271 EXPECT_FALSE(CharBoundaryUnder
.isNegative());
272 EXPECT_TRUE(CharBoundaryUnder
.isNonNegative());
273 EXPECT_TRUE(CharBoundaryUnder
.isStrictlyPositive());
275 EXPECT_FALSE(CharBoundaryOver
.isNegative());
276 EXPECT_TRUE(CharBoundaryOver
.isNonNegative());
277 EXPECT_TRUE(CharBoundaryOver
.isStrictlyPositive());
279 EXPECT_FALSE(CharLarge
.isNegative());
280 EXPECT_TRUE(CharLarge
.isNonNegative());
281 EXPECT_TRUE(CharLarge
.isStrictlyPositive());
283 EXPECT_FALSE(CharMax
.isNegative());
284 EXPECT_TRUE(CharMax
.isNonNegative());
285 EXPECT_TRUE(CharMax
.isStrictlyPositive());
288 } // end anonymous namespace