From 6721a8676860fab922396ba55854fff9b7ebb5a4 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sun, 15 Jul 2018 22:34:29 +0200 Subject: [PATCH] Add method _mismatch and _mismatch_if --- m-algo.h | 30 ++++++++++++++++++++++++++++++ tests/test-malgo.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/m-algo.h b/m-algo.h index f283b906..05ae1000 100644 --- a/m-algo.h +++ b/m-algo.h @@ -182,6 +182,36 @@ } \ \ static inline void \ + M_C(name, _mismatch) (it_t it1, it_t it2, container_t l1, container_t l2 ) \ + { \ + for (M_GET_IT_FIRST cont_oplist (it1, l1), \ + M_GET_IT_FIRST cont_oplist (it2, l2); \ + !M_GET_IT_END_P cont_oplist (it1) && \ + !M_GET_IT_END_P cont_oplist (it2); \ + M_GET_IT_NEXT cont_oplist (it1), \ + M_GET_IT_NEXT cont_oplist (it2)) { \ + if (!M_GET_EQUAL type_oplist (*M_GET_IT_CREF cont_oplist (it1), \ + *M_GET_IT_CREF cont_oplist (it2))) \ + break; \ + } \ + } \ + \ + static inline void \ + M_C(name, _mismatch_if) (it_t it1, it_t it2, container_t l1, container_t l2, bool (*func)(type_t const, type_t const) ) \ + { \ + for (M_GET_IT_FIRST cont_oplist (it1, l1), \ + M_GET_IT_FIRST cont_oplist (it2, l2); \ + !M_GET_IT_END_P cont_oplist (it1) && \ + !M_GET_IT_END_P cont_oplist (it2); \ + M_GET_IT_NEXT cont_oplist (it1), \ + M_GET_IT_NEXT cont_oplist (it2)) { \ + if (!func (*M_GET_IT_CREF cont_oplist (it1), \ + *M_GET_IT_CREF cont_oplist (it2))) \ + break; \ + } \ + } \ + \ + static inline void \ M_C(name, _map) (container_t l, void (*f)(type_t const) ) \ { \ for M_EACH(item, l, cont_oplist) { \ diff --git a/tests/test-malgo.c b/tests/test-malgo.c index 03b902b6..507cdd30 100644 --- a/tests/test-malgo.c +++ b/tests/test-malgo.c @@ -86,6 +86,11 @@ static bool func_test_pos(int d) return d >= 0; } +static bool func_test_both_even_or_odd(int a, int b) +{ + return (a&1) == (b&1); +} + static void test_list(void) { list_int_t l; @@ -140,6 +145,10 @@ static void test_list(void) algo_list_find_if(it1, l, func_test_42); assert (!list_int_end_p (it1)); assert (*list_int_cref(it1) == 42); + + algo_list_mismatch(it1, it2, l, l); + assert (list_int_end_p (it1)); + assert (list_int_end_p (it2)); for(int i = -100; i < 100; i+=2) list_int_push_back (l, i); @@ -236,6 +245,31 @@ static void test_array(void) assert (array_int_size(l) == 102); assert( algo_array_sort_p(l) == true); + M_LET(l2, array_int_t) { + array_int_set(l2, l); + array_int_it_t it1, it2; + algo_array_mismatch(it1, it2, l, l2); + assert (array_int_end_p (it1)); + assert (array_int_end_p (it2)); + array_int_pop_back(NULL, l2); + array_int_push_back(l2, 159); + algo_array_mismatch(it1, it2, l, l2); + assert (!array_int_end_p (it1)); + assert (!array_int_end_p (it2)); + assert (*array_int_cref(it1) == 1742); + assert (*array_int_cref(it2) == 159); + algo_array_mismatch_if(it1, it2, l, l2, func_test_both_even_or_odd); + assert (!array_int_end_p (it1)); + assert (!array_int_end_p (it2)); + assert (*array_int_cref(it1) == 1742); + assert (*array_int_cref(it2) == 159); + array_int_pop_back(NULL, l2); + array_int_push_back(l2, 152); + algo_array_mismatch_if(it1, it2, l, l2, func_test_both_even_or_odd); + assert (array_int_end_p (it1)); + assert (array_int_end_p (it2)); + } + array_int_clean(l); assert (algo_array_min(l) == NULL); assert (algo_array_max(l) == NULL);