[OpenACC] Implement 'collapse' for combined constructs.
[llvm-project.git] / libcxx / test / support / charconv_test_helpers.h
blobfcae09478457b6131b0a32351e6c21a26423b761
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 //===----------------------------------------------------------------------===//
9 #ifndef SUPPORT_CHARCONV_TEST_HELPERS_H
10 #define SUPPORT_CHARCONV_TEST_HELPERS_H
12 #include <algorithm>
13 #include <cassert>
14 #include <cerrno>
15 #include <charconv>
16 #include <cstddef>
17 #include <limits>
18 #include <numeric>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <type_traits>
23 #include "test_macros.h"
25 #if TEST_STD_VER < 11
26 #error This file requires C++11
27 #endif
29 using std::false_type;
30 using std::true_type;
32 template <typename To, typename From>
33 constexpr auto
34 is_non_narrowing(From a) -> decltype(To{a}, true_type())
36 return {};
39 template <typename To>
40 constexpr auto
41 is_non_narrowing(...) -> false_type
43 return {};
46 template <typename X, typename T>
47 constexpr bool
48 _fits_in(T, true_type /* non-narrowing*/, ...)
50 return true;
53 template <typename X, typename T, typename xl = std::numeric_limits<X>>
54 constexpr bool
55 _fits_in(T v, false_type, true_type /* T signed*/, true_type /* X signed */)
57 return xl::lowest() <= v && v <= (xl::max)();
60 template <typename X, typename T, typename xl = std::numeric_limits<X>>
61 constexpr bool
62 _fits_in(T v, false_type, true_type /* T signed */, false_type /* X unsigned*/)
64 return 0 <= v && typename std::make_unsigned<T>::type(v) <= (xl::max)();
67 template <typename X, typename T, typename xl = std::numeric_limits<X>>
68 constexpr bool
69 _fits_in(T v, false_type, false_type /* T unsigned */, ...)
71 return v <= typename std::make_unsigned<X>::type((xl::max)());
74 template <typename X, typename T>
75 constexpr bool
76 fits_in(T v)
78 return _fits_in<X>(v, is_non_narrowing<X>(v), std::is_signed<T>(), std::is_signed<X>());
81 template <typename X>
82 struct to_chars_test_base
84 template <typename T, std::size_t N, typename... Ts>
85 TEST_CONSTEXPR_CXX23 void test(T v, char const (&expect)[N], Ts... args)
87 std::to_chars_result r;
89 constexpr std::size_t len = N - 1;
90 static_assert(len > 0, "expected output won't be empty");
92 if (!fits_in<X>(v))
93 return;
95 r = std::to_chars(buf, buf + len - 1, X(v), args...);
96 assert(r.ptr == buf + len - 1);
97 assert(r.ec == std::errc::value_too_large);
99 r = std::to_chars(buf, buf + sizeof(buf), X(v), args...);
100 assert(r.ptr == buf + len);
101 assert(r.ec == std::errc{});
102 assert(std::equal(buf, buf + len, expect));
105 template <typename... Ts>
106 TEST_CONSTEXPR_CXX23 void test_value(X v, Ts... args)
108 std::to_chars_result r;
110 // Poison the buffer for testing whether a successful std::to_chars
111 // doesn't modify data beyond r.ptr. Use unsigned values to avoid
112 // overflowing char when it's signed.
113 std::iota(buf, buf + sizeof(buf), static_cast<unsigned char>(1));
114 r = std::to_chars(buf, buf + sizeof(buf), v, args...);
115 assert(r.ec == std::errc{});
116 for (std::size_t i = r.ptr - buf; i < sizeof(buf); ++i)
117 assert(static_cast<unsigned char>(buf[i]) == i + 1);
118 *r.ptr = '\0';
120 #ifndef TEST_HAS_NO_INT128
121 if (sizeof(X) == sizeof(__int128_t)) {
122 auto a = fromchars128_impl(buf, r.ptr, args...);
123 assert(v == a);
124 } else
125 #endif
127 auto a = fromchars_impl(buf, r.ptr, args...);
128 assert(v == a);
131 auto ep = r.ptr - 1;
132 r = std::to_chars(buf, ep, v, args...);
133 assert(r.ptr == ep);
134 assert(r.ec == std::errc::value_too_large);
137 private:
138 static TEST_CONSTEXPR_CXX23 long long fromchars_impl(char const* p, char const* ep, int base, true_type)
140 char* last;
141 long long r;
142 if (TEST_IS_CONSTANT_EVALUATED)
143 last = const_cast<char*>(std::from_chars(p, ep, r, base).ptr);
144 else
145 r = strtoll(p, &last, base);
146 assert(last == ep);
148 return r;
151 static TEST_CONSTEXPR_CXX23 unsigned long long fromchars_impl(char const* p, char const* ep, int base, false_type)
153 char* last;
154 unsigned long long r;
155 if (TEST_IS_CONSTANT_EVALUATED)
156 last = const_cast<char*>(std::from_chars(p, ep, r, base).ptr);
157 else
158 r = strtoull(p, &last, base);
159 assert(last == ep);
161 return r;
163 #ifndef TEST_HAS_NO_INT128
164 static TEST_CONSTEXPR_CXX23 __int128_t fromchars128_impl(char const* p, char const* ep, int base, true_type)
166 if (!TEST_IS_CONSTANT_EVALUATED) {
167 char* last;
168 __int128_t r = strtoll(p, &last, base);
169 if(errno != ERANGE) {
170 assert(last == ep);
171 return r;
175 // When the value doesn't fit in a long long use from_chars. This is
176 // not ideal since it does a round-trip test instead if using an
177 // external source.
178 __int128_t r;
179 std::from_chars_result s = std::from_chars(p, ep, r, base);
180 assert(s.ec == std::errc{});
181 assert(s.ptr == ep);
183 return r;
186 static TEST_CONSTEXPR_CXX23 __uint128_t fromchars128_impl(char const* p, char const* ep, int base, false_type)
188 if (!TEST_IS_CONSTANT_EVALUATED) {
189 char* last;
190 __uint128_t r = strtoull(p, &last, base);
191 if(errno != ERANGE) {
192 assert(last == ep);
193 return r;
197 __uint128_t r;
198 std::from_chars_result s = std::from_chars(p, ep, r, base);
199 assert(s.ec == std::errc{});
200 assert(s.ptr == ep);
202 return r;
205 static TEST_CONSTEXPR_CXX23 auto fromchars128_impl(char const* p, char const* ep, int base = 10)
206 -> decltype(fromchars128_impl(p, ep, base, std::is_signed<X>()))
208 return fromchars128_impl(p, ep, base, std::is_signed<X>());
211 #endif
213 static TEST_CONSTEXPR_CXX23 auto fromchars_impl(char const* p, char const* ep, int base = 10)
214 -> decltype(fromchars_impl(p, ep, base, std::is_signed<X>()))
216 return fromchars_impl(p, ep, base, std::is_signed<X>());
219 char buf[150];
222 template <typename X>
223 struct roundtrip_test_base
225 template <typename T, typename... Ts>
226 TEST_CONSTEXPR_CXX23 void test(T v, Ts... args)
228 std::from_chars_result r2;
229 std::to_chars_result r;
230 X x = 0xc;
232 if (fits_in<X>(v))
234 r = std::to_chars(buf, buf + sizeof(buf), v, args...);
235 assert(r.ec == std::errc{});
237 r2 = std::from_chars(buf, r.ptr, x, args...);
238 assert(r2.ptr == r.ptr);
239 assert(x == X(v));
241 else
243 r = std::to_chars(buf, buf + sizeof(buf), v, args...);
244 assert(r.ec == std::errc{});
246 r2 = std::from_chars(buf, r.ptr, x, args...);
248 TEST_DIAGNOSTIC_PUSH
249 TEST_MSVC_DIAGNOSTIC_IGNORED(4127) // conditional expression is constant
251 if (std::is_signed<T>::value && v < 0 && std::is_unsigned<X>::value)
253 assert(x == 0xc);
254 assert(r2.ptr == buf);
255 assert(r2.ec == std::errc::invalid_argument);
257 else
259 assert(x == 0xc);
260 assert(r2.ptr == r.ptr);
261 assert(r2.ec == std::errc::result_out_of_range);
264 TEST_DIAGNOSTIC_POP
268 private:
269 char buf[150];
272 template <typename... T>
273 struct type_list
277 template <typename L1, typename L2>
278 struct type_concat;
280 template <typename... Xs, typename... Ys>
281 struct type_concat<type_list<Xs...>, type_list<Ys...>>
283 using type = type_list<Xs..., Ys...>;
286 template <typename L1, typename L2>
287 using concat_t = typename type_concat<L1, L2>::type;
289 template <typename L1, typename L2>
290 constexpr auto concat(L1, L2) -> concat_t<L1, L2>
292 return {};
295 auto all_signed = type_list<
296 char,
297 signed char,
298 short,
299 int,
300 long,
301 long long
302 #ifndef TEST_HAS_NO_INT128
304 __int128_t
305 #endif
306 >();
307 auto all_unsigned = type_list<
308 unsigned char,
309 unsigned short,
310 unsigned int,
311 unsigned long,
312 unsigned long long
313 #ifndef TEST_HAS_NO_INT128
315 __uint128_t
316 #endif
317 >();
318 auto integrals = concat(all_signed, all_unsigned);
320 auto all_floats = type_list< float, double >(); //TODO: Add long double
322 template <template <typename> class Fn, typename... Ts>
323 TEST_CONSTEXPR_CXX23 void
324 run(type_list<Ts...>)
326 int ls[sizeof...(Ts)] = {(Fn<Ts>{}(), 0)...};
327 (void)ls;
330 #endif // SUPPORT_CHARCONV_TEST_HELPERS_H