Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Lemire committed Jan 28, 2024
1 parent 12ece3c commit 797e3e0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 54 deletions.
6 changes: 1 addition & 5 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ fast_float_add_cpp_test(rcppfastfloat_test)
fast_float_add_cpp_test(example_test)
fast_float_add_cpp_test(example_comma_test)
fast_float_add_cpp_test(basictest)
if(CMAKE_CXX_STANDARD GREATER_EQUAL 20)
option(FASTFLOAT_CONSTEXPR_TESTS "Require constexpr tests (build will fail if the compiler won't support it)" ON)
else()
option(FASTFLOAT_CONSTEXPR_TESTS "Require constexpr tests (build will fail if the compiler won't support it)" OFF)
endif()
option(FASTFLOAT_CONSTEXPR_TESTS "Require constexpr tests (build will fail if the compiler won't support it)" OFF)
if (FASTFLOAT_CONSTEXPR_TESTS)
target_compile_features(basictest PRIVATE cxx_std_20)
target_compile_definitions(basictest PRIVATE FASTFLOAT_CONSTEXPR_TESTS)
Expand Down
93 changes: 44 additions & 49 deletions tests/fast_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
#include "fast_float/fast_float.h"
#include <cstdint>

template <class T>
std::string quoted(T& s) {
return "\""+std::string(s)+"\"";
}

