Skip to content

Commit

Permalink
Add minimal test utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
K-ballo committed Jun 20, 2020
1 parent 99dcc09 commit 8fad5df
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 151 deletions.
128 changes: 61 additions & 67 deletions test/invoke.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@

#include <eggs/invoke.hpp>

#include <cassert>
#include <type_traits>
#include <utility>

#if NDEBUG
#define CHECK(...) (void)(__VA_ARGS__)
#else
#define CHECK(...) assert((__VA_ARGS__))
#endif
#include "test.hpp"

// [func.invoke], invoke

Expand Down Expand Up @@ -97,7 +92,7 @@ T const* addressof(T const& ref)
}

template <typename R, bool IsNothrow, typename F, typename A1>
void check_invoke_obj(
void test_invoke_obj(
R&& r, std::integral_constant<bool, IsNothrow>, F&& f, A1&& a1)
{
CHECK(::addressof(eggs::invoke(std::forward<F>(f), std::forward<A1>(a1))) == ::addressof(r));
Expand All @@ -111,7 +106,7 @@ void check_invoke_obj(
}

template <typename R, bool IsNothrow, typename F, typename... Args>
void check_invoke_fun(
void test_invoke_fun(
R&& r, std::integral_constant<bool, IsNothrow>, F&& f, Args&&... args)
{
CHECK(eggs::invoke(std::forward<F>(f), std::forward<Args>(args)...) == r);
Expand All @@ -133,52 +128,52 @@ void test_mem_obj_ptr()
C& r = x;
C const& cr = x;

check_invoke_obj(r.obj, nothrows, f, r);
check_invoke_obj(cr.obj, nothrows, f, cr);
check_invoke_obj(std::move(r.obj), nothrows, f, std::move(r));
check_invoke_obj(std::move(cr.obj), nothrows, f, std::move(cr));
CHECK_SCOPE(test_invoke_obj(r.obj, nothrows, f, r));
CHECK_SCOPE(test_invoke_obj(cr.obj, nothrows, f, cr));
CHECK_SCOPE(test_invoke_obj(std::move(r.obj), nothrows, f, std::move(r)));
CHECK_SCOPE(test_invoke_obj(std::move(cr.obj), nothrows, f, std::move(cr)));

D d = {42};
D& rd = d;
D const& crd = d;

check_invoke_obj(rd.obj, nothrows, f, rd);
check_invoke_obj(crd.obj, nothrows, f, crd);
check_invoke_obj(std::move(rd.obj), nothrows, f, std::move(rd));
check_invoke_obj(std::move(crd.obj), nothrows, f, std::move(crd));
CHECK_SCOPE(test_invoke_obj(rd.obj, nothrows, f, rd));
CHECK_SCOPE(test_invoke_obj(crd.obj, nothrows, f, crd));
CHECK_SCOPE(test_invoke_obj(std::move(rd.obj), nothrows, f, std::move(rd)));
CHECK_SCOPE(test_invoke_obj(std::move(crd.obj), nothrows, f, std::move(crd)));
}

/* reference wrapper */ {
C x = {42};
std::reference_wrapper<C> r = x;
std::reference_wrapper<C const> cr = x;

check_invoke_obj(r.get().obj, nothrows, f, r);
check_invoke_obj(cr.get().obj, nothrows, f, cr);
CHECK_SCOPE(test_invoke_obj(r.get().obj, nothrows, f, r));
CHECK_SCOPE(test_invoke_obj(cr.get().obj, nothrows, f, cr));
}

/* pointer */ {
C x = {42};
C* p = &x;
C const* cp = &x;

check_invoke_obj((*p).obj, nothrows, f, p);
check_invoke_obj((*cp).obj, nothrows, f, cp);
CHECK_SCOPE(test_invoke_obj((*p).obj, nothrows, f, p));
CHECK_SCOPE(test_invoke_obj((*cp).obj, nothrows, f, cp));
}

/* smart pointer */ {
C x = {42};
smart_ptr<C> p = &x;
smart_ptr<C const> cp = &x;

check_invoke_obj((*p).obj, nothrows, f, p);
check_invoke_obj((*cp).obj, nothrows, f, cp);
CHECK_SCOPE(test_invoke_obj((*p).obj, nothrows, f, p));
CHECK_SCOPE(test_invoke_obj((*cp).obj, nothrows, f, cp));

smart_ptr_throws<C> tp = &x;
smart_ptr_throws<C const> tcp = &x;

check_invoke_obj((*tp).obj, throws, f, tp);
check_invoke_obj((*tcp).obj, throws, f, tcp);
CHECK_SCOPE(test_invoke_obj((*tp).obj, throws, f, tp));
CHECK_SCOPE(test_invoke_obj((*tcp).obj, throws, f, tcp));
}
}

Expand All @@ -196,80 +191,80 @@ void test_mem_fun_ptr()
C& r = x;
C const& cr = x;

check_invoke_fun(r.fun(40), p0012_nothrows, f, r, 40);
check_invoke_fun(r.cfun(40), p0012_nothrows, cf, r, 40);
check_invoke_fun(r.lfun(40), p0012_nothrows, lf, r, 40);
check_invoke_fun(r.clfun(40), p0012_nothrows, clf, r, 40);
CHECK_SCOPE(test_invoke_fun(r.fun(40), p0012_nothrows, f, r, 40));
CHECK_SCOPE(test_invoke_fun(r.cfun(40), p0012_nothrows, cf, r, 40));
CHECK_SCOPE(test_invoke_fun(r.lfun(40), p0012_nothrows, lf, r, 40));
CHECK_SCOPE(test_invoke_fun(r.clfun(40), p0012_nothrows, clf, r, 40));

check_invoke_fun(cr.cfun(40), p0012_nothrows, cf, cr, 40);
check_invoke_fun(cr.clfun(40), p0012_nothrows, clf, cr, 40);
CHECK_SCOPE(test_invoke_fun(cr.cfun(40), p0012_nothrows, cf, cr, 40));
CHECK_SCOPE(test_invoke_fun(cr.clfun(40), p0012_nothrows, clf, cr, 40));

check_invoke_fun(std::move(r).fun(40), p0012_nothrows, f, std::move(r), 40);
check_invoke_fun(std::move(r).cfun(40), p0012_nothrows, cf, std::move(r), 40);
CHECK_SCOPE(test_invoke_fun(std::move(r).fun(40), p0012_nothrows, f, std::move(r), 40));
CHECK_SCOPE(test_invoke_fun(std::move(r).cfun(40), p0012_nothrows, cf, std::move(r), 40));
#if __cplusplus > 201703L || defined(_MSC_VER) // C++20: P0704
check_invoke_fun(std::move(r).clfun(40), p0012_nothrows, clf, std::move(r), 40);
CHECK_SCOPE(test_invoke_fun(std::move(r).clfun(40), p0012_nothrows, clf, std::move(r), 40));
#endif
check_invoke_fun(std::move(r).rfun(40), p0012_nothrows, rf, std::move(r), 40);
check_invoke_fun(std::move(r).crfun(40), p0012_nothrows, crf, std::move(r), 40);
CHECK_SCOPE(test_invoke_fun(std::move(r).rfun(40), p0012_nothrows, rf, std::move(r), 40));
CHECK_SCOPE(test_invoke_fun(std::move(r).crfun(40), p0012_nothrows, crf, std::move(r), 40));

check_invoke_fun(std::move(cr).cfun(40), p0012_nothrows, cf, std::move(cr), 40);
CHECK_SCOPE(test_invoke_fun(std::move(cr).cfun(40), p0012_nothrows, cf, std::move(cr), 40));
#if __cplusplus > 201703L || defined(_MSC_VER) // C++20: P0704
check_invoke_fun(std::move(cr).clfun(40), p0012_nothrows, clf, std::move(cr), 40);
CHECK_SCOPE(test_invoke_fun(std::move(cr).clfun(40), p0012_nothrows, clf, std::move(cr), 40));
#endif
check_invoke_fun(std::move(cr).crfun(40), p0012_nothrows, crf, std::move(cr), 40);
CHECK_SCOPE(test_invoke_fun(std::move(cr).crfun(40), p0012_nothrows, crf, std::move(cr), 40));
}

/* reference wrapper */ {
C x = {42};
std::reference_wrapper<C> r = x;
std::reference_wrapper<C const> cr = x;

check_invoke_fun(r.get().fun(40), p0012_nothrows, f, r, 40);
check_invoke_fun(r.get().cfun(40), p0012_nothrows, cf, r, 40);
check_invoke_fun(r.get().lfun(40), p0012_nothrows, lf, r, 40);
check_invoke_fun(r.get().clfun(40), p0012_nothrows, clf, r, 40);
CHECK_SCOPE(test_invoke_fun(r.get().fun(40), p0012_nothrows, f, r, 40));
CHECK_SCOPE(test_invoke_fun(r.get().cfun(40), p0012_nothrows, cf, r, 40));
CHECK_SCOPE(test_invoke_fun(r.get().lfun(40), p0012_nothrows, lf, r, 40));
CHECK_SCOPE(test_invoke_fun(r.get().clfun(40), p0012_nothrows, clf, r, 40));

check_invoke_fun(cr.get().cfun(40), p0012_nothrows, cf, cr, 40);
check_invoke_fun(cr.get().clfun(40), p0012_nothrows, clf, cr, 40);
CHECK_SCOPE(test_invoke_fun(cr.get().cfun(40), p0012_nothrows, cf, cr, 40));
CHECK_SCOPE(test_invoke_fun(cr.get().clfun(40), p0012_nothrows, clf, cr, 40));
}

/* pointer */ {
C x = {42};
C* p = &x;
C const* cp = &x;

check_invoke_fun((*p).fun(40), p0012_nothrows, f, p, 40);
check_invoke_fun((*p).cfun(40), p0012_nothrows, cf, p, 40);
check_invoke_fun((*p).lfun(40), p0012_nothrows, lf, p, 40);
check_invoke_fun((*p).clfun(40), p0012_nothrows, clf, p, 40);
CHECK_SCOPE(test_invoke_fun((*p).fun(40), p0012_nothrows, f, p, 40));
CHECK_SCOPE(test_invoke_fun((*p).cfun(40), p0012_nothrows, cf, p, 40));
CHECK_SCOPE(test_invoke_fun((*p).lfun(40), p0012_nothrows, lf, p, 40));
CHECK_SCOPE(test_invoke_fun((*p).clfun(40), p0012_nothrows, clf, p, 40));

check_invoke_fun((*cp).cfun(40), p0012_nothrows, cf, cp, 40);
check_invoke_fun((*cp).clfun(40), p0012_nothrows, clf, cp, 40);
CHECK_SCOPE(test_invoke_fun((*cp).cfun(40), p0012_nothrows, cf, cp, 40));
CHECK_SCOPE(test_invoke_fun((*cp).clfun(40), p0012_nothrows, clf, cp, 40));
}

/* smart pointer */ {
C x = {42};
smart_ptr<C> p = &x;
smart_ptr<C const> cp = &x;

check_invoke_fun((*p).fun(40), p0012_nothrows, f, p, 40);
check_invoke_fun((*p).cfun(40), p0012_nothrows, cf, p, 40);
check_invoke_fun((*p).lfun(40), p0012_nothrows, lf, p, 40);
check_invoke_fun((*p).clfun(40), p0012_nothrows, clf, p, 40);
CHECK_SCOPE(test_invoke_fun((*p).fun(40), p0012_nothrows, f, p, 40));
CHECK_SCOPE(test_invoke_fun((*p).cfun(40), p0012_nothrows, cf, p, 40));
CHECK_SCOPE(test_invoke_fun((*p).lfun(40), p0012_nothrows, lf, p, 40));
CHECK_SCOPE(test_invoke_fun((*p).clfun(40), p0012_nothrows, clf, p, 40));

check_invoke_fun((*cp).cfun(40), p0012_nothrows, cf, cp, 40);
check_invoke_fun((*cp).clfun(40), p0012_nothrows, clf, cp, 40);
CHECK_SCOPE(test_invoke_fun((*cp).cfun(40), p0012_nothrows, cf, cp, 40));
CHECK_SCOPE(test_invoke_fun((*cp).clfun(40), p0012_nothrows, clf, cp, 40));

smart_ptr_throws<C> tp = &x;
smart_ptr_throws<C const> tcp = &x;

check_invoke_fun((*tp).fun(40), throws, f, tp, 40);
check_invoke_fun((*tp).cfun(40), throws, cf, tp, 40);
check_invoke_fun((*tp).lfun(40), throws, lf, tp, 40);
check_invoke_fun((*tp).clfun(40), throws, clf, tp, 40);
CHECK_SCOPE(test_invoke_fun((*tp).fun(40), throws, f, tp, 40));
CHECK_SCOPE(test_invoke_fun((*tp).cfun(40), throws, cf, tp, 40));
CHECK_SCOPE(test_invoke_fun((*tp).lfun(40), throws, lf, tp, 40));
CHECK_SCOPE(test_invoke_fun((*tp).clfun(40), throws, clf, tp, 40));

check_invoke_fun((*tcp).cfun(40), throws, cf, tcp, 40);
check_invoke_fun((*tcp).clfun(40), throws, clf, tcp, 40);
CHECK_SCOPE(test_invoke_fun((*tcp).cfun(40), throws, cf, tcp, 40));
CHECK_SCOPE(test_invoke_fun((*tcp).clfun(40), throws, clf, tcp, 40));
}
}

