Tue, 06 Aug 2019 16:26:46 +0200
removes cumbersome array accessor macros
src/array.c | file | annotate | diff | comparison | revisions | |
src/ucx/array.h | file | annotate | diff | comparison | revisions | |
test/array_tests.c | file | annotate | diff | comparison | revisions | |
test/array_tests.h | file | annotate | diff | comparison | revisions | |
test/main.c | file | annotate | diff | comparison | revisions |
1.1 --- a/src/array.c Thu Jul 11 10:11:43 2019 +0200 1.2 +++ b/src/array.c Tue Aug 06 16:26:46 2019 +0200 1.3 @@ -223,7 +223,7 @@ 1.4 return; 1.5 } 1.6 1.7 - // we need memory for one element 1.8 + /* we need memory for one element */ 1.9 void *value = malloc(array->elemsize); 1.10 1.11 while (start <= mid && rightstart <= end) { 1.12 @@ -231,16 +231,16 @@ 1.13 ucx_array_at(*array, rightstart), data) <= 0) { 1.14 start++; 1.15 } else { 1.16 - // save the value from the right 1.17 + /* save the value from the right */ 1.18 memcpy(value, ucx_array_at(*array, rightstart), array->elemsize); 1.19 1.20 - // shift all left elements one element to the right 1.21 + /* shift all left elements one element to the right */ 1.22 size_t shiftcount = rightstart-start; 1.23 void *startptr = ucx_array_at(*array, start); 1.24 void *dest = ucx_array_at(*array, start+1); 1.25 memmove(dest, startptr, shiftcount*array->elemsize); 1.26 1.27 - // bring the first value from the right to the left 1.28 + /* bring the first value from the right to the left */ 1.29 memcpy(startptr, value, array->elemsize); 1.30 1.31 start++; 1.32 @@ -249,7 +249,7 @@ 1.33 } 1.34 } 1.35 1.36 - // free the temporary memory 1.37 + /* free the temporary memory */ 1.38 free(value); 1.39 } 1.40
2.1 --- a/src/ucx/array.h Thu Jul 11 10:11:43 2019 +0200 2.2 +++ b/src/ucx/array.h Tue Aug 06 16:26:46 2019 +0200 2.3 @@ -199,64 +199,6 @@ 2.4 void *ucx_array_at(UcxArray array, size_t index); 2.5 2.6 /** 2.7 - * Returns an element of the specified type by value. 2.8 - * 2.9 - * This expression can also be assigned to. 2.10 - * 2.11 - * If <code>sizeof(type)</code> does not equal the array's element size, the 2.12 - * behavior is undefined. 2.13 - * If the index is out of bounds, the behavior is undefined. 2.14 - * 2.15 - * @param type the type of the element 2.16 - * @param array the array to retrieve the element from 2.17 - * @param index index of the element to return 2.18 - * @return the requested element 2.19 - * @see ucx_array_at() 2.20 - */ 2.21 -#define ucx_array_at_typed(type, array, index) (((type*)((array).data))[index]) 2.22 - 2.23 -/** 2.24 - * Shorthand for ucx_array_at_typed(). 2.25 - */ 2.26 -#define ucx_array_at_int(arr, i) ucx_array_at_typed(int, arr, i) 2.27 - 2.28 -/** 2.29 - * Shorthand for ucx_array_at_typed(). 2.30 - */ 2.31 -#define ucx_array_at_short(arr, i) ucx_array_at_typed(short, arr, i) 2.32 - 2.33 -/** 2.34 - * Shorthand for ucx_array_at_typed(). 2.35 - */ 2.36 -#define ucx_array_at_longint(arr, i) ucx_array_at_typed(long int, arr, i) 2.37 - 2.38 -/** 2.39 - * Shorthand for ucx_array_at_typed(). 2.40 - */ 2.41 -#define ucx_array_at_uint(arr, i) ucx_array_at_typed(unsigned int, arr, i) 2.42 - 2.43 -/** 2.44 - * Shorthand for ucx_array_at_typed(). 2.45 - */ 2.46 -#define ucx_array_at_ushort(arr, i) ucx_array_at_typed(unsigned short, arr, i) 2.47 - 2.48 -/** 2.49 - * Shorthand for ucx_array_at_typed(). 2.50 - */ 2.51 -#define ucx_array_at_ulongint(arr, i) \ 2.52 - ucx_array_at_typed(unsigned long int, arr, i) 2.53 - 2.54 -/** 2.55 - * Shorthand for ucx_array_get_typed(). 2.56 - */ 2.57 -#define ucx_array_get_float(arr, i) ucx_array_get_typed(float, arr, i) 2.58 - 2.59 -/** 2.60 - * Shorthand for ucx_array_get_typed(). 2.61 - */ 2.62 -#define ucx_array_get_double(arr, i) ucx_array_get_typed(double, arr, i) 2.63 - 2.64 -/** 2.65 * Returns the index of an element containing the specified data. 2.66 * 2.67 * This function uses a cmp_func() to compare the data of each list element 2.68 @@ -290,9 +232,14 @@ 2.69 int ucx_array_contains(UcxArray array, void *elem, cmp_func cmpfnc, void *data); 2.70 2.71 /** 2.72 - * Sorts a UcxArray with an almost in-place merge sort. 2.73 + * Sorts a UcxArray with the best available sort algorithm. 2.74 * 2.75 - * This function uses additional memory for exactly one element. 2.76 + * If present, qsort_s() will be used. Otherwise, if no additional data is 2.77 + * specified, qsort() is used as a fall back. 2.78 + * 2.79 + * If qsort_s() is not available and <code>data</code> is not <code>NULL</code>, 2.80 + * a custom almost in-place merge sort algorithm is used, which uses additional 2.81 + * memory for exactly one element. 2.82 * 2.83 * @param array the array to sort 2.84 * @param cmpfnc the function that shall be used to compare the element data
3.1 --- a/test/array_tests.c Thu Jul 11 10:11:43 2019 +0200 3.2 +++ b/test/array_tests.c Tue Aug 06 16:26:46 2019 +0200 3.3 @@ -58,28 +58,32 @@ 3.4 3.5 UCX_TEST(test_ucx_array_append) { 3.6 UcxArray array = ucx_array_new(16, sizeof(int)); 3.7 + int *elements; 3.8 3.9 int x = 42; 3.10 ucx_array_append(&array, &x); 3.11 UCX_TEST_BEGIN 3.12 3.13 - UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, "failed"); 3.14 + elements = array.data; 3.15 + UCX_TEST_ASSERT(elements[0] == 42, "failed"); 3.16 3.17 x = 13; 3.18 ucx_array_append(&array, &x); 3.19 3.20 + elements = array.data; 3.21 UCX_TEST_ASSERT(array.size == 2, "incorrect size after append"); 3.22 - UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13, "failed"); 3.23 - UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, 3.24 + UCX_TEST_ASSERT(elements[1] == 13, "failed"); 3.25 + UCX_TEST_ASSERT(elements[0] == 42, 3.26 "append corrupted previously inserted data"); 3.27 3.28 ucx_array_append(&array, NULL); 3.29 3.30 + elements = array.data; 3.31 UCX_TEST_ASSERT(array.size == 3, "incorrect size after NULL append"); 3.32 - UCX_TEST_ASSERT(ucx_array_at_int(array, 2) == 0, "element is not zeroed"); 3.33 - UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, 3.34 + UCX_TEST_ASSERT(elements[2] == 0, "element is not zeroed"); 3.35 + UCX_TEST_ASSERT(elements[0] == 42, 3.36 "NULL append corrupted previously inserted data"); 3.37 - UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13, 3.38 + UCX_TEST_ASSERT(elements[1] == 13, 3.39 "NULL append corrupted previously inserted data"); 3.40 3.41 UCX_TEST_END 3.42 @@ -88,29 +92,33 @@ 3.43 } 3.44 3.45 UCX_TEST(test_ucx_array_prepend) { 3.46 + int *elems; 3.47 UcxArray array = ucx_array_new(16, sizeof(int)); 3.48 3.49 int x = 42; 3.50 ucx_array_prepend(&array, &x); 3.51 UCX_TEST_BEGIN 3.52 3.53 - UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, "failed"); 3.54 + elems = array.data; 3.55 + UCX_TEST_ASSERT(elems[0] == 42, "failed"); 3.56 3.57 x = 13; 3.58 ucx_array_prepend(&array, &x); 3.59 3.60 + elems = array.data; 3.61 UCX_TEST_ASSERT(array.size == 2, "incorrect size after prepend"); 3.62 - UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 13, "failed"); 3.63 - UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 42, 3.64 + UCX_TEST_ASSERT(elems[0] == 13, "failed"); 3.65 + UCX_TEST_ASSERT(elems[1] == 42, 3.66 "prepend corrupted previously inserted data"); 3.67 3.68 ucx_array_prepend(&array, NULL); 3.69 3.70 + elems = array.data; 3.71 UCX_TEST_ASSERT(array.size == 3, "incorrect size after NULL prepend"); 3.72 - UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 0, "element is not zeroed"); 3.73 - UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13, 3.74 + UCX_TEST_ASSERT(elems[0] == 0, "element is not zeroed"); 3.75 + UCX_TEST_ASSERT(elems[1] == 13, 3.76 "NULL prepend corrupted previously inserted data"); 3.77 - UCX_TEST_ASSERT(ucx_array_at_int(array, 2) == 42, 3.78 + UCX_TEST_ASSERT(elems[2] == 42, 3.79 "NULL prepend corrupted previously inserted data"); 3.80 3.81 UCX_TEST_END 3.82 @@ -119,27 +127,33 @@ 3.83 } 3.84 3.85 UCX_TEST(test_ucx_array_set) { 3.86 + int *elems; 3.87 UcxArray array = ucx_array_new(16, sizeof(int)); 3.88 3.89 + 3.90 int x = 42; 3.91 3.92 UCX_TEST_BEGIN 3.93 3.94 ucx_array_set(&array, 7, &x); 3.95 - UCX_TEST_ASSERT(ucx_array_at_int(array, 7) == 42, "failed"); 3.96 + 3.97 + elems = array.data; 3.98 + UCX_TEST_ASSERT(elems[7] == 42, "failed"); 3.99 UCX_TEST_ASSERT(array.size >= 8, "array not resized on set"); 3.100 UCX_TEST_ASSERT(array.capacity == 16, "capacity changed unnecessarily"); 3.101 3.102 x = 13; 3.103 ucx_array_set(&array, 27, &x); 3.104 3.105 - UCX_TEST_ASSERT(ucx_array_at_int(array, 27) == 13, "failed"); 3.106 + elems = array.data; 3.107 + UCX_TEST_ASSERT(elems[27] == 13, "failed"); 3.108 UCX_TEST_ASSERT(array.size == 28, "array not resized on set"); 3.109 UCX_TEST_ASSERT(array.capacity == 28, "capacity not grown"); 3.110 3.111 ucx_array_set(&array, 7, NULL); 3.112 3.113 - UCX_TEST_ASSERT(ucx_array_at_int(array, 7) == 0, "not zeroed on NULL set"); 3.114 + elems = array.data; 3.115 + UCX_TEST_ASSERT(elems[7] == 0, "not zeroed on NULL set"); 3.116 3.117 UCX_TEST_END 3.118 3.119 @@ -152,30 +166,37 @@ 3.120 UcxArray a3 = ucx_array_new(16, sizeof(long int)); 3.121 UcxArray a4 = ucx_array_new(16, sizeof(int)); 3.122 3.123 + int *intelems; 3.124 + long int *longintelems; 3.125 + 3.126 a1.size = 5; 3.127 - ucx_array_at_int(a1, 0) = 47; 3.128 - ucx_array_at_int(a1, 1) = 11; 3.129 - ucx_array_at_int(a1, 2) = 0; 3.130 - ucx_array_at_int(a1, 3) = 8; 3.131 - ucx_array_at_int(a1, 4) = 15; 3.132 + intelems = a1.data; 3.133 + intelems[0] = 47; 3.134 + intelems[1] = 11; 3.135 + intelems[2] = 0; 3.136 + intelems[3] = 8; 3.137 + intelems[4] = 15; 3.138 a2.size = 5; 3.139 - ucx_array_at_int(a2, 0) = 47; 3.140 - ucx_array_at_int(a2, 1) = 11; 3.141 - ucx_array_at_int(a2, 2) = 0; 3.142 - ucx_array_at_int(a2, 3) = 8; 3.143 - ucx_array_at_int(a2, 4) = 15; 3.144 + intelems = a2.data; 3.145 + intelems[0] = 47; 3.146 + intelems[1] = 11; 3.147 + intelems[2] = 0; 3.148 + intelems[3] = 8; 3.149 + intelems[4] = 15; 3.150 a3.size = 5; 3.151 - ucx_array_at_longint(a3, 0) = 47; 3.152 - ucx_array_at_longint(a3, 1) = 11; 3.153 - ucx_array_at_longint(a3, 2) = 0; 3.154 - ucx_array_at_longint(a3, 3) = 8; 3.155 - ucx_array_at_longint(a3, 4) = 15; 3.156 + longintelems = a3.data; 3.157 + longintelems[0] = 47; 3.158 + longintelems[1] = 11; 3.159 + longintelems[2] = 0; 3.160 + longintelems[3] = 8; 3.161 + longintelems[4] = 15; 3.162 a4.size = 5; 3.163 - ucx_array_at_int(a4, 0) = 47; 3.164 - ucx_array_at_int(a4, 1) = 11; 3.165 - ucx_array_at_int(a4, 2) = -6; 3.166 - ucx_array_at_int(a4, 3) = 8; 3.167 - ucx_array_at_int(a4, 4) = 15; 3.168 + intelems = a4.data; 3.169 + intelems[0] = 47; 3.170 + intelems[1] = 11; 3.171 + intelems[2] = -6; 3.172 + intelems[3] = 8; 3.173 + intelems[4] = 15; 3.174 3.175 UCX_TEST_BEGIN 3.176 3.177 @@ -202,24 +223,28 @@ 3.178 UCX_TEST(test_ucx_array_concat) { 3.179 UcxArray a1 = ucx_array_new(16, sizeof(int)); 3.180 UcxArray a2 = ucx_array_new(16, sizeof(int)); 3.181 + int *elems; 3.182 3.183 a1.size = 2; 3.184 - ucx_array_at_int(a1, 0) = 47; 3.185 - ucx_array_at_int(a1, 1) = 11; 3.186 + elems = a1.data; 3.187 + elems[0] = 47; 3.188 + elems[1] = 11; 3.189 a2.size = 3; 3.190 - ucx_array_at_int(a2, 0) = 0; 3.191 - ucx_array_at_int(a2, 1) = 8; 3.192 - ucx_array_at_int(a2, 2) = 15; 3.193 + elems = a2.data; 3.194 + elems[0] = 0; 3.195 + elems[1] = 8; 3.196 + elems[2] = 15; 3.197 3.198 UCX_TEST_BEGIN 3.199 3.200 UCX_TEST_ASSERT(!ucx_array_concat(&a1, &a2), "failed"); 3.201 UCX_TEST_ASSERT(a1.size == 5, "failed"); 3.202 - UCX_TEST_ASSERT(ucx_array_at_int(a1, 0) == 47, "failed"); 3.203 - UCX_TEST_ASSERT(ucx_array_at_int(a1, 1) == 11, "failed"); 3.204 - UCX_TEST_ASSERT(ucx_array_at_int(a1, 2) == 0, "failed"); 3.205 - UCX_TEST_ASSERT(ucx_array_at_int(a1, 3) == 8, "failed"); 3.206 - UCX_TEST_ASSERT(ucx_array_at_int(a1, 4) == 15, "failed"); 3.207 + elems = a1.data; 3.208 + UCX_TEST_ASSERT(elems[0] == 47, "failed"); 3.209 + UCX_TEST_ASSERT(elems[1] == 11, "failed"); 3.210 + UCX_TEST_ASSERT(elems[2] == 0, "failed"); 3.211 + UCX_TEST_ASSERT(elems[3] == 8, "failed"); 3.212 + UCX_TEST_ASSERT(elems[4] == 15, "failed"); 3.213 3.214 a1.elemsize *= 2; 3.215 UCX_TEST_ASSERT(ucx_array_concat(&a1, &a2), 3.216 @@ -232,40 +257,17 @@ 3.217 ucx_array_free(&a2); 3.218 } 3.219 3.220 -UCX_TEST(test_ucx_array_at) { 3.221 - UcxArray array = ucx_array_new(16, sizeof(int)); 3.222 - 3.223 - int x = 42; 3.224 - ucx_array_append(&array, &x); 3.225 - x = 13; 3.226 - ucx_array_append(&array, &x); 3.227 - x = 5; 3.228 - ucx_array_append(&array, &x); 3.229 - 3.230 - UCX_TEST_BEGIN 3.231 - 3.232 - UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13, "failed"); 3.233 - ucx_array_at_int(array, 1) = 80; 3.234 - UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 80, "assignment failed"); 3.235 - 3.236 - 3.237 - UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, "corrupted data"); 3.238 - UCX_TEST_ASSERT(ucx_array_at_int(array, 2) == 5, "corrupted data"); 3.239 - 3.240 - UCX_TEST_END 3.241 - 3.242 - ucx_array_free(&array); 3.243 -} 3.244 - 3.245 UCX_TEST(test_ucx_array_find) { 3.246 UcxArray array = ucx_array_new(16, sizeof(int)); 3.247 + int *elems; 3.248 3.249 array.size = 5; 3.250 - ucx_array_at_int(array, 0) = 47; 3.251 - ucx_array_at_int(array, 1) = 11; 3.252 - ucx_array_at_int(array, 2) = 0; 3.253 - ucx_array_at_int(array, 3) = 8; 3.254 - ucx_array_at_int(array, 4) = 15; 3.255 + elems = array.data; 3.256 + elems[0] = 47; 3.257 + elems[1] = 11; 3.258 + elems[2] = 0; 3.259 + elems[3] = 8; 3.260 + elems[4] = 15; 3.261 3.262 int x = 8; 3.263 int y = 90; 3.264 @@ -288,13 +290,15 @@ 3.265 3.266 UCX_TEST(test_ucx_array_contains) { 3.267 UcxArray array = ucx_array_new(16, sizeof(int)); 3.268 + int *elems; 3.269 3.270 array.size = 5; 3.271 - ucx_array_at_int(array, 0) = 47; 3.272 - ucx_array_at_int(array, 1) = 11; 3.273 - ucx_array_at_int(array, 2) = 0; 3.274 - ucx_array_at_int(array, 3) = 8; 3.275 - ucx_array_at_int(array, 4) = 15; 3.276 + elems = array.data; 3.277 + elems[0] = 47; 3.278 + elems[1] = 11; 3.279 + elems[2] = 0; 3.280 + elems[3] = 8; 3.281 + elems[4] = 15; 3.282 3.283 int x = 8; 3.284 int y = 90; 3.285 @@ -317,30 +321,34 @@ 3.286 3.287 UCX_TEST(test_ucx_array_remove) { 3.288 UcxArray array = ucx_array_new(16, sizeof(int)); 3.289 + int *elems; 3.290 3.291 array.size = 5; 3.292 - ucx_array_at_int(array, 0) = 47; 3.293 - ucx_array_at_int(array, 1) = 11; 3.294 - ucx_array_at_int(array, 2) = 0; 3.295 - ucx_array_at_int(array, 3) = 8; 3.296 - ucx_array_at_int(array, 4) = 15; 3.297 + elems = array.data; 3.298 + elems[0] = 47; 3.299 + elems[1] = 11; 3.300 + elems[2] = 0; 3.301 + elems[3] = 8; 3.302 + elems[4] = 15; 3.303 3.304 UCX_TEST_BEGIN 3.305 3.306 ucx_array_remove(&array, 2); 3.307 + elems = array.data; 3.308 UCX_TEST_ASSERT( 3.309 - ucx_array_at_int(array, 0) == 47 && 3.310 - ucx_array_at_int(array, 1) == 11 && 3.311 - ucx_array_at_int(array, 2) == 8 && 3.312 - ucx_array_at_int(array, 3) == 15, 3.313 + elems[0] == 47 && 3.314 + elems[1] == 11 && 3.315 + elems[2] == 8 && 3.316 + elems[3] == 15, 3.317 "wrong contents after remove"); 3.318 UCX_TEST_ASSERT(array.size == 4, "wrong size after remove"); 3.319 3.320 ucx_array_remove_fast(&array, 1); 3.321 + elems = array.data; 3.322 UCX_TEST_ASSERT( 3.323 - ucx_array_at_int(array, 0) == 47 && 3.324 - ucx_array_at_int(array, 1) == 15 && 3.325 - ucx_array_at_int(array, 2) == 8, 3.326 + elems[0] == 47 && 3.327 + elems[1] == 15 && 3.328 + elems[2] == 8, 3.329 "wrong contents after fast remove"); 3.330 UCX_TEST_ASSERT(array.size == 3, "wrong size after fast remove"); 3.331 3.332 @@ -349,15 +357,16 @@ 3.333 } 3.334 3.335 UCX_TEST(test_ucx_array_clone) { 3.336 - 3.337 UcxArray array = ucx_array_new(16, sizeof(int)); 3.338 + int *elems; 3.339 3.340 array.size = 5; 3.341 - ucx_array_at_int(array, 0) = 47; 3.342 - ucx_array_at_int(array, 1) = 11; 3.343 - ucx_array_at_int(array, 2) = 0; 3.344 - ucx_array_at_int(array, 3) = 8; 3.345 - ucx_array_at_int(array, 4) = 15; 3.346 + elems = array.data; 3.347 + elems[0] = 47; 3.348 + elems[1] = 11; 3.349 + elems[2] = 0; 3.350 + elems[3] = 8; 3.351 + elems[4] = 15; 3.352 3.353 UcxArray copy = ucx_array_clone(array); 3.354 UCX_TEST_BEGIN 3.355 @@ -375,22 +384,39 @@ 3.356 ucx_array_free(©); 3.357 } 3.358 3.359 +static int ucx_cmp_int_reverse(const void* x, const void* y, void* data) { 3.360 + return -ucx_cmp_int(x,y,data); 3.361 +} 3.362 + 3.363 UCX_TEST(test_ucx_array_sort) { 3.364 - UcxArray array = ucx_array_new(16, sizeof(int)); 3.365 + int *elems; 3.366 + 3.367 + UcxArray array = ucx_array_new(16, sizeof(int)); 3.368 array.size = 5; 3.369 - ucx_array_at_int(array, 0) = 47; 3.370 - ucx_array_at_int(array, 1) = 11; 3.371 - ucx_array_at_int(array, 2) = 0; 3.372 - ucx_array_at_int(array, 3) = 8; 3.373 - ucx_array_at_int(array, 4) = 15; 3.374 + elems = array.data; 3.375 + elems[0] = 47; 3.376 + elems[1] = 11; 3.377 + elems[2] = 0; 3.378 + elems[3] = 8; 3.379 + elems[4] = 15; 3.380 3.381 UcxArray expected = ucx_array_new(16, sizeof(int)); 3.382 expected.size = 5; 3.383 - ucx_array_at_int(expected, 0) = 0; 3.384 - ucx_array_at_int(expected, 1) = 8; 3.385 - ucx_array_at_int(expected, 2) = 11; 3.386 - ucx_array_at_int(expected, 3) = 15; 3.387 - ucx_array_at_int(expected, 4) = 47; 3.388 + elems = expected.data; 3.389 + elems[0] = 0; 3.390 + elems[1] = 8; 3.391 + elems[2] = 11; 3.392 + elems[3] = 15; 3.393 + elems[4] = 47; 3.394 + 3.395 + UcxArray expectedrev = ucx_array_new(16, sizeof(int)); 3.396 + expectedrev.size = 5; 3.397 + elems = expectedrev.data; 3.398 + elems[0] = 47; 3.399 + elems[1] = 15; 3.400 + elems[2] = 11; 3.401 + elems[3] = 8; 3.402 + elems[4] = 0; 3.403 3.404 3.405 UCX_TEST_BEGIN 3.406 @@ -399,16 +425,20 @@ 3.407 UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL), "failed"); 3.408 UCX_TEST_ASSERT(array.size == 5, "size corrupted"); 3.409 UCX_TEST_ASSERT(array.data == original_ptr, "shall not reallocate"); 3.410 - 3.411 + 3.412 + ucx_array_sort(array, ucx_cmp_int_reverse, NULL); 3.413 + UCX_TEST_ASSERT(ucx_array_equals(array, expectedrev, NULL, NULL), "failed"); 3.414 + 3.415 ucx_array_reserve(&array, 32); 3.416 ucx_array_reserve(&expected, 32); 3.417 array.size = expected.size = 32; 3.418 for (size_t i = 0 ; i < 32 ; i++) { 3.419 - ucx_array_at_int(array, i) = ((i%2==0)?-1:1) * ((int) i); 3.420 - ucx_array_at_int(expected, i) = (-30+2*i) - (i > 15 ? 1 : 0); 3.421 + ((int*)array.data)[i]= ((i%2==0)?-1:1) * ((int) i); 3.422 + ((int*)expected.data)[i] = (-30+2*i) - (i > 15 ? 1 : 0); 3.423 } 3.424 3.425 - ucx_array_sort(array, ucx_cmp_int, NULL); 3.426 + /* dummy third argument to trigger a possible fallback for qsort_s */ 3.427 + ucx_array_sort(array, ucx_cmp_int, array.data); 3.428 UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL), 3.429 "failed for bigger arrays"); 3.430 UCX_TEST_END 3.431 @@ -418,10 +448,12 @@ 3.432 } 3.433 3.434 UCX_TEST(test_ucx_array_autogrow) { 3.435 + int *elems; 3.436 UcxArray array = ucx_array_new(4, sizeof(int)); 3.437 array.size = 3; 3.438 - ucx_array_at_int(array, 0) = 47; 3.439 - ucx_array_at_int(array, 1) = 11; 3.440 + elems = array.data; 3.441 + elems[0] = 47; 3.442 + elems[1] = 11; 3.443 int x = 5; 3.444 3.445 UCX_TEST_BEGIN 3.446 @@ -432,10 +464,10 @@ 3.447 UCX_TEST_ASSERT(array.capacity == 4 && array.data == oldptr, 3.448 "array should not grow too early"); 3.449 ucx_array_append(&array, &x); 3.450 + elems = array.data; 3.451 UCX_TEST_ASSERT(array.capacity == 8, "array did not grow"); 3.452 UCX_TEST_ASSERT(array.size == 5, "incorrect size after grow"); 3.453 - UCX_TEST_ASSERT(ucx_array_at_int(array, 3) == 5 && 3.454 - ucx_array_at_int(array, 3) == 5, "corrupt data"); 3.455 + UCX_TEST_ASSERT(elems[3] == 5 && elems[4] == 5, "corrupt data"); 3.456 3.457 UCX_TEST_END 3.458 ucx_array_free(&array);
4.1 --- a/test/array_tests.h Thu Jul 11 10:11:43 2019 +0200 4.2 +++ b/test/array_tests.h Tue Aug 06 16:26:46 2019 +0200 4.3 @@ -38,7 +38,6 @@ 4.4 4.5 UCX_TEST(test_ucx_array_free); 4.6 UCX_TEST(test_ucx_array_new); 4.7 -UCX_TEST(test_ucx_array_at); 4.8 UCX_TEST(test_ucx_array_append); 4.9 UCX_TEST(test_ucx_array_prepend); 4.10 UCX_TEST(test_ucx_array_set);
5.1 --- a/test/main.c Thu Jul 11 10:11:43 2019 +0200 5.2 +++ b/test/main.c Tue Aug 06 16:26:46 2019 +0200 5.3 @@ -145,7 +145,6 @@ 5.4 /* UcxArray Tests */ 5.5 ucx_test_register(suite, test_ucx_array_free); 5.6 ucx_test_register(suite, test_ucx_array_new); 5.7 - ucx_test_register(suite, test_ucx_array_at); 5.8 ucx_test_register(suite, test_ucx_array_append); 5.9 ucx_test_register(suite, test_ucx_array_prepend); 5.10 ucx_test_register(suite, test_ucx_array_set);