unit tester fixes

master
Daniel Kolesa 2017-04-28 01:54:28 +02:00
parent 69890906fd
commit c1e79a4c75
4 changed files with 66 additions and 15 deletions

View File

@ -24,8 +24,16 @@
#include <type_traits> #include <type_traits>
#include <algorithm> #include <algorithm>
#ifdef OSTD_BUILD_TESTS
#include <vector>
#endif
#include "ostd/unit_test.hh"
#include "ostd/range.hh" #include "ostd/range.hh"
#define OSTD_TEST_MODULE libostd_algorithm
namespace ostd { namespace ostd {
/** @addtogroup Ranges /** @addtogroup Ranges
@ -103,6 +111,37 @@ inline auto is_partitioned(Predicate &&pred) {
}; };
} }
#ifdef OSTD_BUILD_TESTS
OSTD_UNIT_TEST {
using ostd::test::fail_if;
using ostd::test::fail_if_not;
/* test with two vectors for pipeable and non-pipeable */
std::vector<int> v1 = { 5, 15, 10, 8, 36, 24 };
std::vector<int> v2 = v1;
auto try_test = [](auto &v, auto h) {
for (auto i: h) {
fail_if(i < 15);
}
for (auto i: iter(v).take(v.size() - h.size())) {
fail_if(i >= 15);
}
};
/* assume they're not partitioned */
fail_if(is_partitioned(iter(v1), [](int &i) { return i < 15; }));
fail_if(iter(v1) | is_partitioned([](int &i) { return i < 15; }));
fail_if(is_partitioned(iter(v2), [](int &i) { return i < 15; }));
fail_if(iter(v2) | is_partitioned([](int &i) { return i < 15; }));
/* partition now */
try_test(v1, partition(iter(v1), [](int &i) { return i < 15; }));
try_test(v2, iter(v2) | partition([](int &i) { return i < 15; }));
/* assume partitioned */
fail_if_not(is_partitioned(iter(v1), [](int &i) { return i < 15; }));
fail_if_not(iter(v1) | is_partitioned([](int &i) { return i < 15; }));
fail_if_not(is_partitioned(iter(v2), [](int &i) { return i < 15; }));
fail_if_not(iter(v2) | is_partitioned([](int &i) { return i < 15; }));
}
#endif
/* sorting */ /* sorting */
namespace detail { namespace detail {
@ -1396,6 +1435,8 @@ inline auto filter(Predicate &&pred) {
} /* namespace ostd */ } /* namespace ostd */
#undef OSTD_TEST_MODULE
#endif #endif
/** @} */ /** @} */

View File

@ -1332,7 +1332,9 @@ inline auto range(T v) {
return detail::number_range<T>(v); return detail::number_range<T>(v);
} }
OSTD_UNIT_TEST({ #ifdef OSTD_BUILD_TESTS
OSTD_UNIT_TEST {
using ostd::test::fail_if;
auto r = range(5, 10, 2); auto r = range(5, 10, 2);
fail_if(r.empty() || (r.front() != 5)); fail_if(r.empty() || (r.front() != 5));
r.pop_front(); r.pop_front();
@ -1341,7 +1343,8 @@ OSTD_UNIT_TEST({
fail_if(r.empty() || (r.front() != 9)); fail_if(r.empty() || (r.front() != 9));
r.pop_front(); r.pop_front();
fail_if(!r.empty()); fail_if(!r.empty());
}); };
#endif
namespace detail { namespace detail {
template<typename T> template<typename T>

View File

@ -47,6 +47,7 @@ namespace test {
namespace detail { namespace detail {
static std::vector<void (*)()> test_cases; static std::vector<void (*)()> test_cases;
static void (*test_dummy)() = nullptr;
static bool add_test(std::string testn, void (*func)()) { static bool add_test(std::string testn, void (*func)()) {
if (testn == OSTD_TEST_MODULE_CURRENT) { if (testn == OSTD_TEST_MODULE_CURRENT) {
@ -58,7 +59,13 @@ namespace detail {
struct test_error {}; struct test_error {};
} }
#define OSTD_TEST_FUNC_CONCAT(p, m, l) p##_##m##_##line
#define OSTD_TEST_FUNC_NAME(p, m, l) OSTD_TEST_FUNC_CONCAT(p, m, l)
/** @brief Defines a unit test. /** @brief Defines a unit test.
*
* The body of the test follows the expansion of this macro like a
* normal function body.
* *
* The test is only enabled if the module name matches the value of the * The test is only enabled if the module name matches the value of the
* `OSTD_BUILD_TESTS` macro, defined before inclusion of this header. The * `OSTD_BUILD_TESTS` macro, defined before inclusion of this header. The
@ -66,14 +73,14 @@ namespace detail {
* name is defined using the `OSTD_TEST_MODULE` macro, which should be defined * name is defined using the `OSTD_TEST_MODULE` macro, which should be defined
* after including any headers and undefined at the end of the file. * after including any headers and undefined at the end of the file.
*/ */
#define OSTD_UNIT_TEST(body) \ #define OSTD_UNIT_TEST \
static bool test_case_##OSTD_TEST_MODULE##_##__LINE__ = \ static void OSTD_TEST_FUNC_NAME(test_func, OSTD_TEST_MODULE, __LINE__)(); \
ostd::test::detail::add_test( static bool OSTD_TEST_FUNC_NAME(test_case, OSTD_TEST_MODULE, __LINE__) = \
OSTD_TEST_MODULE_STR(OSTD_TEST_MODULE), []() { \ ostd::test::detail::add_test( \
using namespace ostd::test; \ OSTD_TEST_MODULE_STR(OSTD_TEST_MODULE), \
body \ &OSTD_TEST_FUNC_NAME(test_func, OSTD_TEST_MODULE, __LINE__) \
} \ ); \
); static void OSTD_TEST_FUNC_NAME(test_func, OSTD_TEST_MODULE, __LINE__)()
/** @brief Makes the test fail if the given value is true. /** @brief Makes the test fail if the given value is true.
* *
@ -133,10 +140,6 @@ std::pair<std::size_t, std::size_t> run() {
return std::make_pair(succ, fail); return std::make_pair(succ, fail);
} }
#else /* OSTD_BUILD_TESTS */
#define OSTD_UNIT_TEST(body)
#endif /* OSTD_BUILD_TESTS */ #endif /* OSTD_BUILD_TESTS */
/** @} */ /** @} */

View File

@ -148,7 +148,11 @@ int main() {
); );
remove(exepath.data()); remove(exepath.data());
++nsuccess; if (fail) {
++nfailed;
} else {
++nsuccess;
}
}); });
} }