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