1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
//===----------------------------------------------------------------------===//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
// <format>
// basic_format_arg<Context> get(size_t i) const noexcept;
#include <format>
#include <cassert>
#include <limits>
#include <type_traits>
#include "test_macros.h"
#include "make_string.h"
template <class Context, class To, class From>
void test(From value) {
auto store = std::make_format_args<Context>(value);
const std::basic_format_args<Context> format_args{store};
auto visitor = [v = To(value)](auto a) {
if constexpr (std::is_same_v<To, decltype(a)>)
assert(v == a);
else
assert(false);
};
#if TEST_STD_VER >= 26 && defined(TEST_HAS_EXPLICIT_THIS_PARAMETER)
format_args.get(0).visit(visitor);
#else
std::visit_format_arg(visitor, format_args.get(0));
#endif
}
// Some types, as an extension, are stored in the variant. The Standard
// requires them to be observed as a handle.
template <class Context, class T>
void test_handle(T value) {
auto store = std::make_format_args<Context>(value);
std::basic_format_args<Context> format_args{store};
auto visitor = [](auto a) { assert((std::is_same_v<decltype(a), typename std::basic_format_arg<Context>::handle>)); };
#if TEST_STD_VER >= 26 && defined(TEST_HAS_EXPLICIT_THIS_PARAMETER)
format_args.get(0).visit(visitor);
#else
std::visit_format_arg(visitor, format_args.get(0));
#endif
}
// Test specific for string and string_view.
//
// Since both result in a string_view there's no need to pass this as a
// template argument.
template <class Context, class From>
void test_string_view(From value) {
auto store = std::make_format_args<Context>(value);
const std::basic_format_args<Context> format_args{store};
using CharT = typename Context::char_type;
using To = std::basic_string_view<CharT>;
using V = std::basic_string<CharT>;
auto visitor = [v = V(value.begin(), value.end())](auto a) {
if constexpr (std::is_same_v<To, decltype(a)>)
assert(v == a);
else
assert(false);
};
#if TEST_STD_VER >= 26 && defined(TEST_HAS_EXPLICIT_THIS_PARAMETER)
format_args.get(0).visit(visitor);
#else
std::visit_format_arg(visitor, format_args.get(0));
#endif
}
template <class CharT>
void test() {
using Context = std::basic_format_context<CharT*, CharT>;
using char_type = typename Context::char_type;
std::basic_string<char_type> empty;
std::basic_string<char_type> str = MAKE_STRING(char_type, "abc");
// Test boolean types.
test<Context, bool>(true);
test<Context, bool>(false);
// Test char_type types.
test<Context, char_type, char_type>('a');
test<Context, char_type, char_type>('z');
test<Context, char_type, char_type>('0');
test<Context, char_type, char_type>('9');
// Test char types.
if (std::is_same_v<char_type, char>) {
// char to char -> char
test<Context, char_type, char>('a');
test<Context, char_type, char>('z');
test<Context, char_type, char>('0');
test<Context, char_type, char>('9');
} else {
if (std::is_same_v<char_type, wchar_t>) {
// char to wchar_t -> wchar_t
test<Context, wchar_t, char>('a');
test<Context, wchar_t, char>('z');
test<Context, wchar_t, char>('0');
test<Context, wchar_t, char>('9');
} else if (std::is_signed_v<char>) {
// char to char_type -> int
// This happens when Context::char_type is a char8_t, char16_t, or
// char32_t and char is a signed type.
// Note if sizeof(char_type) > sizeof(int) this test fails. If there are
// platforms where that occurs extra tests need to be added for char32_t
// testing it against a long long.
test<Context, int, char>('a');
test<Context, int, char>('z');
test<Context, int, char>('0');
test<Context, int, char>('9');
} else {
// char to char_type -> unsigned
// This happens when Context::char_type is a char8_t, char16_t, or
// char32_t and char is an unsigned type.
// Note if sizeof(char_type) > sizeof(unsigned) this test fails. If there
// are platforms where that occurs extra tests need to be added for
// char32_t testing it against an unsigned long long.
test<Context, unsigned, char>('a');
test<Context, unsigned, char>('z');
test<Context, unsigned, char>('0');
test<Context, unsigned, char>('9');
}
}
// Test signed integer types.
test<Context, int, signed char>(std::numeric_limits<signed char>::min());
test<Context, int, signed char>(0);
test<Context, int, signed char>(std::numeric_limits<signed char>::max());
test<Context, int, short>(std::numeric_limits<short>::min());
test<Context, int, short>(std::numeric_limits<signed char>::min());
test<Context, int, short>(0);
test<Context, int, short>(std::numeric_limits<signed char>::max());
test<Context, int, short>(std::numeric_limits<short>::max());
test<Context, int, int>(std::numeric_limits<int>::min());
test<Context, int, int>(std::numeric_limits<short>::min());
test<Context, int, int>(std::numeric_limits<signed char>::min());
test<Context, int, int>(0);
test<Context, int, int>(std::numeric_limits<signed char>::max());
test<Context, int, int>(std::numeric_limits<short>::max());
test<Context, int, int>(std::numeric_limits<int>::max());
using LongToType =
std::conditional_t<sizeof(long) == sizeof(int), int, long long>;
test<Context, LongToType, long>(std::numeric_limits<long>::min());
test<Context, LongToType, long>(std::numeric_limits<int>::min());
test<Context, LongToType, long>(std::numeric_limits<short>::min());
test<Context, LongToType, long>(std::numeric_limits<signed char>::min());
test<Context, LongToType, long>(0);
test<Context, LongToType, long>(std::numeric_limits<signed char>::max());
test<Context, LongToType, long>(std::numeric_limits<short>::max());
test<Context, LongToType, long>(std::numeric_limits<int>::max());
test<Context, LongToType, long>(std::numeric_limits<long>::max());
test<Context, long long, long long>(std::numeric_limits<long long>::min());
test<Context, long long, long long>(std::numeric_limits<long>::min());
test<Context, long long, long long>(std::numeric_limits<int>::min());
test<Context, long long, long long>(std::numeric_limits<short>::min());
test<Context, long long, long long>(std::numeric_limits<signed char>::min());
test<Context, long long, long long>(0);
test<Context, long long, long long>(std::numeric_limits<signed char>::max());
test<Context, long long, long long>(std::numeric_limits<short>::max());
test<Context, long long, long long>(std::numeric_limits<int>::max());
test<Context, long long, long long>(std::numeric_limits<long>::max());
test<Context, long long, long long>(std::numeric_limits<long long>::max());
#ifndef TEST_HAS_NO_INT128
test_handle<Context, __int128_t>(0);
#endif // TEST_HAS_NO_INT128
// Test unsigned integer types.
test<Context, unsigned, unsigned char>(0);
test<Context, unsigned, unsigned char>(
std::numeric_limits<unsigned char>::max());
test<Context, unsigned, unsigned short>(0);
test<Context, unsigned, unsigned short>(
std::numeric_limits<unsigned char>::max());
test<Context, unsigned, unsigned short>(
std::numeric_limits<unsigned short>::max());
test<Context, unsigned, unsigned>(0);
test<Context, unsigned, unsigned>(std::numeric_limits<unsigned char>::max());
test<Context, unsigned, unsigned>(std::numeric_limits<unsigned short>::max());
test<Context, unsigned, unsigned>(std::numeric_limits<unsigned>::max());
using UnsignedLongToType =
std::conditional_t<sizeof(unsigned long) == sizeof(unsigned), unsigned,
unsigned long long>;
test<Context, UnsignedLongToType, unsigned long>(0);
test<Context, UnsignedLongToType, unsigned long>(
std::numeric_limits<unsigned char>::max());
test<Context, UnsignedLongToType, unsigned long>(
std::numeric_limits<unsigned short>::max());
test<Context, UnsignedLongToType, unsigned long>(
std::numeric_limits<unsigned>::max());
test<Context, UnsignedLongToType, unsigned long>(
std::numeric_limits<unsigned long>::max());
test<Context, unsigned long long, unsigned long long>(0);
test<Context, unsigned long long, unsigned long long>(
std::numeric_limits<unsigned char>::max());
test<Context, unsigned long long, unsigned long long>(
std::numeric_limits<unsigned short>::max());
test<Context, unsigned long long, unsigned long long>(
std::numeric_limits<unsigned>::max());
test<Context, unsigned long long, unsigned long long>(
std::numeric_limits<unsigned long>::max());
test<Context, unsigned long long, unsigned long long>(
std::numeric_limits<unsigned long long>::max());
#ifndef TEST_HAS_NO_INT128
test_handle<Context, __uint128_t>(0);
#endif // TEST_HAS_NO_INT128
// Test floating point types.
test<Context, float, float>(-std::numeric_limits<float>::max());
test<Context, float, float>(-std::numeric_limits<float>::min());
test<Context, float, float>(-0.0);
test<Context, float, float>(0.0);
test<Context, float, float>(std::numeric_limits<float>::min());
test<Context, float, float>(std::numeric_limits<float>::max());
test<Context, double, double>(-std::numeric_limits<double>::max());
test<Context, double, double>(-std::numeric_limits<double>::min());
test<Context, double, double>(-0.0);
test<Context, double, double>(0.0);
test<Context, double, double>(std::numeric_limits<double>::min());
test<Context, double, double>(std::numeric_limits<double>::max());
test<Context, long double, long double>(
-std::numeric_limits<long double>::max());
test<Context, long double, long double>(
-std::numeric_limits<long double>::min());
test<Context, long double, long double>(-0.0);
test<Context, long double, long double>(0.0);
test<Context, long double, long double>(
std::numeric_limits<long double>::min());
test<Context, long double, long double>(
std::numeric_limits<long double>::max());
// Test const char_type pointer types.
test<Context, const char_type*, const char_type*>(empty.c_str());
test<Context, const char_type*, const char_type*>(str.c_str());
// Test string_view types.
test<Context, std::basic_string_view<char_type>>(
std::basic_string_view<char_type>());
test<Context, std::basic_string_view<char_type>,
std::basic_string_view<char_type>>(empty);
test<Context, std::basic_string_view<char_type>,
std::basic_string_view<char_type>>(str);
// Test string types.
test<Context, std::basic_string_view<char_type>>(
std::basic_string<char_type>());
test<Context, std::basic_string_view<char_type>,
std::basic_string<char_type>>(empty);
test<Context, std::basic_string_view<char_type>,
std::basic_string<char_type>>(str);
// Test pointer types.
test<Context, const void*>(nullptr);
}
void test() {
test<char>();
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
test<wchar_t>();
#endif
}
int main(int, char**) {
test();
return 0;
}
|