| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 3 |
// See https://llvm.org/LICENSE.txt for license information. |
| 4 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 |
// |
| 6 |
//===----------------------------------------------------------------------===// |
| 7 |
|
| 8 |
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
| 9 |
// UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME |
| 10 |
|
| 11 |
// TODO PRINT Investigate see https://reviews.llvm.org/D156585 |
| 12 |
// UNSUPPORTED: no-filesystem |
| 13 |
|
| 14 |
// XFAIL: availability-fp_to_chars-missing |
| 15 |
|
| 16 |
// <ostream> |
| 17 |
|
| 18 |
// template<class... Args> |
| 19 |
// void print(ostream& os, format_string<Args...> fmt, Args&&... args); |
| 20 |
|
| 21 |
// [ostream.formatted.print]/3 |
| 22 |
// If the function is vprint_unicode and os is a stream that refers to |
| 23 |
// a terminal capable of displaying Unicode which is determined in an |
| 24 |
// implementation-defined manner, writes out to the terminal using the |
| 25 |
// native Unicode API; |
| 26 |
// This is tested in |
| 27 |
// test/libcxx/input.output/iostream.format/output.streams/ostream.formatted/ostream.formatted.print/vprint_unicode.pass.cpp |
| 28 |
|
| 29 |
#include <cassert> |
| 30 |
#include <ostream> |
| 31 |
#include <sstream> |
| 32 |
|
| 33 |
#include "assert_macros.h" |
| 34 |
#include "concat_macros.h" |
| 35 |
#include "print_tests.h" |
| 36 |
#include "test_format_string.h" |
| 37 |
#include "test_macros.h" |
| 38 |
|
| 39 |
auto test_file = []<class... Args>(std::string_view expected, test_format_string<char, Args...> fmt, Args&&... args) { |
| 40 |
std::stringstream sstr; |
| 41 |
std::print(sstr, fmt, std::forward<Args>(args)...); |
| 42 |
|
| 43 |
std::string out = sstr.str(); |
| 44 |
TEST_REQUIRE(out == expected, |
| 45 |
TEST_WRITE_CONCATENATED( |
| 46 |
"\nFormat string ", fmt.get(), "\nExpected output ", expected, "\nActual output ", out, '\n')); |
| 47 |
}; |
| 48 |
|
| 49 |
auto test_exception = []<class... Args>(std::string_view, std::string_view, Args&&...) { |
| 50 |
// After P2216 most exceptions thrown by std::format become ill-formed. |
| 51 |
// Therefore this tests does nothing. |
| 52 |
// A basic ill-formed test is done in format.verify.cpp |
| 53 |
// The exceptions are tested by other functions that don't use the basic-format-string as fmt argument. |
| 54 |
}; |
| 55 |
// [ostream.formatted.print]/3.2 |
| 56 |
// ... |
| 57 |
// After constructing a sentry object, the function initializes an automatic variable via |
| 58 |
// string out = vformat(os.getloc(), fmt, args); |
| 59 |
// This means if both |
| 60 |
// - creating a sentry fails |
| 61 |
// - the formatting fails |
| 62 |
// the first one "wins" and the format_error is not thrown. |
| 63 |
static void test_sentry_failure() { |
| 64 |
// In order for the creation of a sentry to fail a tied stream's |
| 65 |
// sync operation should fail. |
| 66 |
struct sync_failure : public std::basic_streambuf<char> { |
| 67 |
protected: |
| 68 |
int virtual sync() { return -1; } |
| 69 |
}; |
| 70 |
sync_failure buf_tied; |
| 71 |
std::ostream os_tied(&buf_tied); |
| 72 |
os_tied.exceptions(std::stringstream::failbit | std::stringstream::badbit | std::stringstream::eofbit); |
| 73 |
|
| 74 |
std::stringstream os; |
| 75 |
os.tie(&os_tied); |
| 76 |
os.exceptions(std::stringstream::failbit | std::stringstream::badbit | std::stringstream::eofbit); |
| 77 |
|
| 78 |
TEST_THROWS_TYPE(std::ios_base::failure, std::print(os, "valid")); |
| 79 |
os_tied.clear(); |
| 80 |
TEST_THROWS_TYPE(std::ios_base::failure, std::print(os, "throws exception at run-time {0:{0}}", -10)); |
| 81 |
|
| 82 |
os.exceptions(std::stringstream::goodbit); |
| 83 |
os.setstate(std::stringstream::failbit); |
| 84 |
std::print(os, "not called when the os.good() is false, so no exception is thrown {0:{0}}", -10); |
| 85 |
} |
| 86 |
|
| 87 |
// [ostream.formatted.print]/3.2 |
| 88 |
// any exception thrown by the call to vformat is propagated without |
| 89 |
// regard to the value of os.exceptions() and without turning on |
| 90 |
// ios_base::badbit in the error state of os. |
| 91 |
// Most invalid format strings are checked at compile-time. An invalid |
| 92 |
// value for the width can only be tested run-time. |
| 93 |
static void test_format_exception() { |
| 94 |
std::stringstream sstr; |
| 95 |
assert(sstr.good()); |
| 96 |
|
| 97 |
TEST_THROWS_TYPE(std::format_error, std::print(sstr, "no output {0:{0}}", -10)); |
| 98 |
assert(sstr.good()); |
| 99 |
assert(sstr.str().empty()); |
| 100 |
|
| 101 |
sstr.exceptions(std::stringstream::goodbit); |
| 102 |
TEST_THROWS_TYPE(std::format_error, std::print(sstr, "no output {0:{0}}", -10)); |
| 103 |
assert(sstr.good()); |
| 104 |
assert(sstr.str().empty()); |
| 105 |
|
| 106 |
sstr.exceptions(std::stringstream::failbit | std::stringstream::badbit | std::stringstream::eofbit); |
| 107 |
TEST_THROWS_TYPE(std::format_error, std::print(sstr, "no output {0:{0}}", -10)); |
| 108 |
assert(sstr.good()); |
| 109 |
assert(sstr.str().empty()); |
| 110 |
} |
| 111 |
|
| 112 |
static void test_write_failure() { |
| 113 |
// Stream that fails to write a single character. |
| 114 |
struct overflow_failure : public std::basic_streambuf<char> { |
| 115 |
protected: |
| 116 |
int virtual overflow(int) { return std::char_traits<char>::eof(); } |
| 117 |
}; |
| 118 |
overflow_failure buf; |
| 119 |
std::ostream os(&buf); |
| 120 |
os.exceptions(std::stringstream::failbit | std::stringstream::badbit | std::stringstream::eofbit); |
| 121 |
|
| 122 |
TEST_THROWS_TYPE(std::ios_base::failure, std::print(os, "valid")); |
| 123 |
os.clear(); |
| 124 |
// When the parser would directly write to the output instead of |
| 125 |
// formatting first it would fail writing the first character 't' of |
| 126 |
// the string and result in a std::ios_base::failure exception. |
| 127 |
TEST_THROWS_TYPE(std::format_error, std::print(os, "throws exception at run-time {0:{0}}", -10)); |
| 128 |
|
| 129 |
os.exceptions(std::stringstream::goodbit); |
| 130 |
os.clear(); |
| 131 |
std::print(os, "valid"); |
| 132 |
assert(os.fail()); |
| 133 |
} |
| 134 |
|
| 135 |
static void test_stream_formatting() { |
| 136 |
std::stringstream sstr; |
| 137 |
auto test = [&]<class... Args>(std::string_view expected, test_format_string<char, Args...> fmt, Args&&... args) { |
| 138 |
sstr.str(""); |
| 139 |
std::print(sstr, fmt, std::forward<Args>(args)...); |
| 140 |
|
| 141 |
std::string out = sstr.str(); |
| 142 |
TEST_REQUIRE(out == expected, |
| 143 |
TEST_WRITE_CONCATENATED( |
| 144 |
"\nFormat string ", fmt.get(), "\nExpected output ", expected, "\nActual output ", out, '\n')); |
| 145 |
}; |
| 146 |
|
| 147 |
test("hello", "{}", "hello"); |
| 148 |
|
| 149 |
sstr.width(10); |
| 150 |
test(" hello", "{}", "hello"); |
| 151 |
|
| 152 |
sstr.fill('+'); |
| 153 |
|
| 154 |
sstr.width(10); |
| 155 |
test("+++++hello", "{}", "hello"); |
| 156 |
|
| 157 |
// *** Test embedded NUL character *** |
| 158 |
using namespace std::literals; |
| 159 |
sstr.width(15); |
| 160 |
test("++++hello\0world"sv, "hello{}{}", '\0', "world"); |
| 161 |
|
| 162 |
// *** Test Unicode *** |
| 163 |
// Streams count code units not code points |
| 164 |
// 2-byte code points |
| 165 |
sstr.width(5); |
| 166 |
test("+++\u00a1", "{}", "\u00a1"); // INVERTED EXCLAMATION MARK |
| 167 |
sstr.width(5); |
| 168 |
test("+++\u07ff", "{}", "\u07ff"); // NKO TAMAN SIGN |
| 169 |
|
| 170 |
// 3-byte code points |
| 171 |
sstr.width(5); |
| 172 |
test("++\u0800", "{}", "\u0800"); // SAMARITAN LETTER ALAF |
| 173 |
sstr.width(5); |
| 174 |
test("++\ufffd", "{}", "\ufffd"); // REPLACEMENT CHARACTER |
| 175 |
|
| 176 |
// 4-byte code points |
| 177 |
sstr.width(5); |
| 178 |
test("+\U00010000", "{}", "\U00010000"); // LINEAR B SYLLABLE B008 A |
| 179 |
sstr.width(5); |
| 180 |
test("+\U0010FFFF", "{}", "\U0010FFFF"); // Undefined Character |
| 181 |
} |
| 182 |
|
| 183 |
int main(int, char**) { |
| 184 |
print_tests(test_file, test_exception); |
| 185 |
|
| 186 |
test_sentry_failure(); |
| 187 |
test_format_exception(); |
| 188 |
test_write_failure(); |
| 189 |
test_stream_formatting(); |
| 190 |
|
| 191 |
return 0; |
| 192 |
} |