teak-llvm/llvm/unittests/Support/raw_pwrite_stream_test.cpp
Pavel Labath 40837e97b1 raw_ostream: add operator<< overload for std::error_code
Summary:
The main motivation for this is unit tests, which contain a large macro
for pretty-printing std::error_code, and this macro is duplicated in
every file that needs to do this. However, the functionality may be
useful elsewhere too.

In this patch I have reimplemented the existing ASSERT_NO_ERROR macros
to reuse the new functionality, but I have kept the macro (as a
one-liner) as it is slightly more readable than ASSERT_EQ(...,
std::error_code()).

Reviewers: sammccall, ilya-biryukov

Subscribers: zturner, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D65643

llvm-svn: 368849
2019-08-14 13:33:28 +00:00

86 lines
2.4 KiB
C++

//===- raw_pwrite_stream_test.cpp - raw_pwrite_stream tests ---------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/SmallString.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
using namespace llvm;
#define ASSERT_NO_ERROR(x) ASSERT_EQ(x, std::error_code())
namespace {
TEST(raw_pwrite_ostreamTest, TestSVector) {
SmallVector<char, 0> Buffer;
raw_svector_ostream OS(Buffer);
OS << "abcd";
StringRef Test = "test";
OS.pwrite(Test.data(), Test.size(), 0);
EXPECT_EQ(Test, OS.str());
#ifdef GTEST_HAS_DEATH_TEST
#ifndef NDEBUG
EXPECT_DEATH(OS.pwrite("12345", 5, 0),
"We don't support extending the stream");
#endif
#endif
}
#ifdef _WIN32
#define setenv(name, var, ignore) _putenv_s(name, var)
#endif
TEST(raw_pwrite_ostreamTest, TestFD) {
SmallString<64> Path;
int FD;
// If we want to clean up from a death test, we have to remove the file from
// the parent process. Have the parent create the file, pass it via
// environment variable to the child, let the child crash, and then remove it
// in the parent.
const char *ParentPath = getenv("RAW_PWRITE_TEST_FILE");
if (ParentPath) {
Path = ParentPath;
ASSERT_NO_ERROR(sys::fs::openFileForRead(Path, FD));
} else {
ASSERT_NO_ERROR(sys::fs::createTemporaryFile("foo", "bar", FD, Path));
setenv("RAW_PWRITE_TEST_FILE", Path.c_str(), true);
}
FileRemover Cleanup(Path);
raw_fd_ostream OS(FD, true);
OS << "abcd";
StringRef Test = "test";
OS.pwrite(Test.data(), Test.size(), 0);
OS.pwrite(Test.data(), Test.size(), 0);
#ifdef GTEST_HAS_DEATH_TEST
#ifndef NDEBUG
EXPECT_DEATH(OS.pwrite("12345", 5, 0),
"We don't support extending the stream");
#endif
#endif
}
#ifdef LLVM_ON_UNIX
TEST(raw_pwrite_ostreamTest, TestDevNull) {
int FD;
sys::fs::openFileForWrite("/dev/null", FD, sys::fs::CD_OpenExisting);
raw_fd_ostream OS(FD, true);
OS << "abcd";
StringRef Test = "test";
OS.pwrite(Test.data(), Test.size(), 0);
OS.pwrite(Test.data(), Test.size(), 0);
}
#endif
}