| 1 | //===----------------------------------------------------------------------===// |
| 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 | // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
| 10 | // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME |
| 11 | |
| 12 | // TODO PRINT Investigate see https://reviews.llvm.org/D156585 |
| 13 | // UNSUPPORTED: no-filesystem |
| 14 | |
| 15 | // XFAIL: availability-fp_to_chars-missing |
| 16 | |
| 17 | // <ostream> |
| 18 | |
| 19 | // template<class... Args> |
| 20 | // void println(ostream& os, format_string<Args...> fmt, Args&&... args); |
| 21 | // void println(ostream& os); // since C++26 |
| 22 | |
| 23 | // [ostream.formatted.print]/3 |
| 24 | // If the function is vprint_unicode and os is a stream that refers to |
| 25 | // a terminal capable of displaying Unicode which is determined in an |
| 26 | // implementation-defined manner, writes out to the terminal using the |
| 27 | // native Unicode API; |
| 28 | // This is tested in |
| 29 | // test/libcxx/input.output/iostream.format/output.streams/ostream.formatted/ostream.formatted.print/vprint_unicode.pass.cpp |
| 30 | |
| 31 | #include <cassert> |
| 32 | #include <ostream> |
| 33 | #include <sstream> |
| 34 | |
| 35 | #include "assert_macros.h" |
| 36 | #include "concat_macros.h" |
| 37 | #include "print_tests.h" |
| 38 | #include "test_format_string.h" |
| 39 | #include "test_macros.h" |
| 40 | |
| 41 | auto test_file = []<class... Args>(std::string_view e, test_format_string<char, Args...> fmt, Args&&... args) { |
| 42 | std::string expected = std::string{e} + '\n'; |
| 43 | |
| 44 | std::stringstream sstr; |
| 45 | std::println(format: sstr, fmt, std::forward<Args>(args)...); |
| 46 | |
| 47 | std::string out = sstr.str(); |
| 48 | TEST_REQUIRE(out == expected, |
| 49 | TEST_WRITE_CONCATENATED( |
| 50 | "\nFormat string " , fmt.get(), "\nExpected output " , expected, "\nActual output " , out, '\n')); |
| 51 | }; |
| 52 | |
| 53 | auto test_exception = []<class... Args>(std::string_view, std::string_view, Args&&...) { |
| 54 | // After P2216 most exceptions thrown by std::format become ill-formed. |
| 55 | // Therefore this tests does nothing. |
| 56 | // A basic ill-formed test is done in format.verify.cpp |
| 57 | // The exceptions are tested by other functions that don't use the basic-format-string as fmt argument. |
| 58 | }; |
| 59 | |
| 60 | void test_println_blank_line() { |
| 61 | std::string expected{'\n'}; |
| 62 | |
| 63 | std::stringstream sstr; |
| 64 | std::println(sstr); |
| 65 | |
| 66 | std::string out = sstr.str(); |
| 67 | TEST_REQUIRE(out == expected, |
| 68 | TEST_WRITE_CONCATENATED("\nExpected output (blank line) " , expected, "\nActual output " , out, '\n')); |
| 69 | }; |
| 70 | |
| 71 | int main(int, char**) { |
| 72 | print_tests(check: test_file, check_exception: test_exception); |
| 73 | test_println_blank_line(); |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |