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