Skip to content

Commit

Permalink
Add test cases for min and max edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesYang007 committed Jul 12, 2020
1 parent bee36c5 commit e936992
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
9 changes: 5 additions & 4 deletions include/autoppl/math/math.hpp
Expand Up @@ -2,6 +2,7 @@
#include <cmath>
#include <algorithm>
#include <iterator>
#include <autoppl/util/functional.hpp>

namespace ppl {
namespace math {
Expand All @@ -18,8 +19,8 @@ inline constexpr T neg_inf =
-std::numeric_limits<T>::infinity() :
std::numeric_limits<T>::lowest();

template <class Iter, class F>
inline constexpr auto min(Iter begin, Iter end, F f)
template <class Iter, class F = util::identity>
inline constexpr auto min(Iter begin, Iter end, F f = F())
{
using value_t = typename std::iterator_traits<Iter>::value_type;
static_assert(std::is_invocable_v<F, value_t>);
Expand All @@ -37,8 +38,8 @@ inline constexpr auto min(Iter begin, Iter end, F f)
return res;
}

template <class Iter, class F>
inline constexpr auto max(Iter begin, Iter end, F f)
template <class Iter, class F = util::identity>
inline constexpr auto max(Iter begin, Iter end, F f = F())
{
using value_t = typename std::iterator_traits<Iter>::value_type;
static_assert(std::is_invocable_v<F, value_t>);
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Expand Up @@ -89,6 +89,7 @@ add_test(expr_unittest expr_unittest)
add_executable(math_unittest
${CMAKE_CURRENT_SOURCE_DIR}/math/welford_unittest.cpp
${CMAKE_CURRENT_SOURCE_DIR}/math/density_unittest.cpp
${CMAKE_CURRENT_SOURCE_DIR}/math/math_unittest.cpp
)

if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
Expand Down
27 changes: 27 additions & 0 deletions test/math/math_unittest.cpp
@@ -0,0 +1,27 @@
#include "gtest/gtest.h"
#include <array>
#include <autoppl/math/math.hpp>

namespace ppl {
namespace math {

struct math_fixture : ::testing::Test
{
protected:
std::array<double, 3> x = {0};
};

TEST_F(math_fixture, min_edge_case)
{
auto res = min(x.end(), x.begin());
EXPECT_DOUBLE_EQ(res, inf<double>);
}

TEST_F(math_fixture, max_edge_case)
{
auto res = max(x.end(), x.begin());
EXPECT_DOUBLE_EQ(res, neg_inf<double>);
}

} // namespace math
} // namespace ppl

0 comments on commit e936992

Please sign in to comment.