diff --git a/README.md b/README.md index 9a44e5d..b039e63 100644 --- a/README.md +++ b/README.md @@ -18,9 +18,9 @@ requires C++11): from_chars_result from_chars(char const *first, char const *last, float &value, ...); from_chars_result from_chars(char const *first, char const *last, double &value, ...); ``` +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`. -You can also parse integer types: - +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`. ```C++ from_chars_result from_chars(char const *first, char const *last, int &value, ...); from_chars_result from_chars(char const *first, char const *last, unsigned &value, ...); diff --git a/tests/fast_int.cpp b/tests/fast_int.cpp index 49044d3..94e76fd 100644 --- a/tests/fast_int.cpp +++ b/tests/fast_int.cpp @@ -95,6 +95,201 @@ int main() { } } + // char basic test + std::vector const char_basic_test_expected{0, 10, 40, 100, 9}; + std::vector const char_basic_test{"0", "10 ", "40", + "100 with text", "9.999"}; + + for (std::size_t i = 0; i < char_basic_test.size(); ++i) { + auto const f = char_basic_test[i]; + char result; + auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result); + + if (answer.ec != std::errc()) { + std::cerr << "could not convert to char for input: \"" << f + << "\" because of invalid argument" << std::endl; + return EXIT_FAILURE; + } else if (result != char_basic_test_expected[i]) { + std::cerr << "result \"" << f << "\" did not match with expected char: " + << static_cast(char_basic_test_expected[i]) << std::endl; + return EXIT_FAILURE; + } + } + + // short basic test + std::vector const short_basic_test_expected{0, 10, -40, 1001, 9}; + std::vector const short_basic_test{ + "0", "10 ", "-40", "1001 with text", "9.999"}; + + for (std::size_t i = 0; i < short_basic_test.size(); ++i) { + auto const f = short_basic_test[i]; + short result; + auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result); + + if (answer.ec != std::errc()) { + std::cerr << "could not convert to short for input: \"" << f + << "\" because of invalid argument" << std::endl; + return EXIT_FAILURE; + } else if (result != short_basic_test_expected[i]) { + std::cerr << "result \"" << f << "\" did not match with expected short: " + << short_basic_test_expected[i] << std::endl; + return EXIT_FAILURE; + } + } + + // long basic test + std::vector const long_basic_test_expected{0, 10, -40, 1001, 9}; + std::vector const long_basic_test{ + "0", "10 ", "-40", "1001 with text", "9.999"}; + + for (std::size_t i = 0; i < long_basic_test.size(); ++i) { + auto const f = long_basic_test[i]; + long result; + auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result); + + if (answer.ec != std::errc()) { + std::cerr << "could not convert to long for input: \"" << f + << "\" because of invalid argument" << std::endl; + return EXIT_FAILURE; + } else if (result != long_basic_test_expected[i]) { + std::cerr << "result \"" << f << "\" did not match with expected long: " + << long_basic_test_expected[i] << std::endl; + return EXIT_FAILURE; + } + } + + // long long basic test + std::vector const long_long_basic_test_expected{0, 10, -40, 1001, + 9}; + std::vector const long_long_basic_test{ + "0", "10 ", "-40", "1001 with text", "9.999"}; + + for (std::size_t i = 0; i < long_long_basic_test.size(); ++i) { + auto const f = long_long_basic_test[i]; + long long result; + auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result); + + if (answer.ec != std::errc()) { + std::cerr << "could not convert to long long for input: \"" << f + << "\" because of invalid argument" << std::endl; + return EXIT_FAILURE; + } else if (result != long_long_basic_test_expected[i]) { + std::cerr << "result \"" << f + << "\" did not match with expected long long: " + << long_long_basic_test_expected[i] << std::endl; + return EXIT_FAILURE; + } + } + + // unsigned char basic test + std::vector const unsigned_char_basic_test_expected{0, 10, 100, + 9}; + std::vector const unsigned_char_basic_test{ + "0", "10 ", "100 with text", "9.999"}; + + for (std::size_t i = 0; i < unsigned_char_basic_test.size(); ++i) { + auto const &f = unsigned_char_basic_test[i]; + unsigned char result; + auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result); + if (answer.ec != std::errc()) { + std::cerr << "could not convert to unsigned char for input: \"" << f + << "\"" << std::endl; + return EXIT_FAILURE; + } else if (result != unsigned_char_basic_test_expected[i]) { + std::cerr << "result \"" << f + << "\" did not match with expected unsigned char: " + << static_cast(unsigned_char_basic_test_expected[i]) + << std::endl; + return EXIT_FAILURE; + } + } + + // unsigned short basic test + std::vector const unsigned_short_basic_test_expected{0, 10, + 1001, 9}; + std::vector const unsigned_short_basic_test{ + "0", "10 ", "1001 with text", "9.999"}; + + for (std::size_t i = 0; i < unsigned_short_basic_test.size(); ++i) { + auto const &f = unsigned_short_basic_test[i]; + unsigned short result; + auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result); + if (answer.ec != std::errc()) { + std::cerr << "could not convert to unsigned short for input: \"" << f + << "\"" << std::endl; + return EXIT_FAILURE; + } else if (result != unsigned_short_basic_test_expected[i]) { + std::cerr << "result \"" << f + << "\" did not match with expected unsigned short: " + << unsigned_short_basic_test_expected[i] << std::endl; + return EXIT_FAILURE; + } + } + + // unsigned long basic test + std::vector const unsigned_long_basic_test_expected{0, 10, + 1001, 9}; + std::vector const unsigned_long_basic_test{ + "0", "10 ", "1001 with text", "9.999"}; + + for (std::size_t i = 0; i < unsigned_long_basic_test.size(); ++i) { + auto const &f = unsigned_long_basic_test[i]; + unsigned long result; + auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result); + if (answer.ec != std::errc()) { + std::cerr << "could not convert to unsigned long for input: \"" << f + << "\"" << std::endl; + return EXIT_FAILURE; + } else if (result != unsigned_long_basic_test_expected[i]) { + std::cerr << "result \"" << f + << "\" did not match with expected unsigned long: " + << unsigned_long_basic_test_expected[i] << std::endl; + return EXIT_FAILURE; + } + } + + // unsigned long long basic test + std::vector const unsigned_long_long_basic_test_expected{ + 0, 10, 1001, 9}; + std::vector const unsigned_long_long_basic_test{ + "0", "10 ", "1001 with text", "9.999"}; + + for (std::size_t i = 0; i < unsigned_long_long_basic_test.size(); ++i) { + auto const &f = unsigned_long_long_basic_test[i]; + unsigned long long result; + auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result); + if (answer.ec != std::errc()) { + std::cerr << "could not convert to unsigned long long for input: \"" << f + << "\"" << std::endl; + return EXIT_FAILURE; + } else if (result != unsigned_long_long_basic_test_expected[i]) { + std::cerr << "result \"" << f + << "\" did not match with expected unsigned long long: " + << unsigned_long_long_basic_test_expected[i] << std::endl; + return EXIT_FAILURE; + } + } + + // bool basic test + std::vector const bool_basic_test_expected{false, true}; + std::vector const bool_basic_test{"0", "1"}; + + for (std::size_t i = 0; i < bool_basic_test.size(); ++i) { + auto const &f = bool_basic_test[i]; + bool result; + auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result); + if (answer.ec != std::errc()) { + std::cerr << "could not convert to bool for input: \"" << f << "\"" + << std::endl; + return EXIT_FAILURE; + } else if (result != bool_basic_test_expected[i]) { + std::cerr << "result \"" << f << "\" did not match with expected bool: " + << (bool_basic_test_expected[i] ? "true" : "false") + << std::endl; + return EXIT_FAILURE; + } + } + // int invalid error test std::vector const int_invalid_argument_test{ "text", "text with 1002", "+50", " 50"};