Skip to content

Commit

Permalink
core: Add tests for no float charconv
Browse files Browse the repository at this point in the history
  • Loading branch information
rHermes committed May 6, 2024
1 parent 80f0dea commit e8ff9f1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion include/hage/core/charconv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ from_chars(const std::span<const char> 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::floating_point FloatType>
std::from_chars_result
from_chars(const char* first,
Expand Down
10 changes: 10 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
14 changes: 14 additions & 0 deletions src/compile_tests/float_charconv.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Created by rhermes on 2024-05-06.
//
#include <charconv>
#include <string>

int
main()
{
std::string s = "123.3";
float x;
std::from_chars(s.data(), s.data() + s.size(), x);
return 0;
}
9 changes: 2 additions & 7 deletions tests/core_charconv_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#include <doctest/doctest.h>

#if __has_include(<charconv>)

#include <hage/core/charconv.hpp>
#include <string>

Expand All @@ -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")
{
Expand All @@ -49,6 +46,4 @@ TEST_CASE("Charconv floats overloads")
}
}
#endif
TEST_SUITE_END();

#endif
TEST_SUITE_END();

0 comments on commit e8ff9f1

Please sign in to comment.