aboutsummaryrefslogtreecommitdiffstats
path: root/unittests/Support/MemoryBufferTest.cpp
diff options
context:
space:
mode:
authorStephen Hines <srhines@google.com>2013-08-07 15:07:10 -0700
committerStephen Hines <srhines@google.com>2013-08-07 15:07:10 -0700
commitfab2daa4a1127ecb217abe2b07c1769122b6fee1 (patch)
tree268ebfd1963fd98ba412e76819afdf95a7d4267b /unittests/Support/MemoryBufferTest.cpp
parent8197ac1c1a0a91baa70c4dea8cb488f254ef974c (diff)
parent10251753b6897adcd22cc981c0cc42f348c109de (diff)
downloadexternal_llvm-fab2daa4a1127ecb217abe2b07c1769122b6fee1.zip
external_llvm-fab2daa4a1127ecb217abe2b07c1769122b6fee1.tar.gz
external_llvm-fab2daa4a1127ecb217abe2b07c1769122b6fee1.tar.bz2
Merge commit '10251753b6897adcd22cc981c0cc42f348c109de' into merge-20130807
Conflicts: lib/Archive/ArchiveReader.cpp lib/Support/Unix/PathV2.inc Change-Id: I29d8c1e321a4a380b6013f00bac6a8e4b593cc4e
Diffstat (limited to 'unittests/Support/MemoryBufferTest.cpp')
-rw-r--r--unittests/Support/MemoryBufferTest.cpp55
1 files changed, 53 insertions, 2 deletions
diff --git a/unittests/Support/MemoryBufferTest.cpp b/unittests/Support/MemoryBufferTest.cpp
index 1d9f482..de1dbb7 100644
--- a/unittests/Support/MemoryBufferTest.cpp
+++ b/unittests/Support/MemoryBufferTest.cpp
@@ -11,12 +11,16 @@
//
//===----------------------------------------------------------------------===//
+#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/raw_ostream.h"
#include "llvm/ADT/OwningPtr.h"
#include "gtest/gtest.h"
using namespace llvm;
+namespace {
+
class MemoryBufferTest : public testing::Test {
protected:
MemoryBufferTest()
@@ -25,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));
@@ -95,4 +104,46 @@ TEST_F(MemoryBufferTest, make_new) {
EXPECT_EQ(0, Four->getBufferStart()[0]);
}
+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.
+ int TestFD;
+ 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; 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
+ 8000 // Offset
+ );
+ EXPECT_FALSE(EC);
+
+ StringRef BufData = Buf->getBuffer();
+ EXPECT_EQ(BufData.size(), 40000U);
+ EXPECT_EQ(BufData[0], '0');
+ EXPECT_EQ(BufData[9], '9');
+}
+
+TEST_F(MemoryBufferTest, getOpenFileNoReopen) {
+ testGetOpenFileSlice(false);
+}
+
+TEST_F(MemoryBufferTest, getOpenFileReopened) {
+ testGetOpenFileSlice(true);
+}
+
}