|
| 1 | +#include <stddef.h> |
| 2 | +#include <stdarg.h> |
| 3 | +#include <setjmp.h> |
| 4 | +#include <google/cmockery.h> |
| 5 | + |
| 6 | +#include "cstl/cstl_def.h" |
| 7 | +#include "cstl/citerator.h" |
| 8 | +#include "cstl/chash_map.h" |
| 9 | +#include "cstl/cstring.h" |
| 10 | +#include "cstl/cvector.h" |
| 11 | +#include "cstl/clist.h" |
| 12 | +#include "cstl/cdeque.h" |
| 13 | +#include "cstl/cslist.h" |
| 14 | +#include "cstl/cmap.h" |
| 15 | +#include "cstl/cset.h" |
| 16 | +#include "cstl/chash_set.h" |
| 17 | +#include "cstl/calgorithm.h" |
| 18 | +#include "cstl/cfunctional.h" |
| 19 | + |
| 20 | +#include "ut_def.h" |
| 21 | +#include "ut_cstl_function.h" |
| 22 | + |
| 23 | +UT_SUIT_DEFINATION(cstl_function, fun_equal_pointer_usage) |
| 24 | + |
| 25 | +/* |
| 26 | + * test fun_equal_pointer usage |
| 27 | + */ |
| 28 | +UT_CASE_DEFINATION(fun_equal_pointer_usage) |
| 29 | +void test__fun_equal_pointer_usage__algo_unique_copy(void** state) |
| 30 | +{ |
| 31 | + vector_t* pvec = create_vector(void*); |
| 32 | + list_t* plist = create_list(void*); |
| 33 | + deque_t* pdeq = create_deque(void*); |
| 34 | + |
| 35 | + void* src[] = {0x1111, 0x1111, 0x1111, 0x2222, 0x2222, 0x3333, 0x4444, 0x4444, 0x8888, 0x8888}; |
| 36 | + void* result[] = {0x1111, 0x2222, 0x3333, 0x4444, 0x8888, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 37 | + |
| 38 | + vector_init_copy_array(pvec, src, 10); |
| 39 | + list_init_copy_array(plist, result, 10); |
| 40 | + deque_init_n(pdeq, vector_size(pvec)); |
| 41 | + |
| 42 | + algo_unique_copy(vector_begin(pvec), vector_end(pvec), deque_begin(pdeq)); |
| 43 | + assert_true(algo_equal(deque_begin(pdeq), deque_end(pdeq), list_begin(plist))); |
| 44 | + |
| 45 | + vector_destroy(pvec); |
| 46 | + list_destroy(plist); |
| 47 | + deque_destroy(pdeq); |
| 48 | +} |
| 49 | + |
| 50 | +/* |
| 51 | + * test fun_not_equal_pointer usage |
| 52 | + */ |
| 53 | +UT_CASE_DEFINATION(fun_not_equal_pointer_usage) |
| 54 | +void test__fun_not_equal_pointer_usage__algo_unique_copy_if(void** state) |
| 55 | +{ |
| 56 | + vector_t* pvec = create_vector(void*); |
| 57 | + list_t* plist = create_list(void*); |
| 58 | + deque_t* pdeq = create_deque(void*); |
| 59 | + |
| 60 | + void* src[] = {0x1111, 0x8888, 0x9999, 0x2222, 0x1111, 0x3333, 0x1111, 0x4444, 0x8888, 0x8888}; |
| 61 | + void* result[] = {0x1111, 0x1111, 0x1111, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 62 | + |
| 63 | + vector_init_copy_array(pvec, src, 10); |
| 64 | + list_init_copy_array(plist, result, 10); |
| 65 | + deque_init_n(pdeq, vector_size(pvec)); |
| 66 | + |
| 67 | + algo_unique_copy_if(vector_begin(pvec), vector_end(pvec), deque_begin(pdeq), _fun_get_binary(vector_begin(pvec), _NOT_EQUAL_FUN)); |
| 68 | + assert_true(algo_equal(deque_begin(pdeq), deque_end(pdeq), list_begin(plist))); |
| 69 | + |
| 70 | + vector_destroy(pvec); |
| 71 | + list_destroy(plist); |
| 72 | + deque_destroy(pdeq); |
| 73 | +} |
| 74 | + |
| 75 | +/* |
| 76 | + * test fun_greater_pointer usage |
| 77 | + */ |
| 78 | +UT_CASE_DEFINATION(fun_greater_pointer_usage) |
| 79 | +void test__fun_greater_pointer_usage__algo_sort_if(void** state) |
| 80 | +{ |
| 81 | + vector_t* pvec = create_vector(void*); |
| 82 | + list_t* plist = create_list(void*); |
| 83 | + |
| 84 | + void* src[] = {0x1111, 0x8888, 0x9999, 0x2222, 0x1111, 0x3333, 0x1111, 0x4444, 0x8888, 0x8888}; |
| 85 | + void* result[] = {0x9999, 0x8888, 0x8888, 0x8888, 0x4444, 0x3333, 0x2222, 0x1111, 0x1111, 0x1111}; |
| 86 | + |
| 87 | + vector_init_copy_array(pvec, src, 10); |
| 88 | + list_init_copy_array(plist, result, 10); |
| 89 | + |
| 90 | + algo_sort_if(vector_begin(pvec), vector_end(pvec), _fun_get_binary(vector_begin(pvec), _GREATER_FUN)); |
| 91 | + assert_true(algo_equal(vector_begin(pvec), vector_end(pvec), list_begin(plist))); |
| 92 | + |
| 93 | + vector_destroy(pvec); |
| 94 | + list_destroy(plist); |
| 95 | +} |
| 96 | + |
| 97 | +/* |
| 98 | + * test fun_greater_equal_pointer usage |
| 99 | + */ |
| 100 | +UT_CASE_DEFINATION(fun_greater_equal_pointer_usage) |
| 101 | +void test__fun_greater_equal_pointer_usage__algo_sort_if(void** state) |
| 102 | +{ |
| 103 | + vector_t* pvec = create_vector(void*); |
| 104 | + list_t* plist = create_list(void*); |
| 105 | + |
| 106 | + void* src[] = {0x1111, 0x8888, 0x9999, 0x2222, 0x1111, 0x3333, 0x1111, 0x4444, 0x8888, 0x8888}; |
| 107 | + void* result[] = {0x9999, 0x8888, 0x8888, 0x8888, 0x4444, 0x3333, 0x2222, 0x1111, 0x1111, 0x1111}; |
| 108 | + |
| 109 | + vector_init_copy_array(pvec, src, 10); |
| 110 | + list_init_copy_array(plist, result, 10); |
| 111 | + |
| 112 | + algo_sort_if(vector_begin(pvec), vector_end(pvec), _fun_get_binary(vector_begin(pvec), _GREATER_EQUAL_FUN)); |
| 113 | + assert_true(algo_equal(vector_begin(pvec), vector_end(pvec), list_begin(plist))); |
| 114 | + |
| 115 | + vector_destroy(pvec); |
| 116 | + list_destroy(plist); |
| 117 | +} |
| 118 | + |
| 119 | +/* |
| 120 | + * test fun_less_pointer usage |
| 121 | + */ |
| 122 | +UT_CASE_DEFINATION(fun_less_pointer_usage) |
| 123 | +void test__fun_less_pointer_usage__algo_sort_if(void** state) |
| 124 | +{ |
| 125 | + vector_t* pvec = create_vector(void*); |
| 126 | + list_t* plist = create_list(void*); |
| 127 | + |
| 128 | + void* src[] = {0x1111, 0x8888, 0x9999, 0x2222, 0x1111, 0x3333, 0x1111, 0x4444, 0x8888, 0x8888}; |
| 129 | + void* result[] = {0x1111, 0x1111, 0x1111, 0x2222, 0x3333, 0x4444, 0x8888, 0x8888, 0x8888, 0x9999}; |
| 130 | + |
| 131 | + vector_init_copy_array(pvec, src, 10); |
| 132 | + list_init_copy_array(plist, result, 10); |
| 133 | + |
| 134 | + algo_sort_if(vector_begin(pvec), vector_end(pvec), _fun_get_binary(vector_begin(pvec), _LESS_FUN)); |
| 135 | + assert_true(algo_equal(vector_begin(pvec), vector_end(pvec), list_begin(plist))); |
| 136 | + |
| 137 | + vector_destroy(pvec); |
| 138 | + list_destroy(plist); |
| 139 | +} |
| 140 | + |
| 141 | +/* |
| 142 | + * test fun_less_equal_pointer usage |
| 143 | + */ |
| 144 | +UT_CASE_DEFINATION(fun_less_equal_pointer_usage) |
| 145 | +void test__fun_less_equal_pointer_usage__algo_sort_if(void** state) |
| 146 | +{ |
| 147 | + vector_t* pvec = create_vector(void*); |
| 148 | + list_t* plist = create_list(void*); |
| 149 | + |
| 150 | + void* src[] = {0x1111, 0x8888, 0x9999, 0x2222, 0x1111, 0x3333, 0x1111, 0x4444, 0x8888, 0x8888}; |
| 151 | + void* result[] = {0x1111, 0x1111, 0x1111, 0x2222, 0x3333, 0x4444, 0x8888, 0x8888, 0x8888, 0x9999}; |
| 152 | + |
| 153 | + vector_init_copy_array(pvec, src, 10); |
| 154 | + list_init_copy_array(plist, result, 10); |
| 155 | + |
| 156 | + algo_sort_if(vector_begin(pvec), vector_end(pvec), _fun_get_binary(vector_begin(pvec), _LESS_EQUAL_FUN)); |
| 157 | + assert_true(algo_equal(vector_begin(pvec), vector_end(pvec), list_begin(plist))); |
| 158 | + |
| 159 | + vector_destroy(pvec); |
| 160 | + list_destroy(plist); |
| 161 | +} |
0 commit comments