test/array_tests.c

branch
feature/array
changeset 354
7fd13b9f8f60
parent 353
135ce35d5108
child 355
d315a068235a
equal deleted inserted replaced
353:135ce35d5108 354:7fd13b9f8f60
54 "array not using the default allocator"); 54 "array not using the default allocator");
55 UCX_TEST_END 55 UCX_TEST_END
56 ucx_array_destroy(&array); 56 ucx_array_destroy(&array);
57 } 57 }
58 58
59 UCX_TEST(test_ucx_array_append) { 59 UCX_TEST(test_ucx_array_append_from) {
60 UcxArray array = ucx_array_new(16, sizeof(int)); 60 UcxArray array = ucx_array_new(16, sizeof(int));
61 int *elements; 61 int *elements;
62 62
63 int x = 42; 63 int x = 42;
64 ucx_array_append(&array, &x); 64 ucx_array_append_from(&array, &x, 1);
65 UCX_TEST_BEGIN 65 UCX_TEST_BEGIN
66 66
67 elements = array.data; 67 elements = array.data;
68 UCX_TEST_ASSERT(elements[0] == 42, "failed"); 68 UCX_TEST_ASSERT(elements[0] == 42, "failed");
69 69
70 x = 13; 70 int y[2] = {13, 37};
71 ucx_array_append(&array, &x); 71 ucx_array_append_from(&array, y, 2);
72 72
73 elements = array.data; 73 elements = array.data;
74 UCX_TEST_ASSERT(array.size == 2, "incorrect size after append"); 74 UCX_TEST_ASSERT(array.size == 3, "incorrect size after append");
75 UCX_TEST_ASSERT(elements[1] == 13, "failed"); 75 UCX_TEST_ASSERT(elements[1] == 13, "failed");
76 UCX_TEST_ASSERT(elements[2] == 37, "failed");
76 UCX_TEST_ASSERT(elements[0] == 42, 77 UCX_TEST_ASSERT(elements[0] == 42,
77 "append corrupted previously inserted data"); 78 "append corrupted previously inserted data");
78 79
79 ucx_array_append(&array, NULL); 80 ucx_array_append_from(&array, NULL, 2);
80 81
81 elements = array.data; 82 elements = array.data;
82 UCX_TEST_ASSERT(array.size == 3, "incorrect size after NULL append"); 83 UCX_TEST_ASSERT(array.size == 5, "incorrect size after NULL append");
83 UCX_TEST_ASSERT(elements[2] == 0, "element is not zeroed"); 84 UCX_TEST_ASSERT(elements[3] == 0, "element is not zeroed");
85 UCX_TEST_ASSERT(elements[4] == 0, "element is not zeroed");
84 UCX_TEST_ASSERT(elements[0] == 42, 86 UCX_TEST_ASSERT(elements[0] == 42,
85 "NULL append corrupted previously inserted data"); 87 "NULL append corrupted previously inserted data");
86 UCX_TEST_ASSERT(elements[1] == 13, 88 UCX_TEST_ASSERT(elements[1] == 13,
87 "NULL append corrupted previously inserted data"); 89 "NULL append corrupted previously inserted data");
88 90 UCX_TEST_ASSERT(elements[2] == 37,
89 UCX_TEST_END 91 "NULL append corrupted previously inserted data");
90 92
91 ucx_array_destroy(&array); 93 UCX_TEST_END
92 } 94
93 95 ucx_array_destroy(&array);
94 UCX_TEST(test_ucx_array_prepend) { 96 }
97
98 UCX_TEST(test_ucx_array_prepend_from) {
95 int *elems; 99 int *elems;
96 UcxArray array = ucx_array_new(16, sizeof(int)); 100 UcxArray array = ucx_array_new(16, sizeof(int));
97 101
98 int x = 42; 102 int x = 42;
99 ucx_array_prepend(&array, &x); 103 ucx_array_prepend_from(&array, &x, 1);
100 UCX_TEST_BEGIN 104 UCX_TEST_BEGIN
101 105
102 elems = array.data; 106 elems = array.data;
103 UCX_TEST_ASSERT(elems[0] == 42, "failed"); 107 UCX_TEST_ASSERT(elems[0] == 42, "failed");
104 108
105 x = 13; 109 int y[2] = {13, 37};
106 ucx_array_prepend(&array, &x); 110 ucx_array_prepend_from(&array, y, 2);
107 111
108 elems = array.data; 112 elems = array.data;
109 UCX_TEST_ASSERT(array.size == 2, "incorrect size after prepend"); 113 UCX_TEST_ASSERT(array.size == 3, "incorrect size after prepend");
110 UCX_TEST_ASSERT(elems[0] == 13, "failed"); 114 UCX_TEST_ASSERT(elems[0] == 13, "failed");
111 UCX_TEST_ASSERT(elems[1] == 42, 115 UCX_TEST_ASSERT(elems[1] == 37, "failed");
116 UCX_TEST_ASSERT(elems[2] == 42,
112 "prepend corrupted previously inserted data"); 117 "prepend corrupted previously inserted data");
113 118
114 ucx_array_prepend(&array, NULL); 119 ucx_array_prepend_from(&array, NULL, 2);
115 120
116 elems = array.data; 121 elems = array.data;
117 UCX_TEST_ASSERT(array.size == 3, "incorrect size after NULL prepend"); 122 UCX_TEST_ASSERT(array.size == 5, "incorrect size after NULL prepend");
118 UCX_TEST_ASSERT(elems[0] == 0, "element is not zeroed"); 123 UCX_TEST_ASSERT(elems[0] == 0, "element is not zeroed");
119 UCX_TEST_ASSERT(elems[1] == 13, 124 UCX_TEST_ASSERT(elems[1] == 0, "element is not zeroed");
125 UCX_TEST_ASSERT(elems[2] == 13,
120 "NULL prepend corrupted previously inserted data"); 126 "NULL prepend corrupted previously inserted data");
121 UCX_TEST_ASSERT(elems[2] == 42, 127 UCX_TEST_ASSERT(elems[3] == 37,
122 "NULL prepend corrupted previously inserted data"); 128 "NULL prepend corrupted previously inserted data");
123 129 UCX_TEST_ASSERT(elems[4] == 42,
124 UCX_TEST_END 130 "NULL prepend corrupted previously inserted data");
125 131
126 ucx_array_destroy(&array); 132 UCX_TEST_END
127 } 133
128 134 ucx_array_destroy(&array);
129 UCX_TEST(test_ucx_array_set) { 135 }
130 int *elems; 136
131 UcxArray array = ucx_array_new(16, sizeof(int)); 137 UCX_TEST(test_ucx_array_set_from) {
132 138 int *elems;
139 UcxArray array = ucx_array_new(16, sizeof(int));
133 140
134 int x = 42; 141 int x = 42;
135 142
136 UCX_TEST_BEGIN 143 UCX_TEST_BEGIN
137 144
138 ucx_array_set(&array, 7, &x); 145 ucx_array_set_from(&array, 7, &x, 1);
139 146
140 elems = array.data; 147 elems = array.data;
141 UCX_TEST_ASSERT(elems[7] == 42, "failed"); 148 UCX_TEST_ASSERT(elems[7] == 42, "failed");
142 UCX_TEST_ASSERT(array.size >= 8, "array not resized on set"); 149 UCX_TEST_ASSERT(array.size >= 8, "array not resized on set");
143 UCX_TEST_ASSERT(array.capacity == 16, "capacity changed unnecessarily"); 150 UCX_TEST_ASSERT(array.capacity == 16, "capacity changed unnecessarily");
144 151
145 x = 13; 152 int y[2] = {13, 37};
146 ucx_array_set(&array, 27, &x); 153 ucx_array_set_from(&array, 27, y, 2);
147 154
148 elems = array.data; 155 elems = array.data;
149 UCX_TEST_ASSERT(elems[27] == 13, "failed"); 156 UCX_TEST_ASSERT(elems[27] == 13, "failed");
150 UCX_TEST_ASSERT(array.size == 28, "array not resized on set"); 157 UCX_TEST_ASSERT(elems[28] == 37, "failed");
151 UCX_TEST_ASSERT(array.capacity == 28, "capacity not grown"); 158 UCX_TEST_ASSERT(array.size == 29, "array not resized on set");
152 159 UCX_TEST_ASSERT(array.capacity == 32, "capacity not grown");
153 ucx_array_set(&array, 7, NULL); 160
161 ucx_array_set_from(&array, 7, NULL, 2);
154 162
155 elems = array.data; 163 elems = array.data;
156 UCX_TEST_ASSERT(elems[7] == 0, "not zeroed on NULL set"); 164 UCX_TEST_ASSERT(elems[7] == 0, "not zeroed on NULL set");
157 165 UCX_TEST_ASSERT(elems[8] == 0, "not zeroed on NULL set");
166
167 UCX_TEST_END
168
169 ucx_array_destroy(&array);
170 }
171
172 UCX_TEST(test_ucx_array_append) {
173 UcxArray array = ucx_array_new(16, sizeof(int));
174 int *elements;
175
176 ucx_array_append(&array, 42);
177 UCX_TEST_BEGIN
178
179 elements = array.data;
180 UCX_TEST_ASSERT(elements[0] == 42, "failed");
181
182 ucx_array_append(&array, 13);
183 ucx_array_append(&array, 37);
184
185 elements = array.data;
186 UCX_TEST_ASSERT(array.size == 3, "incorrect size after append");
187 UCX_TEST_ASSERT(elements[1] == 13, "failed");
188 UCX_TEST_ASSERT(elements[2] == 37, "failed");
189 UCX_TEST_ASSERT(elements[0] == 42,
190 "append corrupted previously inserted data");
191
192 UCX_TEST_END
193
194 ucx_array_destroy(&array);
195 }
196
197 UCX_TEST(test_ucx_array_prepend) {
198 int *elems;
199 UcxArray array = ucx_array_new(16, sizeof(int));
200
201 ucx_array_prepend(&array, 42);
202 UCX_TEST_BEGIN
203
204 elems = array.data;
205 UCX_TEST_ASSERT(elems[0] == 42, "failed");
206
207 ucx_array_prepend(&array, 37);
208 ucx_array_prepend(&array, 13);
209
210 elems = array.data;
211 UCX_TEST_ASSERT(array.size == 3, "incorrect size after prepend");
212 UCX_TEST_ASSERT(elems[0] == 13, "failed");
213 UCX_TEST_ASSERT(elems[1] == 37, "failed");
214 UCX_TEST_ASSERT(elems[2] == 42,
215 "prepend corrupted previously inserted data");
216
217 UCX_TEST_END
218
219 ucx_array_destroy(&array);
220 }
221
222 UCX_TEST(test_ucx_array_set) {
223 int *elems;
224 UcxArray array = ucx_array_new(16, sizeof(int));
225
226 UCX_TEST_BEGIN
227
228 ucx_array_set(&array, 7, 42);
229
230 elems = array.data;
231 UCX_TEST_ASSERT(elems[7] == 42, "failed");
232 UCX_TEST_ASSERT(array.size == 8, "array not resized on set");
233 UCX_TEST_ASSERT(array.capacity == 16, "capacity changed unnecessarily");
234
235 ucx_array_set(&array, 27, 13);
236 ucx_array_set(&array, 28, 37);
237
238 elems = array.data;
239 UCX_TEST_ASSERT(elems[27] == 13, "failed");
240 UCX_TEST_ASSERT(elems[28] == 37, "failed");
241 UCX_TEST_ASSERT(array.size == 29, "array not resized on set");
242 UCX_TEST_ASSERT(array.capacity == 32, "capacity not grown");
243
158 UCX_TEST_END 244 UCX_TEST_END
159 245
160 ucx_array_destroy(&array); 246 ucx_array_destroy(&array);
161 } 247 }
162 248
258 } 344 }
259 345
260 UCX_TEST(test_ucx_array_at) { 346 UCX_TEST(test_ucx_array_at) {
261 UcxArray array = ucx_array_new(16, sizeof(int)); 347 UcxArray array = ucx_array_new(16, sizeof(int));
262 348
263 int x = 42; 349 int x[3] = {42, 13, 5};
264 ucx_array_append(&array, &x); 350 ucx_array_append_from(&array, x, 3);
265 x = 13;
266 ucx_array_append(&array, &x);
267 x = 5;
268 ucx_array_append(&array, &x);
269 351
270 UCX_TEST_BEGIN 352 UCX_TEST_BEGIN
271 353
272 UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 1) == 13, "failed"); 354 UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 1) == 13, "failed");
273 *(int*)ucx_array_at(array, 1) = 80; 355 *(int*)ucx_array_at(array, 1) = 80;
482 564
483 UCX_TEST_BEGIN 565 UCX_TEST_BEGIN
484 566
485 void* oldptr = array.data; 567 void* oldptr = array.data;
486 568
487 ucx_array_append(&array, &x); 569 ucx_array_append(&array, 5);
488 UCX_TEST_ASSERT(array.capacity == 4 && array.data == oldptr, 570 UCX_TEST_ASSERT(array.capacity == 4 && array.data == oldptr,
489 "array should not grow too early"); 571 "array should not grow too early");
490 ucx_array_append(&array, &x); 572 ucx_array_append(&array, 5);
491 elems = array.data; 573 elems = array.data;
492 UCX_TEST_ASSERT(array.capacity == 8, "array did not grow"); 574 UCX_TEST_ASSERT(array.capacity == 8, "array did not grow");
493 UCX_TEST_ASSERT(array.size == 5, "incorrect size after grow"); 575 UCX_TEST_ASSERT(array.size == 5, "incorrect size after grow");
494 UCX_TEST_ASSERT(elems[3] == 5 && elems[4] == 5, "corrupt data"); 576 UCX_TEST_ASSERT(elems[3] == 5 && elems[4] == 5, "corrupt data");
495 577

mercurial