slightly increase prettiness.
[llvm/avr.git] / unittests / VMCore / MetadataTest.cpp
blobb92b068e259c64b28641c2c7cd40c1426a73cabd
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 //===----------------------------------------------------------------------===//
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"
18 using namespace llvm;
20 namespace {
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));
29 x[2] = 'B';
30 MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
31 EXPECT_NE(s1, s2);
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));
42 EXPECT_EQ(s1, s2);
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);
51 delete[] str;
53 std::string Str;
54 raw_string_ostream oss(Str);
55 s->print(oss);
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));
63 std::string Str;
64 raw_string_ostream oss(Str);
65 s->print(oss);
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;
79 V.push_back(s1);
80 V.push_back(CI);
81 V.push_back(s2);
83 MDNode *n1 = MDNode::get(Context, &V[0], 3);
84 Value *const c1 = n1;
85 MDNode *n2 = MDNode::get(Context, &c1, 1);
86 MDNode *n3 = MDNode::get(Context, &V[0], 3);
87 EXPECT_NE(n1, n2);
88 #ifdef ENABLE_MDNODE_UNIQUING
89 EXPECT_EQ(n1, n3);
90 #else
91 (void) n3;
92 #endif
94 EXPECT_EQ(3u, n1->getNumElements());
95 EXPECT_EQ(s1, n1->getElement(0));
96 EXPECT_EQ(CI, n1->getElement(1));
97 EXPECT_EQ(s2, n1->getElement(2));
99 EXPECT_EQ(1u, n2->getNumElements());
100 EXPECT_EQ(n1, n2->getElement(0));
102 std::string Str;
103 raw_string_ostream oss(Str);
104 n1->print(oss);
105 EXPECT_STREQ("!0 = metadata !{metadata !\"abc\", i8 0, metadata !\"123\"}\n",
106 oss.str().c_str());
107 Str.clear();
108 n2->print(oss);
109 EXPECT_STREQ("!0 = metadata !{metadata !1}\n"
110 "!1 = metadata !{metadata !\"abc\", i8 0, metadata !\"123\"}\n",
111 oss.str().c_str());
114 TEST(MDNodeTest, Delete) {
115 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
116 Instruction *I = new BitCastInst(C, Type::getInt32Ty(getGlobalContext()));
118 Value *const V = I;
119 MDNode *n = MDNode::get(Context, &V, 1);
120 WeakVH wvh = n;
122 EXPECT_EQ(n, wvh);
124 delete I;
126 std::string Str;
127 raw_string_ostream oss(Str);
128 wvh->print(oss);
129 EXPECT_STREQ("!0 = metadata !{null}\n", oss.str().c_str());
132 TEST(NamedMDNodeTest, Search) {
133 Constant *C = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 1);
134 Constant *C2 = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), 2);
136 Value *const V = C;
137 Value *const V2 = C2;
138 MDNode *n = MDNode::get(Context, &V, 1);
139 MDNode *n2 = MDNode::get(Context, &V2, 1);
141 MetadataBase *Nodes[2] = { n, n2 };
143 Module *M = new Module("MyModule", getGlobalContext());
144 const char *Name = "llvm.NMD1";
145 NamedMDNode *NMD = NamedMDNode::Create(getGlobalContext(), Name, &Nodes[0], 2, M);
146 std::string Str;
147 raw_string_ostream oss(Str);
148 NMD->print(oss);
149 EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n!0 = metadata !{i32 1}\n"
150 "!1 = metadata !{i32 2}\n",
151 oss.str().c_str());