forked from OctaForge/libostd
unit tester fixes
This commit is contained in:
parent
69890906fd
commit
c1e79a4c75
|
@ -24,8 +24,16 @@
|
|||
#include <type_traits>
|
||||
#include <algorithm>
|
||||
|
||||
#ifdef OSTD_BUILD_TESTS
|
||||
#include <vector>
|
||||
#endif
|
||||
|
||||
#include "ostd/unit_test.hh"
|
||||
|
||||
#include "ostd/range.hh"
|
||||
|
||||
#define OSTD_TEST_MODULE libostd_algorithm
|
||||
|
||||
namespace ostd {
|
||||
|
||||
/** @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 */
|
||||
|
||||
namespace detail {
|
||||
|
@ -1396,6 +1435,8 @@ inline auto filter(Predicate &&pred) {
|
|||
|
||||
} /* namespace ostd */
|
||||
|
||||
#undef OSTD_TEST_MODULE
|
||||
|
||||
#endif
|
||||
|
||||
/** @} */
|
||||
|
|
|
@ -1332,7 +1332,9 @@ inline auto 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);
|
||||
fail_if(r.empty() || (r.front() != 5));
|
||||
r.pop_front();
|
||||
|
@ -1341,7 +1343,8 @@ OSTD_UNIT_TEST({
|
|||
fail_if(r.empty() || (r.front() != 9));
|
||||
r.pop_front();
|
||||
fail_if(!r.empty());
|
||||
});
|
||||
};
|
||||
#endif
|
||||
|
||||
namespace detail {
|
||||
template<typename T>
|
||||
|
|
|
@ -47,6 +47,7 @@ namespace test {
|
|||
|
||||
namespace detail {
|
||||
static std::vector<void (*)()> test_cases;
|
||||
static void (*test_dummy)() = nullptr;
|
||||
|
||||
static bool add_test(std::string testn, void (*func)()) {
|
||||
if (testn == OSTD_TEST_MODULE_CURRENT) {
|
||||
|
@ -58,7 +59,13 @@ namespace detail {
|
|||
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.
|
||||
*
|
||||
* 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
|
||||
* `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
|
||||
* after including any headers and undefined at the end of the file.
|
||||
*/
|
||||
#define OSTD_UNIT_TEST(body) \
|
||||
static bool test_case_##OSTD_TEST_MODULE##_##__LINE__ = \
|
||||
ostd::test::detail::add_test(
|
||||
OSTD_TEST_MODULE_STR(OSTD_TEST_MODULE), []() { \
|
||||
using namespace ostd::test; \
|
||||
body \
|
||||
} \
|
||||
);
|
||||
#define OSTD_UNIT_TEST \
|
||||
static void OSTD_TEST_FUNC_NAME(test_func, OSTD_TEST_MODULE, __LINE__)(); \
|
||||
static bool OSTD_TEST_FUNC_NAME(test_case, OSTD_TEST_MODULE, __LINE__) = \
|
||||
ostd::test::detail::add_test( \
|
||||
OSTD_TEST_MODULE_STR(OSTD_TEST_MODULE), \
|
||||
&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.
|
||||
*
|
||||
|
@ -133,10 +140,6 @@ std::pair<std::size_t, std::size_t> run() {
|
|||
return std::make_pair(succ, fail);
|
||||
}
|
||||
|
||||
#else /* OSTD_BUILD_TESTS */
|
||||
|
||||
#define OSTD_UNIT_TEST(body)
|
||||
|
||||
#endif /* OSTD_BUILD_TESTS */
|
||||
|
||||
/** @} */
|
||||
|
|
|
@ -148,7 +148,11 @@ int main() {
|
|||
);
|
||||
|
||||
remove(exepath.data());
|
||||
++nsuccess;
|
||||
if (fail) {
|
||||
++nfailed;
|
||||
} else {
|
||||
++nsuccess;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue