1 //===- llvm/unittest/Support/raw_ostream_test.cpp - raw_ostream 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/Support/FileSystem.h" 11 #include "llvm/Support/FileUtilities.h" 12 #include "llvm/Support/Format.h" 13 #include "llvm/Support/MemoryBuffer.h" 14 #include "llvm/Support/raw_ostream.h" 15 #include "llvm/Testing/Support/Error.h" 16 #include "gtest/gtest.h" 17 18 using namespace llvm; 19 20 namespace { 21 22 template<typename T> std::string printToString(const T &Value) { 23 std::string res; 24 llvm::raw_string_ostream OS(res); 25 OS.SetBuffered(); 26 OS << Value; 27 OS.flush(); 28 return res; 29 } 30 31 /// printToString - Print the given value to a stream which only has \arg 32 /// BytesLeftInBuffer bytes left in the buffer. This is useful for testing edge 33 /// cases in the buffer handling logic. 34 template<typename T> std::string printToString(const T &Value, 35 unsigned BytesLeftInBuffer) { 36 // FIXME: This is relying on internal knowledge of how raw_ostream works to 37 // get the buffer position right. 38 SmallString<256> SVec; 39 assert(BytesLeftInBuffer < 256 && "Invalid buffer count!"); 40 llvm::raw_svector_ostream OS(SVec); 41 unsigned StartIndex = 256 - BytesLeftInBuffer; 42 for (unsigned i = 0; i != StartIndex; ++i) 43 OS << '?'; 44 OS << Value; 45 return std::string(OS.str().substr(StartIndex)); 46 } 47 48 template<typename T> std::string printToStringUnbuffered(const T &Value) { 49 std::string res; 50 llvm::raw_string_ostream OS(res); 51 OS.SetUnbuffered(); 52 OS << Value; 53 return res; 54 } 55 56 struct X {}; 57 58 raw_ostream &operator<<(raw_ostream &OS, const X &) { return OS << 'X'; } 59 60 TEST(raw_ostreamTest, Types_Buffered) { 61 // Char 62 EXPECT_EQ("c", printToString('c')); 63 64 // String 65 EXPECT_EQ("hello", printToString("hello")); 66 EXPECT_EQ("hello", printToString(std::string("hello"))); 67 68 // Int 69 EXPECT_EQ("0", printToString(0)); 70 EXPECT_EQ("2425", printToString(2425)); 71 EXPECT_EQ("-2425", printToString(-2425)); 72 73 // Long long 74 EXPECT_EQ("0", printToString(0LL)); 75 EXPECT_EQ("257257257235709", printToString(257257257235709LL)); 76 EXPECT_EQ("-257257257235709", printToString(-257257257235709LL)); 77 78 // Double 79 EXPECT_EQ("1.100000e+00", printToString(1.1)); 80 81 // void* 82 EXPECT_EQ("0x0", printToString((void*) nullptr)); 83 EXPECT_EQ("0xbeef", printToString((void*) 0xbeefLL)); 84 EXPECT_EQ("0xdeadbeef", printToString((void*) 0xdeadbeefLL)); 85 86 // Min and max. 87 EXPECT_EQ("18446744073709551615", printToString(UINT64_MAX)); 88 EXPECT_EQ("-9223372036854775808", printToString(INT64_MIN)); 89 90 // X, checking free operator<<(). 91 EXPECT_EQ("X", printToString(X{})); 92 } 93 94 TEST(raw_ostreamTest, Types_Unbuffered) { 95 // Char 96 EXPECT_EQ("c", printToStringUnbuffered('c')); 97 98 // String 99 EXPECT_EQ("hello", printToStringUnbuffered("hello")); 100 EXPECT_EQ("hello", printToStringUnbuffered(std::string("hello"))); 101 102 // Int 103 EXPECT_EQ("0", printToStringUnbuffered(0)); 104 EXPECT_EQ("2425", printToStringUnbuffered(2425)); 105 EXPECT_EQ("-2425", printToStringUnbuffered(-2425)); 106 107 // Long long 108 EXPECT_EQ("0", printToStringUnbuffered(0LL)); 109 EXPECT_EQ("257257257235709", printToStringUnbuffered(257257257235709LL)); 110 EXPECT_EQ("-257257257235709", printToStringUnbuffered(-257257257235709LL)); 111 112 // Double 113 EXPECT_EQ("1.100000e+00", printToStringUnbuffered(1.1)); 114 115 // void* 116 EXPECT_EQ("0x0", printToStringUnbuffered((void*) nullptr)); 117 EXPECT_EQ("0xbeef", printToStringUnbuffered((void*) 0xbeefLL)); 118 EXPECT_EQ("0xdeadbeef", printToStringUnbuffered((void*) 0xdeadbeefLL)); 119 120 // Min and max. 121 EXPECT_EQ("18446744073709551615", printToStringUnbuffered(UINT64_MAX)); 122 EXPECT_EQ("-9223372036854775808", printToStringUnbuffered(INT64_MIN)); 123 124 // X, checking free operator<<(). 125 EXPECT_EQ("X", printToString(X{})); 126 } 127 128 TEST(raw_ostreamTest, BufferEdge) { 129 EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 1)); 130 EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 2)); 131 EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 3)); 132 EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 4)); 133 EXPECT_EQ("1.20", printToString(format("%.2f", 1.2), 10)); 134 } 135 136 TEST(raw_ostreamTest, TinyBuffer) { 137 std::string Str; 138 raw_string_ostream OS(Str); 139 OS.SetBufferSize(1); 140 OS << "hello"; 141 OS << 1; 142 OS << 'w' << 'o' << 'r' << 'l' << 'd'; 143 OS.flush(); 144 EXPECT_EQ("hello1world", Str); 145 } 146 147 TEST(raw_ostreamTest, WriteEscaped) { 148 std::string Str; 149 150 Str = ""; 151 raw_string_ostream(Str).write_escaped("hi"); 152 EXPECT_EQ("hi", Str); 153 154 Str = ""; 155 raw_string_ostream(Str).write_escaped("\\\t\n\""); 156 EXPECT_EQ("\\\\\\t\\n\\\"", Str); 157 158 Str = ""; 159 raw_string_ostream(Str).write_escaped("\1\10\200"); 160 EXPECT_EQ("\\001\\010\\200", Str); 161 } 162 163 TEST(raw_ostreamTest, Justify) { 164 EXPECT_EQ("xyz ", printToString(left_justify("xyz", 6), 6)); 165 EXPECT_EQ("abc", printToString(left_justify("abc", 3), 3)); 166 EXPECT_EQ("big", printToString(left_justify("big", 1), 3)); 167 EXPECT_EQ(" xyz", printToString(right_justify("xyz", 6), 6)); 168 EXPECT_EQ("abc", printToString(right_justify("abc", 3), 3)); 169 EXPECT_EQ("big", printToString(right_justify("big", 1), 3)); 170 EXPECT_EQ(" on ", printToString(center_justify("on", 9), 9)); 171 EXPECT_EQ(" off ", printToString(center_justify("off", 10), 10)); 172 EXPECT_EQ("single ", printToString(center_justify("single", 7), 7)); 173 EXPECT_EQ("none", printToString(center_justify("none", 1), 4)); 174 EXPECT_EQ("none", printToString(center_justify("none", 1), 1)); 175 } 176 177 TEST(raw_ostreamTest, FormatHex) { 178 EXPECT_EQ("0x1234", printToString(format_hex(0x1234, 6), 6)); 179 EXPECT_EQ("0x001234", printToString(format_hex(0x1234, 8), 8)); 180 EXPECT_EQ("0x00001234", printToString(format_hex(0x1234, 10), 10)); 181 EXPECT_EQ("0x1234", printToString(format_hex(0x1234, 4), 6)); 182 EXPECT_EQ("0xff", printToString(format_hex(255, 4), 4)); 183 EXPECT_EQ("0xFF", printToString(format_hex(255, 4, true), 4)); 184 EXPECT_EQ("0x1", printToString(format_hex(1, 3), 3)); 185 EXPECT_EQ("0x12", printToString(format_hex(0x12, 3), 4)); 186 EXPECT_EQ("0x123", printToString(format_hex(0x123, 3), 5)); 187 EXPECT_EQ("FF", printToString(format_hex_no_prefix(0xFF, 2, true), 4)); 188 EXPECT_EQ("ABCD", printToString(format_hex_no_prefix(0xABCD, 2, true), 4)); 189 EXPECT_EQ("0xffffffffffffffff", 190 printToString(format_hex(UINT64_MAX, 18), 18)); 191 EXPECT_EQ("0x8000000000000000", 192 printToString(format_hex((INT64_MIN), 18), 18)); 193 } 194 195 TEST(raw_ostreamTest, FormatDecimal) { 196 EXPECT_EQ(" 0", printToString(format_decimal(0, 4), 4)); 197 EXPECT_EQ(" -1", printToString(format_decimal(-1, 4), 4)); 198 EXPECT_EQ(" -1", printToString(format_decimal(-1, 6), 6)); 199 EXPECT_EQ("1234567890", printToString(format_decimal(1234567890, 10), 10)); 200 EXPECT_EQ(" 9223372036854775807", 201 printToString(format_decimal(INT64_MAX, 21), 21)); 202 EXPECT_EQ(" -9223372036854775808", 203 printToString(format_decimal(INT64_MIN, 21), 21)); 204 } 205 206 static std::string formatted_bytes_str(ArrayRef<uint8_t> Bytes, 207 llvm::Optional<uint64_t> Offset = None, 208 uint32_t NumPerLine = 16, 209 uint8_t ByteGroupSize = 4) { 210 std::string S; 211 raw_string_ostream Str(S); 212 Str << format_bytes(Bytes, Offset, NumPerLine, ByteGroupSize); 213 Str.flush(); 214 return S; 215 } 216 217 static std::string format_bytes_with_ascii_str(ArrayRef<uint8_t> Bytes, 218 Optional<uint64_t> Offset = None, 219 uint32_t NumPerLine = 16, 220 uint8_t ByteGroupSize = 4) { 221 std::string S; 222 raw_string_ostream Str(S); 223 Str << format_bytes_with_ascii(Bytes, Offset, NumPerLine, ByteGroupSize); 224 Str.flush(); 225 return S; 226 } 227 228 TEST(raw_ostreamTest, FormattedHexBytes) { 229 std::vector<uint8_t> Buf = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 230 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 231 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', 232 '1', '2', '3', '4', '5', '6', '7', '8', '9'}; 233 ArrayRef<uint8_t> B(Buf); 234 235 // Test invalid input. 236 EXPECT_EQ("", formatted_bytes_str(ArrayRef<uint8_t>())); 237 EXPECT_EQ("", format_bytes_with_ascii_str(ArrayRef<uint8_t>())); 238 //---------------------------------------------------------------------- 239 // Test hex byte output with the default 4 byte groups 240 //---------------------------------------------------------------------- 241 EXPECT_EQ("61", formatted_bytes_str(B.take_front())); 242 EXPECT_EQ("61626364 65", formatted_bytes_str(B.take_front(5))); 243 // Test that 16 bytes get written to a line correctly. 244 EXPECT_EQ("61626364 65666768 696a6b6c 6d6e6f70", 245 formatted_bytes_str(B.take_front(16))); 246 // Test raw bytes with default 16 bytes per line wrapping. 247 EXPECT_EQ("61626364 65666768 696a6b6c 6d6e6f70\n71", 248 formatted_bytes_str(B.take_front(17))); 249 // Test raw bytes with 1 bytes per line wrapping. 250 EXPECT_EQ("61\n62\n63\n64\n65\n66", 251 formatted_bytes_str(B.take_front(6), None, 1)); 252 // Test raw bytes with 7 bytes per line wrapping. 253 EXPECT_EQ("61626364 656667\n68696a6b 6c6d6e\n6f7071", 254 formatted_bytes_str(B.take_front(17), None, 7)); 255 // Test raw bytes with 8 bytes per line wrapping. 256 EXPECT_EQ("61626364 65666768\n696a6b6c 6d6e6f70\n71", 257 formatted_bytes_str(B.take_front(17), None, 8)); 258 //---------------------------------------------------------------------- 259 // Test hex byte output with the 1 byte groups 260 //---------------------------------------------------------------------- 261 EXPECT_EQ("61 62 63 64 65", 262 formatted_bytes_str(B.take_front(5), None, 16, 1)); 263 // Test that 16 bytes get written to a line correctly. 264 EXPECT_EQ("61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70", 265 formatted_bytes_str(B.take_front(16), None, 16, 1)); 266 // Test raw bytes with default 16 bytes per line wrapping. 267 EXPECT_EQ("61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70\n71", 268 formatted_bytes_str(B.take_front(17), None, 16, 1)); 269 // Test raw bytes with 7 bytes per line wrapping. 270 EXPECT_EQ("61 62 63 64 65 66 67\n68 69 6a 6b 6c 6d 6e\n6f 70 71", 271 formatted_bytes_str(B.take_front(17), None, 7, 1)); 272 // Test raw bytes with 8 bytes per line wrapping. 273 EXPECT_EQ("61 62 63 64 65 66 67 68\n69 6a 6b 6c 6d 6e 6f 70\n71", 274 formatted_bytes_str(B.take_front(17), None, 8, 1)); 275 276 //---------------------------------------------------------------------- 277 // Test hex byte output with the 2 byte groups 278 //---------------------------------------------------------------------- 279 EXPECT_EQ("6162 6364 65", formatted_bytes_str(B.take_front(5), None, 16, 2)); 280 // Test that 16 bytes get written to a line correctly. 281 EXPECT_EQ("6162 6364 6566 6768 696a 6b6c 6d6e 6f70", 282 formatted_bytes_str(B.take_front(16), None, 16, 2)); 283 // Test raw bytes with default 16 bytes per line wrapping. 284 EXPECT_EQ("6162 6364 6566 6768 696a 6b6c 6d6e 6f70\n71", 285 formatted_bytes_str(B.take_front(17), None, 16, 2)); 286 // Test raw bytes with 7 bytes per line wrapping. 287 EXPECT_EQ("6162 6364 6566 67\n6869 6a6b 6c6d 6e\n6f70 71", 288 formatted_bytes_str(B.take_front(17), None, 7, 2)); 289 // Test raw bytes with 8 bytes per line wrapping. 290 EXPECT_EQ("6162 6364 6566 6768\n696a 6b6c 6d6e 6f70\n71", 291 formatted_bytes_str(B.take_front(17), None, 8, 2)); 292 293 //---------------------------------------------------------------------- 294 // Test hex bytes with offset with the default 4 byte groups. 295 //---------------------------------------------------------------------- 296 EXPECT_EQ("0000: 61", formatted_bytes_str(B.take_front(), 0x0)); 297 EXPECT_EQ("1000: 61", formatted_bytes_str(B.take_front(), 0x1000)); 298 EXPECT_EQ("1000: 61\n1001: 62", 299 formatted_bytes_str(B.take_front(2), 0x1000, 1)); 300 //---------------------------------------------------------------------- 301 // Test hex bytes with ASCII with the default 4 byte groups. 302 //---------------------------------------------------------------------- 303 EXPECT_EQ("61626364 65666768 696a6b6c 6d6e6f70 |abcdefghijklmnop|", 304 format_bytes_with_ascii_str(B.take_front(16))); 305 EXPECT_EQ("61626364 65666768 |abcdefgh|\n" 306 "696a6b6c 6d6e6f70 |ijklmnop|", 307 format_bytes_with_ascii_str(B.take_front(16), None, 8)); 308 EXPECT_EQ("61626364 65666768 |abcdefgh|\n696a6b6c |ijkl|", 309 format_bytes_with_ascii_str(B.take_front(12), None, 8)); 310 std::vector<uint8_t> Unprintable = {'a', '\x1e', 'b', '\x1f'}; 311 // Make sure the ASCII is still lined up correctly when fewer bytes than 16 312 // bytes per line are available. The ASCII should still be aligned as if 16 313 // bytes of hex might be displayed. 314 EXPECT_EQ("611e621f |a.b.|", 315 format_bytes_with_ascii_str(Unprintable)); 316 //---------------------------------------------------------------------- 317 // Test hex bytes with ASCII with offsets with the default 4 byte groups. 318 //---------------------------------------------------------------------- 319 EXPECT_EQ("0000: 61626364 65666768 " 320 "696a6b6c 6d6e6f70 |abcdefghijklmnop|", 321 format_bytes_with_ascii_str(B.take_front(16), 0)); 322 EXPECT_EQ("0000: 61626364 65666768 |abcdefgh|\n" 323 "0008: 696a6b6c 6d6e6f70 |ijklmnop|", 324 format_bytes_with_ascii_str(B.take_front(16), 0, 8)); 325 EXPECT_EQ("0000: 61626364 656667 |abcdefg|\n" 326 "0007: 68696a6b 6c |hijkl|", 327 format_bytes_with_ascii_str(B.take_front(12), 0, 7)); 328 329 //---------------------------------------------------------------------- 330 // Test hex bytes with ASCII with offsets with the default 2 byte groups. 331 //---------------------------------------------------------------------- 332 EXPECT_EQ("0000: 6162 6364 6566 6768 " 333 "696a 6b6c 6d6e 6f70 |abcdefghijklmnop|", 334 format_bytes_with_ascii_str(B.take_front(16), 0, 16, 2)); 335 EXPECT_EQ("0000: 6162 6364 6566 6768 |abcdefgh|\n" 336 "0008: 696a 6b6c 6d6e 6f70 |ijklmnop|", 337 format_bytes_with_ascii_str(B.take_front(16), 0, 8, 2)); 338 EXPECT_EQ("0000: 6162 6364 6566 67 |abcdefg|\n" 339 "0007: 6869 6a6b 6c |hijkl|", 340 format_bytes_with_ascii_str(B.take_front(12), 0, 7, 2)); 341 342 //---------------------------------------------------------------------- 343 // Test hex bytes with ASCII with offsets with the default 1 byte groups. 344 //---------------------------------------------------------------------- 345 EXPECT_EQ("0000: 61 62 63 64 65 66 67 68 " 346 "69 6a 6b 6c 6d 6e 6f 70 |abcdefghijklmnop|", 347 format_bytes_with_ascii_str(B.take_front(16), 0, 16, 1)); 348 EXPECT_EQ("0000: 61 62 63 64 65 66 67 68 |abcdefgh|\n" 349 "0008: 69 6a 6b 6c 6d 6e 6f 70 |ijklmnop|", 350 format_bytes_with_ascii_str(B.take_front(16), 0, 8, 1)); 351 EXPECT_EQ("0000: 61 62 63 64 65 66 67 |abcdefg|\n" 352 "0007: 68 69 6a 6b 6c |hijkl|", 353 format_bytes_with_ascii_str(B.take_front(12), 0, 7, 1)); 354 } 355 356 #ifdef LLVM_ON_UNIX 357 TEST(raw_ostreamTest, Colors) { 358 { 359 std::string S; 360 raw_string_ostream Sos(S); 361 Sos.enable_colors(false); 362 Sos.changeColor(raw_ostream::YELLOW); 363 EXPECT_EQ("", Sos.str()); 364 } 365 366 { 367 std::string S; 368 raw_string_ostream Sos(S); 369 Sos.enable_colors(true); 370 Sos.changeColor(raw_ostream::YELLOW); 371 EXPECT_EQ("\x1B[0;33m", Sos.str()); 372 } 373 } 374 #endif 375 376 TEST(raw_fd_ostreamTest, multiple_raw_fd_ostream_to_stdout) { 377 std::error_code EC; 378 379 { raw_fd_ostream("-", EC, sys::fs::OpenFlags::OF_None); } 380 { raw_fd_ostream("-", EC, sys::fs::OpenFlags::OF_None); } 381 } 382 383 TEST(raw_ostreamTest, flush_tied_to_stream_on_write) { 384 std::string TiedToBuffer; 385 raw_string_ostream TiedTo(TiedToBuffer); 386 TiedTo.SetBuffered(); 387 TiedTo << "a"; 388 389 std::string Buffer; 390 raw_string_ostream TiedStream(Buffer); 391 TiedStream.tie(&TiedTo); 392 // Sanity check that the stream hasn't already been flushed. 393 EXPECT_EQ("", TiedToBuffer); 394 395 // Empty string doesn't cause a flush of TiedTo. 396 TiedStream << ""; 397 EXPECT_EQ("", TiedToBuffer); 398 399 // Non-empty strings trigger flush of TiedTo. 400 TiedStream << "abc"; 401 EXPECT_EQ("a", TiedToBuffer); 402 403 // Single char write flushes TiedTo. 404 TiedTo << "c"; 405 TiedStream << 'd'; 406 EXPECT_EQ("ac", TiedToBuffer); 407 408 // Write to buffered stream without flush does not flush TiedTo. 409 TiedStream.SetBuffered(); 410 TiedStream.SetBufferSize(2); 411 TiedTo << "e"; 412 TiedStream << "f"; 413 EXPECT_EQ("ac", TiedToBuffer); 414 415 // Explicit flush of buffered stream flushes TiedTo. 416 TiedStream.flush(); 417 EXPECT_EQ("ace", TiedToBuffer); 418 419 // Explicit flush of buffered stream with empty buffer does not flush TiedTo. 420 TiedTo << "g"; 421 TiedStream.flush(); 422 EXPECT_EQ("ace", TiedToBuffer); 423 424 // Write of data to empty buffer that is greater than buffer size flushes 425 // TiedTo. 426 TiedStream << "hijklm"; 427 EXPECT_EQ("aceg", TiedToBuffer); 428 429 // Write of data that overflows buffer size also flushes TiedTo. 430 TiedStream.flush(); 431 TiedStream << "n"; 432 TiedTo << "o"; 433 TiedStream << "pq"; 434 EXPECT_EQ("acego", TiedToBuffer); 435 436 // Streams can be tied to each other safely. 437 TiedStream.flush(); 438 Buffer = ""; 439 TiedTo.tie(&TiedStream); 440 TiedTo.SetBufferSize(2); 441 TiedStream << "r"; 442 TiedTo << "s"; 443 EXPECT_EQ("", Buffer); 444 EXPECT_EQ("acego", TiedToBuffer); 445 TiedTo << "tuv"; 446 EXPECT_EQ("r", Buffer); 447 TiedStream << "wxy"; 448 EXPECT_EQ("acegostuv", TiedToBuffer); 449 // The x remains in the buffer, since it was written after the flush of 450 // TiedTo. 451 EXPECT_EQ("rwx", Buffer); 452 TiedTo.tie(nullptr); 453 454 // Calling tie with nullptr unties stream. 455 TiedStream.SetUnbuffered(); 456 TiedStream.tie(nullptr); 457 TiedTo << "y"; 458 TiedStream << "0"; 459 EXPECT_EQ("acegostuv", TiedToBuffer); 460 461 TiedTo.flush(); 462 TiedStream.flush(); 463 } 464 465 TEST(raw_ostreamTest, reserve_stream) { 466 std::string Str; 467 raw_string_ostream OS(Str); 468 OS << "11111111111111111111"; 469 uint64_t CurrentPos = OS.tell(); 470 OS.reserveExtraSpace(1000); 471 EXPECT_TRUE(Str.capacity() >= CurrentPos + 1000); 472 OS << "hello"; 473 OS << 1; 474 OS << 'w' << 'o' << 'r' << 'l' << 'd'; 475 OS.flush(); 476 EXPECT_EQ("11111111111111111111hello1world", Str); 477 } 478 479 static void checkFileData(StringRef FileName, StringRef GoldenData) { 480 ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr = 481 MemoryBuffer::getFileOrSTDIN(FileName); 482 EXPECT_FALSE(BufOrErr.getError()); 483 484 EXPECT_EQ((*BufOrErr)->getBufferSize(), GoldenData.size()); 485 EXPECT_EQ(memcmp((*BufOrErr)->getBufferStart(), GoldenData.data(), 486 GoldenData.size()), 487 0); 488 } 489 490 TEST(raw_ostreamTest, writeToOutputFile) { 491 SmallString<64> Path; 492 int FD; 493 ASSERT_FALSE(sys::fs::createTemporaryFile("foo", "bar", FD, Path)); 494 FileRemover Cleanup(Path); 495 496 ASSERT_THAT_ERROR(writeToOutput(Path, 497 [](raw_ostream &Out) -> Error { 498 Out << "HelloWorld"; 499 return Error::success(); 500 }), 501 Succeeded()); 502 checkFileData(Path, "HelloWorld"); 503 } 504 505 TEST(raw_ostreamTest, writeToNonexistingPath) { 506 StringRef FileName = "/_bad/_path"; 507 std::string ErrorMessage = toString(createFileError( 508 FileName, make_error_code(errc::no_such_file_or_directory))); 509 510 EXPECT_THAT_ERROR(writeToOutput(FileName, 511 [](raw_ostream &Out) -> Error { 512 Out << "HelloWorld"; 513 return Error::success(); 514 }), 515 FailedWithMessage(ErrorMessage)); 516 } 517 518 TEST(raw_ostreamTest, writeToDevNull) { 519 bool DevNullIsUsed = false; 520 521 EXPECT_THAT_ERROR( 522 writeToOutput("/dev/null", 523 [&](raw_ostream &Out) -> Error { 524 DevNullIsUsed = 525 testing::internal::CheckedDowncastToActualType< 526 raw_null_ostream, raw_ostream>(&Out); 527 return Error::success(); 528 }), 529 Succeeded()); 530 531 EXPECT_TRUE(DevNullIsUsed); 532 } 533 534 TEST(raw_ostreamTest, writeToStdOut) { 535 outs().flush(); 536 testing::internal::CaptureStdout(); 537 538 EXPECT_THAT_ERROR(writeToOutput("-", 539 [](raw_ostream &Out) -> Error { 540 Out << "HelloWorld"; 541 return Error::success(); 542 }), 543 Succeeded()); 544 outs().flush(); 545 546 std::string CapturedStdOut = testing::internal::GetCapturedStdout(); 547 EXPECT_EQ(CapturedStdOut, "HelloWorld"); 548 } 549 550 } // namespace 551