1 //===- raw_pwrite_stream_test.cpp - raw_pwrite_stream tests ---------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/SmallString.h"
10 #include "llvm/Config/llvm-config.h"
11 #include "llvm/Support/FileSystem.h"
12 #include "llvm/Support/FileUtilities.h"
13 #include "llvm/Support/raw_ostream.h"
14 #include "gtest/gtest.h"
18 #define ASSERT_NO_ERROR(x) \
19 if (std::error_code ASSERT_NO_ERROR_ec = x) { \
20 SmallString<128> MessageStorage; \
21 raw_svector_ostream Message(MessageStorage); \
22 Message << #x ": did not return errc::success.\n" \
23 << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
24 << "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
25 GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
31 TEST(raw_pwrite_ostreamTest
, TestSVector
) {
32 SmallVector
<char, 0> Buffer
;
33 raw_svector_ostream
OS(Buffer
);
35 StringRef Test
= "test";
36 OS
.pwrite(Test
.data(), Test
.size(), 0);
37 EXPECT_EQ(Test
, OS
.str());
39 #ifdef GTEST_HAS_DEATH_TEST
41 EXPECT_DEATH(OS
.pwrite("12345", 5, 0),
42 "We don't support extending the stream");
48 #define setenv(name, var, ignore) _putenv_s(name, var)
51 TEST(raw_pwrite_ostreamTest
, TestFD
) {
55 // If we want to clean up from a death test, we have to remove the file from
56 // the parent process. Have the parent create the file, pass it via
57 // environment variable to the child, let the child crash, and then remove it
59 const char *ParentPath
= getenv("RAW_PWRITE_TEST_FILE");
62 ASSERT_NO_ERROR(sys::fs::openFileForRead(Path
, FD
));
64 ASSERT_NO_ERROR(sys::fs::createTemporaryFile("foo", "bar", FD
, Path
));
65 setenv("RAW_PWRITE_TEST_FILE", Path
.c_str(), true);
67 FileRemover
Cleanup(Path
);
69 raw_fd_ostream
OS(FD
, true);
71 StringRef Test
= "test";
72 OS
.pwrite(Test
.data(), Test
.size(), 0);
73 OS
.pwrite(Test
.data(), Test
.size(), 0);
75 #ifdef GTEST_HAS_DEATH_TEST
77 EXPECT_DEATH(OS
.pwrite("12345", 5, 0),
78 "We don't support extending the stream");
84 TEST(raw_pwrite_ostreamTest
, TestDevNull
) {
86 sys::fs::openFileForWrite("/dev/null", FD
, sys::fs::CD_OpenExisting
);
87 raw_fd_ostream
OS(FD
, true);
89 StringRef Test
= "test";
90 OS
.pwrite(Test
.data(), Test
.size(), 0);
91 OS
.pwrite(Test
.data(), Test
.size(), 0);