Skip to content

Commit

Permalink
type convertions
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Bać committed Jul 22, 2024
1 parent 7debc01 commit 5fbc575
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 166 deletions.
24 changes: 24 additions & 0 deletions cmake/CPM.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# SPDX-License-Identifier: MIT
#
# SPDX-FileCopyrightText: Copyright (c) 2019-2023 Lars Melchior and contributors

set(CPM_DOWNLOAD_VERSION 0.40.0)
set(CPM_HASH_SUM "7b354f3a5976c4626c876850c93944e52c83ec59a159ae5de5be7983f0e17a2a")

if(CPM_SOURCE_CACHE)
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
else()
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
endif()

# Expand relative path. This is important if the provided path contains a tilde (~)
get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE)

file(DOWNLOAD
https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
${CPM_DOWNLOAD_LOCATION} EXPECTED_HASH SHA256=${CPM_HASH_SUM}
)

include(${CPM_DOWNLOAD_LOCATION})
2 changes: 1 addition & 1 deletion unit_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ endfunction()
# endfunction()

# add_fixedmath_ut( type_traits )
add_fixedmath_ut( integral_type_convertions_ut )
add_fixedmath_ut( type_convertions_ut )
# add_compile_test( floating_point_type_convertions )
# add_compile_test( fixed_construction )
# add_compile_test( addition )
Expand Down
56 changes: 36 additions & 20 deletions unit_tests/include/unit_test_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ constexpr auto
return test_result{expression};
}
}

template<typename T, std::equality_comparable_with<T> U>
constexpr auto expect_eq( T const & left, U const & right, source_location const location = source_location::current()) -> test_result
constexpr auto
expect_eq(T const & left, U const & right, source_location const location = source_location::current()) -> test_result
{
if(std::is_constant_evaluated())
{
Expand All @@ -78,40 +80,53 @@ constexpr auto expect_eq( T const & left, U const & right, source_location const
}
else
{
boost::ut::expect( boost::ut::eq(left,right), location);
boost::ut::expect(boost::ut::eq(left, right), location);
return test_result{left == right};
}
}

template<typename T, std::equality_comparable_with<T> U>
constexpr auto expect_neq( T const & left, U const & right, source_location const location = source_location::current()) -> test_result
constexpr auto expect_neq(T const & left, U const & right, source_location const location = source_location::current())
-> test_result
{
if(std::is_constant_evaluated())
{
if( left == right)
if(left == right)
throw;
return test_result{left != right};
}
else
{
boost::ut::expect( boost::ut::neq(left,right), location);
boost::ut::expect(boost::ut::neq(left, right), location);
return test_result{left != right};
}
}

// constexpr auto expect_approx( T const & left, U const & right, source_location const location = source_location::current()) -> test_result
// {
// if(std::is_constant_evaluated())
// {
// if( left == right)
// throw;
// return test_result{left != right};
// }
// else
// {
// boost::ut::expect( boost::ut::neq(left,right), location);
// return test_result{left != right};
// }
// }
template<typename T, typename U, typename X>
requires requires(T const & t, U const & u, X const & x) {
{ t - u } -> std::three_way_comparable_with<X>;
}
constexpr auto expect_approx(
T const & left, U const & right, X const & approx , source_location const location = source_location::current()
) -> test_result
{
if(std::is_constant_evaluated())
{
using sub_res_type = decltype(left - right);
sub_res_type diff{left - right};
if(diff < sub_res_type(0))
diff = -diff;
if(diff >= approx)
throw;
return test_result{diff < approx};
}
else
{
boost::ut::expect(boost::ut::approx(left, right, approx), location);
return test_result{boost::ut::approx(left, right, approx)};
}
}

template<typename except, typename test_type>
test_result require_throw(test_type const & test) noexcept
{
Expand Down Expand Up @@ -146,7 +161,8 @@ test_result require_throw(test_type const & test) noexcept

template<typename unit_test>
[[maybe_unused]]
constexpr test_result run_constexpr_test(unit_test const & test, source_location const location = source_location::current())
constexpr test_result
run_constexpr_test(unit_test const & test, source_location const location = source_location::current())
{
return expect(test(), location);
}
Expand Down
145 changes: 0 additions & 145 deletions unit_tests/integral_type_convertions_ut.cc

This file was deleted.

Loading

0 comments on commit 5fbc575

Please sign in to comment.