| 75 CX_TEST_ASSERT(arr.data[3] == 8); |
75 CX_TEST_ASSERT(arr.data[3] == 8); |
| 76 CX_TEST_ASSERT(arr.data[4] == 11); |
76 CX_TEST_ASSERT(arr.data[4] == 11); |
| 77 CX_TEST_ASSERT(arr.data[5] == 47); |
77 CX_TEST_ASSERT(arr.data[5] == 47); |
| 78 CX_TEST_ASSERT(arr.size == 6); |
78 CX_TEST_ASSERT(arr.size == 6); |
| 79 CX_TEST_ASSERT(arr.capacity >= 6); |
79 CX_TEST_ASSERT(arr.capacity >= 6); |
| |
80 } |
| |
81 cx_array_free(arr); |
| |
82 } |
| |
83 |
| |
84 CX_TEST(test_array_insert) { |
| |
85 CX_ARRAY(int, arr); |
| |
86 cx_array_init(arr, 5); |
| |
87 arr.data[0] = 2; |
| |
88 arr.data[1] = 3; |
| |
89 arr.data[2] = 5; |
| |
90 arr.data[3] = 7; |
| |
91 arr.size = 4; |
| |
92 int elem = 8, elem2 = 47; |
| |
93 int result; |
| |
94 CX_TEST_DO { |
| |
95 result = cx_array_insert(arr, 3, elem); |
| |
96 CX_TEST_ASSERT(result == 0); |
| |
97 CX_TEST_ASSERT(arr.data[0] == 2); |
| |
98 CX_TEST_ASSERT(arr.data[1] == 3); |
| |
99 CX_TEST_ASSERT(arr.data[2] == 5); |
| |
100 CX_TEST_ASSERT(arr.data[3] == 8); |
| |
101 CX_TEST_ASSERT(arr.data[4] == 7); |
| |
102 CX_TEST_ASSERT(arr.size == 5); |
| |
103 CX_TEST_ASSERT(arr.capacity == 5); |
| |
104 |
| |
105 result = cx_array_insert(arr, 2, elem2); |
| |
106 CX_TEST_ASSERT(result == 0); |
| |
107 CX_TEST_ASSERT(arr.data[0] == 2); |
| |
108 CX_TEST_ASSERT(arr.data[1] == 3); |
| |
109 CX_TEST_ASSERT(arr.data[2] == 47); |
| |
110 CX_TEST_ASSERT(arr.data[3] == 5); |
| |
111 CX_TEST_ASSERT(arr.data[4] == 8); |
| |
112 CX_TEST_ASSERT(arr.data[5] == 7); |
| |
113 CX_TEST_ASSERT(arr.size == 6); |
| |
114 CX_TEST_ASSERT(arr.capacity >= 6); |
| |
115 } |
| |
116 cx_array_free(arr); |
| |
117 } |
| |
118 |
| |
119 CX_TEST(test_array_insert_array) { |
| |
120 CX_ARRAY(int, arr); |
| |
121 cx_array_init(arr, 5); |
| |
122 arr.data[0] = 2; |
| |
123 arr.data[1] = 3; |
| |
124 arr.data[2] = 5; |
| |
125 arr.size = 3; |
| |
126 int arr1[2] = {13, 37}; |
| |
127 int arr2[2] = {47, 11}; |
| |
128 int arr3[2] = {8, 15}; |
| |
129 int result; |
| |
130 CX_TEST_DO { |
| |
131 result = cx_array_insert_array(arr, 1, arr1, 2); |
| |
132 CX_TEST_ASSERT(result == 0); |
| |
133 CX_TEST_ASSERT(arr.size == 5); |
| |
134 CX_TEST_ASSERT(arr.capacity == 5); |
| |
135 CX_TEST_ASSERT(arr.data[0] == 2); |
| |
136 CX_TEST_ASSERT(arr.data[1] == 13); |
| |
137 CX_TEST_ASSERT(arr.data[2] == 37); |
| |
138 CX_TEST_ASSERT(arr.data[3] == 3); |
| |
139 CX_TEST_ASSERT(arr.data[4] == 5); |
| |
140 |
| |
141 result = cx_array_insert_array(arr, 2, arr2, 2); |
| |
142 CX_TEST_ASSERT(result == 0); |
| |
143 CX_TEST_ASSERT(arr.size == 7); |
| |
144 CX_TEST_ASSERT(arr.capacity >= 7); |
| |
145 CX_TEST_ASSERT(arr.data[0] == 2); |
| |
146 CX_TEST_ASSERT(arr.data[1] == 13); |
| |
147 CX_TEST_ASSERT(arr.data[2] == 47); |
| |
148 CX_TEST_ASSERT(arr.data[3] == 11); |
| |
149 CX_TEST_ASSERT(arr.data[4] == 37); |
| |
150 CX_TEST_ASSERT(arr.data[5] == 3); |
| |
151 CX_TEST_ASSERT(arr.data[6] == 5); |
| |
152 |
| |
153 result = cx_array_add_array(arr, arr3, 2); |
| |
154 CX_TEST_ASSERT(result == 0); |
| |
155 CX_TEST_ASSERT(arr.size == 9); |
| |
156 CX_TEST_ASSERT(arr.capacity >= 9); |
| |
157 CX_TEST_ASSERT(arr.data[0] == 2); |
| |
158 CX_TEST_ASSERT(arr.data[1] == 13); |
| |
159 CX_TEST_ASSERT(arr.data[2] == 47); |
| |
160 CX_TEST_ASSERT(arr.data[3] == 11); |
| |
161 CX_TEST_ASSERT(arr.data[4] == 37); |
| |
162 CX_TEST_ASSERT(arr.data[5] == 3); |
| |
163 CX_TEST_ASSERT(arr.data[6] == 5); |
| |
164 CX_TEST_ASSERT(arr.data[7] == 8); |
| |
165 CX_TEST_ASSERT(arr.data[8] == 15); |
| |
166 } |
| |
167 cx_array_free(arr); |
| |
168 } |
| |
169 |
| |
170 CX_TEST(test_array_insert_array_overflow) { |
| |
171 CX_ARRAY(int, arr); |
| |
172 cx_array_init(arr, 5); |
| |
173 int dummy[1]; |
| |
174 int result; |
| |
175 CX_TEST_DO { |
| |
176 // first test: overflow only the memory (but not the capacity) |
| |
177 arr.size = arr.capacity = SIZE_MAX / sizeof(int) - 5; |
| |
178 errno = 0; |
| |
179 result = cx_array_add_array(arr, dummy, 6); |
| |
180 CX_TEST_ASSERT(result != 0); |
| |
181 CX_TEST_ASSERT(errno == EOVERFLOW); |
| |
182 |
| |
183 // first test: overflow the capacity |
| |
184 arr.size = arr.capacity = SIZE_MAX - 5; |
| |
185 errno = 0; |
| |
186 result = cx_array_add_array(arr, dummy, 6); |
| |
187 CX_TEST_ASSERT(result != 0); |
| |
188 CX_TEST_ASSERT(errno == EOVERFLOW); |
| 80 } |
189 } |
| 81 cx_array_free(arr); |
190 cx_array_free(arr); |
| 82 } |
191 } |
| 83 |
192 |
| 84 CX_TEST(test_array_add_capacity_grow_strategy) { |
193 CX_TEST(test_array_add_capacity_grow_strategy) { |
| 3721 CxTestSuite *cx_test_suite_array(void) { |
3830 CxTestSuite *cx_test_suite_array(void) { |
| 3722 CxTestSuite *suite = cx_test_suite_new("array (low-level)"); |
3831 CxTestSuite *suite = cx_test_suite_new("array (low-level)"); |
| 3723 |
3832 |
| 3724 cx_test_register(suite, test_array_add); |
3833 cx_test_register(suite, test_array_add); |
| 3725 cx_test_register(suite, test_array_add_capacity_grow_strategy); |
3834 cx_test_register(suite, test_array_add_capacity_grow_strategy); |
| |
3835 cx_test_register(suite, test_array_insert); |
| |
3836 cx_test_register(suite, test_array_insert_array); |
| |
3837 cx_test_register(suite, test_array_insert_array_overflow); |
| 3726 cx_test_register(suite, test_array_remove); |
3838 cx_test_register(suite, test_array_remove); |
| 3727 cx_test_register(suite, test_array_remove_fast); |
3839 cx_test_register(suite, test_array_remove_fast); |
| 3728 cx_test_register(suite, test_array_remove_array); |
3840 cx_test_register(suite, test_array_remove_array); |
| 3729 cx_test_register(suite, test_array_remove_array_fast); |
3841 cx_test_register(suite, test_array_remove_array_fast); |
| 3730 cx_test_register(suite, test_array_remove_array_fast_ints); |
3842 cx_test_register(suite, test_array_remove_array_fast_ints); |