summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/Support/FileOutputBufferTest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/unittests/Support/FileOutputBufferTest.cpp')
-rw-r--r--llvm/unittests/Support/FileOutputBufferTest.cpp50
1 files changed, 50 insertions, 0 deletions
diff --git a/llvm/unittests/Support/FileOutputBufferTest.cpp b/llvm/unittests/Support/FileOutputBufferTest.cpp
index e7f1fd765bd..16fe01dd0b0 100644
--- a/llvm/unittests/Support/FileOutputBufferTest.cpp
+++ b/llvm/unittests/Support/FileOutputBufferTest.cpp
@@ -11,6 +11,7 @@
#include "llvm/Support/Errc.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
@@ -121,4 +122,53 @@ TEST(FileOutputBuffer, Test) {
// Clean up.
ASSERT_NO_ERROR(fs::remove(TestDirectory.str()));
}
+
+TEST(FileOutputBuffer, TestModify) {
+ // Create unique temporary directory for these tests
+ SmallString<128> TestDirectory;
+ {
+ ASSERT_NO_ERROR(
+ fs::createUniqueDirectory("FileOutputBuffer-modify", TestDirectory));
+ }
+
+ SmallString<128> File1(TestDirectory);
+ File1.append("/file");
+ // First write some data.
+ {
+ Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
+ FileOutputBuffer::create(File1, 10);
+ ASSERT_NO_ERROR(errorToErrorCode(BufferOrErr.takeError()));
+ std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr;
+ memcpy(Buffer->getBufferStart(), "AAAAAAAAAA", 10);
+ ASSERT_NO_ERROR(errorToErrorCode(Buffer->commit()));
+ }
+
+ // Then re-open the file for modify and change only some bytes.
+ {
+ Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
+ FileOutputBuffer::create(File1, size_t(-1), FileOutputBuffer::F_modify);
+ ASSERT_NO_ERROR(errorToErrorCode(BufferOrErr.takeError()));
+ std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr;
+ ASSERT_EQ(10, Buffer->getBufferSize());
+ uint8_t *Data = Buffer->getBufferStart();
+ Data[0] = 'X';
+ Data[9] = 'X';
+ ASSERT_NO_ERROR(errorToErrorCode(Buffer->commit()));
+ }
+
+ // Finally, re-open the file for read and verify that it has the modified
+ // contents.
+ {
+ ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = MemoryBuffer::getFile(File1);
+ ASSERT_NO_ERROR(BufferOrErr.getError());
+ std::unique_ptr<MemoryBuffer> Buffer = std::move(*BufferOrErr);
+ ASSERT_EQ(10, Buffer->getBufferSize());
+ EXPECT_EQ(StringRef("XAAAAAAAAX"), Buffer->getBuffer());
+ }
+
+ // Clean up.
+ ASSERT_NO_ERROR(fs::remove(File1));
+ ASSERT_NO_ERROR(fs::remove(TestDirectory));
+}
+
} // anonymous namespace
OpenPOWER on IntegriCloud