1 //===- llvm/unittest/Support/MemoryBufferTest.cpp - MemoryBuffer 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 // This file implements unit tests for the MemoryBuffer support class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Support/MemoryBuffer.h" 15 #include "llvm/ADT/OwningPtr.h" 16 #include "gtest/gtest.h" 17 18 using namespace llvm; 19 20 class MemoryBufferTest : public testing::Test { 21 protected: 22 MemoryBufferTest() 23 : data("this is some data") 24 { } 25 26 virtual void SetUp() { } 27 28 typedef OwningPtr<MemoryBuffer> OwningBuffer; 29 30 std::string data; 31 }; 32 33 namespace { 34 35 TEST_F(MemoryBufferTest, get) { 36 // Default name and null-terminator flag 37 OwningBuffer MB1(MemoryBuffer::getMemBuffer(data)); 38 EXPECT_TRUE(0 != MB1.get()); 39 40 // RequiresNullTerminator = false 41 OwningBuffer MB2(MemoryBuffer::getMemBuffer(data, "one", false)); 42 EXPECT_TRUE(0 != MB2.get()); 43 44 // RequiresNullTerminator = true 45 OwningBuffer MB3(MemoryBuffer::getMemBuffer(data, "two", true)); 46 EXPECT_TRUE(0 != MB3.get()); 47 48 // verify all 3 buffers point to the same address 49 EXPECT_EQ(MB1->getBufferStart(), MB2->getBufferStart()); 50 EXPECT_EQ(MB2->getBufferStart(), MB3->getBufferStart()); 51 52 // verify the original data is unmodified after deleting the buffers 53 MB1.reset(); 54 MB2.reset(); 55 MB3.reset(); 56 EXPECT_EQ("this is some data", data); 57 } 58 59 TEST_F(MemoryBufferTest, copy) { 60 // copy with no name 61 OwningBuffer MBC1(MemoryBuffer::getMemBufferCopy(data)); 62 EXPECT_TRUE(0 != MBC1.get()); 63 64 // copy with a name 65 OwningBuffer MBC2(MemoryBuffer::getMemBufferCopy(data, "copy")); 66 EXPECT_TRUE(0 != MBC2.get()); 67 68 // verify the two copies do not point to the same place 69 EXPECT_NE(MBC1->getBufferStart(), MBC2->getBufferStart()); 70 } 71 72 TEST_F(MemoryBufferTest, make_new) { 73 // 0-sized buffer 74 OwningBuffer Zero(MemoryBuffer::getNewUninitMemBuffer(0)); 75 EXPECT_TRUE(0 != Zero.get()); 76 77 // uninitialized buffer with no name 78 OwningBuffer One(MemoryBuffer::getNewUninitMemBuffer(321)); 79 EXPECT_TRUE(0 != One.get()); 80 81 // uninitialized buffer with name 82 OwningBuffer Two(MemoryBuffer::getNewUninitMemBuffer(123, "bla")); 83 EXPECT_TRUE(0 != Two.get()); 84 85 // 0-initialized buffer with no name 86 OwningBuffer Three(MemoryBuffer::getNewMemBuffer(321, data)); 87 EXPECT_TRUE(0 != Three.get()); 88 for (size_t i = 0; i < 321; ++i) 89 EXPECT_EQ(0, Three->getBufferStart()[0]); 90 91 // 0-initialized buffer with name 92 OwningBuffer Four(MemoryBuffer::getNewMemBuffer(123, "zeros")); 93 EXPECT_TRUE(0 != Four.get()); 94 for (size_t i = 0; i < 123; ++i) 95 EXPECT_EQ(0, Four->getBufferStart()[0]); 96 } 97 98 } 99