aboutsummaryrefslogtreecommitdiff
blob: c76a32051d87caa94eaee98e2a3c35a83074b301 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//===- llvm/unittest/ADT/APSIntTest.cpp - APSInt unit tests ---------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/APSInt.h"
#include "gtest/gtest.h"

using namespace llvm;

namespace {

TEST(APSIntTest, MoveTest) {
  APSInt A(32, true);
  EXPECT_TRUE(A.isUnsigned());

  APSInt B(128, false);
  A = B;
  EXPECT_FALSE(A.isUnsigned());

  APSInt C(B);
  EXPECT_FALSE(C.isUnsigned());

  APInt Wide(256, 0);
  const uint64_t *Bits = Wide.getRawData();
  APSInt D(std::move(Wide));
  EXPECT_TRUE(D.isUnsigned());
  EXPECT_EQ(Bits, D.getRawData()); // Verify that "Wide" was really moved.

  A = APSInt(64, true);
  EXPECT_TRUE(A.isUnsigned());

  Wide = APInt(128, 1);
  Bits = Wide.getRawData();
  A = std::move(Wide);
  EXPECT_TRUE(A.isUnsigned());
  EXPECT_EQ(Bits, A.getRawData()); // Verify that "Wide" was really moved.
}

TEST(APSIntTest, get) {
  EXPECT_TRUE(APSInt::get(7).isSigned());
  EXPECT_EQ(64u, APSInt::get(7).getBitWidth());
  EXPECT_EQ(7u, APSInt::get(7).getZExtValue());
  EXPECT_EQ(7, APSInt::get(7).getSExtValue());
  EXPECT_TRUE(APSInt::get(-7).isSigned());
  EXPECT_EQ(64u, APSInt::get(-7).getBitWidth());
  EXPECT_EQ(-7, APSInt::get(-7).getSExtValue());
  EXPECT_EQ(UINT64_C(0) - 7, APSInt::get(-7).getZExtValue());
}

TEST(APSIntTest, getUnsigned) {
  EXPECT_TRUE(APSInt::getUnsigned(7).isUnsigned());
  EXPECT_EQ(64u, APSInt::getUnsigned(7).getBitWidth());
  EXPECT_EQ(7u, APSInt::getUnsigned(7).getZExtValue());
  EXPECT_EQ(7, APSInt::getUnsigned(7).getSExtValue());
  EXPECT_TRUE(APSInt::getUnsigned(-7).isUnsigned());
  EXPECT_EQ(64u, APSInt::getUnsigned(-7).getBitWidth());
  EXPECT_EQ(-7, APSInt::getUnsigned(-7).getSExtValue());
  EXPECT_EQ(UINT64_C(0) - 7, APSInt::getUnsigned(-7).getZExtValue());
}

TEST(APSIntTest, getExtValue) {
  EXPECT_TRUE(APSInt(APInt(3, 7), true).isUnsigned());
  EXPECT_TRUE(APSInt(APInt(3, 7), false).isSigned());
  EXPECT_TRUE(APSInt(APInt(4, 7), true).isUnsigned());
  EXPECT_TRUE(APSInt(APInt(4, 7), false).isSigned());
  EXPECT_TRUE(APSInt(APInt(4, -7), true).isUnsigned());
  EXPECT_TRUE(APSInt(APInt(4, -7), false).isSigned());
  EXPECT_EQ(7, APSInt(APInt(3, 7), true).getExtValue());
  EXPECT_EQ(-1, APSInt(APInt(3, 7), false).getExtValue());
  EXPECT_EQ(7, APSInt(APInt(4, 7), true).getExtValue());
  EXPECT_EQ(7, APSInt(APInt(4, 7), false).getExtValue());
  EXPECT_EQ(9, APSInt(APInt(4, -7), true).getExtValue());
  EXPECT_EQ(-7, APSInt(APInt(4, -7), false).getExtValue());
}

TEST(APSIntTest, compareValues) {
  auto U = [](uint64_t V) { return APSInt::getUnsigned(V); };
  auto S = [](int64_t V) { return APSInt::get(V); };

  // Bit-width matches and is-signed.
  EXPECT_TRUE(APSInt::compareValues(S(7), S(8)) < 0);
  EXPECT_TRUE(APSInt::compareValues(S(8), S(7)) > 0);
  EXPECT_TRUE(APSInt::compareValues(S(7), S(7)) == 0);
  EXPECT_TRUE(APSInt::compareValues(S(-7), S(8)) < 0);
  EXPECT_TRUE(APSInt::compareValues(S(8), S(-7)) > 0);
  EXPECT_TRUE(APSInt::compareValues(S(-7), S(-7)) == 0);
  EXPECT_TRUE(APSInt::compareValues(S(-7), S(-8)) > 0);
  EXPECT_TRUE(APSInt::compareValues(S(-8), S(-7)) < 0);
  EXPECT_TRUE(APSInt::compareValues(S(-7), S(-7)) == 0);

  // Bit-width matches and not is-signed.
  EXPECT_TRUE(APSInt::compareValues(U(7), U(8)) < 0);
  EXPECT_TRUE(APSInt::compareValues(U(8), U(7)) > 0);
  EXPECT_TRUE(APSInt::compareValues(U(7), U(7)) == 0);

  // Bit-width matches and mixed signs.
  EXPECT_TRUE(APSInt::compareValues(U(7), S(8)) < 0);
  EXPECT_TRUE(APSInt::compareValues(U(8), S(7)) > 0);
  EXPECT_TRUE(APSInt::compareValues(U(7), S(7)) == 0);
  EXPECT_TRUE(APSInt::compareValues(U(8), S(-7)) > 0);

  // Bit-width mismatch and is-signed.
  EXPECT_TRUE(APSInt::compareValues(S(7).trunc(32), S(8)) < 0);
  EXPECT_TRUE(APSInt::compareValues(S(8).trunc(32), S(7)) > 0);
  EXPECT_TRUE(APSInt::compareValues(S(7).trunc(32), S(7)) == 0);
  EXPECT_TRUE(APSInt::compareValues(S(-7).trunc(32), S(8)) < 0);
  EXPECT_TRUE(APSInt::compareValues(S(8).trunc(32), S(-7)) > 0);
  EXPECT_TRUE(APSInt::compareValues(S(-7).trunc(32), S(-7)) == 0);
  EXPECT_TRUE(APSInt::compareValues(S(-7).trunc(32), S(-8)) > 0);
  EXPECT_TRUE(APSInt::compareValues(S(-8).trunc(32), S(-7)) < 0);
  EXPECT_TRUE(APSInt::compareValues(S(-7).trunc(32), S(-7)) == 0);
  EXPECT_TRUE(APSInt::compareValues(S(7), S(8).trunc(32)) < 0);
  EXPECT_TRUE(APSInt::compareValues(S(8), S(7).trunc(32)) > 0);
  EXPECT_TRUE(APSInt::compareValues(S(7), S(7).trunc(32)) == 0);
  EXPECT_TRUE(APSInt::compareValues(S(-7), S(8).trunc(32)) < 0);
  EXPECT_TRUE(APSInt::compareValues(S(8), S(-7).trunc(32)) > 0);
  EXPECT_TRUE(APSInt::compareValues(S(-7), S(-7).trunc(32)) == 0);
  EXPECT_TRUE(APSInt::compareValues(S(-7), S(-8).trunc(32)) > 0);
  EXPECT_TRUE(APSInt::compareValues(S(-8), S(-7).trunc(32)) < 0);
  EXPECT_TRUE(APSInt::compareValues(S(-7), S(-7).trunc(32)) == 0);

  // Bit-width mismatch and not is-signed.
  EXPECT_TRUE(APSInt::compareValues(U(7), U(8).trunc(32)) < 0);
  EXPECT_TRUE(APSInt::compareValues(U(8), U(7).trunc(32)) > 0);
  EXPECT_TRUE(APSInt::compareValues(U(7), U(7).trunc(32)) == 0);
  EXPECT_TRUE(APSInt::compareValues(U(7).trunc(32), U(8)) < 0);
  EXPECT_TRUE(APSInt::compareValues(U(8).trunc(32), U(7)) > 0);
  EXPECT_TRUE(APSInt::compareValues(U(7).trunc(32), U(7)) == 0);

  // Bit-width mismatch and mixed signs.
  EXPECT_TRUE(APSInt::compareValues(U(7).trunc(32), S(8)) < 0);
  EXPECT_TRUE(APSInt::compareValues(U(8).trunc(32), S(7)) > 0);
  EXPECT_TRUE(APSInt::compareValues(U(7).trunc(32), S(7)) == 0);
  EXPECT_TRUE(APSInt::compareValues(U(8).trunc(32), S(-7)) > 0);
  EXPECT_TRUE(APSInt::compareValues(U(7), S(8).trunc(32)) < 0);
  EXPECT_TRUE(APSInt::compareValues(U(8), S(7).trunc(32)) > 0);
  EXPECT_TRUE(APSInt::compareValues(U(7), S(7).trunc(32)) == 0);
  EXPECT_TRUE(APSInt::compareValues(U(8), S(-7).trunc(32)) > 0);
}

TEST(APSIntTest, FromString) {
  EXPECT_EQ(APSInt("1").getExtValue(), 1);
  EXPECT_EQ(APSInt("-1").getExtValue(), -1);
  EXPECT_EQ(APSInt("0").getExtValue(), 0);
  EXPECT_EQ(APSInt("56789").getExtValue(), 56789);
  EXPECT_EQ(APSInt("-1234").getExtValue(), -1234);
}

#if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG)

TEST(APSIntTest, StringDeath) {
  EXPECT_DEATH(APSInt(""), "Invalid string length");
  EXPECT_DEATH(APSInt("1a"), "Invalid character in digit string");
}

#endif

} // end anonymous namespace