diff options
author | Nick Kledzik <kledzik@apple.com> | 2012-08-01 02:29:50 +0000 |
---|---|---|
committer | Nick Kledzik <kledzik@apple.com> | 2012-08-01 02:29:50 +0000 |
commit | adfe2637b839efe041165f27c9ad57e3befb2be0 (patch) | |
tree | 4a2e92d6d1eb2d3c85b8b8669b3c186baaed2b56 /unittests | |
parent | c15ad8517719a525ac3b88b6c49b451160691eaa (diff) | |
download | external_llvm-adfe2637b839efe041165f27c9ad57e3befb2be0.zip external_llvm-adfe2637b839efe041165f27c9ad57e3befb2be0.tar.gz external_llvm-adfe2637b839efe041165f27c9ad57e3befb2be0.tar.bz2 |
Initial commit of new FileOutputBuffer support class.
Since the llvm::sys::fs::map_file_pages() support function it relies on
is not yet implemented on Windows, the unit tests for FileOutputBuffer
are currently conditionalized to run only on unix.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@161099 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
-rw-r--r-- | unittests/Support/CMakeLists.txt | 1 | ||||
-rw-r--r-- | unittests/Support/FileOutputBufferTest.cpp | 137 |
2 files changed, 138 insertions, 0 deletions
diff --git a/unittests/Support/CMakeLists.txt b/unittests/Support/CMakeLists.txt index 8859678..3b9bf84 100644 --- a/unittests/Support/CMakeLists.txt +++ b/unittests/Support/CMakeLists.txt @@ -12,6 +12,7 @@ add_llvm_unittest(SupportTests ConstantRangeTest.cpp DataExtractorTest.cpp EndianTest.cpp + FileOutputBufferTest.cpp IntegersSubsetTest.cpp LeakDetectorTest.cpp ManagedStatic.cpp diff --git a/unittests/Support/FileOutputBufferTest.cpp b/unittests/Support/FileOutputBufferTest.cpp new file mode 100644 index 0000000..edd350a --- /dev/null +++ b/unittests/Support/FileOutputBufferTest.cpp @@ -0,0 +1,137 @@ +//===- llvm/unittest/Support/FileOutputBuffer.cpp - unit tests ------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/ADT/OwningPtr.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/FileOutputBuffer.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/PathV2.h" +#include "llvm/Support/raw_ostream.h" + +#include "gtest/gtest.h" + +using namespace llvm; +using namespace llvm::sys; + +#define ASSERT_NO_ERROR(x) \ + if (error_code ASSERT_NO_ERROR_ec = x) { \ + errs() << #x ": did not return errc::success.\n" \ + << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ + << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ + } else {} + +namespace { + + +// NOTE: Temporarily run this test on unix only. Once the file mapping +// routines are ported to Windows, this conditional can be removed. +#if LLVM_ON_UNIX + + +TEST(FileOutputBuffer, Test) { + // Create unique temporary directory for these tests + SmallString<128> TestDirectory; + { + int fd; + ASSERT_NO_ERROR( + fs::unique_file("FileOutputBuffer-test-%%-%%-%%-%%/dir", fd, + TestDirectory)); + ::close(fd); + TestDirectory = path::parent_path(TestDirectory); + } + + // TEST 1: Verify commit case. + SmallString<128> File1(TestDirectory); + File1.append("/file1"); + { + OwningPtr<FileOutputBuffer> Buffer; + ASSERT_NO_ERROR(FileOutputBuffer::create(File1, 8192, Buffer)); + // Start buffer with special header. + memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20); + // Write to end of buffer to verify it is writable. + memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20); + // Commit buffer. + ASSERT_NO_ERROR(Buffer->commit()); + } + // Verify file exists and starts with special header. + bool MagicMatches = false; + ASSERT_NO_ERROR(fs::has_magic(Twine(File1), Twine("AABBCCDDEEFFGGHHIIJJ"), + MagicMatches)); + EXPECT_TRUE(MagicMatches); + // Verify file is correct size. + uint64_t File1Size; + ASSERT_NO_ERROR(fs::file_size(Twine(File1), File1Size)); + ASSERT_EQ(File1Size, 8192ULL); + + // TEST 2: Verify abort case. + SmallString<128> File2(TestDirectory); + File2.append("/file2"); + { + OwningPtr<FileOutputBuffer> Buffer2; + ASSERT_NO_ERROR(FileOutputBuffer::create(File2, 8192, Buffer2)); + // Fill buffer with special header. + memcpy(Buffer2->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20); + // Do *not* commit buffer. + } + // Verify file does not exist (because buffer not commited). + bool Exists = false; + ASSERT_NO_ERROR(fs::exists(Twine(File2), Exists)); + EXPECT_FALSE(Exists); + + + // TEST 3: Verify sizing down case. + SmallString<128> File3(TestDirectory); + File3.append("/file3"); + { + OwningPtr<FileOutputBuffer> Buffer; + ASSERT_NO_ERROR(FileOutputBuffer::create(File3, 8192000, Buffer)); + // Start buffer with special header. + memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20); + // Write to end of buffer to verify it is writable. + memcpy(Buffer->getBufferEnd() - 20, "AABBCCDDEEFFGGHHIIJJ", 20); + // Commit buffer, but size down to smaller size + ASSERT_NO_ERROR(Buffer->commit(5000)); + } + // Verify file exists and starts with special header. + bool MagicMatches3 = false; + ASSERT_NO_ERROR(fs::has_magic(Twine(File3), Twine("AABBCCDDEEFFGGHHIIJJ"), + MagicMatches3)); + EXPECT_TRUE(MagicMatches3); + // Verify file is correct size. + uint64_t File3Size; + ASSERT_NO_ERROR(fs::file_size(Twine(File3), File3Size)); + ASSERT_EQ(File3Size, 5000ULL); + + + // TEST 4: Verify file can be made executable. + SmallString<128> File4(TestDirectory); + File4.append("/file4"); + { + OwningPtr<FileOutputBuffer> Buffer; + ASSERT_NO_ERROR(FileOutputBuffer::create(File4, 8192, Buffer, + FileOutputBuffer::F_executable)); + // Start buffer with special header. + memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20); + // Commit buffer. + ASSERT_NO_ERROR(Buffer->commit()); + } + // Verify file exists and is executable. + fs::file_status Status; + ASSERT_NO_ERROR(fs::status(Twine(File4), Status)); + bool IsExecutable = (Status.permissions() & fs::owner_exe); + EXPECT_TRUE(IsExecutable); + + // Clean up. + uint32_t RemovedCount; + ASSERT_NO_ERROR(fs::remove_all(TestDirectory.str(), RemovedCount)); +} + +#endif // LLVM_ON_UNIX + +} // anonymous namespace |