aboutsummaryrefslogtreecommitdiffstats
path: root/unittests/Support/MemoryBufferTest.cpp
diff options
context:
space:
mode:
authorEli Bendersky <eliben@google.com>2013-07-23 20:58:51 +0000
committerEli Bendersky <eliben@google.com>2013-07-23 20:58:51 +0000
commitab99d04c59d649ed6d309603b9e77fc1d36cca4d (patch)
tree8282ed217c3556f700f18aac1feaaedc2ae42390 /unittests/Support/MemoryBufferTest.cpp
parentd2ed53c8e24ddb1e30246c918a8eabb678bd9495 (diff)
downloadexternal_llvm-ab99d04c59d649ed6d309603b9e77fc1d36cca4d.zip
external_llvm-ab99d04c59d649ed6d309603b9e77fc1d36cca4d.tar.gz
external_llvm-ab99d04c59d649ed6d309603b9e77fc1d36cca4d.tar.bz2
Refactor the unit test for MemoryBuffer::getOpenFileSlice
Run in two different modes: with and without reopening the temporary file between creating it and mapping it with MemoryBuffer. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@186986 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests/Support/MemoryBufferTest.cpp')
-rw-r--r--unittests/Support/MemoryBufferTest.cpp33
1 files changed, 27 insertions, 6 deletions
diff --git a/unittests/Support/MemoryBufferTest.cpp b/unittests/Support/MemoryBufferTest.cpp
index d5ea8de..de1dbb7 100644
--- a/unittests/Support/MemoryBufferTest.cpp
+++ b/unittests/Support/MemoryBufferTest.cpp
@@ -19,6 +19,8 @@
using namespace llvm;
+namespace {
+
class MemoryBufferTest : public testing::Test {
protected:
MemoryBufferTest()
@@ -27,13 +29,18 @@ protected:
virtual void SetUp() { }
+ /// 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);
+
typedef OwningPtr<MemoryBuffer> OwningBuffer;
std::string data;
};
-namespace {
-
TEST_F(MemoryBufferTest, get) {
// Default name and null-terminator flag
OwningBuffer MB1(MemoryBuffer::getMemBuffer(data));
@@ -97,7 +104,7 @@ TEST_F(MemoryBufferTest, make_new) {
EXPECT_EQ(0, Four->getBufferStart()[0]);
}
-TEST_F(MemoryBufferTest, getOpenFileNoNullTerminator) {
+void MemoryBufferTest::testGetOpenFileSlice(bool Reopen) {
// 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.
@@ -105,13 +112,19 @@ TEST_F(MemoryBufferTest, getOpenFileNoNullTerminator) {
SmallString<64> TestPath;
// Create a temporary file and write data into it.
sys::fs::createTemporaryFile("prefix", "temp", TestFD, TestPath);
- // OF is responsible for closing the file, and is unbuffered so that
- // the results are immediately visible through the fd.
- raw_fd_ostream OF(TestFD, true, true);
+ // OF is responsible for closing the file; If the file is not
+ // reopened, it will be unbuffered so that the results are
+ // immediately visible through the fd.
+ raw_fd_ostream OF(TestFD, true, !Reopen);
for (int i = 0; i < 60000; ++i) {
OF << "0123456789";
}
+ if (Reopen) {
+ OF.close();
+ EXPECT_FALSE(sys::fs::openFileForRead(TestPath.c_str(), TestFD));
+ }
+
OwningBuffer Buf;
error_code EC = MemoryBuffer::getOpenFileSlice(TestFD, TestPath.c_str(), Buf,
40000, // Size
@@ -125,4 +138,12 @@ TEST_F(MemoryBufferTest, getOpenFileNoNullTerminator) {
EXPECT_EQ(BufData[9], '9');
}
+TEST_F(MemoryBufferTest, getOpenFileNoReopen) {
+ testGetOpenFileSlice(false);
+}
+
+TEST_F(MemoryBufferTest, getOpenFileReopened) {
+ testGetOpenFileSlice(true);
+}
+
}