1 //===- llvm/unittest/Support/raw_fd_stream_test.cpp - raw_fd_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 //===----------------------------------------------------------------------===// 8 9 #include "llvm/ADT/SmallString.h" 10 #include "llvm/Config/llvm-config.h" 11 #include "llvm/Support/Casting.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" 16 17 using namespace llvm; 18 19 namespace { 20 21 TEST(raw_fd_streamTest, ReadAfterWrite) { 22 SmallString<64> Path; 23 int FD; 24 ASSERT_FALSE(sys::fs::createTemporaryFile("foo", "bar", FD, Path)); 25 FileRemover Cleanup(Path); 26 std::error_code EC; 27 raw_fd_stream OS(Path, EC); 28 EXPECT_TRUE(!EC); 29 30 char Bytes[8]; 31 32 OS.write("01234567", 8); 33 34 OS.seek(3); 35 EXPECT_EQ(OS.read(Bytes, 2), 2); 36 EXPECT_EQ(Bytes[0], '3'); 37 EXPECT_EQ(Bytes[1], '4'); 38 39 OS.seek(4); 40 OS.write("xyz", 3); 41 42 OS.seek(0); 43 EXPECT_EQ(OS.read(Bytes, 8), 8); 44 EXPECT_EQ(Bytes[0], '0'); 45 EXPECT_EQ(Bytes[1], '1'); 46 EXPECT_EQ(Bytes[2], '2'); 47 EXPECT_EQ(Bytes[3], '3'); 48 EXPECT_EQ(Bytes[4], 'x'); 49 EXPECT_EQ(Bytes[5], 'y'); 50 EXPECT_EQ(Bytes[6], 'z'); 51 EXPECT_EQ(Bytes[7], '7'); 52 } 53 54 TEST(raw_fd_streamTest, DynCast) { 55 { 56 std::error_code EC; 57 raw_fd_stream OS("-", EC); 58 EXPECT_TRUE(dyn_cast<raw_fd_stream>(&OS)); 59 } 60 { 61 std::error_code EC; 62 raw_fd_ostream OS("-", EC); 63 EXPECT_FALSE(dyn_cast<raw_fd_stream>(&OS)); 64 } 65 } 66 67 } // namespace 68