|
| 1 | +#include <cmath> |
| 2 | +#include <concepts> |
| 3 | +#include <gtest/gtest.h> |
| 4 | +#include <limits> |
| 5 | +#include <type_traits> |
| 6 | + |
| 7 | +import mcpplibs.primitives.algorithms; |
| 8 | +import mcpplibs.primitives.underlying; |
| 9 | + |
| 10 | +#include "../../support/underlying_custom_types.hpp" |
| 11 | + |
| 12 | +using namespace mcpplibs::primitives; |
| 13 | +using namespace mcpplibs::primitives::test_support::underlying; |
| 14 | + |
| 15 | +TEST(AlgorithmLimitsTest, BuiltinIntegerLimitsFollowNumericLimitsShape) { |
| 16 | + using limits_t = algorithms::limits<int>; |
| 17 | + |
| 18 | + static_assert(limits_t::enabled); |
| 19 | + static_assert(limits_t::is_specialized); |
| 20 | + static_assert(std::same_as<typename limits_t::rep_type, int>); |
| 21 | + static_assert(limits_t::kind == underlying::category::integer); |
| 22 | + static_assert(limits_t::digits == std::numeric_limits<int>::digits); |
| 23 | + static_assert(limits_t::radix == std::numeric_limits<int>::radix); |
| 24 | + static_assert(algorithms::limited_type<int>); |
| 25 | + |
| 26 | + EXPECT_EQ(limits_t::lowest(), std::numeric_limits<int>::lowest()); |
| 27 | + EXPECT_EQ(limits_t::max(), std::numeric_limits<int>::max()); |
| 28 | + EXPECT_EQ(algorithms::min_value<int>(), std::numeric_limits<int>::min()); |
| 29 | +} |
| 30 | + |
| 31 | +TEST(AlgorithmLimitsTest, BuiltinFloatingLimitsExposeSpecialValues) { |
| 32 | + using limits_t = algorithms::limits<double>; |
| 33 | + |
| 34 | + static_assert(limits_t::enabled); |
| 35 | + static_assert(limits_t::has_quiet_nan); |
| 36 | + static_assert(limits_t::has_infinity); |
| 37 | + static_assert(limits_t::kind == underlying::category::floating); |
| 38 | + static_assert(limits_t::is_iec559); |
| 39 | + |
| 40 | + EXPECT_EQ(algorithms::epsilon_value<double>(), |
| 41 | + std::numeric_limits<double>::epsilon()); |
| 42 | + EXPECT_EQ(algorithms::infinity_value<double>(), |
| 43 | + std::numeric_limits<double>::infinity()); |
| 44 | + EXPECT_TRUE(std::isnan(algorithms::quiet_nan_value<double>())); |
| 45 | +} |
| 46 | + |
| 47 | +TEST(AlgorithmLimitsTest, CustomUnderlyingUsesRepBackedLimits) { |
| 48 | + using limits_t = algorithms::limits<UserInteger>; |
| 49 | + |
| 50 | + static_assert(limits_t::enabled); |
| 51 | + static_assert(std::same_as<typename limits_t::rep_type, int>); |
| 52 | + static_assert(limits_t::kind == underlying::category::integer); |
| 53 | + |
| 54 | + EXPECT_EQ(underlying::traits<UserInteger>::to_rep(limits_t::lowest()), |
| 55 | + std::numeric_limits<int>::lowest()); |
| 56 | + EXPECT_EQ(underlying::traits<UserInteger>::to_rep(limits_t::max()), |
| 57 | + std::numeric_limits<int>::max()); |
| 58 | +} |
| 59 | + |
| 60 | +TEST(AlgorithmLimitsTest, SelfRepresentedCustomRepNeedsExplicitLimitsSpecialization) { |
| 61 | + static_assert(!algorithms::limits<BigIntLike>::enabled); |
| 62 | + SUCCEED(); |
| 63 | +} |
0 commit comments