tests/test_iterator.c

changeset 1070
0a5a356a4486
parent 951
8f6d6556380a
equal deleted inserted replaced
1069:6565e7ef5ee3 1070:0a5a356a4486
63 size_t size = cx_nmemb(array); 63 size_t size = cx_nmemb(array);
64 for (unsigned i = 0 ; i < size ; i++) array[i] = i; 64 for (unsigned i = 0 ; i < size ; i++) array[i] = i;
65 65
66 CxIterator iter = cxIterator(array, sizeof(unsigned), size); 66 CxIterator iter = cxIterator(array, sizeof(unsigned), size);
67 CX_TEST_DO { 67 CX_TEST_DO {
68 CX_TEST_ASSERT(iter.elem_size == sizeof(unsigned));
69 CX_TEST_ASSERT(iter.elem_count == size);
70 CX_TEST_ASSERT(iter.src_handle.c == array);
68 unsigned expected = 0; 71 unsigned expected = 0;
69 cx_foreach(unsigned *, e, iter) { 72 cx_foreach(unsigned *, e, iter) {
70 CX_TEST_ASSERT(iter.index == expected); 73 CX_TEST_ASSERT(iter.index == expected);
71 CX_TEST_ASSERT(*e == expected); 74 CX_TEST_ASSERT(*e == expected);
72 CX_TEST_ASSERT(iter.elem_size == sizeof(unsigned));
73 CX_TEST_ASSERT(iter.elem_count == size);
74 CX_TEST_ASSERT(iter.src_handle.c == array);
75 CX_TEST_ASSERT(iter.elem_handle == &array[expected]); 75 CX_TEST_ASSERT(iter.elem_handle == &array[expected]);
76 expected++; 76 expected++;
77 } 77 }
78 CX_TEST_ASSERT(expected == size); 78 CX_TEST_ASSERT(expected == size);
79 }
80 }
81
82 CX_TEST(test_iterator_iterate_pointers) {
83 unsigned array[20];
84 unsigned* ptr_array[20];
85 size_t size = cx_nmemb(array);
86 for (unsigned i = 0 ; i < size ; i++) {
87 array[i] = 3*i;
88 ptr_array[i] = &array[i];
89 }
90
91 CxIterator iter = cxIteratorPtr(ptr_array, size);
92 CX_TEST_DO {
93 CX_TEST_ASSERT(iter.elem_size == sizeof(void*));
94 CX_TEST_ASSERT(iter.elem_count == size);
95 CX_TEST_ASSERT(iter.src_handle.c == ptr_array);
96 unsigned idx = 0;
97 cx_foreach(unsigned *, e, iter) {
98 CX_TEST_ASSERT(iter.index == idx);
99 CX_TEST_ASSERT(*e == array[idx]);
100 CX_TEST_ASSERT(iter.elem_handle == &ptr_array[idx]);
101 idx++;
102 }
103 CX_TEST_ASSERT(idx == size);
79 } 104 }
80 } 105 }
81 106
82 CX_TEST(test_iterator_with_slow_remove) { 107 CX_TEST(test_iterator_with_slow_remove) {
83 unsigned array[20]; 108 unsigned array[20];
168 CxTestSuite *cx_test_suite_iterator(void) { 193 CxTestSuite *cx_test_suite_iterator(void) {
169 CxTestSuite *suite = cx_test_suite_new("iterator"); 194 CxTestSuite *suite = cx_test_suite_new("iterator");
170 195
171 cx_test_register(suite, test_iterator_create); 196 cx_test_register(suite, test_iterator_create);
172 cx_test_register(suite, test_iterator_iterate); 197 cx_test_register(suite, test_iterator_iterate);
198 cx_test_register(suite, test_iterator_iterate_pointers);
173 cx_test_register(suite, test_iterator_with_slow_remove); 199 cx_test_register(suite, test_iterator_with_slow_remove);
174 cx_test_register(suite, test_iterator_with_fast_remove); 200 cx_test_register(suite, test_iterator_with_fast_remove);
175 201
176 return suite; 202 return suite;
177 } 203 }

mercurial