Make MDNode use CallbackVH. Also change MDNode to store Value* instead of
[oota-llvm.git] / unittests / VMCore / MetadataTest.cpp
1 //===- llvm/unittest/VMCore/Metadata.cpp - Metadata unit 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 "gtest/gtest.h"
11 #include "llvm/Constants.h"
12 #include "llvm/Instructions.h"
13 #include "llvm/MDNode.h"
14 #include "llvm/Type.h"
15 #include "llvm/Support/ValueHandle.h"
16 #include <sstream>
17
18 using namespace llvm;
19
20 namespace {
21
22 // Test that construction of MDString with different value produces different
23 // MDString objects, even with the same string pointer and nulls in the string.
24 TEST(MDStringTest, CreateDifferent) {
25   char x[3] = { 'f', 0, 'A' };
26   MDString *s1 = MDString::get(&x[0], &x[3]);
27   x[2] = 'B';
28   MDString *s2 = MDString::get(&x[0], &x[3]);
29   EXPECT_NE(s1, s2);
30 }
31
32 // Test that creation of MDStrings with the same string contents produces the
33 // same MDString object, even with different pointers.
34 TEST(MDStringTest, CreateSame) {
35   char x[4] = { 'a', 'b', 'c', 'X' };
36   char y[4] = { 'a', 'b', 'c', 'Y' };
37
38   MDString *s1 = MDString::get(&x[0], &x[3]);
39   MDString *s2 = MDString::get(&y[0], &y[3]);
40   EXPECT_EQ(s1, s2);
41 }
42
43 // Test that MDString prints out the string we fed it.
44 TEST(MDStringTest, PrintingSimple) {
45   char *str = new char[13];
46   strncpy(str, "testing 1 2 3", 13);
47   MDString *s = MDString::get(str, str+13);
48   strncpy(str, "aaaaaaaaaaaaa", 13);
49   delete[] str;
50
51   std::ostringstream oss;
52   s->print(oss);
53   EXPECT_STREQ("{ } !\"testing 1 2 3\"", oss.str().c_str());
54 }
55
56 // Test printing of MDString with non-printable characters.
57 TEST(MDStringTest, PrintingComplex) {
58   char str[5] = {0, '\n', '"', '\\', -1};
59   MDString *s = MDString::get(str+0, str+5);
60   std::ostringstream oss;
61   s->print(oss);
62   EXPECT_STREQ("{ } !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
63 }
64
65 // Test the two constructors, and containing other Constants.
66 TEST(MDNodeTest, Simple) {
67   char x[3] = { 'a', 'b', 'c' };
68   char y[3] = { '1', '2', '3' };
69
70   MDString *s1 = MDString::get(&x[0], &x[3]);
71   MDString *s2 = MDString::get(&y[0], &y[3]);
72   ConstantInt *CI = ConstantInt::get(APInt(8, 0));
73
74   std::vector<Value *> V;
75   V.push_back(s1);
76   V.push_back(CI);
77   V.push_back(s2);
78
79   MDNode *n1 = MDNode::get(&V[0], 3);
80   Value *const c1 = n1;
81   MDNode *n2 = MDNode::get(&c1, 1);
82   MDNode *n3 = MDNode::get(&V[0], 3);
83   EXPECT_NE(n1, n2);
84   EXPECT_EQ(n1, n3);
85
86   EXPECT_EQ(3u, n1->getNumElements());
87   EXPECT_EQ(s1, n1->getElement(0));
88   EXPECT_EQ(CI, n1->getElement(1));
89   EXPECT_EQ(s2, n1->getElement(2));
90
91   EXPECT_EQ(1u, n2->getNumElements());
92   EXPECT_EQ(n1, n2->getElement(0));
93
94   std::ostringstream oss1, oss2;
95   n1->print(oss1);
96   n2->print(oss2);
97   EXPECT_STREQ("{ } !{{ } !\"abc\", i8 0, { } !\"123\"}", oss1.str().c_str());
98   EXPECT_STREQ("{ } !{{ } !{{ } !\"abc\", i8 0, { } !\"123\"}}",
99                oss2.str().c_str());
100 }
101
102 TEST(MDNodeTest, RAUW) {
103   Constant *C = ConstantInt::get(Type::Int32Ty, 1);
104   Instruction *I = new BitCastInst(C, Type::Int32Ty);
105
106   Value *const V1 = I;
107   MDNode *n1 = MDNode::get(&V1, 1);
108   WeakVH wn1 = n1;
109
110   Value *const V2 = C;
111   MDNode *n2 = MDNode::get(&V2, 1);
112   WeakVH wn2 = n2;
113
114   EXPECT_NE(wn1, wn2);
115
116   I->replaceAllUsesWith(C);
117
118   EXPECT_EQ(wn1, wn2);
119 }
120
121 TEST(MDNodeTest, Delete) {
122   Constant *C = ConstantInt::get(Type::Int32Ty, 1);
123   Instruction *I = new BitCastInst(C, Type::Int32Ty);
124
125   Value *const V = I;
126   MDNode *n = MDNode::get(&V, 1);
127   WeakVH wvh = n;
128
129   EXPECT_EQ(n, wvh);
130
131   delete I;
132
133   std::ostringstream oss;
134   wvh->print(oss);
135   EXPECT_STREQ("{ } !{null}", oss.str().c_str());
136 }
137 }