blob: 6cf8bc7c2792d43decec27adb15b9f95c90ae67f [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"
12#include <sstream>
13
14using namespace llvm;
15
16namespace {
17
18// Test that construction of MDString with different value produces different
19// MDString objects, even with the same string pointer and nulls in the string.
20TEST(MDStringTest, CreateDifferent) {
21 char x[3] = { 'f', 0, 'A' };
22 MDString *s1 = MDString::get(&x[0], &x[3]);
23 x[2] = 'B';
24 MDString *s2 = MDString::get(&x[0], &x[3]);
25 EXPECT_NE(s1, s2);
26}
27
28// Test that creation of MDStrings with the same string contents produces the
29// same MDString object, even with different pointers.
30TEST(MDStringTest, CreateSame) {
31 char x[4] = { 'a', 'b', 'c', 'X' };
32 char y[4] = { 'a', 'b', 'c', 'Y' };
33
34 MDString *s1 = MDString::get(&x[0], &x[3]);
35 MDString *s2 = MDString::get(&y[0], &y[3]);
36 EXPECT_EQ(s1, s2);
37}
38
39// Test that MDString prints out the string we fed it.
40TEST(MDStringTest, PrintingSimple) {
41 char *str = new char[13];
42 strncpy(str, "testing 1 2 3", 13);
43 MDString *s = MDString::get(str, str+13);
44 strncpy(str, "aaaaaaaaaaaaa", 13);
45 delete[] str;
46
47 std::ostringstream oss;
48 s->print(oss);
49 EXPECT_STREQ("{ } !\"testing 1 2 3\"", oss.str().c_str());
50}
51
52// Test printing of MDString with non-printable characters.
53TEST(MDStringTest, PrintingComplex) {
54 char str[5] = {0, '\n', '"', '\\', -1};
55 MDString *s = MDString::get(str+0, str+5);
56 std::ostringstream oss;
57 s->print(oss);
58 EXPECT_STREQ("{ } !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
59}
60
61// Test the two constructors, and containing other Constants.
62TEST(MDNodeTest, Everything) {
63 char x[3] = { 'a', 'b', 'c' };
64 char y[3] = { '1', '2', '3' };
65
66 MDString *s1 = MDString::get(&x[0], &x[3]);
67 MDString *s2 = MDString::get(&y[0], &y[3]);
68 ConstantInt *CI = ConstantInt::get(APInt(8, 0));
69
70 std::vector<Constant *> V;
71 V.push_back(s1);
72 V.push_back(CI);
73 V.push_back(s2);
74
75 MDNode *n1 = MDNode::get(&V[0], 3);
Nick Lewycky8b4db2d2009-04-09 03:10:03 +000076 Constant *const c1 = n1;
77 MDNode *n2 = MDNode::get(&c1, 1);
Nick Lewycky49f89192009-04-04 07:22:01 +000078 MDNode *n3 = MDNode::get(&V[0], 3);
79 EXPECT_NE(n1, n2);
80 EXPECT_EQ(n1, n3);
81
82 EXPECT_EQ(3u, n1->getNumOperands());
83 EXPECT_EQ(s1, n1->getOperand(0));
84 EXPECT_EQ(CI, n1->getOperand(1));
85 EXPECT_EQ(s2, n1->getOperand(2));
86
87 EXPECT_EQ(1u, n2->getNumOperands());
88 EXPECT_EQ(n1, n2->getOperand(0));
89
90 std::ostringstream oss1, oss2;
91 n1->print(oss1);
92 n2->print(oss2);
93 EXPECT_STREQ("{ } !{{ } !\"abc\", i8 0, { } !\"123\"}", oss1.str().c_str());
94 EXPECT_STREQ("{ } !{{ } !{{ } !\"abc\", i8 0, { } !\"123\"}}",
95 oss2.str().c_str());
96}
97}