[llvm] r186887 - Add a simple unit test for MemoryBuffer::getOpenFile
Eli Bendersky
eliben at google.com
Mon Jul 22 15:46:22 PDT 2013
Author: eliben
Date: Mon Jul 22 17:46:21 2013
New Revision: 186887
URL: http://llvm.org/viewvc/llvm-project?rev=186887&view=rev
Log:
Add a simple unit test for MemoryBuffer::getOpenFile
Modified:
llvm/trunk/unittests/Support/MemoryBufferTest.cpp
Modified: llvm/trunk/unittests/Support/MemoryBufferTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/MemoryBufferTest.cpp?rev=186887&r1=186886&r2=186887&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/MemoryBufferTest.cpp (original)
+++ llvm/trunk/unittests/Support/MemoryBufferTest.cpp Mon Jul 22 17:46:21 2013
@@ -11,7 +11,9 @@
//
//===----------------------------------------------------------------------===//
+#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"
@@ -95,4 +97,35 @@ TEST_F(MemoryBufferTest, make_new) {
EXPECT_EQ(0, Four->getBufferStart()[0]);
}
+TEST_F(MemoryBufferTest, getOpenFileNoNullTerminator) {
+ // 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, and is unbuffered so that
+ // the results are immediately visible through the fd.
+ raw_fd_ostream OF(TestFD, true, true);
+ for (int i = 0; i < 60000; ++i) {
+ OF << "0123456789";
+ }
+
+ OwningBuffer Buf;
+ error_code EC = MemoryBuffer::getOpenFile(TestFD,
+ TestPath.c_str(),
+ Buf,
+ 40000, // Size
+ -1,
+ 8000, // Offset
+ false);
+ EXPECT_FALSE(EC);
+
+ StringRef BufData = Buf->getBuffer();
+ EXPECT_EQ(BufData.size(), 40000U);
+ EXPECT_EQ(BufData[0], '0');
+ EXPECT_EQ(BufData[9], '9');
+}
+
}
More information about the llvm-commits
mailing list