Expand All @@ -285,10 +280,9 @@ void test_fun_obj()
};
auto f = Fn{};

check_invoke_fun(f(40), nothrows, f, 40);
check_invoke_fun(f(conv_to<int>(40)), nothrows, f, conv_to<int>(40));
check_invoke_fun(
f(conv_to_throws<int>(40)), throws, f, conv_to_throws<int>(40));
CHECK_SCOPE(test_invoke_fun(f(40), nothrows, f, 40));
CHECK_SCOPE(test_invoke_fun(f(conv_to<int>(40)), nothrows, f, conv_to<int>(40)));
CHECK_SCOPE(test_invoke_fun(f(conv_to_throws<int>(40)), throws, f, conv_to_throws<int>(40)));
}

/* fun-ptr */ {
Expand All @@ -301,7 +295,7 @@ void test_fun_obj()
};
auto f = &S::f;

check_invoke_fun(f(40), p0012_nothrows, f, 40);
CHECK_SCOPE(test_invoke_fun(f(40), p0012_nothrows, f, 40));
}
}

Expand Down
7 changes: 1 addition & 6 deletions test/invoke_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@

#include <eggs/invoke.hpp>

#include <cassert>
#include <type_traits>
#include <utility>

#if NDEBUG
#define CHECK(...) (void)(__VA_ARGS__)
#else
#define CHECK(...) assert((__VA_ARGS__))
#endif
#include "test.hpp"

template <typename T>
struct no_result_void
Expand Down
Loading

0 comments on commit 8fad5df

Please sign in to comment.