Skip to content

Commit

Permalink
wip tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Bać committed Jul 22, 2024
1 parent 3c29a8f commit 80440ce
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 142 deletions.
19 changes: 14 additions & 5 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
{
"name": "cfg-common",
"hidden": true,
"binaryDir": "${sourceDir}/build/${presetName}",
"cacheVariables": { "FIXEDMATH_ENABLE_UNIT_TESTS": "ON" }
},
{
"name": "cfg-build-dir",
"hidden": true,
"binaryDir": "${sourceDir}/build/${presetName}"
},
{
"name": "cfg-ninja",
"hidden": true,
Expand All @@ -33,28 +37,33 @@
}
},
{
"name": "ci-clang-18-release",
"name": "clang-18-release",
"inherits": [ "cfg-common", "cfg-ninja", "cfg-c++23" ],
"cacheVariables": { "CMAKE_CXX_COMPILER" : "clang++-18" }
},
{
"name": "ci-clang-18-release",
"inherits": [ "cfg-common", "cfg-build-dir", "cfg-ninja", "cfg-c++23" ],
"cacheVariables": { "CMAKE_CXX_COMPILER" : "clang++-18" }
},
{
"name": "ci-clang-18-libc++-release",
"inherits": ["ci-clang-18-release", "cfg-libc++"]
},
{
"name": "ci-gcc-14-release",
"inherits": [ "cfg-common", "cfg-ninja", "cfg-c++23" ],
"inherits": [ "cfg-common", "cfg-build-dir", "cfg-ninja", "cfg-c++23" ],
"cacheVariables": {"CMAKE_CXX_COMPILER": "g++-14" }
},
{
"name": "ci-clangcl-release",
"inherits": "cfg-common",
"inherits": ["cfg-common", "cfg-build-dir"],
"generator": "Visual Studio 17 2022",
"toolset": "ClangCL"
},
{
"name": "ci-msvc-release",
"inherits": "cfg-common",
"inherits": ["cfg-common", "cfg-build-dir"],
"generator": "Visual Studio 17 2022"
}
],
Expand Down
1 change: 0 additions & 1 deletion fixed_lib/include/fixedmath/limits.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ namespace std
static constexpr bool has_infinity{};
static constexpr bool has_quiet_NaN{ true };
static constexpr bool has_signaling_NaN{};
static constexpr float_denorm_style has_denorm{denorm_absent};
static constexpr bool has_denorm_loss{};
static constexpr float_round_style round_style{round_toward_zero};
static constexpr bool is_iec559{};
Expand Down
24 changes: 4 additions & 20 deletions unit_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,11 @@ function( add_fixedmath_ut name )
add_test( NAME ${name}_test COMMAND ${name} )
endfunction()

# set( TEST_DEFINITIONS -DFIXEDMATH_ENABLE_SQRT_ABACUS_ALGO )
# function(add_compile_test HEADER_FILE_NAME )
# get_filename_component(TEST_NAME ${HEADER_FILE_NAME} NAME_WE)
# set( SOURCE_FILE_NAME test_${TEST_NAME}.cc )
#
# set( FIXED_MATH_INCLUDES ${PROJECT_SOURCE_DIR}/fixed_lib/include )
# file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${SOURCE_FILE_NAME} "#include <fixedmath/unittests/${HEADER_FILE_NAME}.h>\nint main( int argc, char ** argv ) {return fixedmath::${HEADER_FILE_NAME}_unit_tests() ? EXIT_SUCCESS : EXIT_FAILURE; }\n" )
#
# add_test(NAME ${TEST_NAME}_cxx23
# COMMAND ${CMAKE_CXX_COMPILER} -std=c++23 -I${FIXED_MATH_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR}/${SOURCE_FILE_NAME} -o ${CMAKE_CURRENT_BINARY_DIR}/test_${TEST_NAME}_cxx2b.o )
#
# if(FIXEDMATH_ENABLE_DEVEL_CODE)
# add_executable( ${TEST_NAME}_DEVEL ${CMAKE_CURRENT_BINARY_DIR}/${SOURCE_FILE_NAME})
# target_link_libraries( ${TEST_NAME}_DEVEL PRIVATE fixed_math )
# endif()
# endfunction()

# add_fixedmath_ut( type_traits )
add_fixedmath_ut( fixed_construction_ut )
add_fixedmath_ut( type_convertions_ut )
# add_compile_test( floating_point_type_convertions )
# add_compile_test( fixed_construction )
add_fixedmath_ut( addition_ut )


# add_compile_test( addition )
# add_compile_test( substraction )
# add_compile_test( multiplication )
Expand Down
51 changes: 51 additions & 0 deletions unit_tests/addition_ut.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <unit_test_core.h>
#include <unit_test_common.h>

using boost::ut::operator""_test;
using namespace metatests;
using namespace fixedmath;

