Zhihao Yuan | d274896 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame^] | 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 |
Zhihao Yuan | d274896 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef SUPPORT_CHARCONV_TEST_HELPERS_H |
| 10 | #define SUPPORT_CHARCONV_TEST_HELPERS_H |
| 11 | |
| 12 | #include <charconv> |
| 13 | #include <cassert> |
| 14 | #include <limits> |
| 15 | #include <string.h> |
| 16 | #include <stdlib.h> |
| 17 | |
| 18 | #include "test_macros.h" |
| 19 | |
| 20 | #if TEST_STD_VER <= 11 |
| 21 | #error This file requires C++14 |
| 22 | #endif |
| 23 | |
| 24 | using std::false_type; |
| 25 | using std::true_type; |
| 26 | |
| 27 | template <typename To, typename From> |
| 28 | constexpr auto |
| 29 | is_non_narrowing(From a) -> decltype(To{a}, true_type()) |
| 30 | { |
| 31 | return {}; |
| 32 | } |
| 33 | |
| 34 | template <typename To> |
| 35 | constexpr auto |
| 36 | is_non_narrowing(...) -> false_type |
| 37 | { |
| 38 | return {}; |
| 39 | } |
| 40 | |
| 41 | template <typename X, typename T> |
| 42 | constexpr bool |
| 43 | _fits_in(T, true_type /* non-narrowing*/, ...) |
| 44 | { |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | template <typename X, typename T, typename xl = std::numeric_limits<X>> |
| 49 | constexpr bool |
| 50 | _fits_in(T v, false_type, true_type /* T signed*/, true_type /* X signed */) |
| 51 | { |
| 52 | return xl::lowest() <= v && v <= (xl::max)(); |
| 53 | } |
| 54 | |
| 55 | template <typename X, typename T, typename xl = std::numeric_limits<X>> |
| 56 | constexpr bool |
| 57 | _fits_in(T v, false_type, true_type /* T signed */, false_type /* X unsigned*/) |
| 58 | { |
| 59 | return 0 <= v && std::make_unsigned_t<T>(v) <= (xl::max)(); |
| 60 | } |
| 61 | |
| 62 | template <typename X, typename T, typename xl = std::numeric_limits<X>> |
| 63 | constexpr bool |
| 64 | _fits_in(T v, false_type, false_type /* T unsigned */, ...) |
| 65 | { |
| 66 | return v <= std::make_unsigned_t<X>((xl::max)()); |
| 67 | } |
| 68 | |
| 69 | template <typename X, typename T> |
| 70 | constexpr bool |
| 71 | fits_in(T v) |
| 72 | { |
| 73 | return _fits_in<X>(v, is_non_narrowing<X>(v), std::is_signed<T>(), |
| 74 | std::is_signed<X>()); |
| 75 | } |
| 76 | |
| 77 | template <typename X> |
| 78 | struct to_chars_test_base |
| 79 | { |
| 80 | template <typename T, size_t N, typename... Ts> |
| 81 | void test(T v, char const (&expect)[N], Ts... args) |
| 82 | { |
| 83 | using std::to_chars; |
| 84 | std::to_chars_result r; |
| 85 | |
| 86 | constexpr size_t len = N - 1; |
| 87 | static_assert(len > 0, "expected output won't be empty"); |
| 88 | |
| 89 | if (!fits_in<X>(v)) |
| 90 | return; |
| 91 | |
| 92 | r = to_chars(buf, buf + len - 1, X(v), args...); |
| 93 | assert(r.ptr == buf + len - 1); |
| 94 | assert(r.ec == std::errc::value_too_large); |
| 95 | |
| 96 | r = to_chars(buf, buf + sizeof(buf), X(v), args...); |
| 97 | assert(r.ptr == buf + len); |
| 98 | assert(r.ec == std::errc{}); |
| 99 | assert(memcmp(buf, expect, len) == 0); |
| 100 | } |
| 101 | |
| 102 | template <typename... Ts> |
| 103 | void test_value(X v, Ts... args) |
| 104 | { |
| 105 | using std::to_chars; |
| 106 | std::to_chars_result r; |
| 107 | |
| 108 | r = to_chars(buf, buf + sizeof(buf), v, args...); |
| 109 | assert(r.ec == std::errc{}); |
| 110 | *r.ptr = '\0'; |
| 111 | |
| 112 | auto a = fromchars(buf, r.ptr, args...); |
| 113 | assert(v == a); |
| 114 | |
| 115 | auto ep = r.ptr - 1; |
| 116 | r = to_chars(buf, ep, v, args...); |
| 117 | assert(r.ptr == ep); |
| 118 | assert(r.ec == std::errc::value_too_large); |
| 119 | } |
| 120 | |
| 121 | private: |
| 122 | static auto fromchars(char const* p, char const* ep, int base, true_type) |
| 123 | { |
| 124 | char* last; |
| 125 | auto r = strtoll(p, &last, base); |
| 126 | assert(last == ep); |
| 127 | |
| 128 | return r; |
| 129 | } |
| 130 | |
| 131 | static auto fromchars(char const* p, char const* ep, int base, false_type) |
| 132 | { |
| 133 | char* last; |
| 134 | auto r = strtoull(p, &last, base); |
| 135 | assert(last == ep); |
| 136 | |
| 137 | return r; |
| 138 | } |
| 139 | |
| 140 | static auto fromchars(char const* p, char const* ep, int base = 10) |
| 141 | { |
| 142 | return fromchars(p, ep, base, std::is_signed<X>()); |
| 143 | } |
| 144 | |
| 145 | char buf[100]; |
| 146 | }; |
| 147 | |
| 148 | template <typename X> |
| 149 | struct roundtrip_test_base |
| 150 | { |
| 151 | template <typename T, typename... Ts> |
| 152 | void test(T v, Ts... args) |
| 153 | { |
| 154 | using std::from_chars; |
| 155 | using std::to_chars; |
| 156 | std::from_chars_result r2; |
| 157 | std::to_chars_result r; |
| 158 | X x = 0xc; |
| 159 | |
| 160 | if (fits_in<X>(v)) |
| 161 | { |
| 162 | r = to_chars(buf, buf + sizeof(buf), v, args...); |
| 163 | assert(r.ec == std::errc{}); |
| 164 | |
| 165 | r2 = from_chars(buf, r.ptr, x, args...); |
| 166 | assert(r2.ptr == r.ptr); |
| 167 | assert(x == X(v)); |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | r = to_chars(buf, buf + sizeof(buf), v, args...); |
| 172 | assert(r.ec == std::errc{}); |
| 173 | |
| 174 | r2 = from_chars(buf, r.ptr, x, args...); |
| 175 | |
| 176 | if (std::is_signed<T>::value && v < 0 && std::is_unsigned<X>::value) |
| 177 | { |
| 178 | assert(x == 0xc); |
| 179 | assert(r2.ptr == buf); |
Zhihao Yuan | 63ebd3b | 2018-08-01 05:21:26 +0000 | [diff] [blame] | 180 | assert(r2.ec == std::errc::invalid_argument); |
Zhihao Yuan | d274896 | 2018-08-01 02:38:30 +0000 | [diff] [blame] | 181 | } |
| 182 | else |
| 183 | { |
| 184 | assert(x == 0xc); |
| 185 | assert(r2.ptr == r.ptr); |
| 186 | assert(r2.ec == std::errc::result_out_of_range); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | private: |
| 192 | char buf[100]; |
| 193 | }; |
| 194 | |
| 195 | template <typename... T> |
| 196 | struct type_list |
| 197 | { |
| 198 | }; |
| 199 | |
| 200 | template <typename L1, typename L2> |
| 201 | struct type_concat; |
| 202 | |
| 203 | template <typename... Xs, typename... Ys> |
| 204 | struct type_concat<type_list<Xs...>, type_list<Ys...>> |
| 205 | { |
| 206 | using type = type_list<Xs..., Ys...>; |
| 207 | }; |
| 208 | |
| 209 | template <typename L1, typename L2> |
| 210 | using concat_t = typename type_concat<L1, L2>::type; |
| 211 | |
| 212 | template <typename L1, typename L2> |
| 213 | constexpr auto concat(L1, L2) -> concat_t<L1, L2> |
| 214 | { |
| 215 | return {}; |
| 216 | } |
| 217 | |
| 218 | auto all_signed = type_list<char, signed char, short, int, long, long long>(); |
| 219 | auto all_unsigned = type_list<unsigned char, unsigned short, unsigned int, |
| 220 | unsigned long, unsigned long long>(); |
| 221 | auto integrals = concat(all_signed, all_unsigned); |
| 222 | |
| 223 | template <template <typename> class Fn, typename... Ts> |
| 224 | void |
| 225 | run(type_list<Ts...>) |
| 226 | { |
| 227 | int ls[sizeof...(Ts)] = {(Fn<Ts>{}(), 0)...}; |
| 228 | (void)ls; |
| 229 | } |
| 230 | |
| 231 | #endif // SUPPORT_CHARCONV_TEST_HELPERS_H |