Skip to content

Commit

Permalink
Add more assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
kimwalisch committed Jun 18, 2024
1 parent a6c5348 commit f307dd8
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions include/min.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#define MIN_HPP

#include <int128_t.hpp>
#include <macros.hpp>

#include <algorithm>

namespace {
Expand Down Expand Up @@ -47,6 +49,11 @@ inline B min(A a, B b)
static_assert(is_comparable<A, B>::value,
"min(A, B): Cannot compare types A and B");

#if defined(ENABLE_ASSERT)
if (pstd::is_unsigned<A>::value && pstd::is_signed<B>::value) ASSERT(b >= 0);
if (pstd::is_unsigned<B>::value && pstd::is_signed<A>::value) ASSERT(b <= pstd::numeric_limits<A>::max());
#endif

return (B) std::min(a, (A) b);
}

Expand All @@ -56,6 +63,12 @@ inline C min3(A a, B b, C c)
static_assert(is_comparable_3<A, B, C>::value,
"min3(A, B, C): Cannot compare types A, B and C");

#if defined(ENABLE_ASSERT)
if (pstd::is_unsigned<B>::value && pstd::is_signed<C>::value) ASSERT(c >= 0);
if (pstd::is_unsigned<C>::value && pstd::is_signed<B>::value) ASSERT(c <= pstd::numeric_limits<B>::max());
if (pstd::is_unsigned<A>::value && pstd::is_signed<B>::value && pstd::is_signed<C>::value) ASSERT(b >= 0 && c >= 0);
#endif

return (C) std::min(a, (A) std::min(b, (B) c));
}

Expand All @@ -65,6 +78,11 @@ inline A max(A a, B b)
static_assert(is_comparable<A, B>::value,
"max(A, B): Cannot compare types A and B");

#if defined(ENABLE_ASSERT)
if (pstd::is_unsigned<A>::value && pstd::is_signed<B>::value) ASSERT(b >= 0);
if (pstd::is_unsigned<B>::value && pstd::is_signed<A>::value) ASSERT(b <= pstd::numeric_limits<A>::max());
#endif

return std::max(a, (A) b);
}

Expand All @@ -74,6 +92,12 @@ inline A max3(A a, B b, C c)
static_assert(is_comparable_3<A, B, C>::value,
"max3(A, B, C): Cannot compare types A, B and C");

#if defined(ENABLE_ASSERT)
if (pstd::is_unsigned<B>::value && pstd::is_signed<C>::value) ASSERT(c >= 0);
if (pstd::is_unsigned<C>::value && pstd::is_signed<B>::value) ASSERT(c <= pstd::numeric_limits<B>::max());
if (pstd::is_unsigned<A>::value && pstd::is_signed<B>::value && pstd::is_signed<C>::value) ASSERT(b >= 0 || c >= 0);
#endif

return std::max(a, (A) std::max(b, (B) c));
}

Expand Down

0 comments on commit f307dd8

Please sign in to comment.