Merging r259177:
[oota-llvm.git] / unittests / Support / FileOutputBufferTest.cpp
index 0801f8539909bfb27d9cbaea1238431b6f6571e1..090c476e35cf53a9a65aa381631f97beba2e1373 100644 (file)
@@ -7,8 +7,9 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/Support/FileOutputBuffer.h"
+#include "llvm/Support/Errc.h"
 #include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/FileOutputBuffer.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/raw_ostream.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 {}
+#define ASSERT_NO_ERROR(x)                                                     \
+  if (std::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 {
 TEST(FileOutputBuffer, Test) {
@@ -37,8 +39,10 @@ TEST(FileOutputBuffer, Test) {
   SmallString<128> File1(TestDirectory);
        File1.append("/file1");
   {
-    std::unique_ptr<FileOutputBuffer> Buffer;
-    ASSERT_NO_ERROR(FileOutputBuffer::create(File1, 8192, Buffer));
+    ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
+        FileOutputBuffer::create(File1, 8192);
+    ASSERT_NO_ERROR(BufferOrErr.getError());
+    std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr;
     // Start buffer with special header.
     memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
     // Write to end of buffer to verify it is writable.
@@ -46,11 +50,7 @@ TEST(FileOutputBuffer, Test) {
     // 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));
@@ -61,49 +61,48 @@ TEST(FileOutputBuffer, Test) {
   SmallString<128> File2(TestDirectory);
        File2.append("/file2");
   {
-    std::unique_ptr<FileOutputBuffer> Buffer2;
-    ASSERT_NO_ERROR(FileOutputBuffer::create(File2, 8192, Buffer2));
+    ErrorOr<std::unique_ptr<FileOutputBuffer>> Buffer2OrErr =
+        FileOutputBuffer::create(File2, 8192);
+    ASSERT_NO_ERROR(Buffer2OrErr.getError());
+    std::unique_ptr<FileOutputBuffer> &Buffer2 = *Buffer2OrErr;
     // Fill buffer with special header.
     memcpy(Buffer2->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
     // Do *not* commit buffer.
   }
   // Verify file does not exist (because buffer not committed).
-  bool Exists = false;
-  ASSERT_NO_ERROR(fs::exists(Twine(File2), Exists));
-  EXPECT_FALSE(Exists);
+  ASSERT_EQ(fs::access(Twine(File2), fs::AccessMode::Exist),
+            errc::no_such_file_or_directory);
   ASSERT_NO_ERROR(fs::remove(File2.str()));
 
   // TEST 3: Verify sizing down case.
   SmallString<128> File3(TestDirectory);
        File3.append("/file3");
   {
-    std::unique_ptr<FileOutputBuffer> Buffer;
-    ASSERT_NO_ERROR(FileOutputBuffer::create(File3, 8192000, Buffer));
+    ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
+        FileOutputBuffer::create(File3, 8192000);
+    ASSERT_NO_ERROR(BufferOrErr.getError());
+    std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr;
     // 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));
+    ASSERT_NO_ERROR(Buffer->commit());
   }
-  // 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);
+  ASSERT_EQ(File3Size, 8192000ULL);
   ASSERT_NO_ERROR(fs::remove(File3.str()));
 
   // TEST 4: Verify file can be made executable.
   SmallString<128> File4(TestDirectory);
        File4.append("/file4");
   {
-    std::unique_ptr<FileOutputBuffer> Buffer;
-    ASSERT_NO_ERROR(FileOutputBuffer::create(File4, 8192, Buffer,
-                                              FileOutputBuffer::F_executable));
+    ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
+        FileOutputBuffer::create(File4, 8192, FileOutputBuffer::F_executable);
+    ASSERT_NO_ERROR(BufferOrErr.getError());
+    std::unique_ptr<FileOutputBuffer> &Buffer = *BufferOrErr;
     // Start buffer with special header.
     memcpy(Buffer->getBufferStart(), "AABBCCDDEEFFGGHHIIJJ", 20);
     // Commit buffer.