int main()
{
test_result result;
using F = fixedmath::fixed_internal;
"addition"_test = [&result]
{
auto fn_tmpl = [] -> metatests::test_result
{
expect_eq(add(0_fix, 1_fix), 1_fix);
expect_eq(add(1_fix, 1_fix), 2_fix);
expect_eq(add(10_fix, 1_fix), 11_fix);
expect_eq(add(-10_fix, 1_fix), -9_fix);

expect_eq(0_fix + 1_fix, 1_fix);
expect_eq(1_fix + 1_fix, 2_fix);
expect_eq(10.4_fix + 1_fix, 11.4_fix);
expect_eq(-10_fix + 1_fix, -9_fix);

expect_eq(0_fix + 1, 1_fix);
expect_eq(1 + 1_fix, 2_fix);
expect_eq(10.4 + 1_fix, 11.4);
expect_eq(10.5_fix + 10.2, 20.7);
expect_eq(-10.5_fix + uint8_t(10), -0.5_fix);
expect_eq(-10.5_fix + uint16_t(10), -0.5_fix);
expect_eq(-10.5_fix + uint32_t(10), -0.5_fix);
expect_eq(-10.5_fix + uint64_t(10), -0.5_fix);
expect_eq(uint8_t(10) + -10.5_fix, -.5_fix);
expect_eq(uint16_t(10) + -10.5_fix, -.5_fix);
expect_eq(uint32_t(10) + -10.5_fix, -.5_fix);
expect_eq(uint64_t(10) + -10.5_fix, -.5_fix);
expect_eq(-10_fix + 1, -9_fix);
expect_eq(10.5_fix + -10., .5);
expect_eq(10.5_fix + -10.f, .5_fix);
expect_lt(limits_::max(), limits_::quiet_NaN());
expect_neq(limits_::lowest() + 1, limits_::quiet_NaN());

expect_neq(as_fixed(limits_::max().v - 65536) + 1_fix, limits_::quiet_NaN());
return {};
};
result |= run_constexpr_test(fn_tmpl);
result |= run_consteval_test(fn_tmpl);
};
}

38 changes: 38 additions & 0 deletions unit_tests/fixed_construction_ut.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <unit_test_core.h>
#include <unit_test_common.h>

using boost::ut::operator""_test;
using namespace metatests;
using F = fixedmath::fixed_internal;
using namespace fixedmath;

int main()
{
test_result result;
"fixed construction"_test = [&result]
{
auto fn_tmpl = [] -> metatests::test_result
{
expect_eq(fixed_t{1}, 1_fix);
expect_eq(fixed_t{1u}, 1_fix);
expect_eq(fixed_t{1lu}, 1_fix);
expect_eq(fixed_t{1llu}, 1_fix);
expect_eq(fixed_t{-1}, -1_fix);
expect_eq(fixed_t{1.f}, 1_fix);
expect_eq(fixed_t{-1.f}, -1_fix);
expect_eq(fixed_t{2}, 2_fix);
expect_eq(fixed_t{-2}, -2_fix);
expect_eq(fixed_t{2.f}, 2_fix);
expect_eq(fixed_t{-2.f}, -2_fix);
expect_eq(fixed_t{4.f}, 4_fix);
expect_eq(fixed_t{-4.f}, -4_fix);

expect_eq(fixed_t{4096ll}, 4096_fix);
expect_eq(fixed_t{-4096ll}, -4096_fix);
expect_eq(fixed_t{4096llu}, 4096_fix);
return {};
};
result |= run_constexpr_test(fn_tmpl);
result |= run_consteval_test(fn_tmpl);
};
}
25 changes: 0 additions & 25 deletions unit_tests/include/floating_point_type_convertions.h

This file was deleted.

43 changes: 0 additions & 43 deletions unit_tests/include/integral_type_convertions.h

This file was deleted.

46 changes: 25 additions & 21 deletions unit_tests/include/unit_test_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,32 @@
#include <fixedmath/iostream.h>

namespace fixedmath
{
using detail::limits_;
using detail::flimits_;
using detail::dlimits_;
{
using detail::dlimits_;
using detail::flimits_;
using detail::limits_;


#define make_const_value(x) const_val<decltype(x), x>
#define static_assert_equal(x, y) static_assert( assert_equal_t< make_const_value( x ), make_const_value( y ) >::eq() )
#define make_const_value(x) const_val<decltype(x), x>
#define static_assert_equal(x, y) static_assert(assert_equal_t<make_const_value(x), make_const_value(y)>::eq())

template<typename T, T val>
struct const_val {
static constexpr T value { val };
consteval const_val() = default;
consteval const_val(T) {}
template<typename T, T val>
struct const_val
{
static constexpr T value{val};
consteval const_val() = default;

consteval const_val(T) {}
};

template<typename expected, typename input>
consteval bool test_resulting_type( input ) { return std::is_same<expected,input>::value; }

template<concepts::arithmetic value_type>
consteval value_type ut_abs( value_type value )
{
return value > value_type{0} ? value : -value;
}
}
template<typename expected, typename input>
consteval bool test_resulting_type(input)
{
return std::is_same<expected, input>::value;
}

template<concepts::arithmetic value_type>
consteval value_type ut_abs(value_type value)
{
return value > value_type{0} ? value : -value;
}
} // namespace fixedmath
35 changes: 34 additions & 1 deletion unit_tests/include/unit_test_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,45 @@ constexpr auto expect_neq(T const & left, U const & right, source_location const
}
}

template<typename T, std::equality_comparable_with<T> U>
constexpr auto
expect_gt(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::gt(left, right), location);
return test_result{left > right};
}
}

template<typename T, std::equality_comparable_with<T> U>
constexpr auto
expect_lt(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::lt(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()
T const & left, U const & right, X const & approx, source_location const location = source_location::current()
) -> test_result
{
if(std::is_constant_evaluated())
Expand Down
Loading

0 comments on commit 80440ce

Please sign in to comment.