More robust tests for C++23

This commit is contained in:
Daniel Lemire 2024-01-28 15:00:27 -05:00
parent eb584f748e
commit 4dcbd30d3f
3 changed files with 36 additions and 15 deletions

View File

@ -1,6 +1,8 @@
cmake_minimum_required(VERSION 3.9) cmake_minimum_required(VERSION 3.9)
project(fast_float VERSION 6.0.0 LANGUAGES CXX) project(fast_float VERSION 6.0.0 LANGUAGES CXX)
set(FASTFLOAT_CXX_STANDARD 11 CACHE STRING "the C++ standard to use for fastfloat")
set(CMAKE_CXX_STANDARD ${FASTFLOAT_CXX_STANDARD})
option(FASTFLOAT_TEST "Enable tests" OFF) option(FASTFLOAT_TEST "Enable tests" OFF)
if(FASTFLOAT_TEST) if(FASTFLOAT_TEST)
enable_testing() enable_testing()

View File

@ -66,7 +66,11 @@ fast_float_add_cpp_test(rcppfastfloat_test)
fast_float_add_cpp_test(example_test) fast_float_add_cpp_test(example_test)
fast_float_add_cpp_test(example_comma_test) fast_float_add_cpp_test(example_comma_test)
fast_float_add_cpp_test(basictest) 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) option(FASTFLOAT_CONSTEXPR_TESTS "Require constexpr tests (build will fail if the compiler won't support it)" OFF)
endif()
if (FASTFLOAT_CONSTEXPR_TESTS) if (FASTFLOAT_CONSTEXPR_TESTS)
target_compile_features(basictest PRIVATE cxx_std_20) target_compile_features(basictest PRIVATE cxx_std_20)
target_compile_definitions(basictest PRIVATE FASTFLOAT_CONSTEXPR_TESTS) target_compile_definitions(basictest PRIVATE FASTFLOAT_CONSTEXPR_TESTS)
@ -84,11 +88,14 @@ fast_float_add_cpp_test(fast_int)
target_compile_features(fast_int PRIVATE cxx_std_17) target_compile_features(fast_int PRIVATE cxx_std_17)
fast_float_add_cpp_test(json_fmt) fast_float_add_cpp_test(json_fmt)
fast_float_add_cpp_test(fortran) fast_float_add_cpp_test(fortran)
if(CMAKE_CXX_STANDARD GREATER_EQUAL 23)
option(FASTFLOAT_FIXEDWIDTH_TESTS "Require fixed width test for C++23 (build will fail if the compiler won't support it)" ON)
else()
option(FASTFLOAT_FIXEDWIDTH_TESTS "Require fixed width test for C++23 (build will fail if the compiler won't support it)" OFF) option(FASTFLOAT_FIXEDWIDTH_TESTS "Require fixed width test for C++23 (build will fail if the compiler won't support it)" OFF)
endif()
if (FASTFLOAT_FIXEDWIDTH_TESTS) if (FASTFLOAT_FIXEDWIDTH_TESTS)
fast_float_add_cpp_test(fixedwidthtest) fast_float_add_cpp_test(fixedwidthtest)
target_compile_features(fixedwidthtest PRIVATE cxx_std_23) target_compile_features(fixedwidthtest PUBLIC cxx_std_23)
endif() endif()
option(FASTFLOAT_EXHAUSTIVE "Exhaustive tests" OFF) option(FASTFLOAT_EXHAUSTIVE "Exhaustive tests" OFF)

View File

@ -1,46 +1,58 @@
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <iomanip>
#include <cstring> #include <cstring>
#include "fast_float/fast_float.h" #include "fast_float/fast_float.h"
#include <cstdint> #include <cstdint>
#include <stdfloat> #include <stdfloat>
int main() int main() {
{
// Write some testcases for the parsing of floating point numbers in the float32_t type. // Write some testcases for the parsing of floating point numbers in the float32_t type.
// We use the from_chars function defined in this library. // We use the from_chars function defined in this library.
#if __STDCPP_FLOAT32_T__
const std::vector<std::float32_t> float32_test_expected{123.456f, -78.9f, 0.0001f, 3.40282e+038f}; const std::vector<std::float32_t> float32_test_expected{123.456f, -78.9f, 0.0001f, 3.40282e+038f};
const std::vector<std::string> float32_test{"123.456", "-78.9", "0.0001", "3.40282e+038"}; const std::vector<std::string_view> float32_test{"123.456", "-78.9", "0.0001", "3.40282e+038"};
for (std::size_t i = 0; i < float32_test.size(); ++i) { for (std::size_t i = 0; i < float32_test.size(); ++i) {
const auto& f = float32_test[i]; const auto& f = float32_test[i];
std::float32_t result; std::float32_t result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result); auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
if (answer.ec != std::errc() || result != float32_test_expected[i]) { if (answer.ec != std::errc()) {
std::cerr << "Test failed for input: " << std::quoted(f) << std::endl; std::cerr << "Failed to parse: \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
if(result != float32_test_expected[i]) {
std::cerr << "Test failed for input: \"" << f << "\" expected " << float32_test_expected[i] << " got " << result << std::endl;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
} }
#else
std::cout << "No std::float32_t type available." << std::endl;
#endif
#if __STDCPP_FLOAT64_T__
// Test cases for std::float64_t // Test cases for std::float64_t
const std::vector<std::float64_t> float64_test_expected{1.23e4, -5.67e-8, 1.7976931348623157e+308, -1.7976931348623157e+308}; const std::vector<std::float64_t> float64_test_expected{1.23e4, -5.67e-8, 1.7976931348623157e+308, -1.7976931348623157e+308};
const std::vector<std::string> float64_test{"1.23e4", "-5.67e-8", "1.7976931348623157e+308", "-1.7976931348623157e+308"}; const std::vector<std::string_view> float64_test{"1.23e4", "-5.67e-8", "1.7976931348623157e+308", "-1.7976931348623157e+308"};
for (std::size_t i = 0; i < float64_test.size(); ++i) { for (std::size_t i = 0; i < float64_test.size(); ++i) {
const auto& f = float64_test[i]; const auto& f = float64_test[i];
std::float64_t result; std::float64_t result;
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result); auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
if (answer.ec != std::errc() || result != float64_test_expected[i]) { if (answer.ec != std::errc()) {
std::cerr << "Test failed for input: " << std::quoted(f) << std::endl; std::cerr << "Failed to parse: \"" << f << "\"" << std::endl;
return EXIT_FAILURE;
}
if(result != float32_test_expected[i]) {
std::cerr << "Test failed for input: \"" << f << "\" expected " << float32_test_expected[i] << " got " << result << std::endl;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
} }
#else
std::cout << "No std::float64_t type available." << std::endl;
#endif
std::cout << "All tests passed successfully." << std::endl; std::cout << "All tests passed successfully." << std::endl;
return EXIT_SUCCESS; return EXIT_SUCCESS;