-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
226 lines (185 loc) · 8.46 KB
/
test.cpp
File metadata and controls
226 lines (185 loc) · 8.46 KB
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
#include <random>
#include <string_view>
#include <system_error>
#include "charconv_ext/charconv_ext.hpp"
namespace charconv_ext {
namespace {
#ifdef CHARCONV_EXT_128_BIT_IMPLEMENTATION
static_assert(detail::u64_max_representable_digits_naive(2) == 64);
static_assert(detail::u64_max_representable_digits(2) == 64);
static_assert(detail::u64_max_representable_digits_naive(8) == 21);
static_assert(detail::u64_max_representable_digits(8) == 21);
static_assert(detail::u64_max_representable_digits_naive(10) == 19);
static_assert(detail::u64_max_representable_digits(10) == 19);
static_assert(detail::u64_max_representable_digits_naive(16) == 16);
static_assert(detail::u64_max_representable_digits(16) == 16);
static_assert(detail::u64_max_power(2) == 0);
static_assert(detail::u64_max_power(8) == 0x8000000000000000ull);
static_assert(detail::u64_max_power(10) == 10000000000000000000ull);
static_assert(detail::u64_max_power(16) == 0);
#endif
template <typename T>
struct test_case {
T value;
std::string_view str;
int base;
};
constexpr auto u128_test = (uint128_t(1) << 100) / 10;
constexpr auto u128_max = uint128_t(-1);
constexpr auto i128_min = int128_t(1) << 127;
// clang-format off
static constexpr test_case<uint128_t> test_cases_u128[] {
{ 0, "0", 2 },
{ 0, "0", 5 },
{ 0, "0", 8 },
{ 0, "0", 10 },
{ 0, "0", 16 },
{ 0, "0", 32 },
{ 255, "11111111", 2 },
{ 255, "2010", 5 },
{ 255, "377", 8 },
{ 255, "255", 10 },
{ 255, "ff", 16 },
{ 255, "7v", 32 },
{ u128_test, "1100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001", 2 },
{ u128_test, "234321103241341010413041402403011100224122", 5 },
{ u128_test, "146314631463146314631463146314631", 8 },
{ u128_test, "126765060022822940149670320537", 10 },
{ u128_test, "1999999999999999999999999", 16 },
{ u128_test, "36cpj6cpj6cpj6cpj6cp", 32 },
{ u128_max, "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", 2 },
{ u128_max, "11031110441201303134210404233413032443021130230130231310", 5 },
{ u128_max, "3777777777777777777777777777777777777777777", 8 },
{ u128_max, "340282366920938463463374607431768211455", 10 },
{ u128_max, "ffffffffffffffffffffffffffffffff", 16 },
{ u128_max, "7vvvvvvvvvvvvvvvvvvvvvvvvv", 32 },
};
static constexpr test_case<int128_t> test_cases_i128[] {
{ 0, "0", 2 },
{ 0, "0", 5 },
{ 0, "0", 8 },
{ 0, "0", 10 },
{ 0, "0", 16 },
{ 0, "0", 32 },
{ 255, "11111111", 2 },
{ 255, "2010", 5 },
{ 255, "377", 8 },
{ 255, "255", 10 },
{ 255, "ff", 16 },
{ 255, "7v", 32 },
{ u128_test, "1100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001", 2 },
{ u128_test, "234321103241341010413041402403011100224122", 5 },
{ u128_test, "146314631463146314631463146314631", 8 },
{ u128_test, "126765060022822940149670320537", 10 },
{ u128_test, "1999999999999999999999999", 16 },
{ u128_test, "36cpj6cpj6cpj6cpj6cp", 32 },
{ i128_min, "-10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 2 },
{ i128_min, "-3013030220323124042102424341431241221233040112312340403", 5 },
{ i128_min, "-2000000000000000000000000000000000000000000", 8 },
{ i128_min, "-170141183460469231731687303715884105728", 10 },
{ i128_min, "-80000000000000000000000000000000", 16 },
{ i128_min, "-40000000000000000000000000", 32 },
};
// clang-format on
void run_manual_tests()
{
char buffer[1024];
for (const auto& test : test_cases_u128) {
const auto [p, ec] = to_chars(buffer, std::end(buffer), test.value, test.base);
assert(ec == std::errc {});
assert(std::size_t(p - buffer) == test.str.size());
const std::string_view result = { buffer, p };
assert(std::ranges::equal(result, test.str));
uint128_t value;
const auto [from_p, from_ec] = from_chars(buffer, p, value, test.base);
assert(from_ec == std::errc {});
assert(p == from_p);
assert(value == test.value);
}
for (const auto& test : test_cases_i128) {
const auto [p, ec] = to_chars(buffer, std::end(buffer), test.value, test.base);
assert(ec == std::errc {});
assert(std::size_t(p - buffer) == test.str.size());
const std::string_view result = { buffer, p };
assert(std::ranges::equal(result, test.str));
int128_t value;
const auto [from_p, from_ec] = from_chars(buffer, p, value, test.base);
assert(from_ec == std::errc {});
assert(p == from_p);
assert(value == test.value);
}
}
void run_fuzz_tests()
{
constexpr int iterations = 1'000'000;
char buffer[1024];
std::default_random_engine rng { 12345 };
std::uniform_int_distribution<int> base_distr { 2, 36 };
std::uniform_int_distribution<uint64_t> u64_distr;
for (int i = 0; i < iterations; ++i) {
const int base = base_distr(rng);
const auto lo = u64_distr(rng);
const auto hi = u64_distr(rng);
const auto u128 = (uint128_t(hi) << 64) | lo;
const auto i128 = int128_t(u128);
{
const auto [p1, ec1] = to_chars(buffer, std::end(buffer), u128, base);
assert(ec1 == std::errc {});
*p1 = 0;
uint128_t u128_parsed;
const auto [p2, ec2] = from_chars(buffer, std::end(buffer), u128_parsed, base);
assert(ec2 == std::errc {});
assert(p1 == p2);
assert(u128 == u128_parsed);
}
{
const auto [p1, ec1] = to_chars(buffer, std::end(buffer), i128, base);
assert(ec1 == std::errc {});
*p1 = 0;
int128_t i128_parsed;
const auto [p2, ec2] = from_chars(buffer, std::end(buffer), i128_parsed, base);
assert(ec2 == std::errc {});
assert(p1 == p2);
assert(i128 == i128_parsed);
}
}
}
} // namespace
#ifdef CHARCONV_EXT_BITINT_MAXWIDTH
template std::to_chars_result to_chars(char*, char*, bit_int<2>, int);
template std::to_chars_result to_chars(char*, char*, bit_int<8>, int);
template std::to_chars_result to_chars(char*, char*, bit_int<16>, int);
template std::to_chars_result to_chars(char*, char*, bit_int<32>, int);
template std::to_chars_result to_chars(char*, char*, bit_int<64>, int);
template std::to_chars_result to_chars(char*, char*, bit_uint<1>, int);
template std::to_chars_result to_chars(char*, char*, bit_uint<8>, int);
template std::to_chars_result to_chars(char*, char*, bit_uint<16>, int);
template std::to_chars_result to_chars(char*, char*, bit_uint<32>, int);
template std::to_chars_result to_chars(char*, char*, bit_uint<64>, int);
template std::from_chars_result from_chars(const char*, const char*, bit_int<2>&, int);
template std::from_chars_result from_chars(const char*, const char*, bit_int<8>&, int);
template std::from_chars_result from_chars(const char*, const char*, bit_int<16>&, int);
template std::from_chars_result from_chars(const char*, const char*, bit_int<32>&, int);
template std::from_chars_result from_chars(const char*, const char*, bit_int<64>&, int);
template std::from_chars_result from_chars(const char*, const char*, bit_uint<1>&, int);
template std::from_chars_result from_chars(const char*, const char*, bit_uint<8>&, int);
template std::from_chars_result from_chars(const char*, const char*, bit_uint<16>&, int);
template std::from_chars_result from_chars(const char*, const char*, bit_uint<32>&, int);
template std::from_chars_result from_chars(const char*, const char*, bit_uint<64>&, int);
#if CHARCONV_EXT_BITINT_MAXWIDTH >= 128
template std::to_chars_result to_chars(char*, char*, bit_int<100>, int);
template std::to_chars_result to_chars(char*, char*, bit_int<128>, int);
template std::to_chars_result to_chars(char*, char*, bit_uint<100>, int);
template std::to_chars_result to_chars(char*, char*, bit_uint<128>, int);
template std::from_chars_result from_chars(const char*, const char*, bit_int<100>&, int);
template std::from_chars_result from_chars(const char*, const char*, bit_int<128>&, int);
template std::from_chars_result from_chars(const char*, const char*, bit_uint<100>&, int);
template std::from_chars_result from_chars(const char*, const char*, bit_uint<128>&, int);
#endif
#endif
} // namespace charconv_ext
int main()
{
charconv_ext::run_manual_tests();
charconv_ext::run_fuzz_tests();
}