[MIParser] Set RegClassOrRegBank during instruction parsing
[llvm-complete.git] / unittests / ADT / APSIntTest.cpp
blob9045f3e48f7dfc744ce8a3ef86464c807c884bed
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 #if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG)
155 TEST(APSIntTest, StringDeath) {
156 EXPECT_DEATH((void)APSInt(""), "Invalid string length");
157 EXPECT_DEATH((void)APSInt("1a"), "Invalid character in digit string");
160 #endif
162 TEST(APSIntTest, SignedHighBit) {
163 APSInt False(APInt(1, 0), false);
164 APSInt True(APInt(1, 1), false);
165 APSInt CharMin(APInt(8, 0), false);
166 APSInt CharSmall(APInt(8, 0x13), false);
167 APSInt CharBoundaryUnder(APInt(8, 0x7f), false);
168 APSInt CharBoundaryOver(APInt(8, 0x80), false);
169 APSInt CharLarge(APInt(8, 0xd9), false);
170 APSInt CharMax(APInt(8, 0xff), false);
172 EXPECT_FALSE(False.isNegative());
173 EXPECT_TRUE(False.isNonNegative());
174 EXPECT_FALSE(False.isStrictlyPositive());
176 EXPECT_TRUE(True.isNegative());
177 EXPECT_FALSE(True.isNonNegative());
178 EXPECT_FALSE(True.isStrictlyPositive());
180 EXPECT_FALSE(CharMin.isNegative());
181 EXPECT_TRUE(CharMin.isNonNegative());
182 EXPECT_FALSE(CharMin.isStrictlyPositive());
184 EXPECT_FALSE(CharSmall.isNegative());
185 EXPECT_TRUE(CharSmall.isNonNegative());
186 EXPECT_TRUE(CharSmall.isStrictlyPositive());
188 EXPECT_FALSE(CharBoundaryUnder.isNegative());
189 EXPECT_TRUE(CharBoundaryUnder.isNonNegative());
190 EXPECT_TRUE(CharBoundaryUnder.isStrictlyPositive());
192 EXPECT_TRUE(CharBoundaryOver.isNegative());
193 EXPECT_FALSE(CharBoundaryOver.isNonNegative());
194 EXPECT_FALSE(CharBoundaryOver.isStrictlyPositive());
196 EXPECT_TRUE(CharLarge.isNegative());
197 EXPECT_FALSE(CharLarge.isNonNegative());
198 EXPECT_FALSE(CharLarge.isStrictlyPositive());
200 EXPECT_TRUE(CharMax.isNegative());
201 EXPECT_FALSE(CharMax.isNonNegative());
202 EXPECT_FALSE(CharMax.isStrictlyPositive());
205 TEST(APSIntTest, UnsignedHighBit) {
206 APSInt False(APInt(1, 0));
207 APSInt True(APInt(1, 1));
208 APSInt CharMin(APInt(8, 0));
209 APSInt CharSmall(APInt(8, 0x13));
210 APSInt CharBoundaryUnder(APInt(8, 0x7f));
211 APSInt CharBoundaryOver(APInt(8, 0x80));
212 APSInt CharLarge(APInt(8, 0xd9));
213 APSInt CharMax(APInt(8, 0xff));
215 EXPECT_FALSE(False.isNegative());
216 EXPECT_TRUE(False.isNonNegative());
217 EXPECT_FALSE(False.isStrictlyPositive());
219 EXPECT_FALSE(True.isNegative());
220 EXPECT_TRUE(True.isNonNegative());
221 EXPECT_TRUE(True.isStrictlyPositive());
223 EXPECT_FALSE(CharMin.isNegative());
224 EXPECT_TRUE(CharMin.isNonNegative());
225 EXPECT_FALSE(CharMin.isStrictlyPositive());
227 EXPECT_FALSE(CharSmall.isNegative());
228 EXPECT_TRUE(CharSmall.isNonNegative());
229 EXPECT_TRUE(CharSmall.isStrictlyPositive());
231 EXPECT_FALSE(CharBoundaryUnder.isNegative());
232 EXPECT_TRUE(CharBoundaryUnder.isNonNegative());
233 EXPECT_TRUE(CharBoundaryUnder.isStrictlyPositive());
235 EXPECT_FALSE(CharBoundaryOver.isNegative());
236 EXPECT_TRUE(CharBoundaryOver.isNonNegative());
237 EXPECT_TRUE(CharBoundaryOver.isStrictlyPositive());
239 EXPECT_FALSE(CharLarge.isNegative());
240 EXPECT_TRUE(CharLarge.isNonNegative());
241 EXPECT_TRUE(CharLarge.isStrictlyPositive());
243 EXPECT_FALSE(CharMax.isNegative());
244 EXPECT_TRUE(CharMax.isNonNegative());
245 EXPECT_TRUE(CharMax.isStrictlyPositive());
248 } // end anonymous namespace