From eb11fae6d08f479c0799db45860a98af528fa6e7 Mon Sep 17 00:00:00 2001 From: Dimitry Andric Date: Sat, 28 Jul 2018 10:51:19 +0000 Subject: Vendor import of llvm trunk r338150: https://llvm.org/svn/llvm-project/llvm/trunk@338150 --- unittests/Support/FileOutputBufferTest.cpp | 50 ++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'unittests/Support/FileOutputBufferTest.cpp') diff --git a/unittests/Support/FileOutputBufferTest.cpp b/unittests/Support/FileOutputBufferTest.cpp index e7f1fd765bde..554859587d45 100644 --- a/unittests/Support/FileOutputBufferTest.cpp +++ b/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> BufferOrErr = + FileOutputBuffer::create(File1, 10); + ASSERT_NO_ERROR(errorToErrorCode(BufferOrErr.takeError())); + std::unique_ptr &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> BufferOrErr = + FileOutputBuffer::create(File1, size_t(-1), FileOutputBuffer::F_modify); + ASSERT_NO_ERROR(errorToErrorCode(BufferOrErr.takeError())); + std::unique_ptr &Buffer = *BufferOrErr; + ASSERT_EQ(10U, 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> BufferOrErr = MemoryBuffer::getFile(File1); + ASSERT_NO_ERROR(BufferOrErr.getError()); + std::unique_ptr Buffer = std::move(*BufferOrErr); + ASSERT_EQ(10U, Buffer->getBufferSize()); + EXPECT_EQ(StringRef("XAAAAAAAAX"), Buffer->getBuffer()); + } + + // Clean up. + ASSERT_NO_ERROR(fs::remove(File1)); + ASSERT_NO_ERROR(fs::remove(TestDirectory)); +} + } // anonymous namespace -- cgit v1.2.3