|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 |
|
13 | 13 | #include "llvm/Support/MemoryBuffer.h"
|
| 14 | +#include "llvm/ADT/ScopeExit.h" |
14 | 15 | #include "llvm/Support/FileSystem.h"
|
15 | 16 | #include "llvm/Support/FileUtilities.h"
|
16 | 17 | #include "llvm/Support/raw_ostream.h"
|
|
19 | 20 |
|
20 | 21 | using namespace llvm;
|
21 | 22 |
|
| 23 | +#define ASSERT_NO_ERROR(x) \ |
| 24 | + if (std::error_code ASSERT_NO_ERROR_ec = x) { \ |
| 25 | + SmallString<128> MessageStorage; \ |
| 26 | + raw_svector_ostream Message(MessageStorage); \ |
| 27 | + Message << #x ": did not return errc::success.\n" \ |
| 28 | + << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ |
| 29 | + << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ |
| 30 | + GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ |
| 31 | + } else { \ |
| 32 | + } |
| 33 | + |
| 34 | +#define ASSERT_ERROR(x) \ |
| 35 | + if (!x) { \ |
| 36 | + SmallString<128> MessageStorage; \ |
| 37 | + raw_svector_ostream Message(MessageStorage); \ |
| 38 | + Message << #x ": did not return a failure error code.\n"; \ |
| 39 | + GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ |
| 40 | + } |
| 41 | + |
22 | 42 | namespace {
|
23 | 43 |
|
24 | 44 | class MemoryBufferTest : public testing::Test {
|
@@ -65,6 +85,37 @@ TEST_F(MemoryBufferTest, get) {
|
65 | 85 | EXPECT_EQ("this is some data", data);
|
66 | 86 | }
|
67 | 87 |
|
| 88 | +TEST_F(MemoryBufferTest, getOpenFile) { |
| 89 | + int FD; |
| 90 | + SmallString<64> TestPath; |
| 91 | + ASSERT_EQ(sys::fs::createTemporaryFile("MemoryBufferTest_getOpenFile", "temp", |
| 92 | + FD, TestPath), |
| 93 | + std::error_code()); |
| 94 | + |
| 95 | + FileRemover Cleanup(TestPath); |
| 96 | + raw_fd_ostream OF(FD, /*shouldClose*/ true); |
| 97 | + OF << "12345678"; |
| 98 | + OF.close(); |
| 99 | + |
| 100 | + { |
| 101 | + Expected<sys::fs::file_t> File = sys::fs::openNativeFileForRead(TestPath); |
| 102 | + ASSERT_THAT_EXPECTED(File, Succeeded()); |
| 103 | + auto OnExit = |
| 104 | + make_scope_exit([&] { ASSERT_NO_ERROR(sys::fs::closeFile(*File)); }); |
| 105 | + ErrorOr<OwningBuffer> MB = MemoryBuffer::getOpenFile(*File, TestPath, 6); |
| 106 | + ASSERT_NO_ERROR(MB.getError()); |
| 107 | + EXPECT_EQ("123456", MB.get()->getBuffer()); |
| 108 | + } |
| 109 | + { |
| 110 | + Expected<sys::fs::file_t> File = sys::fs::openNativeFileForWrite( |
| 111 | + TestPath, sys::fs::CD_OpenExisting, sys::fs::OF_None); |
| 112 | + ASSERT_THAT_EXPECTED(File, Succeeded()); |
| 113 | + auto OnExit = |
| 114 | + make_scope_exit([&] { ASSERT_NO_ERROR(sys::fs::closeFile(*File)); }); |
| 115 | + ASSERT_ERROR(MemoryBuffer::getOpenFile(*File, TestPath, 6).getError()); |
| 116 | + } |
| 117 | +} |
| 118 | + |
68 | 119 | TEST_F(MemoryBufferTest, NullTerminator4K) {
|
69 | 120 | // Test that a file with size that is a multiple of the page size can be null
|
70 | 121 | // terminated correctly by MemoryBuffer.
|
|
0 commit comments