| 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_TIME_TIME_SYN_FORMATTER_TESTS_H |
| 9 |
#define TEST_STD_TIME_TIME_SYN_FORMATTER_TESTS_H |
| 10 |
|
| 11 |
#include "assert_macros.h" |
| 12 |
#include "concat_macros.h" |
| 13 |
#include "make_string.h" |
| 14 |
#include "string_literal.h" |
| 15 |
#include "test_format_string.h" |
| 16 |
#include "test_macros.h" |
| 17 |
|
| 18 |
#include <algorithm> |
| 19 |
#include <cassert> |
| 20 |
#include <iostream> |
| 21 |
#include <set> |
| 22 |
#include <string> |
| 23 |
#include <string_view> |
| 24 |
|
| 25 |
#define STR(S) MAKE_STRING(CharT, S) |
| 26 |
#define SV(S) MAKE_STRING_VIEW(CharT, S) |
| 27 |
|
| 28 |
#ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 29 |
template <class CharT> |
| 30 |
using format_context = std::conditional_t<std::same_as<CharT, char>, std::format_context, std::wformat_context>; |
| 31 |
#else |
| 32 |
template <class CharT> |
| 33 |
using format_context = std::format_context; |
| 34 |
#endif |
| 35 |
|
| 36 |
template <class CharT, class... Args> |
| 37 |
void check(std::basic_string_view<CharT> expected, test_format_string<CharT, Args...> fmt, Args&&... args) { |
| 38 |
std::basic_string<CharT> out = std::format(fmt, std::forward<Args>(args)...); |
| 39 |
TEST_REQUIRE(out == expected, |
| 40 |
TEST_WRITE_CONCATENATED( |
| 41 |
"\nFormat string ", fmt.get(), "\nExpected output ", expected, "\nActual output ", out, '\n')); |
| 42 |
} |
| 43 |
|
| 44 |
template <class CharT, class... Args> |
| 45 |
void check(const std::locale& loc, |
| 46 |
std::basic_string_view<CharT> expected, |
| 47 |
test_format_string<CharT, Args...> fmt, |
| 48 |
Args&&... args) { |
| 49 |
std::basic_string<CharT> out = std::format(loc, fmt, std::forward<Args>(args)...); |
| 50 |
TEST_REQUIRE(out == expected, |
| 51 |
TEST_WRITE_CONCATENATED( |
| 52 |
"\nFormat string ", fmt.get(), "\nExpected output ", expected, "\nActual output ", out, '\n')); |
| 53 |
} |
| 54 |
|
| 55 |
template <class CharT, class... Args> |
| 56 |
void check_exception([[maybe_unused]] std::string_view what, |
| 57 |
[[maybe_unused]] std::basic_string_view<CharT> fmt, |
| 58 |
[[maybe_unused]] const Args&... args) { |
| 59 |
TEST_VALIDATE_EXCEPTION( |
| 60 |
std::format_error, |
| 61 |
[&]([[maybe_unused]] const std::format_error& e) { |
| 62 |
TEST_LIBCPP_REQUIRE( |
| 63 |
e.what() == what, |
| 64 |
TEST_WRITE_CONCATENATED( |
| 65 |
"\nFormat string ", fmt, "\nExpected exception ", what, "\nActual exception ", e.what(), '\n')); |
| 66 |
}, |
| 67 |
TEST_IGNORE_NODISCARD std::vformat(fmt, std::make_format_args<format_context<CharT>>(args...))); |
| 68 |
} |
| 69 |
|
| 70 |
template <class CharT, class T> |
| 71 |
void check_invalid_type(const std::set<std::basic_string_view<CharT>>& valid_types, |
| 72 |
std::string_view what, |
| 73 |
std::basic_string<CharT> type, |
| 74 |
const T& arg) { |
| 75 |
std::basic_string<CharT> fmt{STR("{:%") + type + STR("}")}; |
| 76 |
|
| 77 |
if (valid_types.contains(std::basic_string_view<CharT>{type})) { |
| 78 |
#ifndef TEST_HAS_NO_EXCEPTIONS |
| 79 |
try { |
| 80 |
#endif |
| 81 |
TEST_IGNORE_NODISCARD std::vformat( |
| 82 |
std::basic_string_view<CharT>{fmt}, std::make_format_args<format_context<CharT>>(arg)); |
| 83 |
#ifndef TEST_HAS_NO_EXCEPTIONS |
| 84 |
} catch (const std::format_error& e) { |
| 85 |
(void)e; |
| 86 |
if constexpr (std::same_as<CharT, char>) |
| 87 |
std::cerr << "\nFormat string " << fmt << "\nUnexpected exception " << e.what() << '\n'; |
| 88 |
assert(false); |
| 89 |
} |
| 90 |
#endif |
| 91 |
} else { |
| 92 |
check_exception(what, std::basic_string_view<CharT>{fmt}, arg); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
template <class CharT, class T> |
| 97 |
void check_invalid_types(const std::set<std::basic_string_view<CharT>>& valid_types, const T& arg) { |
| 98 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a weekday", STR("a"), arg); |
| 99 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a weekday", STR("A"), arg); |
| 100 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a month", STR("b"), arg); |
| 101 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a month", STR("B"), arg); |
| 102 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a date and time", STR("c"), arg); |
| 103 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a year", STR("C"), arg); |
| 104 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a day", STR("d"), arg); |
| 105 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a date", STR("D"), arg); |
| 106 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a day", STR("e"), arg); |
| 107 |
// E - the modifier is checked separately |
| 108 |
check_invalid_type(valid_types, "The date time type specifier is invalid", STR("f"), arg); |
| 109 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a date", STR("F"), arg); |
| 110 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a date", STR("g"), arg); |
| 111 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a date", STR("G"), arg); |
| 112 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a month", STR("h"), arg); |
| 113 |
check_invalid_type(valid_types, "The supplied date time doesn't contain an hour", STR("H"), arg); |
| 114 |
check_invalid_type(valid_types, "The date time type specifier is invalid", STR("i"), arg); |
| 115 |
check_invalid_type(valid_types, "The supplied date time doesn't contain an hour", STR("I"), arg); |
| 116 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a date or duration", STR("j"), arg); |
| 117 |
check_invalid_type(valid_types, "The date time type specifier is invalid", STR("J"), arg); |
| 118 |
check_invalid_type(valid_types, "The date time type specifier is invalid", STR("k"), arg); |
| 119 |
check_invalid_type(valid_types, "The date time type specifier is invalid", STR("K"), arg); |
| 120 |
check_invalid_type(valid_types, "The date time type specifier is invalid", STR("l"), arg); |
| 121 |
check_invalid_type(valid_types, "The date time type specifier is invalid", STR("L"), arg); |
| 122 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a month", STR("m"), arg); |
| 123 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a minute", STR("M"), arg); |
| 124 |
// n - valid |
| 125 |
check_invalid_type(valid_types, "The date time type specifier is invalid", STR("N"), arg); |
| 126 |
check_invalid_type(valid_types, "The date time type specifier is invalid", STR("o"), arg); |
| 127 |
// O - the modifier is checked separately |
| 128 |
check_invalid_type(valid_types, "The supplied date time doesn't contain an hour", STR("p"), arg); |
| 129 |
check_invalid_type(valid_types, "The date time type specifier is invalid", STR("P"), arg); |
| 130 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a duration", STR("q"), arg); |
| 131 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a duration", STR("Q"), arg); |
| 132 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a time", STR("r"), arg); |
| 133 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a time", STR("R"), arg); |
| 134 |
check_invalid_type(valid_types, "The date time type specifier is invalid", STR("s"), arg); |
| 135 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a second", STR("S"), arg); |
| 136 |
// t - valid |
| 137 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a time", STR("T"), arg); |
| 138 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a weekday", STR("u"), arg); |
| 139 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a date", STR("U"), arg); |
| 140 |
check_invalid_type(valid_types, "The date time type specifier is invalid", STR("v"), arg); |
| 141 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a date", STR("V"), arg); |
| 142 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a weekday", STR("w"), arg); |
| 143 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a date", STR("W"), arg); |
| 144 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a date", STR("x"), arg); |
| 145 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a time", STR("X"), arg); |
| 146 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a year", STR("y"), arg); |
| 147 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a year", STR("y"), arg); |
| 148 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a time zone", STR("z"), arg); |
| 149 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a time zone", STR("Z"), arg); |
| 150 |
|
| 151 |
// *** E modifier |
| 152 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("Ea"), arg); |
| 153 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("EA"), arg); |
| 154 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("Eb"), arg); |
| 155 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("EB"), arg); |
| 156 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a date and time", STR("Ec"), arg); |
| 157 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a year", STR("EC"), arg); |
| 158 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("Ed"), arg); |
| 159 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("ED"), arg); |
| 160 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("Ee"), arg); |
| 161 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("EE"), arg); |
| 162 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("Ef"), arg); |
| 163 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("EF"), arg); |
| 164 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("Eg"), arg); |
| 165 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("EG"), arg); |
| 166 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("Eh"), arg); |
| 167 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("EH"), arg); |
| 168 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("Ei"), arg); |
| 169 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("EI"), arg); |
| 170 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("Ej"), arg); |
| 171 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("EJ"), arg); |
| 172 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("Ek"), arg); |
| 173 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("EK"), arg); |
| 174 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("El"), arg); |
| 175 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("EL"), arg); |
| 176 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("Em"), arg); |
| 177 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("EM"), arg); |
| 178 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("En"), arg); |
| 179 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("EN"), arg); |
| 180 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("Eo"), arg); |
| 181 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("EO"), arg); |
| 182 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("Ep"), arg); |
| 183 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("EP"), arg); |
| 184 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("Eq"), arg); |
| 185 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("EQ"), arg); |
| 186 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("Er"), arg); |
| 187 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("ER"), arg); |
| 188 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("Es"), arg); |
| 189 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("ES"), arg); |
| 190 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("Et"), arg); |
| 191 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("ET"), arg); |
| 192 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("Eu"), arg); |
| 193 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("EU"), arg); |
| 194 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("Ev"), arg); |
| 195 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("EV"), arg); |
| 196 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("Ew"), arg); |
| 197 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("EW"), arg); |
| 198 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a date", STR("Ex"), arg); |
| 199 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a time", STR("EX"), arg); |
| 200 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a year", STR("Ey"), arg); |
| 201 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a year", STR("EY"), arg); |
| 202 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a time zone", STR("Ez"), arg); |
| 203 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("EZ"), arg); |
| 204 |
check_invalid_type(valid_types, "The date time type specifier for modifier E is invalid", STR("E%"), arg); |
| 205 |
|
| 206 |
// *** O modifier |
| 207 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("Oa"), arg); |
| 208 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("OA"), arg); |
| 209 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("Ob"), arg); |
| 210 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("OB"), arg); |
| 211 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("Oc"), arg); |
| 212 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("OC"), arg); |
| 213 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a day", STR("Od"), arg); |
| 214 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("OD"), arg); |
| 215 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a day", STR("Oe"), arg); |
| 216 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("OE"), arg); |
| 217 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("Of"), arg); |
| 218 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("OF"), arg); |
| 219 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("Og"), arg); |
| 220 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("OG"), arg); |
| 221 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("Oh"), arg); |
| 222 |
check_invalid_type(valid_types, "The supplied date time doesn't contain an hour", STR("OH"), arg); |
| 223 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("Oi"), arg); |
| 224 |
check_invalid_type(valid_types, "The supplied date time doesn't contain an hour", STR("OI"), arg); |
| 225 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("Oj"), arg); |
| 226 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("OJ"), arg); |
| 227 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("Ok"), arg); |
| 228 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("OK"), arg); |
| 229 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("Ol"), arg); |
| 230 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("OL"), arg); |
| 231 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a month", STR("Om"), arg); |
| 232 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a minute", STR("OM"), arg); |
| 233 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("On"), arg); |
| 234 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("ON"), arg); |
| 235 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("Oo"), arg); |
| 236 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("OO"), arg); |
| 237 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("Op"), arg); |
| 238 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("OP"), arg); |
| 239 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("Oq"), arg); |
| 240 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("OQ"), arg); |
| 241 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("Or"), arg); |
| 242 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("OR"), arg); |
| 243 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("Os"), arg); |
| 244 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a second", STR("OS"), arg); |
| 245 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("Ot"), arg); |
| 246 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("OT"), arg); |
| 247 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a weekday", STR("Ou"), arg); |
| 248 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a date", STR("OU"), arg); |
| 249 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("Ov"), arg); |
| 250 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a date", STR("OV"), arg); |
| 251 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a weekday", STR("Ow"), arg); |
| 252 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a date", STR("OW"), arg); |
| 253 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("Ox"), arg); |
| 254 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("OX"), arg); |
| 255 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a year", STR("Oy"), arg); |
| 256 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("OY"), arg); |
| 257 |
check_invalid_type(valid_types, "The supplied date time doesn't contain a time zone", STR("Oz"), arg); |
| 258 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("OZ"), arg); |
| 259 |
check_invalid_type(valid_types, "The date time type specifier for modifier O is invalid", STR("O%"), arg); |
| 260 |
} |
| 261 |
|
| 262 |
#endif // TEST_STD_TIME_TIME_SYN_FORMATTER_TESTS_H |