[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / unittests / ADT / APSIntTest.cpp
blob932f54c19f18d2edd6b4879f44938de6a56fcbdf
1 //===- llvm/unittest/ADT/APSIntTest.cpp - APSInt unit tests ---------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/APSInt.h"
10 #include "gtest/gtest.h"
12 using namespace llvm;
14 namespace {
16 TEST(APSIntTest, MoveTest) {
17 APSInt A(32, true);
18 EXPECT_TRUE(A.isUnsigned());
20 APSInt B(128, false);
21 A = B;
22 EXPECT_FALSE(A.isUnsigned());
24 APSInt C(B);
25 EXPECT_FALSE(C.isUnsigned());
27 APInt Wide(256, 0);
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.
33 A = APSInt(64, true);
34 EXPECT_TRUE(A.isUnsigned());
36 Wide = APInt(128, 1);
37 Bits = Wide.getRawData();
38 A = std::move(Wide);
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, getExtValue) {
66 EXPECT_TRUE(APSInt(APInt(3, 7), true).isUnsigned());
67 EXPECT_TRUE(APSInt(APInt(3, 7), false).isSigned());
68 EXPECT_TRUE(APSInt(APInt(4, 7), true).isUnsigned());
69 EXPECT_TRUE(APSInt(APInt(4, 7), false).isSigned());
70 EXPECT_TRUE(APSInt(APInt(4, -7), true).isUnsigned());
71 EXPECT_TRUE(APSInt(APInt(4, -7), false).isSigned());
72 EXPECT_EQ(7, APSInt(APInt(3, 7), true).getExtValue());
73 EXPECT_EQ(-1, APSInt(APInt(3, 7), false).getExtValue());
74 EXPECT_EQ(7, APSInt(APInt(4, 7), true).getExtValue());
75 EXPECT_EQ(7, APSInt(APInt(4, 7), false).getExtValue());
76 EXPECT_EQ(9, APSInt(APInt(4, -7), true).getExtValue());
77 EXPECT_EQ(-7, APSInt(APInt(4, -7), false).getExtValue());
80 TEST(APSIntTest, compareValues) {
81 auto U = [](uint64_t V) { return APSInt::getUnsigned(V); };
82 auto S = [](int64_t V) { return APSInt::get(V); };
84 // Bit-width matches and is-signed.
85 EXPECT_TRUE(APSInt::compareValues(S(7), S(8)) < 0);
86 EXPECT_TRUE(APSInt::compareValues(S(8), S(7)) > 0);
87 EXPECT_TRUE(APSInt::compareValues(S(7), S(7)) == 0);
88 EXPECT_TRUE(APSInt::compareValues(S(-7), S(8)) < 0);
89 EXPECT_TRUE(APSInt::compareValues(S(8), S(-7)) > 0);
90 EXPECT_TRUE(APSInt::compareValues(S(-7), S(-7)) == 0);
91 EXPECT_TRUE(APSInt::compareValues(S(-7), S(-8)) > 0);
92 EXPECT_TRUE(APSInt::compareValues(S(-8), S(-7)) < 0);
93 EXPECT_TRUE(APSInt::compareValues(S(-7), S(-7)) == 0);
95 // Bit-width matches and not is-signed.
96 EXPECT_TRUE(APSInt::compareValues(U(7), U(8)) < 0);
97 EXPECT_TRUE(APSInt::compareValues(U(8), U(7)) > 0);
98 EXPECT_TRUE(APSInt::compareValues(U(7), U(7)) == 0);
100 // Bit-width matches and mixed signs.
101 EXPECT_TRUE(APSInt::compareValues(U(7), S(8)) < 0);
102 EXPECT_TRUE(APSInt::compareValues(U(8), S(7)) > 0);
103 EXPECT_TRUE(APSInt::compareValues(U(7), S(7)) == 0);
104 EXPECT_TRUE(APSInt::compareValues(U(8), S(-7)) > 0);
106 // Bit-width mismatch and is-signed.
107 EXPECT_TRUE(APSInt::compareValues(S(7).trunc(32), S(8)) < 0);
108 EXPECT_TRUE(APSInt::compareValues(S(8).trunc(32), S(7)) > 0);
109 EXPECT_TRUE(APSInt::compareValues(S(7).trunc(32), S(7)) == 0);
110 EXPECT_TRUE(APSInt::compareValues(S(-7).trunc(32), S(8)) < 0);
111 EXPECT_TRUE(APSInt::compareValues(S(8).trunc(32), S(-7)) > 0);
112 EXPECT_TRUE(APSInt::compareValues(S(-7).trunc(32), S(-7)) == 0);
113 EXPECT_TRUE(APSInt::compareValues(S(-7).trunc(32), S(-8)) > 0);
114 EXPECT_TRUE(APSInt::compareValues(S(-8).trunc(32), S(-7)) < 0);
115 EXPECT_TRUE(APSInt::compareValues(S(-7).trunc(32), S(-7)) == 0);
116 EXPECT_TRUE(APSInt::compareValues(S(7), S(8).trunc(32)) < 0);
117 EXPECT_TRUE(APSInt::compareValues(S(8), S(7).trunc(32)) > 0);
118 EXPECT_TRUE(APSInt::compareValues(S(7), S(7).trunc(32)) == 0);
119 EXPECT_TRUE(APSInt::compareValues(S(-7), S(8).trunc(32)) < 0);
120 EXPECT_TRUE(APSInt::compareValues(S(8), S(-7).trunc(32)) > 0);
121 EXPECT_TRUE(APSInt::compareValues(S(-7), S(-7).trunc(32)) == 0);
122 EXPECT_TRUE(APSInt::compareValues(S(-7), S(-8).trunc(32)) > 0);
123 EXPECT_TRUE(APSInt::compareValues(S(-8), S(-7).trunc(32)) < 0);
124 EXPECT_TRUE(APSInt::compareValues(S(-7), S(-7).trunc(32)) == 0);
126 // Bit-width mismatch and not is-signed.
127 EXPECT_TRUE(APSInt::compareValues(U(7), U(8).trunc(32)) < 0);
128 EXPECT_TRUE(APSInt::compareValues(U(8), U(7).trunc(32)) > 0);
129 EXPECT_TRUE(APSInt::compareValues(U(7), U(7).trunc(32)) == 0);
130 EXPECT_TRUE(APSInt::compareValues(U(7).trunc(32), U(8)) < 0);
131 EXPECT_TRUE(APSInt::compareValues(U(8).trunc(32), U(7)) > 0);
132 EXPECT_TRUE(APSInt::compareValues(U(7).trunc(32), U(7)) == 0);
134 // Bit-width mismatch and mixed signs.
135 EXPECT_TRUE(APSInt::compareValues(U(7).trunc(32), S(8)) < 0);
136 EXPECT_TRUE(APSInt::compareValues(U(8).trunc(32), S(7)) > 0);
137 EXPECT_TRUE(APSInt::compareValues(U(7).trunc(32), S(7)) == 0);
138 EXPECT_TRUE(APSInt::compareValues(U(8).trunc(32), S(-7)) > 0);
139 EXPECT_TRUE(APSInt::compareValues(U(7), S(8).trunc(32)) < 0);
140 EXPECT_TRUE(APSInt::compareValues(U(8), S(7).trunc(32)) > 0);
141 EXPECT_TRUE(APSInt::compareValues(U(7), S(7).trunc(32)) == 0);
142 EXPECT_TRUE(APSInt::compareValues(U(8), S(-7).trunc(32)) > 0);
145 TEST(APSIntTest, FromString) {
146 EXPECT_EQ(APSInt("1").getExtValue(), 1);
147 EXPECT_EQ(APSInt("-1").getExtValue(), -1);
148 EXPECT_EQ(APSInt("0").getExtValue(), 0);
149 EXPECT_EQ(APSInt("56789").getExtValue(), 56789);
150 EXPECT_EQ(APSInt("-1234").getExtValue(), -1234);
153 TEST(APSIntTest, FromStringBitWidth) {
154 EXPECT_EQ(APSInt("0").getBitWidth(), 1U);
155 EXPECT_EQ(APSInt("000").getBitWidth(), 1U);
156 EXPECT_EQ(APSInt("1").getBitWidth(), 1U);
157 EXPECT_EQ(APSInt("2").getBitWidth(), 2U);
158 EXPECT_EQ(APSInt("3").getBitWidth(), 2U);
159 EXPECT_EQ(APSInt("003").getBitWidth(), 2U);
160 EXPECT_EQ(APSInt("15").getBitWidth(), 4U);
161 EXPECT_EQ(APSInt("16").getBitWidth(), 5U);
162 EXPECT_EQ(APSInt("17").getBitWidth(), 5U);
164 EXPECT_EQ(APSInt("-0").getBitWidth(), 1U);
165 EXPECT_EQ(APSInt("-000").getBitWidth(), 1U);
166 EXPECT_EQ(APSInt("-1").getBitWidth(), 1U);
167 EXPECT_EQ(APSInt("-2").getBitWidth(), 2U);
168 EXPECT_EQ(APSInt("-3").getBitWidth(), 3U);
169 EXPECT_EQ(APSInt("-003").getBitWidth(), 3U);
170 EXPECT_EQ(APSInt("-5").getBitWidth(), 4U);
171 EXPECT_EQ(APSInt("-15").getBitWidth(), 5U);
172 EXPECT_EQ(APSInt("-16").getBitWidth(), 5U);
173 EXPECT_EQ(APSInt("-17").getBitWidth(), 6U);
176 #if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG)
178 TEST(APSIntTest, StringDeath) {
179 EXPECT_DEATH((void)APSInt(""), "Invalid string length");
180 EXPECT_DEATH((void)APSInt("1a"), "Invalid character in digit string");
183 #endif
185 TEST(APSIntTest, SignedHighBit) {
186 APSInt False(APInt(1, 0), false);
187 APSInt True(APInt(1, 1), false);
188 APSInt CharMin(APInt(8, 0), false);
189 APSInt CharSmall(APInt(8, 0x13), false);
190 APSInt CharBoundaryUnder(APInt(8, 0x7f), false);
191 APSInt CharBoundaryOver(APInt(8, 0x80), false);
192 APSInt CharLarge(APInt(8, 0xd9), false);
193 APSInt CharMax(APInt(8, 0xff), false);
195 EXPECT_FALSE(False.isNegative());
196 EXPECT_TRUE(False.isNonNegative());
197 EXPECT_FALSE(False.isStrictlyPositive());
199 EXPECT_TRUE(True.isNegative());
200 EXPECT_FALSE(True.isNonNegative());
201 EXPECT_FALSE(True.isStrictlyPositive());
203 EXPECT_FALSE(CharMin.isNegative());
204 EXPECT_TRUE(CharMin.isNonNegative());
205 EXPECT_FALSE(CharMin.isStrictlyPositive());
207 EXPECT_FALSE(CharSmall.isNegative());
208 EXPECT_TRUE(CharSmall.isNonNegative());
209 EXPECT_TRUE(CharSmall.isStrictlyPositive());
211 EXPECT_FALSE(CharBoundaryUnder.isNegative());
212 EXPECT_TRUE(CharBoundaryUnder.isNonNegative());
213 EXPECT_TRUE(CharBoundaryUnder.isStrictlyPositive());
215 EXPECT_TRUE(CharBoundaryOver.isNegative());
216 EXPECT_FALSE(CharBoundaryOver.isNonNegative());
217 EXPECT_FALSE(CharBoundaryOver.isStrictlyPositive());
219 EXPECT_TRUE(CharLarge.isNegative());
220 EXPECT_FALSE(CharLarge.isNonNegative());
221 EXPECT_FALSE(CharLarge.isStrictlyPositive());
223 EXPECT_TRUE(CharMax.isNegative());
224 EXPECT_FALSE(CharMax.isNonNegative());
225 EXPECT_FALSE(CharMax.isStrictlyPositive());
228 TEST(APSIntTest, UnsignedHighBit) {
229 APSInt False(APInt(1, 0));
230 APSInt True(APInt(1, 1));
231 APSInt CharMin(APInt(8, 0));
232 APSInt CharSmall(APInt(8, 0x13));
233 APSInt CharBoundaryUnder(APInt(8, 0x7f));
234 APSInt CharBoundaryOver(APInt(8, 0x80));
235 APSInt CharLarge(APInt(8, 0xd9));
236 APSInt CharMax(APInt(8, 0xff));
238 EXPECT_FALSE(False.isNegative());
239 EXPECT_TRUE(False.isNonNegative());
240 EXPECT_FALSE(False.isStrictlyPositive());
242 EXPECT_FALSE(True.isNegative());
243 EXPECT_TRUE(True.isNonNegative());
244 EXPECT_TRUE(True.isStrictlyPositive());
246 EXPECT_FALSE(CharMin.isNegative());
247 EXPECT_TRUE(CharMin.isNonNegative());
248 EXPECT_FALSE(CharMin.isStrictlyPositive());
250 EXPECT_FALSE(CharSmall.isNegative());
251 EXPECT_TRUE(CharSmall.isNonNegative());
252 EXPECT_TRUE(CharSmall.isStrictlyPositive());
254 EXPECT_FALSE(CharBoundaryUnder.isNegative());
255 EXPECT_TRUE(CharBoundaryUnder.isNonNegative());
256 EXPECT_TRUE(CharBoundaryUnder.isStrictlyPositive());
258 EXPECT_FALSE(CharBoundaryOver.isNegative());
259 EXPECT_TRUE(CharBoundaryOver.isNonNegative());
260 EXPECT_TRUE(CharBoundaryOver.isStrictlyPositive());
262 EXPECT_FALSE(CharLarge.isNegative());
263 EXPECT_TRUE(CharLarge.isNonNegative());
264 EXPECT_TRUE(CharLarge.isStrictlyPositive());
266 EXPECT_FALSE(CharMax.isNegative());
267 EXPECT_TRUE(CharMax.isNonNegative());
268 EXPECT_TRUE(CharMax.isStrictlyPositive());
271 } // end anonymous namespace