1 //===- llvm/unittest/VMCore/Metadata.cpp - Metadata unit tests ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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/raw_ostream.h"
17 #include "llvm/Support/ValueHandle.h"
22 LLVMContext &Context = getGlobalContext();
24 // Test that construction of MDString with different value produces different
25 // MDString objects, even with the same string pointer and nulls in the string.
26 TEST(MDStringTest, CreateDifferent) {
27 char x[3] = { 'f', 0, 'A' };
28 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
30 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
34 // Test that creation of MDStrings with the same string contents produces the
35 // same MDString object, even with different pointers.
36 TEST(MDStringTest, CreateSame) {
37 char x[4] = { 'a', 'b', 'c', 'X' };
38 char y[4] = { 'a', 'b', 'c', 'Y' };
40 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
41 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
45 // Test that MDString prints out the string we fed it.
46 TEST(MDStringTest, PrintingSimple) {
47 char *str = new char[13];
48 strncpy(str, "testing 1 2 3", 13);
49 MDString *s = MDString::get(Context, StringRef(str, 13));
50 strncpy(str, "aaaaaaaaaaaaa", 13);
54 raw_string_ostream oss(Str);
56 EXPECT_STREQ("metadata !\"testing 1 2 3\"", oss.str().c_str());
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));
64 raw_string_ostream oss(Str);
66 EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
69 // Test the two constructors, and containing other Constants.
70 TEST(MDNodeTest, Simple) {
71 char x[3] = { 'a', 'b', 'c' };
72 char y[3] = { '1', '2', '3' };
74 MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
75 MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
76 ConstantInt *CI = ConstantInt::get(getGlobalContext(), APInt(8, 0));
78 std::vector<Value *> V;
83 MDNode *n1 = MDNode::get(Context, &V[0], 3);
85 MDNode *n2 = MDNode::get(Context, &c1, 1);
86 MDNode *n3 = MDNode::get(Context, &V[0], 3);
88 // FIXME: Enable uniqueness test. EXPECT_EQ(n1, n3);
90 EXPECT_EQ(3u, n1->getNumElements());
91 EXPECT_EQ(s1, n1->getElement(0));
92 EXPECT_EQ(CI, n1->getElement(1));
93 EXPECT_EQ(s2, n1->getElement(2));
95 EXPECT_EQ(1u, n2->getNumElements());
96 EXPECT_EQ(n1, n2->getElement(0));
99 raw_string_ostream oss(Str);
101 EXPECT_STREQ("!0 = metadata !{metadata !\"abc\", i8 0, metadata !\"123\"}\n",
105 EXPECT_STREQ("!0 = metadata !{metadata !1}\n"
106 "!1 = metadata !{metadata !\"abc\", i8 0, metadata !\"123\"}\n",
110 TEST(MDNodeTest, Delete) {
111 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
112 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
115 MDNode *n = MDNode::get(Context, &V, 1);
123 raw_string_ostream oss(Str);
125 EXPECT_STREQ("!0 = metadata !{null}\n", oss.str().c_str());
128 TEST(NamedMDNodeTest, Search) {
129 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
130 Constant *C2 = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 2);
133 Value *const V2 = C2;
134 MDNode *n = MDNode::get(Context, &V, 1);
135 MDNode *n2 = MDNode::get(Context, &V2, 1);
137 MetadataBase *Nodes[2] = { n, n2 };
139 Module *M = new Module("MyModule", getGlobalContext());
140 const char *Name = "llvm.NMD1";
141 NamedMDNode *NMD = NamedMDNode::Create(getGlobalContext(), Name, &Nodes[0], 2, M);
143 raw_string_ostream oss(Str);
145 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n!0 = metadata !{i32 1}\n"
146 "!1 = metadata !{i32 2}\n",