Skip to content

Commit 6b72e26

Browse files
committed
documenting better which types we support
1 parent f8c573d commit 6b72e26

File tree

2 files changed

+197
-2
lines changed

2 files changed

+197
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ requires C++11):
1818
from_chars_result from_chars(char const *first, char const *last, float &value, ...);
1919
from_chars_result from_chars(char const *first, char const *last, double &value, ...);
2020
```
21+
If they are available on your system, we also support fixed-width floating-point types such as `std::float64_t`, `std::float32_t`, `std::float16_t`, and `std::bfloat16_t`.
2122
22-
You can also parse integer types:
23-
23+
You can also parse integer types such as `char`, `short`, `long`, `long long`, `unsigned char`, `unsigned short`, `unsigned long`, `unsigned long long`, `bool` (0/1), `int8_t`, `int16_t`, `int32_t`, `int64_t`, `uint8_t`, `uint16_t`, `uint32_t`, `uint64_t`.
2424
```C++
2525
from_chars_result from_chars(char const *first, char const *last, int &value, ...);
2626
from_chars_result from_chars(char const *first, char const *last, unsigned &value, ...);

tests/fast_int.cpp

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,201 @@ int main() {
9595
}
9696
}
9797

98+
// char basic test
99+
std::vector<char> const char_basic_test_expected{0, 10, 40, 100, 9};
100+
std::vector<std::string_view> const char_basic_test{"0", "10 ", "40",
101+
"100 with text", "9.999"};
102+
103+
for (std::size_t i = 0; i < char_basic_test.size(); ++i) {
104+
auto const f = char_basic_test[i];
105+
char result;
106+
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
107+
108+
if (answer.ec != std::errc()) {
109+
std::cerr << "could not convert to char for input: \"" << f
110+
<< "\" because of invalid argument" << std::endl;
111+
return EXIT_FAILURE;
112+
} else if (result != char_basic_test_expected[i]) {
113+
std::cerr << "result \"" << f << "\" did not match with expected char: "
114+
<< static_cast<int>(char_basic_test_expected[i]) << std::endl;
115+
return EXIT_FAILURE;
116+
}
117+
}
118+
119+
// short basic test
120+
std::vector<short> const short_basic_test_expected{0, 10, -40, 1001, 9};
121+
std::vector<std::string_view> const short_basic_test{
122+
"0", "10 ", "-40", "1001 with text", "9.999"};
123+
124+
for (std::size_t i = 0; i < short_basic_test.size(); ++i) {
125+
auto const f = short_basic_test[i];
126+
short result;
127+
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
128+
129+
if (answer.ec != std::errc()) {
130+
std::cerr << "could not convert to short for input: \"" << f
131+
<< "\" because of invalid argument" << std::endl;
132+
return EXIT_FAILURE;
133+
} else if (result != short_basic_test_expected[i]) {
134+
std::cerr << "result \"" << f << "\" did not match with expected short: "
135+
<< short_basic_test_expected[i] << std::endl;
136+
return EXIT_FAILURE;
137+
}
138+
}
139+
140+
// long basic test
141+
std::vector<long> const long_basic_test_expected{0, 10, -40, 1001, 9};
142+
std::vector<std::string_view> const long_basic_test{
143+
"0", "10 ", "-40", "1001 with text", "9.999"};
144+
145+
for (std::size_t i = 0; i < long_basic_test.size(); ++i) {
146+
auto const f = long_basic_test[i];
147+
long result;
148+
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
149+
150+
if (answer.ec != std::errc()) {
151+
std::cerr << "could not convert to long for input: \"" << f
152+
<< "\" because of invalid argument" << std::endl;
153+
return EXIT_FAILURE;
154+
} else if (result != long_basic_test_expected[i]) {
155+
std::cerr << "result \"" << f << "\" did not match with expected long: "
156+
<< long_basic_test_expected[i] << std::endl;
157+
return EXIT_FAILURE;
158+
}
159+
}
160+
161+
// long long basic test
162+
std::vector<long long> const long_long_basic_test_expected{0, 10, -40, 1001,
163+
9};
164+
std::vector<std::string_view> const long_long_basic_test{
165+
"0", "10 ", "-40", "1001 with text", "9.999"};
166+
167+
for (std::size_t i = 0; i < long_long_basic_test.size(); ++i) {
168+
auto const f = long_long_basic_test[i];
169+
long long result;
170+
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
171+
172+
if (answer.ec != std::errc()) {
173+
std::cerr << "could not convert to long long for input: \"" << f
174+
<< "\" because of invalid argument" << std::endl;
175+
return EXIT_FAILURE;
176+
} else if (result != long_long_basic_test_expected[i]) {
177+
std::cerr << "result \"" << f
178+
<< "\" did not match with expected long long: "
179+
<< long_long_basic_test_expected[i] << std::endl;
180+
return EXIT_FAILURE;
181+
}
182+
}
183+
184+
// unsigned char basic test
185+
std::vector<unsigned char> const unsigned_char_basic_test_expected{0, 10, 100,
186+
9};
187+
std::vector<std::string_view> const unsigned_char_basic_test{
188+
"0", "10 ", "100 with text", "9.999"};
189+
190+
for (std::size_t i = 0; i < unsigned_char_basic_test.size(); ++i) {
191+
auto const &f = unsigned_char_basic_test[i];
192+
unsigned char result;
193+
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
194+
if (answer.ec != std::errc()) {
195+
std::cerr << "could not convert to unsigned char for input: \"" << f
196+
<< "\"" << std::endl;
197+
return EXIT_FAILURE;
198+
} else if (result != unsigned_char_basic_test_expected[i]) {
199+
std::cerr << "result \"" << f
200+
<< "\" did not match with expected unsigned char: "
201+
<< static_cast<int>(unsigned_char_basic_test_expected[i])
202+
<< std::endl;
203+
return EXIT_FAILURE;
204+
}
205+
}
206+
207+
// unsigned short basic test
208+
std::vector<unsigned short> const unsigned_short_basic_test_expected{0, 10,
209+
1001, 9};
210+
std::vector<std::string_view> const unsigned_short_basic_test{
211+
"0", "10 ", "1001 with text", "9.999"};
212+
213+
for (std::size_t i = 0; i < unsigned_short_basic_test.size(); ++i) {
214+
auto const &f = unsigned_short_basic_test[i];
215+
unsigned short result;
216+
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
217+
if (answer.ec != std::errc()) {
218+
std::cerr << "could not convert to unsigned short for input: \"" << f
219+
<< "\"" << std::endl;
220+
return EXIT_FAILURE;
221+
} else if (result != unsigned_short_basic_test_expected[i]) {
222+
std::cerr << "result \"" << f
223+
<< "\" did not match with expected unsigned short: "
224+
<< unsigned_short_basic_test_expected[i] << std::endl;
225+
return EXIT_FAILURE;
226+
}
227+
}
228+
229+
// unsigned long basic test
230+
std::vector<unsigned long> const unsigned_long_basic_test_expected{0, 10,
231+
1001, 9};
232+
std::vector<std::string_view> const unsigned_long_basic_test{
233+
"0", "10 ", "1001 with text", "9.999"};
234+
235+
for (std::size_t i = 0; i < unsigned_long_basic_test.size(); ++i) {
236+
auto const &f = unsigned_long_basic_test[i];
237+
unsigned long result;
238+
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
239+
if (answer.ec != std::errc()) {
240+
std::cerr << "could not convert to unsigned long for input: \"" << f
241+
<< "\"" << std::endl;
242+
return EXIT_FAILURE;
243+
} else if (result != unsigned_long_basic_test_expected[i]) {
244+
std::cerr << "result \"" << f
245+
<< "\" did not match with expected unsigned long: "
246+
<< unsigned_long_basic_test_expected[i] << std::endl;
247+
return EXIT_FAILURE;
248+
}
249+
}
250+
251+
// unsigned long long basic test
252+
std::vector<unsigned long long> const unsigned_long_long_basic_test_expected{
253+
0, 10, 1001, 9};
254+
std::vector<std::string_view> const unsigned_long_long_basic_test{
255+
"0", "10 ", "1001 with text", "9.999"};
256+
257+
for (std::size_t i = 0; i < unsigned_long_long_basic_test.size(); ++i) {
258+
auto const &f = unsigned_long_long_basic_test[i];
259+
unsigned long long result;
260+
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
261+
if (answer.ec != std::errc()) {
262+
std::cerr << "could not convert to unsigned long long for input: \"" << f
263+
<< "\"" << std::endl;
264+
return EXIT_FAILURE;
265+
} else if (result != unsigned_long_long_basic_test_expected[i]) {
266+
std::cerr << "result \"" << f
267+
<< "\" did not match with expected unsigned long long: "
268+
<< unsigned_long_long_basic_test_expected[i] << std::endl;
269+
return EXIT_FAILURE;
270+
}
271+
}
272+
273+
// bool basic test
274+
std::vector<bool> const bool_basic_test_expected{false, true};
275+
std::vector<std::string_view> const bool_basic_test{"0", "1"};
276+
277+
for (std::size_t i = 0; i < bool_basic_test.size(); ++i) {
278+
auto const &f = bool_basic_test[i];
279+
bool result;
280+
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
281+
if (answer.ec != std::errc()) {
282+
std::cerr << "could not convert to bool for input: \"" << f << "\""
283+
<< std::endl;
284+
return EXIT_FAILURE;
285+
} else if (result != bool_basic_test_expected[i]) {
286+
std::cerr << "result \"" << f << "\" did not match with expected bool: "
287+
<< (bool_basic_test_expected[i] ? "true" : "false")
288+
<< std::endl;
289+
return EXIT_FAILURE;
290+
}
291+
}
292+
98293
// int invalid error test
99294
std::vector<std::string_view> const int_invalid_argument_test{
100295
"text", "text with 1002", "+50", " 50"};

0 commit comments

Comments
 (0)