[llvm] [cmake] Add possibility to use ChooseMSVCCRT.cmake when include LLVM library
[llvm-core.git] / unittests / Support / raw_pwrite_stream_test.cpp
blob459eb940a43d573a4bf8362a49100db3f44f1b11
1 //===- raw_pwrite_stream_test.cpp - raw_pwrite_stream tests ---------------===//
2 //
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
6 //
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"
16 using namespace llvm;
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()); \
26 } else { \
29 namespace {
31 TEST(raw_pwrite_ostreamTest, TestSVector) {
32 SmallVector<char, 0> Buffer;
33 raw_svector_ostream OS(Buffer);
34 OS << "abcd";
35 StringRef Test = "test";
36 OS.pwrite(Test.data(), Test.size(), 0);
37 EXPECT_EQ(Test, OS.str());
39 #ifdef GTEST_HAS_DEATH_TEST
40 #ifndef NDEBUG
41 EXPECT_DEATH(OS.pwrite("12345", 5, 0),
42 "We don't support extending the stream");
43 #endif
44 #endif
47 #ifdef _WIN32
48 #define setenv(name, var, ignore) _putenv_s(name, var)
49 #endif
51 TEST(raw_pwrite_ostreamTest, TestFD) {
52 SmallString<64> Path;
53 int FD;
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
58 // in the parent.
59 const char *ParentPath = getenv("RAW_PWRITE_TEST_FILE");
60 if (ParentPath) {
61 Path = ParentPath;
62 ASSERT_NO_ERROR(sys::fs::openFileForRead(Path, FD));
63 } else {
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);
70 OS << "abcd";
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
76 #ifndef NDEBUG
77 EXPECT_DEATH(OS.pwrite("12345", 5, 0),
78 "We don't support extending the stream");
79 #endif
80 #endif
83 #ifdef LLVM_ON_UNIX
84 TEST(raw_pwrite_ostreamTest, TestDevNull) {
85 int FD;
86 sys::fs::openFileForWrite("/dev/null", FD, sys::fs::CD_OpenExisting);
87 raw_fd_ostream OS(FD, true);
88 OS << "abcd";
89 StringRef Test = "test";
90 OS.pwrite(Test.data(), Test.size(), 0);
91 OS.pwrite(Test.data(), Test.size(), 0);
93 #endif