tests/test_iterator.c

Sun, 22 Dec 2024 22:10:04 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 22 Dec 2024 22:10:04 +0100
changeset 1047
40aad3f0bc9e
parent 951
8f6d6556380a
permissions
-rw-r--r--

don't trust that size_t always has word width

it should be the case on all platforms supported by UCX, but it's not strictly defined in POSIX that it must be the case

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 {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 unsigned expected = 0;
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 cx_foreach(unsigned *, e, iter) {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 CX_TEST_ASSERT(iter.index == expected);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 CX_TEST_ASSERT(*e == expected);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 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
73 CX_TEST_ASSERT(iter.elem_count == size);
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 851
diff changeset
74 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
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
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 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
83 unsigned array[20];
8f6d6556380a remove dynamic arrays which are not supported by msvc
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
84 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
85 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
86
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 size_t elem_counts[] = {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 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
89 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
90 };
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 size_t indices[] = {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 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
93 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
94 };
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 unsigned expected_result[] = {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 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
97 };
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 851
diff changeset
99 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
100 CX_TEST_DO {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 unsigned expected = 0;
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 cx_foreach(unsigned *, e, iter) {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 CX_TEST_ASSERT(*e == expected);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 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
105 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
106 CX_TEST_ASSERT(iter.elem_count == elem_counts[expected]);
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 851
diff changeset
107 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
108 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
109 expected++;
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 if (expected % 2 == 0) {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 cxIteratorFlagRemoval(iter);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 CX_TEST_ASSERT(expected == 20);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 CX_TEST_ASSERT(iter.index == 10);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 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
117 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
118 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
119 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 }
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 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
124 unsigned array[20];
8f6d6556380a remove dynamic arrays which are not supported by msvc
Mike Becker <universe@uap-core.de>
parents: 853
diff changeset
125 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
126 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
127
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 size_t elem_counts[] = {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 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
130 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
131 };
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 size_t indices[] = {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 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
134 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
135 };
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 unsigned expected_result[] = {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137 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
138 };
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 unsigned expected_visits[] = {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 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
141 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
142 };
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 851
diff changeset
144 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
145 CX_TEST_DO {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 unsigned expected = 0;
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 cx_foreach(unsigned *, e, iter) {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 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
149 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
150 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
151 CX_TEST_ASSERT(iter.elem_count == elem_counts[expected]);
853
d4baf4dd55c3 simplify iterator structures
Mike Becker <universe@uap-core.de>
parents: 851
diff changeset
152 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
153 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
154 expected++;
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 if (expected % 2 == 0) {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 cxIteratorFlagRemoval(iter);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159 CX_TEST_ASSERT(expected == 20);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 CX_TEST_ASSERT(iter.index == 10);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161 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
162 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
163 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
164 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
166 }
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 CxTestSuite *cx_test_suite_iterator(void) {
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
169 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
170
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
171 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
172 cx_test_register(suite, test_iterator_iterate);
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173 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
174 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
175
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 return suite;
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 }
b2bc48c2b251 add iterator over raw C arrays - closes #389
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178

mercurial