Skip to content

Commit 4dcbd30

Browse files
author
Daniel Lemire
committed
More robust tests for C++23
1 parent eb584f7 commit 4dcbd30

3 files changed

Lines changed: 36 additions & 15 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
cmake_minimum_required(VERSION 3.9)
22

33
project(fast_float VERSION 6.0.0 LANGUAGES CXX)
4+
set(FASTFLOAT_CXX_STANDARD 11 CACHE STRING "the C++ standard to use for fastfloat")
5+
set(CMAKE_CXX_STANDARD ${FASTFLOAT_CXX_STANDARD})
46
option(FASTFLOAT_TEST "Enable tests" OFF)
57
if(FASTFLOAT_TEST)
68
enable_testing()

tests/CMakeLists.txt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ fast_float_add_cpp_test(rcppfastfloat_test)
6666
fast_float_add_cpp_test(example_test)
6767
fast_float_add_cpp_test(example_comma_test)
6868
fast_float_add_cpp_test(basictest)
69-
option(FASTFLOAT_CONSTEXPR_TESTS "Require constexpr tests (build will fail if the compiler won't support it)" OFF)
69+
if(CMAKE_CXX_STANDARD GREATER_EQUAL 20)
70+
option(FASTFLOAT_CONSTEXPR_TESTS "Require constexpr tests (build will fail if the compiler won't support it)" ON)
71+
else()
72+
option(FASTFLOAT_CONSTEXPR_TESTS "Require constexpr tests (build will fail if the compiler won't support it)" OFF)
73+
endif()
7074
if (FASTFLOAT_CONSTEXPR_TESTS)
7175
target_compile_features(basictest PRIVATE cxx_std_20)
7276
target_compile_definitions(basictest PRIVATE FASTFLOAT_CONSTEXPR_TESTS)
@@ -84,11 +88,14 @@ fast_float_add_cpp_test(fast_int)
8488
target_compile_features(fast_int PRIVATE cxx_std_17)
8589
fast_float_add_cpp_test(json_fmt)
8690
fast_float_add_cpp_test(fortran)
87-
88-
option(FASTFLOAT_FIXEDWIDTH_TESTS "Require fixed width test for C++23 (build will fail if the compiler won't support it)" OFF)
91+
if(CMAKE_CXX_STANDARD GREATER_EQUAL 23)
92+
option(FASTFLOAT_FIXEDWIDTH_TESTS "Require fixed width test for C++23 (build will fail if the compiler won't support it)" ON)
93+
else()
94+
option(FASTFLOAT_FIXEDWIDTH_TESTS "Require fixed width test for C++23 (build will fail if the compiler won't support it)" OFF)
95+
endif()
8996
if (FASTFLOAT_FIXEDWIDTH_TESTS)
9097
fast_float_add_cpp_test(fixedwidthtest)
91-
target_compile_features(fixedwidthtest PRIVATE cxx_std_23)
98+
target_compile_features(fixedwidthtest PUBLIC cxx_std_23)
9299
endif()
93100

94101
option(FASTFLOAT_EXHAUSTIVE "Exhaustive tests" OFF)

tests/fixedwidthtest.cpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,58 @@
11
#include <cstdlib>
22
#include <iostream>
33
#include <vector>
4-
#include <iomanip>
54
#include <cstring>
65
#include "fast_float/fast_float.h"
76
#include <cstdint>
87
#include <stdfloat>
98

10-
int main()
11-
{
9+
int main() {
1210
// Write some testcases for the parsing of floating point numbers in the float32_t type.
1311
// We use the from_chars function defined in this library.
14-
12+
#if __STDCPP_FLOAT32_T__
1513
const std::vector<std::float32_t> float32_test_expected{123.456f, -78.9f, 0.0001f, 3.40282e+038f};
16-
const std::vector<std::string> float32_test{"123.456", "-78.9", "0.0001", "3.40282e+038"};
14+
const std::vector<std::string_view> float32_test{"123.456", "-78.9", "0.0001", "3.40282e+038"};
1715

1816
for (std::size_t i = 0; i < float32_test.size(); ++i) {
1917
const auto& f = float32_test[i];
2018
std::float32_t result;
2119
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
2220

23-
if (answer.ec != std::errc() || result != float32_test_expected[i]) {
24-
std::cerr << "Test failed for input: " << std::quoted(f) << std::endl;
21+
if (answer.ec != std::errc()) {
22+
std::cerr << "Failed to parse: \"" << f << "\"" << std::endl;
23+
return EXIT_FAILURE;
24+
}
25+
if(result != float32_test_expected[i]) {
26+
std::cerr << "Test failed for input: \"" << f << "\" expected " << float32_test_expected[i] << " got " << result << std::endl;
2527
return EXIT_FAILURE;
2628
}
2729
}
30+
#else
31+
std::cout << "No std::float32_t type available." << std::endl;
32+
#endif
2833

34+
#if __STDCPP_FLOAT64_T__
2935
// Test cases for std::float64_t
3036
const std::vector<std::float64_t> float64_test_expected{1.23e4, -5.67e-8, 1.7976931348623157e+308, -1.7976931348623157e+308};
31-
const std::vector<std::string> float64_test{"1.23e4", "-5.67e-8", "1.7976931348623157e+308", "-1.7976931348623157e+308"};
37+
const std::vector<std::string_view> float64_test{"1.23e4", "-5.67e-8", "1.7976931348623157e+308", "-1.7976931348623157e+308"};
3238

3339
for (std::size_t i = 0; i < float64_test.size(); ++i) {
3440
const auto& f = float64_test[i];
3541
std::float64_t result;
3642
auto answer = fast_float::from_chars(f.data(), f.data() + f.size(), result);
3743

38-
if (answer.ec != std::errc() || result != float64_test_expected[i]) {
39-
std::cerr << "Test failed for input: " << std::quoted(f) << std::endl;
44+
if (answer.ec != std::errc()) {
45+
std::cerr << "Failed to parse: \"" << f << "\"" << std::endl;
46+
return EXIT_FAILURE;
47+
}
48+
if(result != float32_test_expected[i]) {
49+
std::cerr << "Test failed for input: \"" << f << "\" expected " << float32_test_expected[i] << " got " << result << std::endl;
4050
return EXIT_FAILURE;
4151
}
4252
}
43-
53+
#else
54+
std::cout << "No std::float64_t type available." << std::endl;
55+
#endif
4456
std::cout << "All tests passed successfully." << std::endl;
4557
return EXIT_SUCCESS;
4658

0 commit comments

Comments
 (0)