Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "gtest/gtest.h" |
| 11 | #include "llvm/Constants.h" |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 12 | #include "llvm/Instructions.h" |
Benjamin Kramer | 28983a4 | 2009-07-28 22:03:24 +0000 | [diff] [blame^] | 13 | #include "llvm/Metadata.h" |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 14 | #include "llvm/Type.h" |
| 15 | #include "llvm/Support/ValueHandle.h" |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 16 | #include <sstream> |
| 17 | |
| 18 | using namespace llvm; |
| 19 | |
| 20 | namespace { |
| 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. |
| 24 | TEST(MDStringTest, CreateDifferent) { |
| 25 | char x[3] = { 'f', 0, 'A' }; |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 26 | MDString *s1 = getGlobalContext().getMDString(StringRef(&x[0], 3)); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 27 | x[2] = 'B'; |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 28 | MDString *s2 = getGlobalContext().getMDString(StringRef(&x[0], 3)); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 29 | 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. |
| 34 | TEST(MDStringTest, CreateSame) { |
| 35 | char x[4] = { 'a', 'b', 'c', 'X' }; |
| 36 | char y[4] = { 'a', 'b', 'c', 'Y' }; |
| 37 | |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 38 | MDString *s1 = getGlobalContext().getMDString(StringRef(&x[0], 3)); |
| 39 | MDString *s2 = getGlobalContext().getMDString(StringRef(&y[0], 3)); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 40 | EXPECT_EQ(s1, s2); |
| 41 | } |
| 42 | |
| 43 | // Test that MDString prints out the string we fed it. |
| 44 | TEST(MDStringTest, PrintingSimple) { |
| 45 | char *str = new char[13]; |
| 46 | strncpy(str, "testing 1 2 3", 13); |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 47 | MDString *s = getGlobalContext().getMDString(StringRef(str, 13)); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 48 | strncpy(str, "aaaaaaaaaaaaa", 13); |
| 49 | delete[] str; |
| 50 | |
| 51 | std::ostringstream oss; |
| 52 | s->print(oss); |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 53 | EXPECT_STREQ("metadata !\"testing 1 2 3\"", oss.str().c_str()); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // Test printing of MDString with non-printable characters. |
| 57 | TEST(MDStringTest, PrintingComplex) { |
| 58 | char str[5] = {0, '\n', '"', '\\', -1}; |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 59 | MDString *s = getGlobalContext().getMDString(StringRef(str+0, 5)); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 60 | std::ostringstream oss; |
| 61 | s->print(oss); |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 62 | EXPECT_STREQ("metadata !\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str()); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // Test the two constructors, and containing other Constants. |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 66 | TEST(MDNodeTest, Simple) { |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 67 | char x[3] = { 'a', 'b', 'c' }; |
| 68 | char y[3] = { '1', '2', '3' }; |
| 69 | |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 70 | MDString *s1 = getGlobalContext().getMDString(StringRef(&x[0], 3)); |
| 71 | MDString *s2 = getGlobalContext().getMDString(StringRef(&y[0], 3)); |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 72 | ConstantInt *CI = ConstantInt::get(getGlobalContext(), APInt(8, 0)); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 73 | |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 74 | std::vector<Value *> V; |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 75 | V.push_back(s1); |
| 76 | V.push_back(CI); |
| 77 | V.push_back(s2); |
| 78 | |
Owen Anderson | b6b2530 | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 79 | MDNode *n1 = getGlobalContext().getMDNode(&V[0], 3); |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 80 | Value *const c1 = n1; |
Owen Anderson | b6b2530 | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 81 | MDNode *n2 = getGlobalContext().getMDNode(&c1, 1); |
| 82 | MDNode *n3 = getGlobalContext().getMDNode(&V[0], 3); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 83 | EXPECT_NE(n1, n2); |
| 84 | EXPECT_EQ(n1, n3); |
| 85 | |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 86 | 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 Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 90 | |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 91 | EXPECT_EQ(1u, n2->getNumElements()); |
| 92 | EXPECT_EQ(n1, n2->getElement(0)); |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 93 | |
| 94 | std::ostringstream oss1, oss2; |
| 95 | n1->print(oss1); |
| 96 | n2->print(oss2); |
Devang Patel | f04d63a | 2009-07-08 21:57:07 +0000 | [diff] [blame] | 97 | EXPECT_STREQ("!0 = metadata !{metadata !\"abc\", i8 0, metadata !\"123\"}\n", |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 98 | oss1.str().c_str()); |
Devang Patel | f04d63a | 2009-07-08 21:57:07 +0000 | [diff] [blame] | 99 | EXPECT_STREQ("!0 = metadata !{metadata !1}\n" |
| 100 | "!1 = metadata !{metadata !\"abc\", i8 0, metadata !\"123\"}\n", |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 101 | oss2.str().c_str()); |
| 102 | } |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 103 | |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 104 | TEST(MDNodeTest, Delete) { |
Owen Anderson | edb4a70 | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 105 | Constant *C = ConstantInt::get(Type::Int32Ty, 1); |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 106 | Instruction *I = new BitCastInst(C, Type::Int32Ty); |
| 107 | |
| 108 | Value *const V = I; |
Owen Anderson | b6b2530 | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 109 | MDNode *n = getGlobalContext().getMDNode(&V, 1); |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 110 | WeakVH wvh = n; |
| 111 | |
| 112 | EXPECT_EQ(n, wvh); |
| 113 | |
| 114 | delete I; |
| 115 | |
| 116 | std::ostringstream oss; |
| 117 | wvh->print(oss); |
Devang Patel | f04d63a | 2009-07-08 21:57:07 +0000 | [diff] [blame] | 118 | EXPECT_STREQ("!0 = metadata !{null}\n", oss.str().c_str()); |
Nick Lewycky | b8f9b7a | 2009-05-10 20:57:05 +0000 | [diff] [blame] | 119 | } |
Nick Lewycky | 49f8919 | 2009-04-04 07:22:01 +0000 | [diff] [blame] | 120 | } |