blob: 7980758f9fdd67dea1e5a9e807f35bda593b90c6 [file] [log] [blame]
Jeffrey Yasskin487fa012009-04-27 20:32:07 +00001//===- llvm/unittest/Support/ValueHandleTest.cpp - ValueHandle tests --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Support/ValueHandle.h"
11
12#include "llvm/Constants.h"
13#include "llvm/Instructions.h"
14
15#include "gtest/gtest.h"
16
17#include <memory>
18
19using namespace llvm;
20
21namespace {
22
23class ValueHandle : public testing::Test {
24protected:
25 Constant *ConstantV;
26 std::auto_ptr<BitCastInst> BitcastV;
27
28 ValueHandle() : ConstantV(ConstantInt::get(Type::Int32Ty, 0)),
29 BitcastV(new BitCastInst(ConstantV, Type::Int32Ty)) {
30 }
31};
32
33TEST_F(ValueHandle, WeakVH_BasicOperation) {
34 WeakVH WVH(BitcastV.get());
35 EXPECT_EQ(BitcastV.get(), WVH);
36 WVH = ConstantV;
37 EXPECT_EQ(ConstantV, WVH);
38
39 // Make sure I can call a method on the underlying Value. It
40 // doesn't matter which method.
41 EXPECT_EQ(Type::Int32Ty, WVH->getType());
42 EXPECT_EQ(Type::Int32Ty, (*WVH).getType());
43}
44
45TEST_F(ValueHandle, WeakVH_Comparisons) {
46 WeakVH BitcastWVH(BitcastV.get());
47 WeakVH ConstantWVH(ConstantV);
48
49 EXPECT_TRUE(BitcastWVH == BitcastWVH);
50 EXPECT_TRUE(BitcastV.get() == BitcastWVH);
51 EXPECT_TRUE(BitcastWVH == BitcastV.get());
52 EXPECT_FALSE(BitcastWVH == ConstantWVH);
53
54 EXPECT_TRUE(BitcastWVH != ConstantWVH);
55 EXPECT_TRUE(BitcastV.get() != ConstantWVH);
56 EXPECT_TRUE(BitcastWVH != ConstantV);
57 EXPECT_FALSE(BitcastWVH != BitcastWVH);
58
59 // Cast to Value* so comparisons work.
60 Value *BV = BitcastV.get();
61 Value *CV = ConstantV;
62 EXPECT_EQ(BV < CV, BitcastWVH < ConstantWVH);
63 EXPECT_EQ(BV <= CV, BitcastWVH <= ConstantWVH);
64 EXPECT_EQ(BV > CV, BitcastWVH > ConstantWVH);
65 EXPECT_EQ(BV >= CV, BitcastWVH >= ConstantWVH);
66
67 EXPECT_EQ(BV < CV, BitcastV.get() < ConstantWVH);
68 EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantWVH);
69 EXPECT_EQ(BV > CV, BitcastV.get() > ConstantWVH);
70 EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantWVH);
71
72 EXPECT_EQ(BV < CV, BitcastWVH < ConstantV);
73 EXPECT_EQ(BV <= CV, BitcastWVH <= ConstantV);
74 EXPECT_EQ(BV > CV, BitcastWVH > ConstantV);
75 EXPECT_EQ(BV >= CV, BitcastWVH >= ConstantV);
76}
77
78TEST_F(ValueHandle, WeakVH_FollowsRAUW) {
79 WeakVH WVH(BitcastV.get());
80 WeakVH WVH_Copy(WVH);
81 WeakVH WVH_Recreated(BitcastV.get());
82 BitcastV->replaceAllUsesWith(ConstantV);
83 EXPECT_EQ(ConstantV, WVH);
84 EXPECT_EQ(ConstantV, WVH_Copy);
85 EXPECT_EQ(ConstantV, WVH_Recreated);
86}
87
88TEST_F(ValueHandle, WeakVH_NullOnDeletion) {
89 WeakVH WVH(BitcastV.get());
90 WeakVH WVH_Copy(WVH);
91 WeakVH WVH_Recreated(BitcastV.get());
92 BitcastV.reset();
93 Value *null_value = NULL;
94 EXPECT_EQ(null_value, WVH);
95 EXPECT_EQ(null_value, WVH_Copy);
96 EXPECT_EQ(null_value, WVH_Recreated);
97}
98
99
100TEST_F(ValueHandle, AssertingVH_BasicOperation) {
101 AssertingVH<CastInst> AVH(BitcastV.get());
102 CastInst *implicit_to_exact_type = AVH;
103 implicit_to_exact_type = implicit_to_exact_type; // Avoid warning.
104
105 AssertingVH<Value> GenericAVH(BitcastV.get());
106 EXPECT_EQ(BitcastV.get(), GenericAVH);
107 GenericAVH = ConstantV;
108 EXPECT_EQ(ConstantV, GenericAVH);
109
110 // Make sure I can call a method on the underlying CastInst. It
111 // doesn't matter which method.
112 EXPECT_FALSE(AVH->mayWriteToMemory());
113 EXPECT_FALSE((*AVH).mayWriteToMemory());
114}
115
116TEST_F(ValueHandle, AssertingVH_Comparisons) {
117 AssertingVH<Value> BitcastAVH(BitcastV.get());
118 AssertingVH<Value> ConstantAVH(ConstantV);
119
120 EXPECT_TRUE(BitcastAVH == BitcastAVH);
121 EXPECT_TRUE(BitcastV.get() == BitcastAVH);
122 EXPECT_TRUE(BitcastAVH == BitcastV.get());
123 EXPECT_FALSE(BitcastAVH == ConstantAVH);
124
125 EXPECT_TRUE(BitcastAVH != ConstantAVH);
126 EXPECT_TRUE(BitcastV.get() != ConstantAVH);
127 EXPECT_TRUE(BitcastAVH != ConstantV);
128 EXPECT_FALSE(BitcastAVH != BitcastAVH);
129
130 // Cast to Value* so comparisons work.
131 Value *BV = BitcastV.get();
132 Value *CV = ConstantV;
133 EXPECT_EQ(BV < CV, BitcastAVH < ConstantAVH);
134 EXPECT_EQ(BV <= CV, BitcastAVH <= ConstantAVH);
135 EXPECT_EQ(BV > CV, BitcastAVH > ConstantAVH);
136 EXPECT_EQ(BV >= CV, BitcastAVH >= ConstantAVH);
137
138 EXPECT_EQ(BV < CV, BitcastV.get() < ConstantAVH);
139 EXPECT_EQ(BV <= CV, BitcastV.get() <= ConstantAVH);
140 EXPECT_EQ(BV > CV, BitcastV.get() > ConstantAVH);
141 EXPECT_EQ(BV >= CV, BitcastV.get() >= ConstantAVH);
142
143 EXPECT_EQ(BV < CV, BitcastAVH < ConstantV);
144 EXPECT_EQ(BV <= CV, BitcastAVH <= ConstantV);
145 EXPECT_EQ(BV > CV, BitcastAVH > ConstantV);
146 EXPECT_EQ(BV >= CV, BitcastAVH >= ConstantV);
147}
148
149TEST_F(ValueHandle, AssertingVH_DoesNotFollowRAUW) {
150 AssertingVH<Value> AVH(BitcastV.get());
151 BitcastV->replaceAllUsesWith(ConstantV);
152 EXPECT_EQ(BitcastV.get(), AVH);
153}
154
155#ifdef NDEBUG
156
157TEST_F(ValueHandle, AssertingVH_ReducesToPointer) {
158 EXPECT_EQ(sizeof(CastInst *), sizeof(AssertingVH<CastInst>));
159}
160
161#else // !NDEBUG
162
163#ifdef GTEST_HAS_DEATH_TEST
164
165TEST_F(ValueHandle, AssertingVH_Asserts) {
166 AssertingVH<Value> AVH(BitcastV.get());
167 EXPECT_DEATH({BitcastV.reset();},
168 "An asserting value handle still pointed to this value!");
169 AssertingVH<Value> Copy(AVH);
170 AVH = NULL;
171 EXPECT_DEATH({BitcastV.reset();},
172 "An asserting value handle still pointed to this value!");
173 Copy = NULL;
174 BitcastV.reset();
175}
176
177#endif // GTEST_HAS_DEATH_TEST
178
179#endif // NDEBUG
180
181}