tests/test_list.c

changeset 818
2be8fe3d5a2d
parent 807
c8d692131b1e
child 819
5da2ead43077
equal deleted inserted replaced
817:949908c97474 818:2be8fe3d5a2d
33 33
34 #include "cx/array_list.h" 34 #include "cx/array_list.h"
35 #include "cx/linked_list.h" 35 #include "cx/linked_list.h"
36 36
37 #include <stdarg.h> 37 #include <stdarg.h>
38
39 CX_TEST(test_array_add) {
40 int stackspace[5] = {1,1,2,3,5};
41 int *stackarray = stackspace;
42 size_t stackarray_size = 3;
43 size_t stackarray_capacity = 5;
44 int *heaparray = calloc(5, sizeof(int));
45 heaparray[0] = 2;
46 heaparray[1] = 3;
47 heaparray[2] = 5;
48 heaparray[3] = 7;
49 heaparray[4] = 11;
50 size_t heaparray_size = 3;
51 size_t heaparray_capacity = 5;
52 int elem = 8, elem2 = 47;
53 enum cx_array_copy_result result;
54 CX_TEST_DO {
55 result = cx_array_add(&stackarray, &stackarray_size, &stackarray_capacity, sizeof(int), &elem, NULL);
56 CX_TEST_ASSERT(result == CX_ARRAY_COPY_SUCCESS);
57 CX_TEST_ASSERT(stackarray[0] == 1);
58 CX_TEST_ASSERT(stackarray[1] == 1);
59 CX_TEST_ASSERT(stackarray[2] == 2);
60 CX_TEST_ASSERT(stackarray[3] == 8);
61 CX_TEST_ASSERT(stackarray[4] == 5);
62 CX_TEST_ASSERT(stackarray_size == 4);
63 CX_TEST_ASSERT(stackarray_capacity == 5);
64
65 stackarray_size = 5;
66 result = cx_array_add(&stackarray, &stackarray_size, &stackarray_capacity, sizeof(int), &elem2, NULL);
67 CX_TEST_ASSERT(result == CX_ARRAY_COPY_REALLOC_NOT_SUPPORTED);
68 CX_TEST_ASSERT(stackarray[0] == 1);
69 CX_TEST_ASSERT(stackarray[1] == 1);
70 CX_TEST_ASSERT(stackarray[2] == 2);
71 CX_TEST_ASSERT(stackarray[3] == 8);
72 CX_TEST_ASSERT(stackarray[4] == 5);
73 CX_TEST_ASSERT(stackarray_size == 5);
74 CX_TEST_ASSERT(stackarray_capacity == 5);
75
76 result = cx_array_add(&heaparray, &heaparray_size, &heaparray_capacity, sizeof(int), &elem, cx_array_default_reallocator);
77 CX_TEST_ASSERT(result == CX_ARRAY_COPY_SUCCESS);
78 CX_TEST_ASSERT(heaparray[0] == 2);
79 CX_TEST_ASSERT(heaparray[1] == 3);
80 CX_TEST_ASSERT(heaparray[2] == 5);
81 CX_TEST_ASSERT(heaparray[3] == 8);
82 CX_TEST_ASSERT(heaparray[4] == 11);
83 CX_TEST_ASSERT(heaparray_size == 4);
84 CX_TEST_ASSERT(heaparray_capacity == 5);
85
86 heaparray_size = 5;
87 result = cx_array_add(&heaparray, &heaparray_size, &heaparray_capacity, sizeof(int), &elem2, cx_array_default_reallocator);
88 CX_TEST_ASSERT(result == CX_ARRAY_COPY_SUCCESS);
89 CX_TEST_ASSERT(heaparray[0] == 2);
90 CX_TEST_ASSERT(heaparray[1] == 3);
91 CX_TEST_ASSERT(heaparray[2] == 5);
92 CX_TEST_ASSERT(heaparray[3] == 8);
93 CX_TEST_ASSERT(heaparray[4] == 11);
94 CX_TEST_ASSERT(heaparray[5] == 47);
95 CX_TEST_ASSERT(heaparray_size == 6);
96 CX_TEST_ASSERT(heaparray_capacity >= 6);
97 }
98 free(heaparray);
99 }
38 100
39 typedef struct node { 101 typedef struct node {
40 struct node *next; 102 struct node *next;
41 struct node *prev; 103 struct node *prev;
42 int data; 104 int data;
1341 free(testdata); 1403 free(testdata);
1342 }) 1404 })
1343 1405
1344 CxTestSuite *cx_test_suite_array_list(void) { 1406 CxTestSuite *cx_test_suite_array_list(void) {
1345 CxTestSuite *suite = cx_test_suite_new("array_list"); 1407 CxTestSuite *suite = cx_test_suite_new("array_list");
1408
1409 cx_test_register(suite, test_array_add);
1346 1410
1347 cx_test_register(suite, test_list_arl_create); 1411 cx_test_register(suite, test_list_arl_create);
1348 cx_test_register(suite, test_list_arl_create_simple); 1412 cx_test_register(suite, test_list_arl_create_simple);
1349 cx_test_register(suite, test_list_arl_create_simple_for_pointers); 1413 cx_test_register(suite, test_list_arl_create_simple_for_pointers);
1350 cx_test_register(suite, test_list_parl_destroy_no_destr); 1414 cx_test_register(suite, test_list_parl_destroy_no_destr);

mercurial