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"
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.
20 TEST(MDStringTest, CreateDifferent) {
21 char x[3] = { 'f', 0, 'A' };
22 MDString *s1 = MDString::get(&x[0], &x[3]);
24 MDString *s2 = MDString::get(&x[0], &x[3]);
28 // Test that creation of MDStrings with the same string contents produces the
29 // same MDString object, even with different pointers.
30 TEST(MDStringTest, CreateSame) {
31 char x[4] = { 'a', 'b', 'c', 'X' };
32 char y[4] = { 'a', 'b', 'c', 'Y' };
34 MDString *s1 = MDString::get(&x[0], &x[3]);
35 MDString *s2 = MDString::get(&y[0], &y[3]);
39 // Test that MDString prints out the string we fed it.
40 TEST(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);
47 std::ostringstream oss;
49 EXPECT_STREQ("{ } !\"testing 1 2 3\"", oss.str().c_str());
52 // Test printing of MDString with non-printable characters.
53 TEST(MDStringTest, PrintingComplex) {
54 char str[5] = {0, '\n', '"', '\\', -1};
55 MDString *s = MDString::get(str+0, str+5);
56 std::ostringstream oss;
58 EXPECT_STREQ("{ } !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str());
61 // Test the two constructors, and containing other Constants.
62 TEST(MDNodeTest, Everything) {
63 char x[3] = { 'a', 'b', 'c' };
64 char y[3] = { '1', '2', '3' };
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));
70 std::vector<Constant *> V;
75 MDNode *n1 = MDNode::get(&V[0], 3);
76 Constant *const c1 = n1;
77 MDNode *n2 = MDNode::get(&c1, 1);
78 MDNode *n3 = MDNode::get(&V[0], 3);
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));
87 EXPECT_EQ(1u, n2->getNumOperands());
88 EXPECT_EQ(n1, n2->getOperand(0));
90 std::ostringstream oss1, oss2;
93 EXPECT_STREQ("{ } !{{ } !\"abc\", i8 0, { } !\"123\"}", oss1.str().c_str());
94 EXPECT_STREQ("{ } !{{ } !{{ } !\"abc\", i8 0, { } !\"123\"}}",