1 //===- raw_pwrite_stream_test.cpp - raw_pwrite_stream tests ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/SmallString.h"
11 #include "llvm/Config/llvm-config.h"
12 #include "llvm/Support/FileSystem.h"
13 #include "llvm/Support/FileUtilities.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include "gtest/gtest.h"
19 #define ASSERT_NO_ERROR(x) \
20 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
21 SmallString<128> MessageStorage; \
22 raw_svector_ostream Message(MessageStorage); \
23 Message << #x ": did not return errc::success.\n" \
24 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
25 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
26 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
32 TEST(raw_pwrite_ostreamTest, TestSVector) {
33 SmallVector<char, 0> Buffer;
34 raw_svector_ostream OS(Buffer);
36 StringRef Test = "test";
37 OS.pwrite(Test.data(), Test.size(), 0);
38 EXPECT_EQ(Test, OS.str());
40 #ifdef GTEST_HAS_DEATH_TEST
42 EXPECT_DEATH(OS.pwrite("12345", 5, 0),
43 "We don't support extending the stream");
49 #define setenv(name, var, ignore) _putenv_s(name, var)
52 TEST(raw_pwrite_ostreamTest, TestFD) {
56 // If we want to clean up from a death test, we have to remove the file from
57 // the parent process. Have the parent create the file, pass it via
58 // environment variable to the child, let the child crash, and then remove it
60 const char *ParentPath = getenv("RAW_PWRITE_TEST_FILE");
63 ASSERT_NO_ERROR(sys::fs::openFileForRead(Path, FD));
65 ASSERT_NO_ERROR(sys::fs::createTemporaryFile("foo", "bar", FD, Path));
66 setenv("RAW_PWRITE_TEST_FILE", Path.c_str(), true);
68 FileRemover Cleanup(Path);
70 raw_fd_ostream OS(FD, true);
72 StringRef Test = "test";
73 OS.pwrite(Test.data(), Test.size(), 0);
74 OS.pwrite(Test.data(), Test.size(), 0);
76 #ifdef GTEST_HAS_DEATH_TEST
78 EXPECT_DEATH(OS.pwrite("12345", 5, 0),
79 "We don't support extending the stream");
85 TEST(raw_pwrite_ostreamTest, TestDevNull) {
87 sys::fs::openFileForWrite("/dev/null", FD, sys::fs::CD_OpenExisting);
88 raw_fd_ostream OS(FD, true);
90 StringRef Test = "test";
91 OS.pwrite(Test.data(), Test.size(), 0);
92 OS.pwrite(Test.data(), Test.size(), 0);