blob: d612808b9f6f239f54a4474f9d044c988f3f49ef [file] [log] [blame]
Nick Lewycky49f89192009-04-04 07:22:01 +00001//===- 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"
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000012#include "llvm/Instructions.h"
13#include "llvm/MDNode.h"
14#include "llvm/Type.h"
15#include "llvm/Support/ValueHandle.h"
Nick Lewycky49f89192009-04-04 07:22:01 +000016#include <sstream>
17
18using namespace llvm;
19
20namespace {
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.
24TEST(MDStringTest, CreateDifferent) {
25 char x[3] = { 'f', 0, 'A' };
Devang Patel62920032009-07-23 02:00:51 +000026 MDString *s1 = getGlobalContext().getMDString(&x[0], 3);
Nick Lewycky49f89192009-04-04 07:22:01 +000027 x[2] = 'B';
Devang Patel62920032009-07-23 02:00:51 +000028 MDString *s2 = getGlobalContext().getMDString(&x[0], 3);
Nick Lewycky49f89192009-04-04 07:22:01 +000029 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.
34TEST(MDStringTest, CreateSame) {
35 char x[4] = { 'a', 'b', 'c', 'X' };
36 char y[4] = { 'a', 'b', 'c', 'Y' };
37
Devang Patel62920032009-07-23 02:00:51 +000038 MDString *s1 = getGlobalContext().getMDString(&x[0], 3);
39 MDString *s2 = getGlobalContext().getMDString(&y[0], 3);
Nick Lewycky49f89192009-04-04 07:22:01 +000040 EXPECT_EQ(s1, s2);
41}
42
43// Test that MDString prints out the string we fed it.
44TEST(MDStringTest, PrintingSimple) {
45 char *str = new char[13];
46 strncpy(str, "testing 1 2 3", 13);
Devang Patel62920032009-07-23 02:00:51 +000047 MDString *s = getGlobalContext().getMDString(str, 13);
Nick Lewycky49f89192009-04-04 07:22:01 +000048 strncpy(str, "aaaaaaaaaaaaa", 13);
49 delete[] str;
50
51 std::ostringstream oss;
52 s->print(oss);
Nick Lewyckyadbc2842009-05-30 05:06:04 +000053 EXPECT_STREQ("metadata !\"testing 1 2 3\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +000054}
55
56// Test printing of MDString with non-printable characters.
57TEST(MDStringTest, PrintingComplex) {
58 char str[5] = {0, '\n', '"', '\\', -1};
Devang Patel62920032009-07-23 02:00:51 +000059 MDString *s = getGlobalContext().getMDString(str+0, 5);
Nick Lewycky49f89192009-04-04 07:22:01 +000060 std::ostringstream oss;
61 s->print(oss);
Nick Lewyckyadbc2842009-05-30 05:06:04 +000062 EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
Nick Lewycky49f89192009-04-04 07:22:01 +000063}
64
65// Test the two constructors, and containing other Constants.
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000066TEST(MDNodeTest, Simple) {
Nick Lewycky49f89192009-04-04 07:22:01 +000067 char x[3] = { 'a', 'b', 'c' };
68 char y[3] = { '1', '2', '3' };
69
Devang Patel62920032009-07-23 02:00:51 +000070 MDString *s1 = getGlobalContext().getMDString(&x[0], 3);
71 MDString *s2 = getGlobalContext().getMDString(&y[0], 3);
Owen Andersonb6b25302009-07-14 23:09:55 +000072 ConstantInt *CI = getGlobalContext().getConstantInt(APInt(8, 0));
Nick Lewycky49f89192009-04-04 07:22:01 +000073
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000074 std::vector<Value *> V;
Nick Lewycky49f89192009-04-04 07:22:01 +000075 V.push_back(s1);
76 V.push_back(CI);
77 V.push_back(s2);
78
Owen Andersonb6b25302009-07-14 23:09:55 +000079 MDNode *n1 = getGlobalContext().getMDNode(&V[0], 3);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000080 Value *const c1 = n1;
Owen Andersonb6b25302009-07-14 23:09:55 +000081 MDNode *n2 = getGlobalContext().getMDNode(&c1, 1);
82 MDNode *n3 = getGlobalContext().getMDNode(&V[0], 3);
Nick Lewycky49f89192009-04-04 07:22:01 +000083 EXPECT_NE(n1, n2);
84 EXPECT_EQ(n1, n3);
85
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000086 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));
Nick Lewycky49f89192009-04-04 07:22:01 +000090
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +000091 EXPECT_EQ(1u, n2->getNumElements());
92 EXPECT_EQ(n1, n2->getElement(0));
Nick Lewycky49f89192009-04-04 07:22:01 +000093
94 std::ostringstream oss1, oss2;
95 n1->print(oss1);
96 n2->print(oss2);
Devang Patelf04d63a2009-07-08 21:57:07 +000097 EXPECT_STREQ("!0 = metadata !{metadata !\"abc\", i8 0, metadata !\"123\"}\n",
Nick Lewyckyadbc2842009-05-30 05:06:04 +000098 oss1.str().c_str());
Devang Patelf04d63a2009-07-08 21:57:07 +000099 EXPECT_STREQ("!0 = metadata !{metadata !1}\n"
100 "!1 = metadata !{metadata !\"abc\", i8 0, metadata !\"123\"}\n",
Nick Lewycky49f89192009-04-04 07:22:01 +0000101 oss2.str().c_str());
102}
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000103
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000104TEST(MDNodeTest, Delete) {
Owen Andersonb6b25302009-07-14 23:09:55 +0000105 Constant *C = getGlobalContext().getConstantInt(Type::Int32Ty, 1);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000106 Instruction *I = new BitCastInst(C, Type::Int32Ty);
107
108 Value *const V = I;
Owen Andersonb6b25302009-07-14 23:09:55 +0000109 MDNode *n = getGlobalContext().getMDNode(&V, 1);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000110 WeakVH wvh = n;
111
112 EXPECT_EQ(n, wvh);
113
114 delete I;
115
116 std::ostringstream oss;
117 wvh->print(oss);
Devang Patelf04d63a2009-07-08 21:57:07 +0000118 EXPECT_STREQ("!0 = metadata !{null}\n", oss.str().c_str());
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000119}
Nick Lewycky49f89192009-04-04 07:22:01 +0000120}