From e8ff9f1a431eec3bb0911c052d069d494079f137 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teodor=20Sp=C3=A6ren?= Date: Mon, 6 May 2024 14:19:15 +0200 Subject: [PATCH] core: Add tests for no float charconv --- CMakeLists.txt | 2 +- include/hage/core/charconv.hpp | 2 +- src/CMakeLists.txt | 10 ++++++++++ src/compile_tests/float_charconv.cpp | 14 ++++++++++++++ tests/core_charconv_tests.cpp | 9 ++------- 5 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 src/compile_tests/float_charconv.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b174cb5..83f1997 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ # Based on: https://gitlab.com/CLIUtils/modern-cmake -cmake_minimum_required(VERSION 3.22) +cmake_minimum_required(VERSION 3.25) project( hage VERSION 0.1 diff --git a/include/hage/core/charconv.hpp b/include/hage/core/charconv.hpp index be16196..01b6ada 100644 --- a/include/hage/core/charconv.hpp +++ b/include/hage/core/charconv.hpp @@ -22,7 +22,7 @@ from_chars(const std::span data, IntType& value, const int base = 10 return std::from_chars(std::to_address(data.begin()), std::to_address(data.end()), value, base); } -#if defined __cpp_lib_to_chars +#ifndef HAGE_NO_FLOAT_CHARCONV template std::from_chars_result from_chars(const char* first, diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 559b71a..da49fa3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -41,8 +41,18 @@ set(LOGGING_HEADER_LIST "${hage_SOURCE_DIR}/include/hage/logging/console_sink.hpp" ) + add_library(hage_core INTERFACE) +# some compilers don't support the float charconvs yet +try_compile(HAS_FLOAT_CHARCONV SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/compile_tests/float_charconv.cpp + CXX_STANDARD 20 CXX_EXTENSIONS OFF) + +if(NOT HAS_FLOAT_CHARCONV) + target_compile_definitions(hage_core INTERFACE HAGE_NO_FLOAT_CHARCONV=1) +endif() + + target_sources(hage_core INTERFACE ${CORE_HEADER_LIST}) target_include_directories(hage_core INTERFACE ../include) diff --git a/src/compile_tests/float_charconv.cpp b/src/compile_tests/float_charconv.cpp new file mode 100644 index 0000000..1eb56bf --- /dev/null +++ b/src/compile_tests/float_charconv.cpp @@ -0,0 +1,14 @@ +// +// Created by rhermes on 2024-05-06. +// +#include +#include + +int +main() +{ + std::string s = "123.3"; + float x; + std::from_chars(s.data(), s.data() + s.size(), x); + return 0; +} diff --git a/tests/core_charconv_tests.cpp b/tests/core_charconv_tests.cpp index 33a8e92..d089285 100644 --- a/tests/core_charconv_tests.cpp +++ b/tests/core_charconv_tests.cpp @@ -1,7 +1,4 @@ #include - -#if __has_include() - #include #include @@ -27,7 +24,7 @@ TEST_CASE("Charconv integer overloads") } } -#if defined __cpp_lib_to_chars +#ifndef HAGE_NO_FLOAT_CHARCONV TEST_CASE("Charconv floats overloads") { @@ -49,6 +46,4 @@ TEST_CASE("Charconv floats overloads") } } #endif -TEST_SUITE_END(); - -#endif \ No newline at end of file +TEST_SUITE_END(); \ No newline at end of file