/*
all tests conducted are to check fast_float::from_chars functionality with int and unsigned
test cases include:
Expand All @@ -54,24 +49,24 @@ int main() {
const std::vector<std::string_view> int_basic_test { "0", "10 ", "-40", "1001 with text", "9.999" };

for (std::size_t i = 0; i < int_basic_test.size(); ++i) {
const auto& f = int_basic_test[i];
const auto f = int_basic_test[i];
int result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);

if (answer.ec != std::errc()) {
if (answer.ec == std::errc::invalid_argument) {
std::cerr << "could not convert to int for input: " << quoted(f) << " because of invalid arguement" << std::endl;
std::cerr << "could not convert to int for input: \"" << f << "\" because of invalid argument" << std::endl;
}
else if (answer.ec == std::errc::result_out_of_range) {
std::cerr << "could not convert to int for input: " << quoted(f) << " because it's out of range" << std::endl;
std::cerr << "could not convert to int for input: \"" << f << "\" because it's out of range" << std::endl;
}
else {
std::cerr << "could not convert to int for input: " << quoted(f) << " because of an unknown error" << std::endl;
std::cerr << "could not convert to int for input: \"" << f << "\" because of an unknown error" << std::endl;
}
return EXIT_FAILURE;
}
else if (result != int_basic_test_expected[i]) {
std::cerr << "result " << quoted(f) << " did not match with expected int: " << int_basic_test_expected[i] << std::endl;
std::cerr << "result \"" << f << "\" did not match with expected int: " << int_basic_test_expected[i] << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -85,11 +80,11 @@ int main() {
unsigned 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 for input: " << quoted(f) << std::endl;
std::cerr << "could not convert to unsigned for input: \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
else if (result != unsigned_basic_test_expected[i]) {
std::cerr << "result " << quoted(f) << " did not match with expected unsigned: " << unsigned_basic_test_expected[i] << std::endl;
std::cerr << "result \"" << f << "\" did not match with expected unsigned: " << unsigned_basic_test_expected[i] << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -102,7 +97,7 @@ int main() {
int result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
if (answer.ec != std::errc::invalid_argument) {
std::cerr << "expected error should be 'invalid_argument' for: " << quoted(f) << std::endl;
std::cerr << "expected error should be 'invalid_argument' for: \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -115,7 +110,7 @@ int main() {
unsigned result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
if (answer.ec != std::errc::invalid_argument) {
std::cerr << "expected error should be 'invalid_argument' for: " << quoted(f) << std::endl;
std::cerr << "expected error should be 'invalid_argument' for: \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -128,7 +123,7 @@ int main() {
int8_t result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
if (answer.ec != std::errc::result_out_of_range) {
std::cerr << "expected error for should be 'result_out_of_range': " << quoted(f) << std::endl;
std::cerr << "expected error for should be 'result_out_of_range': \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -141,7 +136,7 @@ int main() {
int16_t result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
if (answer.ec != std::errc::result_out_of_range) {
std::cerr << "expected error for should be 'result_out_of_range': " << quoted(f) << std::endl;
std::cerr << "expected error for should be 'result_out_of_range': \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -154,7 +149,7 @@ int main() {
int32_t result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
if (answer.ec != std::errc::result_out_of_range) {
std::cerr << "expected error for should be 'result_out_of_range': " << quoted(f) << std::endl;
std::cerr << "expected error for should be 'result_out_of_range': \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -167,7 +162,7 @@ int main() {
int64_t result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
if (answer.ec != std::errc::result_out_of_range) {
std::cerr << "expected error for should be 'result_out_of_range': " << quoted(f) << std::endl;
std::cerr << "expected error for should be 'result_out_of_range': \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -180,7 +175,7 @@ int main() {
uint8_t result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
if (answer.ec != std::errc::result_out_of_range) {
std::cerr << "expected error for should be 'result_out_of_range': " << quoted(f) << std::endl;
std::cerr << "expected error for should be 'result_out_of_range': \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -193,7 +188,7 @@ int main() {
uint16_t result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
if (answer.ec != std::errc::result_out_of_range) {
std::cerr << "expected error for should be 'result_out_of_range': " << quoted(f) << std::endl;
std::cerr << "expected error for should be 'result_out_of_range': \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -206,7 +201,7 @@ int main() {
uint32_t result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
if (answer.ec != std::errc::result_out_of_range) {
std::cerr << "expected error for should be 'result_out_of_range': " << quoted(f) << std::endl;
std::cerr << "expected error for should be 'result_out_of_range': \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -219,7 +214,7 @@ int main() {
uint64_t result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
if (answer.ec != std::errc::result_out_of_range) {
std::cerr << "expected error for should be 'result_out_of_range': " << quoted(f) << std::endl;
std::cerr << "expected error for should be 'result_out_of_range': \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -232,11 +227,11 @@ int main() {
int 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 int for input: " << quoted(f) << std::endl;
std::cerr << "could not convert to int for input: \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
else if (strcmp(answer.ptr, "") != 0) {
std::cerr << "ptr of result " << quoted(f) << " did not match with expected ptr: " << quoted("") << std::endl;
std::cerr << "ptr of result " << f << " did not match with expected ptr: \"\"" << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -248,7 +243,7 @@ int main() {
int result2;
auto answer2 = fast_float::from_chars(f2.data(), f2.data() + f2.size(), result2);
if (strcmp(answer2.ptr, " with text") != 0) {
std::cerr << "ptr of result " << quoted(f2) << " did not match with expected ptr: " << quoted(" with text") << std::endl;
std::cerr << "ptr of result " << f2 << " did not match with expected ptr: \"with text\"" << std::endl;
return EXIT_FAILURE;
}

Expand All @@ -259,7 +254,7 @@ int main() {
int result3;
auto answer3 = fast_float::from_chars(f3.data(), f3.data() + f3.size(), result3);
if (strcmp(answer3.ptr, " with text\n") != 0) {
std::cerr << "ptr of result " << quoted(f3) << " did not match with expected ptr: " << quoted(" with text") << std::endl;
std::cerr << "ptr of result " << f3 << " did not match with expected ptr: with text" << std::endl;
return EXIT_FAILURE;
}

Expand All @@ -270,7 +265,7 @@ int main() {
int result4;
auto answer4 = fast_float::from_chars(f4.data(), f4.data() + f4.size(), result4);
if (strcmp(answer4.ptr, ".999") != 0) {
std::cerr << "ptr of result " << quoted(f4) << " did not match with expected ptr: " << quoted(".999") << std::endl;
std::cerr << "ptr of result " << f4 << " did not match with expected ptr: .999" << std::endl;
return EXIT_FAILURE;
}

Expand All @@ -281,7 +276,7 @@ int main() {
int result5;
auto answer5 = fast_float::from_chars(f5.data(), f5.data() + f5.size(), result5);
if (strcmp(answer5.ptr, "+50") != 0) {
std::cerr << "ptr of result " << quoted(f5) << " did not match with expected ptr: " << quoted("+50") << std::endl;
std::cerr << "ptr of result " << f5 << " did not match with expected ptr: +50" << std::endl;
return EXIT_FAILURE;
}

Expand All @@ -292,7 +287,7 @@ int main() {
unsigned result6;
auto answer6 = fast_float::from_chars(f6.data(), f6.data() + f6.size(), result6);
if (strcmp(answer6.ptr, " with text") != 0) {
std::cerr << "ptr of result " << quoted(f6) << " did not match with expected ptr: " << quoted(" with text") << std::endl;
std::cerr << "ptr of result " << f6 << " did not match with expected ptr: with text" << std::endl;
return EXIT_FAILURE;
}

Expand All @@ -303,7 +298,7 @@ int main() {
unsigned result7;
auto answer7 = fast_float::from_chars(f7.data(), f7.data() + f7.size(), result7);
if (strcmp(answer7.ptr, "-50") != 0) {
std::cerr << "ptr of result " << quoted(f7) << " did not match with expected ptr: " << quoted("-50") << std::endl;
std::cerr << "ptr of result " << f7 << " did not match with expected ptr: -50" << std::endl;
return EXIT_FAILURE;
}

Expand All @@ -312,15 +307,15 @@ int main() {
const std::vector<std::string_view> int_base_2_test { "0", "1", "100", "010", "-1" };

for (std::size_t i = 0; i < int_base_2_test.size(); ++i) {
const auto& f = int_base_2_test[i];
const auto f = int_base_2_test[i];
int result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result, 2);
if (answer.ec != std::errc()) {
std::cerr << "could not convert to int for input: " << quoted(f) << std::endl;
std::cerr << "could not convert to int for input: \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
else if (result != int_base_2_test_expected[i]) {
std::cerr << "result " << quoted(f) << " did not match with expected int: " << int_base_2_test_expected[i] << std::endl;
std::cerr << "result " << f << " did not match with expected int: " << int_base_2_test_expected[i] << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -334,11 +329,11 @@ int main() {
unsigned result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result, 2);
if (answer.ec != std::errc()) {
std::cerr << "could not convert to unsigned for input: " << quoted(f) << std::endl;
std::cerr << "could not convert to unsigned for input: \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
else if (result != unsigned_base_2_test_expected[i]) {
std::cerr << "result " << quoted(f) << " did not match with expected unsigned: " << unsigned_base_2_test_expected[i] << std::endl;
std::cerr << "result " << f << " did not match with expected unsigned: " << unsigned_base_2_test_expected[i] << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -351,7 +346,7 @@ int main() {
int result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result, 2);
if (answer.ec != std::errc::invalid_argument) {
std::cerr << "expected error should be 'invalid_argument' for: " << quoted(f) << std::endl;
std::cerr << "expected error should be 'invalid_argument' for: \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -364,7 +359,7 @@ int main() {
unsigned result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result, 2);
if (answer.ec != std::errc::invalid_argument) {
std::cerr << "expected error should be 'invalid_argument' for: " << quoted(f) << std::endl;
std::cerr << "expected error should be 'invalid_argument' for: \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -378,11 +373,11 @@ int main() {
int result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result, 8);
if (answer.ec != std::errc()) {
std::cerr << "could not convert to int for input: " << quoted(f) << std::endl;
std::cerr << "could not convert to int for input: \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
else if (result != base_octal_test_expected[i]) {
std::cerr << "result " << quoted(f) << " did not match with expected int: " << base_octal_test_expected[i] << std::endl;
std::cerr << "result " << f << " did not match with expected int: " << base_octal_test_expected[i] << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -396,11 +391,11 @@ int main() {
int result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result, 16);
if (answer.ec != std::errc()) {
std::cerr << "could not convert to int for input: " << quoted(f) << std::endl;
std::cerr << "could not convert to int for input: \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
else if (result != base_hex_test_expected[i]) {
std::cerr << "result " << quoted(f) << " did not match with expected int: " << base_hex_test_expected[i] << std::endl;
std::cerr << "result " << f << " did not match with expected int: " << base_hex_test_expected[i] << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -413,7 +408,7 @@ int main() {
int result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result, -1);
if (answer.ec != std::errc::invalid_argument) {
std::cerr << "expected error should be 'invalid_argument' for: " << quoted(f) << std::endl;
std::cerr << "expected error should be 'invalid_argument' for: \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -426,7 +421,7 @@ int main() {
int result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result, 37);
if (answer.ec != std::errc::invalid_argument) {
std::cerr << "expected error should be 'invalid_argument' for: " << quoted(f) << std::endl;
std::cerr << "expected error should be 'invalid_argument' for: \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
}
Expand Down Expand Up @@ -508,7 +503,7 @@ int main() {
int64_t result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result, int(2 + (i / 2)));
if (answer.ec != std::errc::result_out_of_range) {
std::cerr << "expected error for should be 'result_out_of_range': " << quoted(f) << std::endl;
std::cerr << "expected error for should be 'result_out_of_range': \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
}
Expand Down Expand Up @@ -555,7 +550,7 @@ int main() {
uint64_t result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result, base_unsigned);
if (answer.ec != std::errc::result_out_of_range) {
std::cerr << "expected error for should be 'result_out_of_range': " << quoted(f) << std::endl;
std::cerr << "expected error for should be 'result_out_of_range': \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
++base_unsigned;
Expand Down Expand Up @@ -638,7 +633,7 @@ int main() {
int64_t result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result, int(2 + (i / 2)));
if (answer.ec != std::errc()) {
std::cerr << "converting " << quoted(f) << " to int failed (most likely out of range)" << std::endl;
std::cerr << "converting " << f << " to int failed (most likely out of range)" << std::endl;
return EXIT_FAILURE;
}
}
Expand Down Expand Up @@ -685,7 +680,7 @@ int main() {
uint64_t result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result, base_unsigned2);
if (answer.ec != std::errc()) {
std::cerr << "converting " << quoted(f) << " to unsigned failed (most likely out of range)" << std::endl;
std::cerr << "converting " << f << " to unsigned failed (most likely out of range)" << std::endl;
return EXIT_FAILURE;
}
++base_unsigned2;
Expand Down Expand Up @@ -733,11 +728,11 @@ int main() {
int result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result, int(i + 2));
if (answer.ec != std::errc()) {
std::cerr << "could not convert to int for input: " << quoted(f) << std::endl;
std::cerr << "could not convert to int for input: \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
else if (result != 1015) {
std::cerr << "result " << quoted(f) << " did not match with expected int: " << 1015 << std::endl;
std::cerr << "result " << f << " did not match with expected int: " << 1015 << std::endl;
return EXIT_FAILURE;
}
}
Expand Down

0 comments on commit 797e3e0

Please sign in to comment.