1.1 --- a/test/array_tests.c Mon Dec 30 09:54:10 2019 +0100 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,642 +0,0 @@ 1.4 -/* 1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 1.6 - * 1.7 - * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved. 1.8 - * 1.9 - * Redistribution and use in source and binary forms, with or without 1.10 - * modification, are permitted provided that the following conditions are met: 1.11 - * 1.12 - * 1. Redistributions of source code must retain the above copyright 1.13 - * notice, this list of conditions and the following disclaimer. 1.14 - * 1.15 - * 2. Redistributions in binary form must reproduce the above copyright 1.16 - * notice, this list of conditions and the following disclaimer in the 1.17 - * documentation and/or other materials provided with the distribution. 1.18 - * 1.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 1.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1.29 - * POSSIBILITY OF SUCH DAMAGE. 1.30 - */ 1.31 - 1.32 -#include "array_tests.h" 1.33 -#include <ucx/utils.h> 1.34 - 1.35 -UCX_TEST(test_ucx_array_destroy) { 1.36 - UcxArray array; 1.37 - ucx_array_init(&array, 16, sizeof(int)); 1.38 - 1.39 - UCX_TEST_BEGIN 1.40 - ucx_array_destroy(&array); 1.41 - UCX_TEST_ASSERT(array.data == NULL, "data pointer not NULL after destroy"); 1.42 - UCX_TEST_ASSERT(array.size == 0, "size not zero after destroy"); 1.43 - UCX_TEST_ASSERT(array.capacity == 0, "capacity not zero after destroy"); 1.44 - UCX_TEST_ASSERT(array.allocator == ucx_default_allocator(), 1.45 - "allocator corrupted during destroy"); 1.46 - UCX_TEST_END 1.47 -} 1.48 - 1.49 -UCX_TEST(test_ucx_array_new) { 1.50 - UcxArray* array = ucx_array_new(16, 47); 1.51 - 1.52 - UCX_TEST_BEGIN 1.53 - UCX_TEST_ASSERT(array->data, "no memory allocated"); 1.54 - UCX_TEST_ASSERT(array->size == 0, "size not initially zero"); 1.55 - UCX_TEST_ASSERT(array->capacity == 16, "capacity not as requested"); 1.56 - UCX_TEST_ASSERT(array->elemsize == 47, "element size not as requested"); 1.57 - UCX_TEST_ASSERT(array->allocator == ucx_default_allocator(), 1.58 - "array not using the default allocator"); 1.59 - UCX_TEST_END 1.60 - ucx_array_free(array); 1.61 -} 1.62 - 1.63 -UCX_TEST(test_ucx_array_append_from) { 1.64 - UcxArray *array = ucx_array_new(16, sizeof(int)); 1.65 - int *elements; 1.66 - 1.67 - int x = 42; 1.68 - ucx_array_append_from(array, &x, 1); 1.69 - UCX_TEST_BEGIN 1.70 - 1.71 - elements = array->data; 1.72 - UCX_TEST_ASSERT(elements[0] == 42, "failed"); 1.73 - 1.74 - int y[2] = {13, 37}; 1.75 - ucx_array_append_from(array, y, 2); 1.76 - 1.77 - elements = array->data; 1.78 - UCX_TEST_ASSERT(array->size == 3, "incorrect size after append"); 1.79 - UCX_TEST_ASSERT(elements[1] == 13, "failed"); 1.80 - UCX_TEST_ASSERT(elements[2] == 37, "failed"); 1.81 - UCX_TEST_ASSERT(elements[0] == 42, 1.82 - "append corrupted previously inserted data"); 1.83 - 1.84 - ucx_array_append_from(array, NULL, 2); 1.85 - 1.86 - elements = array->data; 1.87 - UCX_TEST_ASSERT(array->size == 5, "incorrect size after NULL append"); 1.88 - UCX_TEST_ASSERT(elements[3] == 0, "element is not zeroed"); 1.89 - UCX_TEST_ASSERT(elements[4] == 0, "element is not zeroed"); 1.90 - UCX_TEST_ASSERT(elements[0] == 42, 1.91 - "NULL append corrupted previously inserted data"); 1.92 - UCX_TEST_ASSERT(elements[1] == 13, 1.93 - "NULL append corrupted previously inserted data"); 1.94 - UCX_TEST_ASSERT(elements[2] == 37, 1.95 - "NULL append corrupted previously inserted data"); 1.96 - 1.97 - UCX_TEST_END 1.98 - 1.99 - ucx_array_free(array); 1.100 -} 1.101 - 1.102 -UCX_TEST(test_ucx_array_append_from_struct) { 1.103 - struct teststruct { 1.104 - unsigned long long x; 1.105 - unsigned long long y; 1.106 - unsigned long long z; 1.107 - }; 1.108 - 1.109 - UcxArray *array = ucx_array_new(16, sizeof(struct teststruct)); 1.110 - struct teststruct *elements; 1.111 - 1.112 - struct teststruct data; 1.113 - data.x = 13; data.y = 37; data.z = 47; 1.114 - 1.115 - ucx_array_append_from(array, &data, 1); 1.116 - UCX_TEST_BEGIN 1.117 - 1.118 - elements = array->data; 1.119 - UCX_TEST_ASSERT(elements[0].x == 13, "failed"); 1.120 - UCX_TEST_ASSERT(elements[0].y == 37, "failed"); 1.121 - UCX_TEST_ASSERT(elements[0].z == 47, "failed"); 1.122 - 1.123 - data.x = 0; data.y = 8; data.z = 15; 1.124 - ucx_array_append_from(array, &data, 1); 1.125 - 1.126 - elements = array->data; 1.127 - UCX_TEST_ASSERT(array->size == 2, "incorrect size after append"); 1.128 - UCX_TEST_ASSERT(elements[1].x == 0, "failed"); 1.129 - UCX_TEST_ASSERT(elements[1].y == 8, "failed"); 1.130 - UCX_TEST_ASSERT(elements[1].z == 15, "failed"); 1.131 - 1.132 - UCX_TEST_ASSERT(elements[0].x == 13, 1.133 - "append corrupted previously inserted data"); 1.134 - UCX_TEST_ASSERT(elements[0].y == 37, 1.135 - "append corrupted previously inserted data"); 1.136 - UCX_TEST_ASSERT(elements[0].z == 47, 1.137 - "append corrupted previously inserted data"); 1.138 - 1.139 - UCX_TEST_END 1.140 - 1.141 - ucx_array_destroy(array); 1.142 -} 1.143 - 1.144 -UCX_TEST(test_ucx_array_prepend_from) { 1.145 - int *elems; 1.146 - UcxArray *array = ucx_array_new(16, sizeof(int)); 1.147 - 1.148 - int x = 42; 1.149 - ucx_array_prepend_from(array, &x, 1); 1.150 - UCX_TEST_BEGIN 1.151 - 1.152 - elems = array->data; 1.153 - UCX_TEST_ASSERT(elems[0] == 42, "failed"); 1.154 - 1.155 - int y[2] = {13, 37}; 1.156 - ucx_array_prepend_from(array, y, 2); 1.157 - 1.158 - elems = array->data; 1.159 - UCX_TEST_ASSERT(array->size == 3, "incorrect size after prepend"); 1.160 - UCX_TEST_ASSERT(elems[0] == 13, "failed"); 1.161 - UCX_TEST_ASSERT(elems[1] == 37, "failed"); 1.162 - UCX_TEST_ASSERT(elems[2] == 42, 1.163 - "prepend corrupted previously inserted data"); 1.164 - 1.165 - ucx_array_prepend_from(array, NULL, 2); 1.166 - 1.167 - elems = array->data; 1.168 - UCX_TEST_ASSERT(array->size == 5, "incorrect size after NULL prepend"); 1.169 - UCX_TEST_ASSERT(elems[0] == 0, "element is not zeroed"); 1.170 - UCX_TEST_ASSERT(elems[1] == 0, "element is not zeroed"); 1.171 - UCX_TEST_ASSERT(elems[2] == 13, 1.172 - "NULL prepend corrupted previously inserted data"); 1.173 - UCX_TEST_ASSERT(elems[3] == 37, 1.174 - "NULL prepend corrupted previously inserted data"); 1.175 - UCX_TEST_ASSERT(elems[4] == 42, 1.176 - "NULL prepend corrupted previously inserted data"); 1.177 - 1.178 - UCX_TEST_END 1.179 - 1.180 - ucx_array_free(array); 1.181 -} 1.182 - 1.183 -UCX_TEST(test_ucx_array_set_from) { 1.184 - int *elems; 1.185 - UcxArray *array = ucx_array_new(16, sizeof(int)); 1.186 - 1.187 - int x = 42; 1.188 - 1.189 - UCX_TEST_BEGIN 1.190 - 1.191 - ucx_array_set_from(array, 7, &x, 1); 1.192 - 1.193 - elems = array->data; 1.194 - UCX_TEST_ASSERT(elems[7] == 42, "failed"); 1.195 - UCX_TEST_ASSERT(array->size >= 8, "array not resized on set"); 1.196 - UCX_TEST_ASSERT(array->capacity == 16, "capacity changed unnecessarily"); 1.197 - 1.198 - int y[2] = {13, 37}; 1.199 - ucx_array_set_from(array, 27, y, 2); 1.200 - 1.201 - elems = array->data; 1.202 - UCX_TEST_ASSERT(elems[27] == 13, "failed"); 1.203 - UCX_TEST_ASSERT(elems[28] == 37, "failed"); 1.204 - UCX_TEST_ASSERT(array->size == 29, "array not resized on set"); 1.205 - UCX_TEST_ASSERT(array->capacity == 32, "capacity not grown"); 1.206 - 1.207 - ucx_array_set_from(array, 7, NULL, 2); 1.208 - 1.209 - elems = array->data; 1.210 - UCX_TEST_ASSERT(elems[7] == 0, "not zeroed on NULL set"); 1.211 - UCX_TEST_ASSERT(elems[8] == 0, "not zeroed on NULL set"); 1.212 - 1.213 - UCX_TEST_END 1.214 - 1.215 - ucx_array_free(array); 1.216 -} 1.217 - 1.218 - 1.219 -UCX_TEST(test_ucx_array_equals) { 1.220 - UcxArray *a1 = ucx_array_new(16, sizeof(int32_t)); 1.221 - UcxArray *a2 = ucx_array_new(16, sizeof(int32_t)); 1.222 - UcxArray *a3 = ucx_array_new(16, sizeof(int64_t)); 1.223 - UcxArray *a4 = ucx_array_new(16, sizeof(int32_t)); 1.224 - 1.225 - int32_t *intelems; 1.226 - int64_t *longintelems; 1.227 - 1.228 - a1->size = 5; 1.229 - intelems = a1->data; 1.230 - intelems[0] = 47; 1.231 - intelems[1] = 11; 1.232 - intelems[2] = 0; 1.233 - intelems[3] = 8; 1.234 - intelems[4] = 15; 1.235 - a2->size = 5; 1.236 - intelems = a2->data; 1.237 - intelems[0] = 47; 1.238 - intelems[1] = 11; 1.239 - intelems[2] = 0; 1.240 - intelems[3] = 8; 1.241 - intelems[4] = 15; 1.242 - a3->size = 5; 1.243 - longintelems = a3->data; 1.244 - longintelems[0] = 47; 1.245 - longintelems[1] = 11; 1.246 - longintelems[2] = 0; 1.247 - longintelems[3] = 8; 1.248 - longintelems[4] = 15; 1.249 - a4->size = 5; 1.250 - intelems = a4->data; 1.251 - intelems[0] = 47; 1.252 - intelems[1] = 11; 1.253 - intelems[2] = -6; 1.254 - intelems[3] = 8; 1.255 - intelems[4] = 15; 1.256 - 1.257 - UCX_TEST_BEGIN 1.258 - 1.259 - UCX_TEST_ASSERT(ucx_array_equals(a1, a2, ucx_cmp_int32, NULL), "failed"); 1.260 - UCX_TEST_ASSERT(!ucx_array_equals(a1, a4, ucx_cmp_int32, NULL), "failed"); 1.261 - UCX_TEST_ASSERT(!ucx_array_equals(a4, a1, ucx_cmp_int32, NULL), "failed"); 1.262 - UCX_TEST_ASSERT(!ucx_array_equals(a1, a3, ucx_cmp_int64, NULL), 1.263 - "comparing arrays of different element size shall fail"); 1.264 - UCX_TEST_ASSERT(!ucx_array_equals(a3, a1, ucx_cmp_int64, NULL), 1.265 - "comparing arrays of different element size shall fail"); 1.266 - 1.267 - UCX_TEST_ASSERT(ucx_array_equals(a1, a2, NULL, NULL), 1.268 - "compare using memcmp() failed"); 1.269 - UCX_TEST_ASSERT(!ucx_array_equals(a1, a4, NULL, NULL), 1.270 - "compare using memcmp() failed"); 1.271 - 1.272 - UCX_TEST_END 1.273 - ucx_array_free(a1); 1.274 - ucx_array_free(a2); 1.275 - ucx_array_free(a3); 1.276 - ucx_array_free(a4); 1.277 -} 1.278 - 1.279 -UCX_TEST(test_ucx_array_concat) { 1.280 - UcxArray *a1 = ucx_array_new(16, sizeof(int)); 1.281 - UcxArray *a2 = ucx_array_new(16, sizeof(int)); 1.282 - int *elems; 1.283 - 1.284 - a1->size = 2; 1.285 - elems = a1->data; 1.286 - elems[0] = 47; 1.287 - elems[1] = 11; 1.288 - a2->size = 3; 1.289 - elems = a2->data; 1.290 - elems[0] = 0; 1.291 - elems[1] = 8; 1.292 - elems[2] = 15; 1.293 - 1.294 - UCX_TEST_BEGIN 1.295 - 1.296 - UCX_TEST_ASSERT(!ucx_array_concat(a1, a2), "failed"); 1.297 - UCX_TEST_ASSERT(a1->size == 5, "failed"); 1.298 - elems = a1->data; 1.299 - UCX_TEST_ASSERT(elems[0] == 47, "failed"); 1.300 - UCX_TEST_ASSERT(elems[1] == 11, "failed"); 1.301 - UCX_TEST_ASSERT(elems[2] == 0, "failed"); 1.302 - UCX_TEST_ASSERT(elems[3] == 8, "failed"); 1.303 - UCX_TEST_ASSERT(elems[4] == 15, "failed"); 1.304 - 1.305 - a1->elemsize *= 2; 1.306 - UCX_TEST_ASSERT(ucx_array_concat(a1, a2), 1.307 - "arrays of different element size must not be concatenated"); 1.308 - UCX_TEST_ASSERT(a1->size == 5, 1.309 - "arrays of different element size must not be concatenated"); 1.310 - 1.311 - UCX_TEST_END 1.312 - ucx_array_free(a1); 1.313 - ucx_array_free(a2); 1.314 -} 1.315 - 1.316 -UCX_TEST(test_ucx_array_at) { 1.317 - UcxArray *array = ucx_array_new(16, sizeof(int)); 1.318 - 1.319 - int x[3] = {42, 13, 5}; 1.320 - ucx_array_append_from(array, x, 3); 1.321 - 1.322 - UCX_TEST_BEGIN 1.323 - 1.324 - UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 1) == 13, "failed"); 1.325 - *(int*)ucx_array_at(array, 1) = 80; 1.326 - UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 1) == 80, "assignment failed"); 1.327 - 1.328 - UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 0) == 42, "corrupted data"); 1.329 - UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 2) == 5, "corrupted data"); 1.330 - 1.331 - UCX_TEST_END 1.332 - 1.333 - ucx_array_free(array); 1.334 -} 1.335 - 1.336 -UCX_TEST(test_ucx_array_find) { 1.337 - UcxArray *array = ucx_array_new(16, sizeof(int)); 1.338 - int *elems; 1.339 - 1.340 - array->size = 5; 1.341 - elems = array->data; 1.342 - elems[0] = 47; 1.343 - elems[1] = 11; 1.344 - elems[2] = 0; 1.345 - elems[3] = 8; 1.346 - elems[4] = 15; 1.347 - 1.348 - int x = 8; 1.349 - int y = 90; 1.350 - 1.351 - UCX_TEST_BEGIN 1.352 - 1.353 - UCX_TEST_ASSERT(ucx_array_find(array,(void*)&x,ucx_cmp_int,NULL) == 3, 1.354 - "doesn't find element"); 1.355 - UCX_TEST_ASSERT(ucx_array_find(array,(void*)&y,ucx_cmp_int,NULL) == 5, 1.356 - "finds non-existing element"); 1.357 - 1.358 - UCX_TEST_ASSERT(ucx_array_find(array,(void*)&x,NULL,NULL) == 3, 1.359 - "failed using memcmp()"); 1.360 - UCX_TEST_ASSERT(ucx_array_find(array,(void*)&y,NULL,NULL) == 5, 1.361 - "failed using memcmp()"); 1.362 - 1.363 - UCX_TEST_END 1.364 - ucx_array_free(array); 1.365 -} 1.366 - 1.367 -UCX_TEST(test_ucx_array_contains) { 1.368 - UcxArray *array = ucx_array_new(16, sizeof(int)); 1.369 - int *elems; 1.370 - 1.371 - array->size = 5; 1.372 - elems = array->data; 1.373 - elems[0] = 47; 1.374 - elems[1] = 11; 1.375 - elems[2] = 0; 1.376 - elems[3] = 8; 1.377 - elems[4] = 15; 1.378 - 1.379 - int x = 8; 1.380 - int y = 90; 1.381 - 1.382 - UCX_TEST_BEGIN 1.383 - 1.384 - UCX_TEST_ASSERT(ucx_array_contains(array,(void*)&x,ucx_cmp_int,NULL), 1.385 - "false negative"); 1.386 - UCX_TEST_ASSERT(!ucx_array_contains(array,(void*)&y,ucx_cmp_int,NULL), 1.387 - "false positive"); 1.388 - 1.389 - UCX_TEST_ASSERT(ucx_array_contains(array,(void*)&x,NULL,NULL), 1.390 - "false negative using memcmp()"); 1.391 - UCX_TEST_ASSERT(!ucx_array_contains(array,(void*)&y,NULL,NULL), 1.392 - "false positive using memcmp()"); 1.393 - 1.394 - UCX_TEST_END 1.395 - ucx_array_free(array); 1.396 -} 1.397 - 1.398 -UCX_TEST(test_ucx_array_remove) { 1.399 - UcxArray *array = ucx_array_new(16, sizeof(int)); 1.400 - int *elems; 1.401 - 1.402 - array->size = 5; 1.403 - elems = array->data; 1.404 - elems[0] = 47; 1.405 - elems[1] = 11; 1.406 - elems[2] = 0; 1.407 - elems[3] = 8; 1.408 - elems[4] = 15; 1.409 - 1.410 - UCX_TEST_BEGIN 1.411 - 1.412 - ucx_array_remove(array, 2); 1.413 - elems = array->data; 1.414 - UCX_TEST_ASSERT( 1.415 - elems[0] == 47 && 1.416 - elems[1] == 11 && 1.417 - elems[2] == 8 && 1.418 - elems[3] == 15, 1.419 - "wrong contents after remove"); 1.420 - UCX_TEST_ASSERT(array->size == 4, "wrong size after remove"); 1.421 - 1.422 - ucx_array_remove_fast(array, 1); 1.423 - elems = array->data; 1.424 - UCX_TEST_ASSERT( 1.425 - elems[0] == 47 && 1.426 - elems[1] == 15 && 1.427 - elems[2] == 8, 1.428 - "wrong contents after fast remove"); 1.429 - UCX_TEST_ASSERT(array->size == 3, "wrong size after fast remove"); 1.430 - 1.431 - UCX_TEST_END 1.432 - ucx_array_free(array); 1.433 -} 1.434 - 1.435 -UCX_TEST(test_ucx_array_clone) { 1.436 - UcxArray array; 1.437 - UcxArray copy; 1.438 - ucx_array_init(&array, 16, sizeof(int)); 1.439 - ucx_array_init(©, 4, 2*sizeof(double)); 1.440 - int *elems; 1.441 - 1.442 - array.size = 5; 1.443 - elems = array.data; 1.444 - elems[0] = 47; 1.445 - elems[1] = 11; 1.446 - elems[2] = 0; 1.447 - elems[3] = 8; 1.448 - elems[4] = 15; 1.449 - 1.450 - ucx_array_clone(©, &array); 1.451 - UCX_TEST_BEGIN 1.452 - 1.453 - UCX_TEST_ASSERT(array.data != copy.data, "no true copy"); 1.454 - UCX_TEST_ASSERT(array.size == copy.size, "size mismatch"); 1.455 - UCX_TEST_ASSERT(array.capacity == copy.capacity, "capacity mismatch"); 1.456 - UCX_TEST_ASSERT(array.elemsize == copy.elemsize, "element size mismatch"); 1.457 - UCX_TEST_ASSERT(array.allocator == copy.allocator, "allocator mismatch"); 1.458 - UCX_TEST_ASSERT(ucx_array_equals(&array, ©, ucx_cmp_int, NULL), 1.459 - "contents do not match after clone"); 1.460 - 1.461 - UCX_TEST_END 1.462 - 1.463 - ucx_array_destroy(&array); 1.464 - ucx_array_destroy(©); 1.465 -} 1.466 - 1.467 -static int ucx_cmp_int_reverse(const void* x, const void* y, void* data) { 1.468 - return -ucx_cmp_int(x,y,data); 1.469 -} 1.470 - 1.471 -UCX_TEST(test_ucx_array_sort) { 1.472 - int *elems; 1.473 - 1.474 - UcxArray *array = ucx_array_new(16, sizeof(int)); 1.475 - array->size = 5; 1.476 - elems = array->data; 1.477 - elems[0] = 47; 1.478 - elems[1] = 11; 1.479 - elems[2] = 0; 1.480 - elems[3] = 8; 1.481 - elems[4] = 15; 1.482 - 1.483 - UcxArray *expected = ucx_array_new(16, sizeof(int)); 1.484 - expected->size = 5; 1.485 - elems = expected->data; 1.486 - elems[0] = 0; 1.487 - elems[1] = 8; 1.488 - elems[2] = 11; 1.489 - elems[3] = 15; 1.490 - elems[4] = 47; 1.491 - 1.492 - UcxArray *expectedrev = ucx_array_new(16, sizeof(int)); 1.493 - expectedrev->size = 5; 1.494 - elems = expectedrev->data; 1.495 - elems[0] = 47; 1.496 - elems[1] = 15; 1.497 - elems[2] = 11; 1.498 - elems[3] = 8; 1.499 - elems[4] = 0; 1.500 - 1.501 - 1.502 - UCX_TEST_BEGIN 1.503 - void* original_ptr = array->data; 1.504 - ucx_array_sort(array, ucx_cmp_int, NULL); 1.505 - UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL), "failed"); 1.506 - UCX_TEST_ASSERT(array->size == 5, "size corrupted"); 1.507 - UCX_TEST_ASSERT(array->data == original_ptr, "shall not reallocate"); 1.508 - 1.509 - ucx_array_sort(array, ucx_cmp_int_reverse, NULL); 1.510 - UCX_TEST_ASSERT(ucx_array_equals(array, expectedrev, NULL, NULL), "failed"); 1.511 - 1.512 - ucx_array_reserve(array, 32); 1.513 - ucx_array_reserve(expected, 32); 1.514 - array->size = expected->size = 32; 1.515 - for (size_t i = 0 ; i < 32 ; i++) { 1.516 - ((int*)array->data)[i]= ((i%2==0)?-1:1) * ((int) i); 1.517 - ((int*)expected->data)[i] = (-30+2*i) - (i > 15 ? 1 : 0); 1.518 - } 1.519 - 1.520 - /* dummy third argument to trigger a possible fallback for qsort_s */ 1.521 - ucx_array_sort(array, ucx_cmp_int, array->data); 1.522 - UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL), 1.523 - "failed for bigger arrays"); 1.524 - UCX_TEST_END 1.525 - 1.526 - ucx_array_free(expectedrev); 1.527 - ucx_array_free(expected); 1.528 - ucx_array_free(array); 1.529 -} 1.530 - 1.531 -UCX_TEST(test_ucx_array_shrink) { 1.532 - UcxArray *array = ucx_array_new(16, sizeof(int)); 1.533 - array->size = 4; 1.534 - 1.535 - UCX_TEST_BEGIN 1.536 - UCX_TEST_ASSERT(!ucx_array_shrink(array), "failed"); 1.537 - UCX_TEST_ASSERT(array->capacity == 4, "incorrect capacity after shrink"); 1.538 - UCX_TEST_END 1.539 - ucx_array_free(array); 1.540 -} 1.541 - 1.542 -UCX_TEST(test_ucx_array_resize) { 1.543 - UcxArray *array = ucx_array_new(16, sizeof(int)); 1.544 - array->size = 8; 1.545 - 1.546 - UCX_TEST_BEGIN 1.547 - 1.548 - UCX_TEST_ASSERT(!ucx_array_resize(array, 32), "failed"); 1.549 - UCX_TEST_ASSERT(array->capacity == 32, "incorrect capacity after resize"); 1.550 - UCX_TEST_ASSERT(array->size == 8, "incorrect size after resize"); 1.551 - 1.552 - UCX_TEST_ASSERT(!ucx_array_resize(array, 4), "failed"); 1.553 - UCX_TEST_ASSERT(array->capacity == 4, "incorrect capacity after resize"); 1.554 - UCX_TEST_ASSERT(array->size == 4, "incorrect size after resize"); 1.555 - 1.556 - UCX_TEST_END 1.557 - ucx_array_free(array); 1.558 -} 1.559 - 1.560 -UCX_TEST(test_ucx_array_reserve) { 1.561 - UcxArray *array = ucx_array_new(16, sizeof(int)); 1.562 - 1.563 - UCX_TEST_BEGIN 1.564 - 1.565 - UCX_TEST_ASSERT(!ucx_array_reserve(array, 4), "failed"); 1.566 - UCX_TEST_ASSERT(array->capacity == 16, "reserve shall not shrink"); 1.567 - 1.568 - UCX_TEST_ASSERT(!ucx_array_resize(array, 32), "failed"); 1.569 - UCX_TEST_ASSERT(array->capacity == 32, "incorrect capacity after reserve"); 1.570 - 1.571 - UCX_TEST_END 1.572 - ucx_array_free(array); 1.573 -} 1.574 - 1.575 -UCX_TEST(test_ucx_array_grow) { 1.576 - UcxArray *array = ucx_array_new(16, sizeof(int)); 1.577 - array->size = 12; 1.578 - 1.579 - UCX_TEST_BEGIN 1.580 - 1.581 - UCX_TEST_ASSERT(!ucx_array_grow(array, 4), "failed"); 1.582 - UCX_TEST_ASSERT(array->capacity == 16, "shall be noop if contents fit"); 1.583 - /* subsequent calls shall also be noops */ 1.584 - UCX_TEST_ASSERT(!ucx_array_grow(array, 4), "failed"); 1.585 - UCX_TEST_ASSERT(array->capacity == 16, "shall be noop if contents fit"); 1.586 - 1.587 - UCX_TEST_ASSERT(!ucx_array_grow(array, 6), "failed"); 1.588 - UCX_TEST_ASSERT(array->capacity == 18, "incorrect capacity after grow"); 1.589 - 1.590 - UCX_TEST_END 1.591 - ucx_array_free(array); 1.592 -} 1.593 - 1.594 -UCX_TEST(test_ucx_array_util_set) { 1.595 - size_t capacity = 16; 1.596 - int* array = malloc(sizeof(int)*capacity); 1.597 - int x; 1.598 - 1.599 - UCX_TEST_BEGIN 1.600 - 1.601 - x = 42; 1.602 - ucx_array_util_set(&array, &capacity, sizeof(int), 7, &x); 1.603 - 1.604 - UCX_TEST_ASSERT(array[7] == 42, "failed"); 1.605 - UCX_TEST_ASSERT(capacity == 16, "capacity changed unnecessarily"); 1.606 - 1.607 - x = 13; 1.608 - ucx_array_util_set(&array, &capacity, sizeof(int), 37, &x); 1.609 - x = 37; 1.610 - ucx_array_util_set(&array, &capacity, sizeof(int), 38, &x); 1.611 - 1.612 - UCX_TEST_ASSERT(array[37] == 13, "failed"); 1.613 - UCX_TEST_ASSERT(array[38] == 37, "failed"); 1.614 - UCX_TEST_ASSERT(capacity == 64, "capacity not grown"); 1.615 - 1.616 - UCX_TEST_END 1.617 - 1.618 - free(array); 1.619 -} 1.620 - 1.621 - 1.622 -UCX_TEST(test_ucx_array_util_setptr) { 1.623 - size_t capacity = 16; 1.624 - double** array = malloc(sizeof(double*)*capacity); 1.625 - double x, y, z; 1.626 - 1.627 - UCX_TEST_BEGIN 1.628 - 1.629 - ucx_array_util_setptr(&array, &capacity, 7, &x); 1.630 - 1.631 - UCX_TEST_ASSERT(array[7] == &x, "failed"); 1.632 - UCX_TEST_ASSERT(capacity == 16, "capacity changed unnecessarily"); 1.633 - 1.634 - ucx_array_util_setptr(&array, &capacity, 37, &y); 1.635 - ucx_array_util_setptr(&array, &capacity, 38, &z); 1.636 - 1.637 - UCX_TEST_ASSERT(array[37] == &y, "failed"); 1.638 - UCX_TEST_ASSERT(array[38] == &z, "failed"); 1.639 - UCX_TEST_ASSERT(capacity == 64, "capacity not grown"); 1.640 - 1.641 - UCX_TEST_END 1.642 - 1.643 - free(array); 1.644 -} 1.645 -