tests/test_iterator.c

Wed, 01 Jan 2025 14:03:49 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 01 Jan 2025 14:03:49 +0100
changeset 1070
0a5a356a4486
parent 951
8f6d6556380a
permissions
-rw-r--r--

add array iterator over pointer arrays

850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved.
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 */
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include "cx/test.h"
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 #include "cx/iterator.h"
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 CX_TEST(test_iterator_create) {
951
8f6d6556380a remove dynamic arrays which are not supported by msvc
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
34 unsigned array[20];
8f6d6556380a remove dynamic arrays which are not supported by msvc
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
35 size_t size = cx_nmemb(array);
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 for (unsigned i = 0 ; i < size ; i++) array[i] = i;
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37
851
adb4e0737c33 issue #389 : add separate function for immutable arrays
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
38 CxIterator iter = cxIterator(array, sizeof(unsigned), size);
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 CX_TEST_DO {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 CX_TEST_ASSERT(iter.index == 0);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 CX_TEST_ASSERT(iter.elem_size == sizeof(unsigned));
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 CX_TEST_ASSERT(iter.elem_count == size);
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 851
diff changeset
43 CX_TEST_ASSERT(iter.src_handle.c == array);
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 CX_TEST_ASSERT(iter.elem_handle == &array[0]);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 CX_TEST_ASSERT(cxIteratorValid(iter));
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 CX_TEST(test_iterator_create_null) {
851
adb4e0737c33 issue #389 : add separate function for immutable arrays
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
50 CxIterator iter = cxIterator(NULL, sizeof(unsigned), 47);
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 CX_TEST_DO {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 CX_TEST_ASSERT(iter.index == 0);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 CX_TEST_ASSERT(iter.elem_size == sizeof(unsigned));
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 CX_TEST_ASSERT(iter.elem_count == 0);
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 851
diff changeset
55 CX_TEST_ASSERT(iter.src_handle.c == NULL);
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 CX_TEST_ASSERT(iter.elem_handle == NULL);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 CX_TEST_ASSERT(!cxIteratorValid(iter));
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 CX_TEST(test_iterator_iterate) {
951
8f6d6556380a remove dynamic arrays which are not supported by msvc
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
62 unsigned array[20];
8f6d6556380a remove dynamic arrays which are not supported by msvc
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
63 size_t size = cx_nmemb(array);
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 for (unsigned i = 0 ; i < size ; i++) array[i] = i;
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65
851
adb4e0737c33 issue #389 : add separate function for immutable arrays
Mike Becker <universe@uap-core.de>
parents: 850
diff changeset
66 CxIterator iter = cxIterator(array, sizeof(unsigned), size);
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 CX_TEST_DO {
1070
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
68 CX_TEST_ASSERT(iter.elem_size == sizeof(unsigned));
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
69 CX_TEST_ASSERT(iter.elem_count == size);
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
70 CX_TEST_ASSERT(iter.src_handle.c == array);
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 unsigned expected = 0;
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 cx_foreach(unsigned *, e, iter) {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 CX_TEST_ASSERT(iter.index == expected);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 CX_TEST_ASSERT(*e == expected);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 CX_TEST_ASSERT(iter.elem_handle == &array[expected]);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 expected++;
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 CX_TEST_ASSERT(expected == size);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81
1070
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
82 CX_TEST(test_iterator_iterate_pointers) {
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
83 unsigned array[20];
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
84 unsigned* ptr_array[20];
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
85 size_t size = cx_nmemb(array);
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
86 for (unsigned i = 0 ; i < size ; i++) {
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
87 array[i] = 3*i;
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
88 ptr_array[i] = &array[i];
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
89 }
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
90
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
91 CxIterator iter = cxIteratorPtr(ptr_array, size);
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
92 CX_TEST_DO {
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
93 CX_TEST_ASSERT(iter.elem_size == sizeof(void*));
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
94 CX_TEST_ASSERT(iter.elem_count == size);
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
95 CX_TEST_ASSERT(iter.src_handle.c == ptr_array);
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
96 unsigned idx = 0;
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
97 cx_foreach(unsigned *, e, iter) {
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
98 CX_TEST_ASSERT(iter.index == idx);
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
99 CX_TEST_ASSERT(*e == array[idx]);
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
100 CX_TEST_ASSERT(iter.elem_handle == &ptr_array[idx]);
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
101 idx++;
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
102 }
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
103 CX_TEST_ASSERT(idx == size);
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
104 }
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
105 }
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
106
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 CX_TEST(test_iterator_with_slow_remove) {
951
8f6d6556380a remove dynamic arrays which are not supported by msvc
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
108 unsigned array[20];
8f6d6556380a remove dynamic arrays which are not supported by msvc
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
109 size_t size = cx_nmemb(array);
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 for (unsigned i = 0 ; i < size ; i++) array[i] = i;
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 size_t elem_counts[] = {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 20, 20, 19, 19, 18, 18, 17, 17, 16, 16,
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 15, 15, 14, 14, 13, 13, 12, 12, 11, 11
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 };
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 size_t indices[] = {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 6, 6, 7, 7, 8, 8, 9, 9, 10
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 };
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 unsigned expected_result[] = {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 0, 2, 4, 6, 8, 10, 12, 14, 16, 18
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 };
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 851
diff changeset
124 CxIterator iter = cxMutIterator(array, sizeof(unsigned), size, true);
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 CX_TEST_DO {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 unsigned expected = 0;
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 cx_foreach(unsigned *, e, iter) {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 CX_TEST_ASSERT(*e == expected);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 CX_TEST_ASSERT(iter.index == indices[expected]);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 CX_TEST_ASSERT(iter.elem_size == sizeof(unsigned));
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 CX_TEST_ASSERT(iter.elem_count == elem_counts[expected]);
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 851
diff changeset
132 CX_TEST_ASSERT(iter.src_handle.m == array);
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 CX_TEST_ASSERT(iter.elem_handle == &array[indices[expected]]);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 expected++;
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135 if (expected % 2 == 0) {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 cxIteratorFlagRemoval(iter);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 CX_TEST_ASSERT(expected == 20);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 CX_TEST_ASSERT(iter.index == 10);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 CX_TEST_ASSERT(iter.elem_count == 10);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 for (unsigned i = 0 ; i < 9 ; i++) {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 CX_TEST_ASSERT(array[i] == expected_result[i]);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 CX_TEST(test_iterator_with_fast_remove) {
951
8f6d6556380a remove dynamic arrays which are not supported by msvc
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
149 unsigned array[20];
8f6d6556380a remove dynamic arrays which are not supported by msvc
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
150 size_t size = cx_nmemb(array);
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151 for (unsigned i = 0 ; i < size ; i++) array[i] = i;
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 size_t elem_counts[] = {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154 20, 20, 19, 19, 18, 18, 17, 17, 16, 16,
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 15, 15, 14, 14, 13, 13, 12, 12, 11, 11
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 };
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 size_t indices[] = {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159 6, 6, 7, 7, 8, 8, 9, 9, 10
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 };
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161 unsigned expected_result[] = {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162 0, 19, 18, 17, 16, 15, 14, 13, 12, 11
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163 };
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
164 unsigned expected_visits[] = {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 0, 1, 19, 2, 18, 3, 17, 4, 16, 5,
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
166 15, 6, 14, 7, 13, 8, 12, 9, 11, 10
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
167 };
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
168
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 851
diff changeset
169 CxIterator iter = cxMutIterator(array, sizeof(unsigned), size, false);
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170 CX_TEST_DO {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
171 unsigned expected = 0;
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172 cx_foreach(unsigned *, e, iter) {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173 CX_TEST_ASSERT(*e == expected_visits[expected]);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174 CX_TEST_ASSERT(iter.index == indices[expected]);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 CX_TEST_ASSERT(iter.elem_size == sizeof(unsigned));
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 CX_TEST_ASSERT(iter.elem_count == elem_counts[expected]);
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 851
diff changeset
177 CX_TEST_ASSERT(iter.src_handle.m == array);
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178 CX_TEST_ASSERT(iter.elem_handle == &array[indices[expected]]);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179 expected++;
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180 if (expected % 2 == 0) {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181 cxIteratorFlagRemoval(iter);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184 CX_TEST_ASSERT(expected == 20);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185 CX_TEST_ASSERT(iter.index == 10);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
186 CX_TEST_ASSERT(iter.elem_count == 10);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187 for (unsigned i = 0 ; i < 9 ; i++) {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
188 CX_TEST_ASSERT(array[i] == expected_result[i]);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
189 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 CxTestSuite *cx_test_suite_iterator(void) {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 CxTestSuite *suite = cx_test_suite_new("iterator");
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 cx_test_register(suite, test_iterator_create);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 cx_test_register(suite, test_iterator_iterate);
1070
0a5a356a4486 add array iterator over pointer arrays
Mike Becker <universe@uap-core.de>
parents: 951
diff changeset
198 cx_test_register(suite, test_iterator_iterate_pointers);
850
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 cx_test_register(suite, test_iterator_with_slow_remove);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
200 cx_test_register(suite, test_iterator_with_fast_remove);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
201
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
202 return suite;
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
204

mercurial