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