2012-10-05 04:29:44 +08:00
|
|
|
//===- llvm/unittest/Support/MemoryBufferTest.cpp - MemoryBuffer tests ----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements unit tests for the MemoryBuffer support class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-07-23 06:46:21 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-10-05 04:29:44 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2013-07-23 06:46:21 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-10-05 04:29:44 +08:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2013-07-24 04:58:51 +08:00
|
|
|
namespace {
|
|
|
|
|
2012-10-05 04:29:44 +08:00
|
|
|
class MemoryBufferTest : public testing::Test {
|
|
|
|
protected:
|
|
|
|
MemoryBufferTest()
|
|
|
|
: data("this is some data")
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual void SetUp() { }
|
|
|
|
|
2013-07-24 04:58:51 +08:00
|
|
|
/// Common testing for different modes of getOpenFileSlice.
|
|
|
|
/// Creates a temporary file with known contents, and uses
|
|
|
|
/// MemoryBuffer::getOpenFileSlice to map it.
|
|
|
|
/// If \p Reopen is true, the file is closed after creating and reopened
|
|
|
|
/// anew before using MemoryBuffer.
|
|
|
|
void testGetOpenFileSlice(bool Reopen);
|
|
|
|
|
2014-05-19 05:01:46 +08:00
|
|
|
typedef std::unique_ptr<MemoryBuffer> OwningBuffer;
|
2012-10-05 04:29:44 +08:00
|
|
|
|
|
|
|
std::string data;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(MemoryBufferTest, get) {
|
|
|
|
// Default name and null-terminator flag
|
|
|
|
OwningBuffer MB1(MemoryBuffer::getMemBuffer(data));
|
2014-06-09 06:29:17 +08:00
|
|
|
EXPECT_TRUE(nullptr != MB1.get());
|
2012-10-05 04:29:44 +08:00
|
|
|
|
|
|
|
// RequiresNullTerminator = false
|
|
|
|
OwningBuffer MB2(MemoryBuffer::getMemBuffer(data, "one", false));
|
2014-06-09 06:29:17 +08:00
|
|
|
EXPECT_TRUE(nullptr != MB2.get());
|
2012-10-05 04:29:44 +08:00
|
|
|
|
|
|
|
// RequiresNullTerminator = true
|
|
|
|
OwningBuffer MB3(MemoryBuffer::getMemBuffer(data, "two", true));
|
2014-06-09 06:29:17 +08:00
|
|
|
EXPECT_TRUE(nullptr != MB3.get());
|
2012-10-05 04:29:44 +08:00
|
|
|
|
|
|
|
// verify all 3 buffers point to the same address
|
|
|
|
EXPECT_EQ(MB1->getBufferStart(), MB2->getBufferStart());
|
|
|
|
EXPECT_EQ(MB2->getBufferStart(), MB3->getBufferStart());
|
|
|
|
|
|
|
|
// verify the original data is unmodified after deleting the buffers
|
|
|
|
MB1.reset();
|
|
|
|
MB2.reset();
|
|
|
|
MB3.reset();
|
|
|
|
EXPECT_EQ("this is some data", data);
|
|
|
|
}
|
|
|
|
|
2013-09-05 02:02:13 +08:00
|
|
|
TEST_F(MemoryBufferTest, NullTerminator4K) {
|
|
|
|
// Test that a file with size that is a multiple of the page size can be null
|
|
|
|
// terminated correctly by MemoryBuffer.
|
|
|
|
int TestFD;
|
|
|
|
SmallString<64> TestPath;
|
|
|
|
sys::fs::createTemporaryFile("MemoryBufferTest_NullTerminator4K", "temp",
|
|
|
|
TestFD, TestPath);
|
|
|
|
raw_fd_ostream OF(TestFD, true, /*unbuffered=*/true);
|
|
|
|
for (unsigned i = 0; i < 4096 / 16; ++i) {
|
|
|
|
OF << "0123456789abcdef";
|
|
|
|
}
|
|
|
|
OF.close();
|
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<OwningBuffer> MB = MemoryBuffer::getFile(TestPath.c_str());
|
|
|
|
std::error_code EC = MB.getError();
|
2013-09-05 02:02:13 +08:00
|
|
|
ASSERT_FALSE(EC);
|
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
const char *BufData = MB.get()->getBufferStart();
|
2013-09-05 02:02:13 +08:00
|
|
|
EXPECT_EQ('f', BufData[4095]);
|
|
|
|
EXPECT_EQ('\0', BufData[4096]);
|
|
|
|
}
|
|
|
|
|
2012-10-05 04:29:44 +08:00
|
|
|
TEST_F(MemoryBufferTest, copy) {
|
|
|
|
// copy with no name
|
|
|
|
OwningBuffer MBC1(MemoryBuffer::getMemBufferCopy(data));
|
2014-06-09 06:29:17 +08:00
|
|
|
EXPECT_TRUE(nullptr != MBC1.get());
|
2012-10-05 04:29:44 +08:00
|
|
|
|
|
|
|
// copy with a name
|
|
|
|
OwningBuffer MBC2(MemoryBuffer::getMemBufferCopy(data, "copy"));
|
2014-06-09 06:29:17 +08:00
|
|
|
EXPECT_TRUE(nullptr != MBC2.get());
|
2012-10-05 04:29:44 +08:00
|
|
|
|
|
|
|
// verify the two copies do not point to the same place
|
|
|
|
EXPECT_NE(MBC1->getBufferStart(), MBC2->getBufferStart());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MemoryBufferTest, make_new) {
|
|
|
|
// 0-sized buffer
|
|
|
|
OwningBuffer Zero(MemoryBuffer::getNewUninitMemBuffer(0));
|
2014-06-09 06:29:17 +08:00
|
|
|
EXPECT_TRUE(nullptr != Zero.get());
|
2012-10-05 04:29:44 +08:00
|
|
|
|
|
|
|
// uninitialized buffer with no name
|
|
|
|
OwningBuffer One(MemoryBuffer::getNewUninitMemBuffer(321));
|
2014-06-09 06:29:17 +08:00
|
|
|
EXPECT_TRUE(nullptr != One.get());
|
2012-10-05 04:29:44 +08:00
|
|
|
|
|
|
|
// uninitialized buffer with name
|
|
|
|
OwningBuffer Two(MemoryBuffer::getNewUninitMemBuffer(123, "bla"));
|
2014-06-09 06:29:17 +08:00
|
|
|
EXPECT_TRUE(nullptr != Two.get());
|
2012-10-05 04:29:44 +08:00
|
|
|
|
|
|
|
// 0-initialized buffer with no name
|
|
|
|
OwningBuffer Three(MemoryBuffer::getNewMemBuffer(321, data));
|
2014-06-09 06:29:17 +08:00
|
|
|
EXPECT_TRUE(nullptr != Three.get());
|
2012-10-05 04:29:44 +08:00
|
|
|
for (size_t i = 0; i < 321; ++i)
|
|
|
|
EXPECT_EQ(0, Three->getBufferStart()[0]);
|
|
|
|
|
|
|
|
// 0-initialized buffer with name
|
|
|
|
OwningBuffer Four(MemoryBuffer::getNewMemBuffer(123, "zeros"));
|
2014-06-09 06:29:17 +08:00
|
|
|
EXPECT_TRUE(nullptr != Four.get());
|
2012-10-05 04:29:44 +08:00
|
|
|
for (size_t i = 0; i < 123; ++i)
|
|
|
|
EXPECT_EQ(0, Four->getBufferStart()[0]);
|
|
|
|
}
|
|
|
|
|
2013-07-24 04:58:51 +08:00
|
|
|
void MemoryBufferTest::testGetOpenFileSlice(bool Reopen) {
|
2013-07-23 06:46:21 +08:00
|
|
|
// Test that MemoryBuffer::getOpenFile works properly when no null
|
|
|
|
// terminator is requested and the size is large enough to trigger
|
|
|
|
// the usage of memory mapping.
|
|
|
|
int TestFD;
|
|
|
|
SmallString<64> TestPath;
|
|
|
|
// Create a temporary file and write data into it.
|
|
|
|
sys::fs::createTemporaryFile("prefix", "temp", TestFD, TestPath);
|
2013-08-22 23:14:45 +08:00
|
|
|
// OF is responsible for closing the file; If the file is not
|
2013-07-24 04:58:51 +08:00
|
|
|
// reopened, it will be unbuffered so that the results are
|
|
|
|
// immediately visible through the fd.
|
|
|
|
raw_fd_ostream OF(TestFD, true, !Reopen);
|
2013-07-23 06:46:21 +08:00
|
|
|
for (int i = 0; i < 60000; ++i) {
|
|
|
|
OF << "0123456789";
|
|
|
|
}
|
|
|
|
|
2013-07-24 04:58:51 +08:00
|
|
|
if (Reopen) {
|
|
|
|
OF.close();
|
|
|
|
EXPECT_FALSE(sys::fs::openFileForRead(TestPath.c_str(), TestFD));
|
|
|
|
}
|
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<OwningBuffer> Buf =
|
|
|
|
MemoryBuffer::getOpenFileSlice(TestFD, TestPath.c_str(),
|
2014-06-13 11:20:08 +08:00
|
|
|
40000, // Size
|
|
|
|
80000 // Offset
|
|
|
|
);
|
2014-07-07 01:43:13 +08:00
|
|
|
|
|
|
|
std::error_code EC = Buf.getError();
|
2013-07-23 06:46:21 +08:00
|
|
|
EXPECT_FALSE(EC);
|
|
|
|
|
2014-07-07 01:43:13 +08:00
|
|
|
StringRef BufData = Buf.get()->getBuffer();
|
2013-07-23 06:46:21 +08:00
|
|
|
EXPECT_EQ(BufData.size(), 40000U);
|
|
|
|
EXPECT_EQ(BufData[0], '0');
|
|
|
|
EXPECT_EQ(BufData[9], '9');
|
|
|
|
}
|
|
|
|
|
2013-07-24 04:58:51 +08:00
|
|
|
TEST_F(MemoryBufferTest, getOpenFileNoReopen) {
|
|
|
|
testGetOpenFileSlice(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MemoryBufferTest, getOpenFileReopened) {
|
|
|
|
testGetOpenFileSlice(true);
|
|
|
|
}
|
|
|
|
|
2012-10-05 04:29:44 +08:00
|
|
|
}
|