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 #ifndef TEST_STD_INPUT_OUTPUT_IOSTREAM_FORMAT_PRINT_FUN_PRINT_TESTS_H
9 #define TEST_STD_INPUT_OUTPUT_IOSTREAM_FORMAT_PRINT_FUN_PRINT_TESTS_H
10
11 template <class TestFunction, class ExceptionTest>
print_tests(TestFunction check,ExceptionTest check_exception)12 void print_tests(TestFunction check, ExceptionTest check_exception) {
13 // *** Test escaping ***
14
15 check("{", "{{");
16 check("{:^}", "{{:^}}");
17 check("{: ^}", "{{:{}^}}", ' ');
18 check("{:{}^}", "{{:{{}}^}}");
19 check("{:{ }^}", "{{:{{{}}}^}}", ' ');
20
21 // *** Test argument ID ***
22 check("hello false true", "hello {0:} {1:}", false, true);
23 check("hello true false", "hello {1:} {0:}", false, true);
24
25 // *** Test many arguments ***
26 check(
27 "1234567890\t1234567890",
28 "{}{}{}{}{}{}{}{}{}{}\t{}{}{}{}{}{}{}{}{}{}",
29 1,
30 2,
31 3,
32 4,
33 5,
34 6,
35 7,
36 8,
37 9,
38 0,
39 1,
40 2,
41 3,
42 4,
43 5,
44 6,
45 7,
46 8,
47 9,
48 0);
49
50 // *** Test embedded NUL character ***
51 using namespace std::literals;
52 check("hello\0world"sv, "hello{}{}", '\0', "world");
53 check("hello\0world"sv, "hello\0{}"sv, "world");
54 check("hello\0world"sv, "hello{}", "\0world"sv);
55
56 // *** Test Unicode ***
57 // 2-byte code points
58 check("\u00a1"sv, "{}"sv, "\u00a1"); // INVERTED EXCLAMATION MARK
59 check("\u07ff"sv, "{:}"sv, "\u07ff"); // NKO TAMAN SIGN
60
61 // 3-byte code points
62 check("\u0800"sv, "{}"sv, "\u0800"); // SAMARITAN LETTER ALAF
63 check("\ufffd"sv, "{}"sv, "\ufffd"); // REPLACEMENT CHARACTER
64
65 // 4-byte code points
66 check("\U00010000"sv, "{}"sv, "\U00010000"); // LINEAR B SYLLABLE B008 A
67 check("\U0010FFFF"sv, "{}"sv, "\U0010FFFF"); // Undefined Character
68
69 // *** Test invalid format strings ***
70 check_exception("The format string terminates at a '{'", "{");
71 check_exception("The replacement field misses a terminating '}'", "{:", 42);
72
73 check_exception("The format string contains an invalid escape sequence", "}");
74 check_exception("The format string contains an invalid escape sequence", "{:}-}", 42);
75
76 check_exception("The format string contains an invalid escape sequence", "} ");
77 check_exception("The argument index starts with an invalid character", "{-", 42);
78 check_exception("The argument index value is too large for the number of arguments supplied", "hello {}");
79 check_exception("The argument index value is too large for the number of arguments supplied", "hello {0}");
80 check_exception("The argument index value is too large for the number of arguments supplied", "hello {1}", 42);
81 }
82
83 #endif // TEST_STD_INPUT_OUTPUT_IOSTREAM_FORMAT_PRINT_FUN_PRINT_TESTS_H
84