libamxc  1.10.3
C Generic Data Containers
test_amxc_array.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 
55 #include <stdio.h>
56 #include <stdarg.h>
57 #include <stddef.h>
58 #include <setjmp.h>
59 #include <cmocka.h>
60 
61 #include <amxc/amxc_array.h>
62 
63 #include "test_amxc_array.h"
64 
65 #include <amxc/amxc_macros.h>
66 static int counter = 0;
67 
68 static amxc_array_t* array1 = NULL;
69 char data[] = "abcdefghij";
70 
71 static void amxc_reset_test_array() {
72  amxc_array_clean(array1, NULL);
77 }
78 
80  counter++;
81  assert_ptr_not_equal(it->data, NULL);
82 }
83 
84 int test_amxc_array_setup(UNUSED void** state) {
85  assert_int_equal(amxc_array_new(&array1, 10), 0);
86  assert_ptr_not_equal(array1, NULL);
87  assert_ptr_not_equal(array1->buffer, NULL);
88 
90  assert_int_equal(array1->first_used, 3);
91  assert_int_equal(array1->last_used, 3);
92 
94  assert_int_equal(array1->first_used, 3);
95  assert_int_equal(array1->last_used, 6);
96 
98  assert_int_equal(array1->first_used, 3);
99  assert_int_equal(array1->last_used, 9);
100 
101  return 0;
102 }
103 
104 int test_amxc_array_teardown(UNUSED void** state) {
105  amxc_array_delete(&array1, NULL);
106 
107  return 0;
108 }
109 
110 
112  amxc_array_t* array = NULL;
113 
114  // passing NULL pointers should not lead to segfault
115  assert_int_not_equal(amxc_array_new(NULL, 0), 0);
116  assert_ptr_equal(array, NULL);
117  amxc_array_delete(NULL, NULL);
118 }
119 
121  // passing NULL pointers should not lead to segfault
122  assert_int_equal(amxc_array_init(NULL, 0), -1);
123  amxc_array_clean(NULL, NULL);
124 }
125 
126 void test_amxc_array_init_clean(UNUSED void** state) {
128 
129  // initialize array with 0 items
130  assert_int_equal(amxc_array_init(&array, 0), 0);
131  assert_ptr_equal(array.buffer, NULL);
132  assert_int_equal(array.items, 0);
133  amxc_array_clean(&array, NULL);
134  assert_int_equal(array.items, 0);
135 
136  // initialize array with 10 items
137  assert_int_equal(amxc_array_init(&array, 10), 0);
138  assert_ptr_not_equal(array.buffer, NULL);
139  assert_int_equal(array.items, 10);
140  amxc_array_clean(&array, NULL);
141  assert_int_equal(array.items, 0);
142 }
143 
144 void test_amxc_array_clean_cb(UNUSED void** state) {
146 
147  // initialize array with 10 items
148  assert_int_equal(amxc_array_init(&array, 10), 0);
149  assert_ptr_not_equal(array.buffer, NULL);
150  assert_int_equal(array.items, 10);
151  counter = 0;
152  amxc_array_clean(&array, NULL);
153  assert_int_equal(counter, 0);
154  assert_int_equal(array.items, 0);
155 
156  // initialize array with 10 items
157  assert_int_equal(amxc_array_init(&array, 10), 0);
158  assert_ptr_not_equal(array.buffer, amxc_check_array_it_delete);
159  assert_int_equal(array.items, 10);
160  const char data[10] = "1234567890";
161  for(unsigned int index = 0; index < 10; index++) {
162  assert_ptr_equal(amxc_array_set_data_at(&array, index, (void*) &data[index]), &array.buffer[index]);
163  }
164  counter = 0;
166  assert_int_equal(array.items, 0);
167  assert_int_equal(counter, 10);
168 }
169 
171  amxc_array_t* array = NULL;
172  amxc_array_it_t* it = NULL;
173 
174  it = amxc_array_get_at(NULL, 5);
175  assert_ptr_equal(it, NULL);
176 
177  assert_int_equal(amxc_array_new(&array, 0), 0);
178  it = amxc_array_get_at(array, 0);
179  assert_ptr_equal(it, NULL);
180 
181  amxc_array_delete(&array, NULL);
182  assert_ptr_equal(array, NULL);
183 }
184 
185 void test_amxc_array_get_at(UNUSED void** state) {
186  amxc_array_it_t* it = NULL;
187  for(int i = 0; i < 10; i++) {
189  assert_ptr_equal(it, &array1->buffer[i]);
190  assert_ptr_equal(it->array, array1);
191  if((i == 0) || ((i % 3) != 0)) {
192  assert_ptr_equal(it->data, NULL);
193  } else {
194  assert_ptr_equal(it->data, &data[i]);
195  }
196  }
197 }
198 
200  amxc_array_it_t* it = NULL;
201  it = amxc_array_get_first(NULL);
202  assert_ptr_equal(it, NULL);
203 }
204 
205 void test_amxc_array_get_first(UNUSED void** state) {
206  amxc_array_it_t* it = NULL;
208  assert_ptr_equal(it, &array1->buffer[3]);
209  assert_ptr_equal(it->array, array1);
210  assert_ptr_equal(it->data, &data[3]);
211 }
212 
214  amxc_array_t* array = NULL;
215  assert_int_not_equal(amxc_array_new(&array, 10), -1);
216  assert_ptr_not_equal(array, NULL);
217  assert_ptr_not_equal(array->buffer, NULL);
218 
219  amxc_array_it_t* it = NULL;
221  assert_ptr_equal(it, NULL);
222 
223  amxc_array_delete(&array, NULL);
224 }
225 
227  amxc_array_it_t* it = NULL;
229  assert_ptr_equal(it, NULL);
230 }
231 
233  amxc_array_it_t* it = NULL;
235  assert_ptr_equal(it, &array1->buffer[0]);
236  assert_ptr_equal(it->array, array1);
237  assert_ptr_equal(it->data, NULL);
238 }
239 
241  amxc_array_it_t* it = NULL;
242  it = amxc_array_get_last(NULL);
243  assert_ptr_equal(it, NULL);
244 }
245 
246 void test_amxc_array_get_last(UNUSED void** state) {
247  amxc_array_it_t* it = NULL;
249  assert_ptr_equal(it, &array1->buffer[9]);
250  assert_ptr_equal(it->array, array1);
251  assert_ptr_equal(it->data, &data[9]);
252 }
253 
255  amxc_array_t* array = NULL;
256  assert_int_not_equal(amxc_array_new(&array, 10), -1);
257  assert_ptr_not_equal(array, NULL);
258  assert_ptr_not_equal(array->buffer, NULL);
259 
260  amxc_array_it_t* it = NULL;
262  assert_ptr_equal(it, NULL);
263 
264  amxc_array_delete(&array, NULL);
265 }
266 
268  amxc_array_it_t* it = NULL;
270  assert_ptr_equal(it, NULL);
271 }
272 
274  amxc_array_it_t* it = NULL;
276  assert_ptr_not_equal(it, NULL);
277  assert_ptr_equal(it, &array1->buffer[8]);
278  assert_ptr_equal(it->array, array1);
279  assert_ptr_equal(it->data, NULL);
280 }
281 
283  for(int i = 0; i < 10; i++) {
285  }
286 
288  assert_ptr_equal(it, NULL);
289 }
290 
292  for(int i = 0; i < 10; i++) {
294  }
295 
297  assert_ptr_equal(it, NULL);
298 }
299 
301  amxc_array_it_t* it = NULL;
302  it = amxc_array_it_get_next(NULL);
303  assert_ptr_equal(it, NULL);
304 }
305 
307  amxc_array_it_t* it = NULL;
309 
311  assert_ptr_equal(it, &array1->buffer[6]);
312  assert_ptr_equal(it->array, array1);
313  assert_ptr_equal(it->data, &data[6]);
314 
316  assert_ptr_equal(it, &array1->buffer[9]);
317  assert_ptr_equal(it->array, array1);
318  assert_ptr_equal(it->data, &data[9]);
319 
321  assert_ptr_equal(it, NULL);
322 }
323 
325  amxc_array_it_t* it = NULL;
327  assert_ptr_equal(it, NULL);
328 }
329 
331  amxc_array_it_t* it = NULL;
333 
335  assert_ptr_equal(it, &array1->buffer[4]);
336  assert_ptr_equal(it->array, array1);
337  assert_ptr_equal(it->data, NULL);
338 
340  assert_ptr_equal(it, &array1->buffer[5]);
341  assert_ptr_equal(it->array, array1);
342  assert_ptr_equal(it->data, NULL);
343 
345  assert_ptr_equal(it, &array1->buffer[7]);
346  assert_ptr_equal(it->array, array1);
347  assert_ptr_equal(it->data, NULL);
348 
350  assert_ptr_equal(it, &array1->buffer[8]);
351  assert_ptr_equal(it->array, array1);
352  assert_ptr_equal(it->data, NULL);
353 
355  assert_ptr_equal(it, NULL);
356 }
357 
359  amxc_array_it_t* it = NULL;
361  assert_ptr_equal(it, NULL);
362 }
363 
365  amxc_array_it_t* it = NULL;
367 
369 
371  assert_ptr_equal(it, &array1->buffer[6]);
372  assert_ptr_equal(it->array, array1);
373  assert_ptr_equal(it->data, &data[6]);
374 
376  assert_ptr_equal(it, &array1->buffer[3]);
377  assert_ptr_equal(it->array, array1);
378  assert_ptr_equal(it->data, &data[3]);
379 
381  assert_ptr_equal(it, &array1->buffer[1]);
382  assert_ptr_equal(it->array, array1);
383  assert_ptr_equal(it->data, &data[1]);
384 
386  assert_ptr_equal(it, NULL);
387 
389 }
390 
392  amxc_array_it_t* it = NULL;
394  assert_ptr_equal(it, NULL);
395 }
396 
398  amxc_array_it_t* it = NULL;
400 
402  assert_ptr_equal(it, &array1->buffer[8]);
403  assert_ptr_equal(it->array, array1);
404  assert_ptr_equal(it->data, NULL);
405 
407  assert_ptr_equal(it, &array1->buffer[7]);
408  assert_ptr_equal(it->array, array1);
409  assert_ptr_equal(it->data, NULL);
410 
412  assert_ptr_equal(it, &array1->buffer[5]);
413  assert_ptr_equal(it->array, array1);
414  assert_ptr_equal(it->data, NULL);
415 
417  assert_ptr_equal(it, &array1->buffer[4]);
418  assert_ptr_equal(it->array, array1);
419  assert_ptr_equal(it->data, NULL);
420 
422  assert_ptr_equal(it, &array1->buffer[2]);
423  assert_ptr_equal(it->array, array1);
424  assert_ptr_equal(it->data, NULL);
425 
427  assert_ptr_equal(it, &array1->buffer[1]);
428  assert_ptr_equal(it->array, array1);
429  assert_ptr_equal(it->data, NULL);
430 
432  assert_ptr_equal(it, &array1->buffer[0]);
433  assert_ptr_equal(it->array, array1);
434  assert_ptr_equal(it->data, NULL);
435 
437  assert_ptr_equal(it, NULL);
438 }
439 
441  assert_ptr_equal(amxc_array_it_get_data(NULL), NULL);
442 }
443 
445  amxc_array_it_t* it = NULL;
447  assert_ptr_equal(amxc_array_it_get_data(it), NULL);
448 
450  assert_ptr_equal(amxc_array_it_get_data(it), NULL);
451 
453  assert_ptr_equal(amxc_array_it_get_data(it), NULL);
454 
456  assert_ptr_equal(amxc_array_it_get_data(it), &data[3]);
457 }
458 
460  assert_int_equal(amxc_array_is_empty(NULL), true);
461 }
462 
463 void test_amxc_array_is_empty(UNUSED void** state) {
464  assert_int_equal(amxc_array_is_empty(array1), false);
465  amxc_array_clean(array1, NULL);
466  assert_int_equal(amxc_array_is_empty(array1), true);
467 }
468 
469 void test_amxc_array_size_null(UNUSED void** state) {
470  assert_int_equal(amxc_array_size(NULL), 0);
471 }
472 
473 void test_amxc_array_size(UNUSED void** state) {
474  assert_int_equal(amxc_array_size(array1), 3);
475 }
476 
478  assert_int_equal(amxc_array_capacity(NULL), 0);
479 }
480 
481 void test_amxc_array_capacity(UNUSED void** state) {
482  assert_int_equal(amxc_array_capacity(array1), 10);
483 }
484 
485 void test_amxc_array_grow_null(UNUSED void** state) {
486  assert_int_equal(amxc_array_grow(NULL, 0), -1);
487 }
488 
489 void test_amxc_array_grow(UNUSED void** state) {
490  assert_int_equal(amxc_array_grow(array1, 10), 0);
491  assert_int_equal(amxc_array_capacity(array1), 20);
492  assert_int_equal(array1->first_used, 3);
493  assert_int_equal(array1->last_used, 9);
494 
495  assert_int_equal(amxc_array_grow(array1, 0), 0);
496  assert_int_equal(amxc_array_capacity(array1), 20);
497  assert_int_equal(array1->first_used, 3);
498  assert_int_equal(array1->last_used, 9);
499 
500  assert_int_equal(amxc_array_shrink(array1, 10, NULL), 0);
501 }
502 
504  assert_int_equal(amxc_array_shrink(NULL, 0, NULL), -1);
505 }
506 
507 void test_amxc_array_shrink(UNUSED void** state) {
508  assert_int_equal(amxc_array_shrink(array1, 15, NULL), -1);
509 
510  assert_int_equal(amxc_array_shrink(array1, 5, NULL), 0);
511  assert_int_equal(amxc_array_capacity(array1), 5);
512  assert_int_equal(array1->first_used, 3);
513  assert_int_equal(array1->last_used, 3);
514 
515  assert_int_equal(amxc_array_shrink(array1, 3, NULL), 0);
516  assert_int_equal(amxc_array_capacity(array1), 2);
517  assert_int_equal(array1->first_used, 0);
518  assert_int_equal(array1->last_used, 0);
519 
520  assert_int_equal(amxc_array_shrink(array1, 2, NULL), 0);
521  assert_int_equal(amxc_array_capacity(array1), 0);
522  assert_int_equal(array1->first_used, 0);
523  assert_int_equal(array1->last_used, 0);
524 
525  assert_int_equal(amxc_array_grow(array1, 10), 0);
527  assert_int_equal(array1->first_used, 3);
528  assert_int_equal(array1->last_used, 3);
529 
531  assert_int_equal(array1->first_used, 3);
532  assert_int_equal(array1->last_used, 6);
533 
535  assert_int_equal(array1->first_used, 3);
536  assert_int_equal(array1->last_used, 9);
537 }
538 
539 void test_amxc_array_shift_null(UNUSED void** state) {
540  assert_int_equal(amxc_array_shift_left(NULL, 0, NULL), -1);
541  assert_int_equal(amxc_array_shift_right(NULL, 0, NULL), -1);
542 }
543 
544 void test_amxc_array_shift_left(UNUSED void** state) {
545  assert_int_equal(amxc_array_shift_left(array1, 15, NULL), -1);
546  assert_int_equal(amxc_array_shift_left(array1, 0, NULL), 0);
547 
548  assert_int_equal(amxc_array_shift_left(array1, 1, NULL), 0);
549  assert_int_equal(amxc_array_capacity(array1), 10);
550  assert_int_equal(amxc_array_size(array1), 3);
551  assert_int_equal(array1->first_used, 2);
552  assert_int_equal(array1->last_used, 8);
553  assert_ptr_equal(array1->buffer[2].data, &data[3]);
554  assert_ptr_equal(array1->buffer[3].data, NULL);
555  assert_ptr_equal(array1->buffer[5].data, &data[6]);
556  assert_ptr_equal(array1->buffer[6].data, NULL);
557  assert_ptr_equal(array1->buffer[8].data, &data[9]);
558  assert_ptr_equal(array1->buffer[9].data, NULL);
559 
560  assert_int_equal(amxc_array_shift_left(array1, 3, NULL), 0);
561  assert_int_equal(amxc_array_capacity(array1), 10);
562  assert_int_equal(amxc_array_size(array1), 2);
563  assert_ptr_equal(array1->buffer[2].data, &data[6]);
564  assert_ptr_equal(array1->buffer[5].data, &data[9]);
565  assert_ptr_equal(array1->buffer[8].data, NULL);
566  assert_int_equal(array1->first_used, 2);
567  assert_int_equal(array1->last_used, 5);
568 
569  assert_int_equal(amxc_array_shift_left(array1, 6, NULL), 0);
570  assert_int_equal(amxc_array_capacity(array1), 10);
571  assert_int_equal(amxc_array_size(array1), 0);
572  assert_int_equal(array1->first_used, 0);
573  assert_int_equal(array1->last_used, 0);
574 }
575 
577  assert_int_equal(amxc_array_shift_left(array1, 10, NULL), 0);
578  assert_int_equal(amxc_array_capacity(array1), 10);
579  assert_int_equal(amxc_array_size(array1), 0);
580  assert_int_equal(array1->first_used, 0);
581  assert_int_equal(array1->last_used, 0);
582  assert_ptr_equal(array1->buffer[3].data, NULL);
583  assert_ptr_equal(array1->buffer[6].data, NULL);
584  assert_ptr_equal(array1->buffer[9].data, NULL);
585 
587 }
588 
590  assert_int_equal(amxc_array_shift_right(array1, 15, NULL), -1);
591  assert_int_equal(amxc_array_shift_right(array1, 0, NULL), 0);
592 
593  assert_int_equal(amxc_array_shift_right(array1, 1, NULL), 0);
594  assert_int_equal(amxc_array_capacity(array1), 10);
595  assert_int_equal(amxc_array_size(array1), 2);
596  assert_int_equal(array1->first_used, 4);
597  assert_int_equal(array1->last_used, 7);
598  assert_ptr_equal(array1->buffer[4].data, &data[3]);
599  assert_ptr_equal(array1->buffer[3].data, NULL);
600  assert_ptr_equal(array1->buffer[7].data, &data[6]);
601  assert_ptr_equal(array1->buffer[6].data, NULL);
602  assert_ptr_equal(array1->buffer[9].data, NULL);
603 
604  assert_int_equal(amxc_array_shift_right(array1, 3, NULL), 0);
605  assert_int_equal(amxc_array_capacity(array1), 10);
606  assert_int_equal(amxc_array_size(array1), 1);
607  assert_ptr_equal(array1->buffer[7].data, &data[3]);
608  assert_ptr_equal(array1->buffer[4].data, NULL);
609  assert_int_equal(array1->first_used, 7);
610  assert_int_equal(array1->last_used, 7);
611 
612  assert_int_equal(amxc_array_shift_right(array1, 3, NULL), 0);
613  assert_int_equal(amxc_array_capacity(array1), 10);
614  assert_int_equal(amxc_array_size(array1), 0);
615  assert_int_equal(array1->first_used, 0);
616  assert_int_equal(array1->last_used, 0);
617 
619 }
620 
622  assert_int_equal(amxc_array_shift_right(array1, 10, NULL), 0);
623  assert_int_equal(amxc_array_capacity(array1), 10);
624  assert_int_equal(amxc_array_size(array1), 0);
625  assert_int_equal(array1->first_used, 0);
626  assert_int_equal(array1->last_used, 0);
627  assert_ptr_equal(array1->buffer[3].data, NULL);
628  assert_ptr_equal(array1->buffer[6].data, NULL);
629  assert_ptr_equal(array1->buffer[9].data, NULL);
630 
632 }
633 
634 
636  assert_ptr_equal(amxc_array_set_data_at(NULL, 0, NULL), NULL);
637 }
638 
639 void test_amxc_array_set_at(UNUSED void** state) {
640  assert_ptr_equal(amxc_array_set_data_at(array1, 2, NULL), &array1->buffer[2]);
641  assert_int_equal(array1->first_used, 3);
642  assert_int_equal(array1->last_used, 9);
643  assert_int_equal(amxc_array_is_empty(array1), false);
644  assert_int_equal(amxc_array_size(array1), 3);
645 
646  assert_ptr_equal(amxc_array_set_data_at(array1, 9, NULL), &array1->buffer[9]);
647  assert_int_equal(array1->first_used, 3);
648  assert_int_equal(array1->last_used, 6);
649  assert_int_equal(amxc_array_is_empty(array1), false);
650  assert_int_equal(amxc_array_size(array1), 2);
651 
652  assert_ptr_equal(amxc_array_set_data_at(array1, 3, NULL), &array1->buffer[3]);
653  assert_int_equal(array1->first_used, 6);
654  assert_int_equal(array1->last_used, 6);
655  assert_int_equal(amxc_array_is_empty(array1), false);
656  assert_int_equal(amxc_array_size(array1), 1);
657 
658  assert_ptr_equal(amxc_array_set_data_at(array1, 6, NULL), &array1->buffer[6]);
659  assert_int_equal(array1->first_used, 0);
660  assert_int_equal(array1->last_used, 0);
661  assert_int_equal(amxc_array_is_empty(array1), true);
662  assert_int_equal(amxc_array_size(array1), 0);
663 
664  assert_ptr_equal(amxc_array_set_data_at(array1, 7, &data[7]), &array1->buffer[7]);
665  assert_int_equal(array1->first_used, 7);
666  assert_int_equal(array1->last_used, 7);
667  assert_int_equal(amxc_array_is_empty(array1), false);
668  assert_int_equal(amxc_array_size(array1), 1);
669 
670  assert_ptr_equal(amxc_array_set_data_at(array1, 2, &data[2]), &array1->buffer[2]);
671  assert_int_equal(array1->first_used, 2);
672  assert_int_equal(array1->last_used, 7);
673  assert_int_equal(amxc_array_is_empty(array1), false);
674  assert_int_equal(amxc_array_size(array1), 2);
675 
676  assert_ptr_equal(amxc_array_set_data_at(array1, 5, &data[5]), &array1->buffer[5]);
677  assert_int_equal(array1->first_used, 2);
678  assert_int_equal(array1->last_used, 7);
679  assert_int_equal(amxc_array_is_empty(array1), false);
680  assert_int_equal(amxc_array_size(array1), 3);
681 
682  assert_ptr_equal(amxc_array_set_data_at(array1, 10, NULL), NULL);
683  assert_ptr_equal(amxc_array_set_data_at(array1, 11, NULL), NULL);
684 
686 }
687 
689  assert_int_equal(amxc_array_it_set_data(NULL, NULL), -1);
691  assert_int_equal(amxc_array_it_set_data(it, NULL), -1);
692  assert_int_equal(array1->first_used, 3);
693  assert_int_equal(amxc_array_grow(array1, 10), 0);
694  it = amxc_array_get_at(array1, 15);
695  assert_int_equal(amxc_array_it_set_data(it, NULL), -1);
696  assert_int_equal(array1->last_used, 9);
697 }
698 
700  amxc_array_it_t* it = NULL;
701 
702  amxc_array_clean(array1, NULL);
704 
706  assert_int_equal(amxc_array_it_set_data(it, &data[0]), 0);
707  assert_int_equal(array1->first_used, 0);
708 
710  assert_int_equal(amxc_array_it_set_data(it, &data[2]), 0);
711  assert_int_equal(array1->first_used, 0);
712  assert_int_equal(array1->last_used, 2);
713 
714  assert_int_equal(amxc_array_grow(array1, 10), 0);
715  it = amxc_array_get_at(array1, 15);
716  assert_int_equal(amxc_array_it_set_data(it, &data[5]), 0);
717  assert_int_equal(array1->last_used, 15);
718 
720 }
721 
723  assert_ptr_equal(amxc_array_append_data(NULL, NULL), NULL);
724 }
725 
728  assert_ptr_equal(it, NULL);
729  assert_int_equal(amxc_array_capacity(array1), 10);
730  assert_int_equal(amxc_array_size(array1), 3);
731  assert_int_equal(array1->last_used, 9);
732 
734  assert_ptr_not_equal(it, NULL);
735  assert_int_equal(amxc_array_capacity(array1), 13);
736  assert_int_equal(amxc_array_size(array1), 4);
737  assert_int_equal(array1->last_used, 10);
738 
740  assert_ptr_not_equal(it, NULL);
741  assert_int_equal(amxc_array_capacity(array1), 13);
742  assert_int_equal(amxc_array_size(array1), 5);
743  assert_int_equal(array1->last_used, 11);
744 
745  amxc_array_clean(array1, NULL);
746  assert_int_equal(amxc_array_capacity(array1), 0);
747 
749  assert_ptr_not_equal(it, NULL);
750  assert_int_equal(amxc_array_capacity(array1), 3);
751  assert_int_equal(amxc_array_size(array1), 1);
752  assert_int_equal(array1->last_used, 0);
753  assert_int_equal(array1->first_used, 0);
754 
756  assert_ptr_not_equal(it, NULL);
757  assert_int_equal(amxc_array_capacity(array1), 3);
758  assert_int_equal(amxc_array_size(array1), 2);
759  assert_int_equal(array1->last_used, 1);
760  assert_int_equal(array1->first_used, 0);
761 
763 }
764 
766  assert_ptr_equal(amxc_array_prepend_data(NULL, NULL), NULL);
767 }
768 
771  assert_ptr_equal(it, NULL);
772  assert_int_equal(amxc_array_capacity(array1), 10);
773  assert_int_equal(amxc_array_size(array1), 3);
774  assert_int_equal(array1->first_used, 3);
775 
777  assert_ptr_not_equal(it, NULL);
778  assert_int_equal(amxc_array_capacity(array1), 10);
779  assert_int_equal(amxc_array_size(array1), 4);
780  assert_int_equal(array1->first_used, 2);
781 
783  assert_ptr_not_equal(it, NULL);
784  assert_int_equal(amxc_array_capacity(array1), 10);
785  assert_int_equal(amxc_array_size(array1), 5);
786  assert_int_equal(array1->first_used, 1);
787 
788  amxc_array_clean(array1, NULL);
789  assert_int_equal(amxc_array_capacity(array1), 0);
790 
792  assert_ptr_not_equal(it, NULL);
793  assert_int_equal(amxc_array_capacity(array1), 3);
794  assert_int_equal(amxc_array_size(array1), 1);
795  assert_int_equal(array1->last_used, 0);
796  assert_int_equal(array1->first_used, 0);
797 
799  assert_ptr_not_equal(it, NULL);
800  assert_int_equal(amxc_array_capacity(array1), 6);
801  assert_int_equal(amxc_array_size(array1), 2);
802  assert_int_equal(array1->last_used, 3);
803  assert_int_equal(array1->first_used, 2);
804 
806 }
807 
809  assert_ptr_equal(amxc_array_take_first_data(NULL), NULL);
810 }
811 
814  assert_ptr_equal(d, &data[3]);
815  assert_int_equal(amxc_array_capacity(array1), 10);
816  assert_int_equal(amxc_array_size(array1), 2);
817  assert_int_equal(array1->first_used, 6);
818 
820  assert_ptr_equal(d, &data[6]);
821  assert_int_equal(amxc_array_capacity(array1), 10);
822  assert_int_equal(amxc_array_size(array1), 1);
823  assert_int_equal(array1->first_used, 9);
824 
826  assert_ptr_equal(d, &data[9]);
827  assert_int_equal(amxc_array_capacity(array1), 10);
828  assert_int_equal(amxc_array_size(array1), 0);
829  assert_int_equal(array1->first_used, 0);
830 
832 }
833 
835  assert_ptr_equal(amxc_array_take_last_data(NULL), NULL);
836 }
837 
840  assert_ptr_equal(d, &data[9]);
841  assert_int_equal(amxc_array_capacity(array1), 10);
842  assert_int_equal(amxc_array_size(array1), 2);
843  assert_int_equal(array1->last_used, 6);
844 
846  assert_ptr_equal(d, &data[6]);
847  assert_int_equal(amxc_array_capacity(array1), 10);
848  assert_int_equal(amxc_array_size(array1), 1);
849  assert_int_equal(array1->last_used, 3);
850 
852  assert_ptr_equal(d, &data[3]);
853  assert_int_equal(amxc_array_capacity(array1), 10);
854  assert_int_equal(amxc_array_size(array1), 0);
855  assert_int_equal(array1->first_used, 0);
856 
858 }
859 
861  assert_int_equal(amxc_array_it_index(NULL), 0);
862 }
863 
865  assert_ptr_equal(amxc_array_it_take_data(NULL), NULL);
867  assert_ptr_equal(amxc_array_it_take_data(it), &data[3]);
868  assert_int_equal(array1->first_used, 6);
869  assert_int_equal(array1->last_used, 9);
870 
872  assert_ptr_equal(amxc_array_it_take_data(it), NULL);
873  assert_int_equal(array1->first_used, 6);
874  assert_int_equal(array1->last_used, 9);
875 
877  assert_ptr_equal(amxc_array_it_take_data(it), &data[9]);
878  assert_int_equal(array1->first_used, 6);
879  assert_int_equal(array1->last_used, 6);
880 
882  assert_ptr_equal(amxc_array_it_take_data(it), &data[6]);
883  assert_int_equal(array1->first_used, 0);
884  assert_int_equal(array1->last_used, 0);
885 
886  assert_int_equal(amxc_array_is_empty(array1), true);
887 }
888 
890  assert_int_equal(amxc_array_it_index(NULL), 0);
891 }
892 
893 void test_amxc_array_it_index(UNUSED void** state) {
894  for(unsigned int index = 0; index < amxc_array_capacity(array1); index++) {
896  assert_int_equal(amxc_array_it_index(it), index);
897  }
898 }
899 
901  char* d1 = amxc_array_it_get_data(it1);
902  char* d2 = amxc_array_it_get_data(it2);
903 
904  if(d1[0] < d2[0]) {
905  return -1;
906  } else if(d1[0] > d2[0]) {
907  return 1;
908  }
909 
910  return 0;
911 }
912 
913 void test_amxc_array_sort(UNUSED void** state) {
915  const char data[16] = "KRATELEPUIMQCXOS";
916  const char sorted[16] = "ACEEIKLMOPQRSTUX";
917 
918  const char data_sets[6][3] = {
919  "ABC", "BCA", "CAB", "CBA", "BAC", "ACB"
920  };
921 
922  // initialize array with 10 items
923  assert_int_equal(amxc_array_init(&array, 20), 0);
924  for(unsigned int index = 0; index < 16; index++) {
925  assert_ptr_equal(amxc_array_set_data_at(&array, index, (void*) &data[index]), &array.buffer[index]);
926  }
927 
928  assert_int_equal(amxc_array_sort(&array, test_cmp), 0);
929  for(unsigned int index = 0; index < 16; index++) {
930  char* d = amxc_array_it_get_data(amxc_array_get_at(&array, index));
931  printf("%c\n", d[0]);
932  assert_int_equal(d[0], sorted[index]);
933  }
934  amxc_array_clean(&array, NULL);
935 
936  assert_int_equal(amxc_array_init(&array, 40), 0);
937  for(unsigned int index = 0; index < 32; index += 2) {
938  assert_ptr_equal(amxc_array_set_data_at(&array, index, (void*) &data[index / 2]), &array.buffer[index]);
939  }
940 
941  assert_int_equal(amxc_array_sort(&array, test_cmp), 0);
942  for(unsigned int index = 0; index < 16; index++) {
943  char* d = amxc_array_it_get_data(amxc_array_get_at(&array, index));
944  printf("%c\n", d[0]);
945  assert_int_equal(d[0], sorted[index]);
946  }
947  assert_int_equal(amxc_array_size(&array), 16);
948  amxc_array_clean(&array, NULL);
949 
950  for(int sets = 0; sets < 6; sets++) {
951  printf("\n %d - ", sets);
952  assert_int_equal(amxc_array_init(&array, 20), 0);
953  for(unsigned int index = 0; index < 3; index++) {
954  assert_ptr_equal(amxc_array_set_data_at(&array, index, (void*) &data_sets[sets][index]), &array.buffer[index]);
955  }
956 
957  assert_int_equal(amxc_array_sort(&array, test_cmp), 0);
958  for(unsigned int index = 0; index < 3; index++) {
959  char* d = amxc_array_it_get_data(amxc_array_get_at(&array, index));
960  printf("%c", d[0]);
961  }
962  amxc_array_clean(&array, NULL);
963  }
964  printf("\n");
965 
966  assert_int_not_equal(amxc_array_sort(NULL, test_cmp), 0);
967  assert_int_equal(amxc_array_init(&array, 20), 0);
968  assert_int_not_equal(amxc_array_sort(&array, NULL), 0);
969  assert_int_equal(amxc_array_sort(&array, test_cmp), 0);
970  amxc_array_clean(&array, NULL);
971  assert_int_equal(amxc_array_init(&array, 0), 0);
972  assert_int_equal(amxc_array_sort(&array, test_cmp), 0);
973  amxc_array_clean(&array, NULL);
974 }
Ambiorix array API header file.
#define UNUSED
Definition: amxc_macros.h:70
amxc_array_it_t * amxc_array_it_get_next_free(const amxc_array_it_t *const reference)
Gets the next free item in the array, starting from the provided array iterator.
Definition: amxc_array_it.c:87
int amxc_array_it_set_data(amxc_array_it_t *const it, void *data)
Sets the data pointer of an array iterator.
amxc_array_it_t * amxc_array_it_get_previous(const amxc_array_it_t *const reference)
Gets the previous used item in the array, starting from the provided array iterator.
amxc_array_it_t * amxc_array_it_get_next(const amxc_array_it_t *const reference)
Gets the next used item in the array, starting from the provided array iterator.
Definition: amxc_array_it.c:66
void * amxc_array_it_take_data(amxc_array_it_t *const it)
Gets and removes a data pointer from the iterator.
amxc_array_it_t * amxc_array_it_get_previous_free(const amxc_array_it_t *const reference)
Gets the previous free item in the array, starting from the provided array iterator.
AMXC_INLINE void * amxc_array_it_get_data(const amxc_array_it_t *const it)
Gets the data pointer of array iterator.
Definition: amxc_array.h:729
unsigned int amxc_array_it_index(const amxc_array_it_t *const it)
Gets the index of the iterator in the array.
amxc_array_it_t * amxc_array_get_last(const amxc_array_t *const array)
Gets the item iterator of the last used item in the array.
Definition: amxc_array.c:546
AMXC_INLINE size_t amxc_array_capacity(const amxc_array_t *const array)
Gets the capacity of the array.
Definition: amxc_array.h:694
int amxc_array_shift_right(amxc_array_t *const array, const size_t items, amxc_array_it_delete_t func)
Shift all items to the right in the array.
Definition: amxc_array.c:323
amxc_array_it_t * amxc_array_set_data_at(amxc_array_t *const array, const unsigned int index, void *data)
Sets data at the given index.
Definition: amxc_array.c:474
void * amxc_array_take_first_data(amxc_array_t *const array)
Takes the data pointer from the first used item in the array.
Definition: amxc_array.c:576
size_t amxc_array_size(const amxc_array_t *const array)
Calculates the number of used items in the array.
Definition: amxc_array.c:415
amxc_array_it_t * amxc_array_get_last_free(const amxc_array_t *const array)
Gets the last free position in the array.
Definition: amxc_array.c:558
int amxc_array_shift_left(amxc_array_t *const array, const size_t items, amxc_array_it_delete_t func)
Shift all items to the left in the array.
Definition: amxc_array.c:361
void * amxc_array_take_last_data(amxc_array_t *const array)
Takes the data pointer from the last used item in the array.
Definition: amxc_array.c:586
int amxc_array_init(amxc_array_t *const array, const size_t items)
Initializes an array.
Definition: amxc_array.c:231
amxc_array_it_t * amxc_array_get_at(const amxc_array_t *const array, const unsigned int index)
Gets the item iterator for the given index.
Definition: amxc_array.c:504
int amxc_array_shrink(amxc_array_t *const array, const size_t items, amxc_array_it_delete_t func)
Shrinks the array.
Definition: amxc_array.c:298
amxc_array_it_t * amxc_array_get_first(const amxc_array_t *const array)
Gets the item iterator of the first used item in the array.
Definition: amxc_array.c:517
amxc_array_it_t * amxc_array_prepend_data(amxc_array_t *const array, void *data)
Adds an item before the first used item in the array.
Definition: amxc_array.c:449
bool amxc_array_is_empty(const amxc_array_t *const array)
Checks that the array is empty.
Definition: amxc_array.c:399
int8_t amxc_array_new(amxc_array_t **array, const size_t items)
Allocates an array.
Definition: amxc_array.c:179
void amxc_array_clean(amxc_array_t *const array, amxc_array_it_delete_t func)
Removes all items from the array.
Definition: amxc_array.c:261
amxc_array_it_t * amxc_array_get_first_free(const amxc_array_t *const array)
Gets the first free position in the array.
Definition: amxc_array.c:529
amxc_array_it_t * amxc_array_append_data(amxc_array_t *const array, void *data)
Adds an item after the last used item in the array.
Definition: amxc_array.c:429
int amxc_array_sort(amxc_array_t *const array, amxc_array_it_cmp_t cmp)
Sorts the content of the array.
Definition: amxc_array.c:596
int amxc_array_grow(amxc_array_t *const array, const size_t items)
Expands the array.
Definition: amxc_array.c:280
void amxc_array_delete(amxc_array_t **array, const amxc_array_it_delete_t func)
Frees the previously allocated array.
Definition: amxc_array.c:213
The array iterator structure.
Definition: amxc_array.h:174
The array structure.
Definition: amxc_array.h:162
struct _amxc_array_it * buffer
Definition: amxc_array.h:166
size_t last_used
Definition: amxc_array.h:165
size_t first_used
Definition: amxc_array.h:164
void test_amxc_array_is_empty(UNUSED void **state)
void test_amxc_array_is_empty_null(UNUSED void **state)
void test_amxc_array_it_get_data_null(UNUSED void **state)
void test_amxc_array_take_last_data_null(UNUSED void **state)
void test_amxc_array_it_get_next_free(UNUSED void **state)
void test_amxc_array_get_at(UNUSED void **state)
void test_amxc_array_capacity(UNUSED void **state)
void test_amxc_array_it_take_data(UNUSED void **state)
static int test_cmp(amxc_array_it_t *it1, amxc_array_it_t *it2)
void test_amxc_array_set_at(UNUSED void **state)
void test_amxc_array_get_last_free_null(UNUSED void **state)
void test_amxc_array_it_get_previous(UNUSED void **state)
void test_amxc_array_prepend_data_null(UNUSED void **state)
void test_amxc_array_get_first_free_full(UNUSED void **state)
void test_amxc_array_get_first_free(UNUSED void **state)
void test_amxc_array_get_at_null(UNUSED void **state)
void test_amxc_array_prepend_data(UNUSED void **state)
void test_amxc_array_get_last_free(UNUSED void **state)
void test_amxc_array_get_last_free_full(UNUSED void **state)
void test_amxc_array_get_first_null(UNUSED void **state)
void test_amxc_array_append_data_null(UNUSED void **state)
void test_amxc_array_get_first_empty(UNUSED void **state)
void test_amxc_array_it_set_data_null(UNUSED void **state)
int test_amxc_array_setup(UNUSED void **state)
static int counter
void test_amxc_array_size(UNUSED void **state)
void test_amxc_array_shift_left_all(UNUSED void **state)
void test_amxc_array_get_last_empty(UNUSED void **state)
void test_amxc_array_it_get_previous_null(UNUSED void **state)
void test_amxc_array_it_get_next_null(UNUSED void **state)
void test_amxc_array_it_get_data(UNUSED void **state)
void test_amxc_array_grow_null(UNUSED void **state)
void test_amxc_array_shift_null(UNUSED void **state)
void test_amxc_array_init_clean(UNUSED void **state)
void test_amxc_array_it_get_next(UNUSED void **state)
static amxc_array_t * array1
int test_amxc_array_teardown(UNUSED void **state)
void test_amxc_array_it_take_data_null(UNUSED void **state)
void test_amxc_array_get_first(UNUSED void **state)
void test_amxc_array_get_last_null(UNUSED void **state)
void test_amxc_array_clean_cb(UNUSED void **state)
void test_amxc_array_sort(UNUSED void **state)
void test_amxc_array_grow(UNUSED void **state)
void test_amxc_array_it_get_previous_free_null(UNUSED void **state)
char data[]
void test_amxc_array_set_at_null(UNUSED void **state)
void test_amxc_array_take_first_data_null(UNUSED void **state)
void test_amxc_array_capacity_null(UNUSED void **state)
void test_amxc_array_shift_left(UNUSED void **state)
void test_amxc_array_size_null(UNUSED void **state)
void test_amxc_array_it_index_null(UNUSED void **state)
void test_amxc_array_get_first_free_null(UNUSED void **state)
void test_amxc_array_it_index(UNUSED void **state)
static void amxc_check_array_it_delete(amxc_array_it_t *it)
void test_amxc_array_append_data(UNUSED void **state)
void test_amxc_array_it_get_previous_free(UNUSED void **state)
void test_amxc_array_shrink(UNUSED void **state)
void test_amxc_array_it_set_data(UNUSED void **state)
void test_amxc_array_new_delete_null(UNUSED void **state)
void test_amxc_array_shift_right_all(UNUSED void **state)
void test_amxc_array_init_clean_null(UNUSED void **state)
void test_amxc_array_take_first_data(UNUSED void **state)
void test_amxc_array_get_last(UNUSED void **state)
void test_amxc_array_take_last_data(UNUSED void **state)
static void amxc_reset_test_array()
void test_amxc_array_shrink_null(UNUSED void **state)
void test_amxc_array_shift_right(UNUSED void **state)
void test_amxc_array_it_get_next_free_null(UNUSED void **state)
static unsigned int array[2006]
static amxc_htable_it_t it[2000]
static amxc_llist_it_t it2
static amxc_llist_it_t it1