test/array_tests.c

Mon, 30 Dec 2019 09:52:44 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 30 Dec 2019 09:52:44 +0100
changeset 388
871a8ffe6c9d
parent 369
28a8ccc442b0
permissions
-rw-r--r--

merges closed feature/array branch

olaf@9 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@103 3 *
universe@334 4 * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved.
universe@103 5 *
universe@103 6 * Redistribution and use in source and binary forms, with or without
universe@103 7 * modification, are permitted provided that the following conditions are met:
universe@103 8 *
universe@103 9 * 1. Redistributions of source code must retain the above copyright
universe@103 10 * notice, this list of conditions and the following disclaimer.
universe@103 11 *
universe@103 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@103 13 * notice, this list of conditions and the following disclaimer in the
universe@103 14 * documentation and/or other materials provided with the distribution.
universe@103 15 *
universe@103 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@103 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@103 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@103 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@103 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@103 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@103 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@103 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@103 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@103 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@103 26 * POSSIBILITY OF SUCH DAMAGE.
olaf@9 27 */
olaf@9 28
universe@334 29 #include "array_tests.h"
universe@251 30 #include <ucx/utils.h>
olaf@9 31
universe@356 32 UCX_TEST(test_ucx_array_destroy) {
universe@356 33 UcxArray array;
universe@356 34 ucx_array_init(&array, 16, sizeof(int));
universe@334 35
universe@334 36 UCX_TEST_BEGIN
universe@353 37 ucx_array_destroy(&array);
universe@356 38 UCX_TEST_ASSERT(array.data == NULL, "data pointer not NULL after destroy");
universe@356 39 UCX_TEST_ASSERT(array.size == 0, "size not zero after destroy");
universe@356 40 UCX_TEST_ASSERT(array.capacity == 0, "capacity not zero after destroy");
universe@334 41 UCX_TEST_ASSERT(array.allocator == ucx_default_allocator(),
universe@356 42 "allocator corrupted during destroy");
universe@334 43 UCX_TEST_END
universe@334 44 }
universe@334 45
universe@334 46 UCX_TEST(test_ucx_array_new) {
universe@356 47 UcxArray* array = ucx_array_new(16, 47);
universe@334 48
universe@334 49 UCX_TEST_BEGIN
universe@356 50 UCX_TEST_ASSERT(array->data, "no memory allocated");
universe@356 51 UCX_TEST_ASSERT(array->size == 0, "size not initially zero");
universe@356 52 UCX_TEST_ASSERT(array->capacity == 16, "capacity not as requested");
universe@356 53 UCX_TEST_ASSERT(array->elemsize == 47, "element size not as requested");
universe@356 54 UCX_TEST_ASSERT(array->allocator == ucx_default_allocator(),
universe@334 55 "array not using the default allocator");
universe@334 56 UCX_TEST_END
universe@356 57 ucx_array_free(array);
universe@334 58 }
universe@334 59
universe@354 60 UCX_TEST(test_ucx_array_append_from) {
universe@356 61 UcxArray *array = ucx_array_new(16, sizeof(int));
universe@342 62 int *elements;
universe@334 63
universe@334 64 int x = 42;
universe@356 65 ucx_array_append_from(array, &x, 1);
universe@33 66 UCX_TEST_BEGIN
universe@27 67
universe@356 68 elements = array->data;
universe@342 69 UCX_TEST_ASSERT(elements[0] == 42, "failed");
universe@27 70
universe@354 71 int y[2] = {13, 37};
universe@356 72 ucx_array_append_from(array, y, 2);
universe@27 73
universe@356 74 elements = array->data;
universe@356 75 UCX_TEST_ASSERT(array->size == 3, "incorrect size after append");
universe@342 76 UCX_TEST_ASSERT(elements[1] == 13, "failed");
universe@354 77 UCX_TEST_ASSERT(elements[2] == 37, "failed");
universe@342 78 UCX_TEST_ASSERT(elements[0] == 42,
universe@334 79 "append corrupted previously inserted data");
universe@334 80
universe@356 81 ucx_array_append_from(array, NULL, 2);
universe@334 82
universe@356 83 elements = array->data;
universe@356 84 UCX_TEST_ASSERT(array->size == 5, "incorrect size after NULL append");
universe@354 85 UCX_TEST_ASSERT(elements[3] == 0, "element is not zeroed");
universe@354 86 UCX_TEST_ASSERT(elements[4] == 0, "element is not zeroed");
universe@342 87 UCX_TEST_ASSERT(elements[0] == 42,
universe@334 88 "NULL append corrupted previously inserted data");
universe@342 89 UCX_TEST_ASSERT(elements[1] == 13,
universe@334 90 "NULL append corrupted previously inserted data");
universe@354 91 UCX_TEST_ASSERT(elements[2] == 37,
universe@354 92 "NULL append corrupted previously inserted data");
universe@354 93
universe@354 94 UCX_TEST_END
universe@354 95
universe@356 96 ucx_array_free(array);
universe@354 97 }
universe@354 98
universe@366 99 UCX_TEST(test_ucx_array_append_from_struct) {
universe@366 100 struct teststruct {
universe@366 101 unsigned long long x;
universe@366 102 unsigned long long y;
universe@366 103 unsigned long long z;
universe@366 104 };
universe@366 105
universe@366 106 UcxArray *array = ucx_array_new(16, sizeof(struct teststruct));
universe@366 107 struct teststruct *elements;
universe@366 108
universe@366 109 struct teststruct data;
universe@366 110 data.x = 13; data.y = 37; data.z = 47;
universe@366 111
universe@366 112 ucx_array_append_from(array, &data, 1);
universe@366 113 UCX_TEST_BEGIN
universe@366 114
universe@366 115 elements = array->data;
universe@366 116 UCX_TEST_ASSERT(elements[0].x == 13, "failed");
universe@366 117 UCX_TEST_ASSERT(elements[0].y == 37, "failed");
universe@366 118 UCX_TEST_ASSERT(elements[0].z == 47, "failed");
universe@366 119
universe@366 120 data.x = 0; data.y = 8; data.z = 15;
universe@366 121 ucx_array_append_from(array, &data, 1);
universe@366 122
universe@366 123 elements = array->data;
universe@366 124 UCX_TEST_ASSERT(array->size == 2, "incorrect size after append");
universe@366 125 UCX_TEST_ASSERT(elements[1].x == 0, "failed");
universe@366 126 UCX_TEST_ASSERT(elements[1].y == 8, "failed");
universe@366 127 UCX_TEST_ASSERT(elements[1].z == 15, "failed");
universe@366 128
universe@366 129 UCX_TEST_ASSERT(elements[0].x == 13,
universe@366 130 "append corrupted previously inserted data");
universe@366 131 UCX_TEST_ASSERT(elements[0].y == 37,
universe@366 132 "append corrupted previously inserted data");
universe@366 133 UCX_TEST_ASSERT(elements[0].z == 47,
universe@366 134 "append corrupted previously inserted data");
universe@366 135
universe@366 136 UCX_TEST_END
universe@366 137
universe@366 138 ucx_array_destroy(array);
universe@366 139 }
universe@366 140
universe@354 141 UCX_TEST(test_ucx_array_prepend_from) {
universe@354 142 int *elems;
universe@356 143 UcxArray *array = ucx_array_new(16, sizeof(int));
universe@354 144
universe@354 145 int x = 42;
universe@356 146 ucx_array_prepend_from(array, &x, 1);
universe@354 147 UCX_TEST_BEGIN
universe@354 148
universe@356 149 elems = array->data;
universe@354 150 UCX_TEST_ASSERT(elems[0] == 42, "failed");
universe@354 151
universe@354 152 int y[2] = {13, 37};
universe@356 153 ucx_array_prepend_from(array, y, 2);
universe@354 154
universe@356 155 elems = array->data;
universe@356 156 UCX_TEST_ASSERT(array->size == 3, "incorrect size after prepend");
universe@354 157 UCX_TEST_ASSERT(elems[0] == 13, "failed");
universe@354 158 UCX_TEST_ASSERT(elems[1] == 37, "failed");
universe@354 159 UCX_TEST_ASSERT(elems[2] == 42,
universe@354 160 "prepend corrupted previously inserted data");
universe@354 161
universe@356 162 ucx_array_prepend_from(array, NULL, 2);
universe@354 163
universe@356 164 elems = array->data;
universe@356 165 UCX_TEST_ASSERT(array->size == 5, "incorrect size after NULL prepend");
universe@354 166 UCX_TEST_ASSERT(elems[0] == 0, "element is not zeroed");
universe@354 167 UCX_TEST_ASSERT(elems[1] == 0, "element is not zeroed");
universe@354 168 UCX_TEST_ASSERT(elems[2] == 13,
universe@354 169 "NULL prepend corrupted previously inserted data");
universe@354 170 UCX_TEST_ASSERT(elems[3] == 37,
universe@354 171 "NULL prepend corrupted previously inserted data");
universe@354 172 UCX_TEST_ASSERT(elems[4] == 42,
universe@354 173 "NULL prepend corrupted previously inserted data");
universe@354 174
universe@354 175 UCX_TEST_END
universe@354 176
universe@356 177 ucx_array_free(array);
universe@354 178 }
universe@354 179
universe@354 180 UCX_TEST(test_ucx_array_set_from) {
universe@354 181 int *elems;
universe@356 182 UcxArray *array = ucx_array_new(16, sizeof(int));
universe@354 183
universe@354 184 int x = 42;
universe@354 185
universe@354 186 UCX_TEST_BEGIN
universe@354 187
universe@356 188 ucx_array_set_from(array, 7, &x, 1);
universe@354 189
universe@356 190 elems = array->data;
universe@354 191 UCX_TEST_ASSERT(elems[7] == 42, "failed");
universe@356 192 UCX_TEST_ASSERT(array->size >= 8, "array not resized on set");
universe@356 193 UCX_TEST_ASSERT(array->capacity == 16, "capacity changed unnecessarily");
universe@354 194
universe@354 195 int y[2] = {13, 37};
universe@356 196 ucx_array_set_from(array, 27, y, 2);
universe@354 197
universe@356 198 elems = array->data;
universe@354 199 UCX_TEST_ASSERT(elems[27] == 13, "failed");
universe@354 200 UCX_TEST_ASSERT(elems[28] == 37, "failed");
universe@356 201 UCX_TEST_ASSERT(array->size == 29, "array not resized on set");
universe@356 202 UCX_TEST_ASSERT(array->capacity == 32, "capacity not grown");
universe@354 203
universe@356 204 ucx_array_set_from(array, 7, NULL, 2);
universe@354 205
universe@356 206 elems = array->data;
universe@354 207 UCX_TEST_ASSERT(elems[7] == 0, "not zeroed on NULL set");
universe@354 208 UCX_TEST_ASSERT(elems[8] == 0, "not zeroed on NULL set");
universe@354 209
universe@354 210 UCX_TEST_END
universe@354 211
universe@356 212 ucx_array_free(array);
universe@354 213 }
universe@354 214
universe@337 215
universe@334 216 UCX_TEST(test_ucx_array_equals) {
universe@356 217 UcxArray *a1 = ucx_array_new(16, sizeof(int32_t));
universe@356 218 UcxArray *a2 = ucx_array_new(16, sizeof(int32_t));
universe@356 219 UcxArray *a3 = ucx_array_new(16, sizeof(int64_t));
universe@356 220 UcxArray *a4 = ucx_array_new(16, sizeof(int32_t));
universe@27 221
universe@350 222 int32_t *intelems;
universe@350 223 int64_t *longintelems;
universe@342 224
universe@356 225 a1->size = 5;
universe@356 226 intelems = a1->data;
universe@342 227 intelems[0] = 47;
universe@342 228 intelems[1] = 11;
universe@342 229 intelems[2] = 0;
universe@342 230 intelems[3] = 8;
universe@342 231 intelems[4] = 15;
universe@356 232 a2->size = 5;
universe@356 233 intelems = a2->data;
universe@342 234 intelems[0] = 47;
universe@342 235 intelems[1] = 11;
universe@342 236 intelems[2] = 0;
universe@342 237 intelems[3] = 8;
universe@342 238 intelems[4] = 15;
universe@356 239 a3->size = 5;
universe@356 240 longintelems = a3->data;
universe@342 241 longintelems[0] = 47;
universe@342 242 longintelems[1] = 11;
universe@342 243 longintelems[2] = 0;
universe@342 244 longintelems[3] = 8;
universe@342 245 longintelems[4] = 15;
universe@356 246 a4->size = 5;
universe@356 247 intelems = a4->data;
universe@342 248 intelems[0] = 47;
universe@342 249 intelems[1] = 11;
universe@342 250 intelems[2] = -6;
universe@342 251 intelems[3] = 8;
universe@342 252 intelems[4] = 15;
universe@27 253
universe@123 254 UCX_TEST_BEGIN
universe@123 255
universe@350 256 UCX_TEST_ASSERT(ucx_array_equals(a1, a2, ucx_cmp_int32, NULL), "failed");
universe@350 257 UCX_TEST_ASSERT(!ucx_array_equals(a1, a4, ucx_cmp_int32, NULL), "failed");
universe@350 258 UCX_TEST_ASSERT(!ucx_array_equals(a4, a1, ucx_cmp_int32, NULL), "failed");
universe@350 259 UCX_TEST_ASSERT(!ucx_array_equals(a1, a3, ucx_cmp_int64, NULL),
universe@336 260 "comparing arrays of different element size shall fail");
universe@350 261 UCX_TEST_ASSERT(!ucx_array_equals(a3, a1, ucx_cmp_int64, NULL),
universe@336 262 "comparing arrays of different element size shall fail");
universe@334 263
universe@336 264 UCX_TEST_ASSERT(ucx_array_equals(a1, a2, NULL, NULL),
universe@334 265 "compare using memcmp() failed");
universe@336 266 UCX_TEST_ASSERT(!ucx_array_equals(a1, a4, NULL, NULL),
universe@334 267 "compare using memcmp() failed");
universe@27 268
universe@33 269 UCX_TEST_END
universe@356 270 ucx_array_free(a1);
universe@356 271 ucx_array_free(a2);
universe@356 272 ucx_array_free(a3);
universe@356 273 ucx_array_free(a4);
olaf@9 274 }
olaf@11 275
universe@334 276 UCX_TEST(test_ucx_array_concat) {
universe@356 277 UcxArray *a1 = ucx_array_new(16, sizeof(int));
universe@356 278 UcxArray *a2 = ucx_array_new(16, sizeof(int));
universe@342 279 int *elems;
universe@334 280
universe@356 281 a1->size = 2;
universe@356 282 elems = a1->data;
universe@342 283 elems[0] = 47;
universe@342 284 elems[1] = 11;
universe@356 285 a2->size = 3;
universe@356 286 elems = a2->data;
universe@342 287 elems[0] = 0;
universe@342 288 elems[1] = 8;
universe@342 289 elems[2] = 15;
universe@27 290
universe@123 291 UCX_TEST_BEGIN
universe@123 292
universe@356 293 UCX_TEST_ASSERT(!ucx_array_concat(a1, a2), "failed");
universe@356 294 UCX_TEST_ASSERT(a1->size == 5, "failed");
universe@356 295 elems = a1->data;
universe@342 296 UCX_TEST_ASSERT(elems[0] == 47, "failed");
universe@342 297 UCX_TEST_ASSERT(elems[1] == 11, "failed");
universe@342 298 UCX_TEST_ASSERT(elems[2] == 0, "failed");
universe@342 299 UCX_TEST_ASSERT(elems[3] == 8, "failed");
universe@342 300 UCX_TEST_ASSERT(elems[4] == 15, "failed");
universe@27 301
universe@356 302 a1->elemsize *= 2;
universe@356 303 UCX_TEST_ASSERT(ucx_array_concat(a1, a2),
universe@334 304 "arrays of different element size must not be concatenated");
universe@356 305 UCX_TEST_ASSERT(a1->size == 5,
universe@334 306 "arrays of different element size must not be concatenated");
universe@27 307
universe@33 308 UCX_TEST_END
universe@356 309 ucx_array_free(a1);
universe@356 310 ucx_array_free(a2);
universe@27 311 }
universe@27 312
universe@344 313 UCX_TEST(test_ucx_array_at) {
universe@356 314 UcxArray *array = ucx_array_new(16, sizeof(int));
universe@344 315
universe@354 316 int x[3] = {42, 13, 5};
universe@356 317 ucx_array_append_from(array, x, 3);
universe@344 318
universe@344 319 UCX_TEST_BEGIN
universe@344 320
universe@344 321 UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 1) == 13, "failed");
universe@344 322 *(int*)ucx_array_at(array, 1) = 80;
universe@344 323 UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 1) == 80, "assignment failed");
universe@344 324
universe@344 325 UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 0) == 42, "corrupted data");
universe@344 326 UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 2) == 5, "corrupted data");
universe@344 327
universe@344 328 UCX_TEST_END
universe@344 329
universe@356 330 ucx_array_free(array);
universe@344 331 }
universe@344 332
universe@334 333 UCX_TEST(test_ucx_array_find) {
universe@356 334 UcxArray *array = ucx_array_new(16, sizeof(int));
universe@342 335 int *elems;
universe@334 336
universe@356 337 array->size = 5;
universe@356 338 elems = array->data;
universe@342 339 elems[0] = 47;
universe@342 340 elems[1] = 11;
universe@342 341 elems[2] = 0;
universe@342 342 elems[3] = 8;
universe@342 343 elems[4] = 15;
universe@334 344
universe@334 345 int x = 8;
universe@334 346 int y = 90;
universe@27 347
universe@123 348 UCX_TEST_BEGIN
universe@123 349
universe@334 350 UCX_TEST_ASSERT(ucx_array_find(array,(void*)&x,ucx_cmp_int,NULL) == 3,
universe@334 351 "doesn't find element");
universe@334 352 UCX_TEST_ASSERT(ucx_array_find(array,(void*)&y,ucx_cmp_int,NULL) == 5,
universe@334 353 "finds non-existing element");
universe@27 354
universe@334 355 UCX_TEST_ASSERT(ucx_array_find(array,(void*)&x,NULL,NULL) == 3,
universe@334 356 "failed using memcmp()");
universe@334 357 UCX_TEST_ASSERT(ucx_array_find(array,(void*)&y,NULL,NULL) == 5,
universe@334 358 "failed using memcmp()");
universe@27 359
universe@33 360 UCX_TEST_END
universe@356 361 ucx_array_free(array);
universe@27 362 }
universe@27 363
universe@334 364 UCX_TEST(test_ucx_array_contains) {
universe@356 365 UcxArray *array = ucx_array_new(16, sizeof(int));
universe@342 366 int *elems;
universe@334 367
universe@356 368 array->size = 5;
universe@356 369 elems = array->data;
universe@342 370 elems[0] = 47;
universe@342 371 elems[1] = 11;
universe@342 372 elems[2] = 0;
universe@342 373 elems[3] = 8;
universe@342 374 elems[4] = 15;
universe@334 375
universe@334 376 int x = 8;
universe@334 377 int y = 90;
universe@123 378
universe@123 379 UCX_TEST_BEGIN
universe@123 380
universe@334 381 UCX_TEST_ASSERT(ucx_array_contains(array,(void*)&x,ucx_cmp_int,NULL),
universe@334 382 "false negative");
universe@334 383 UCX_TEST_ASSERT(!ucx_array_contains(array,(void*)&y,ucx_cmp_int,NULL),
universe@334 384 "false positive");
universe@123 385
universe@334 386 UCX_TEST_ASSERT(ucx_array_contains(array,(void*)&x,NULL,NULL),
universe@334 387 "false negative using memcmp()");
universe@334 388 UCX_TEST_ASSERT(!ucx_array_contains(array,(void*)&y,NULL,NULL),
universe@334 389 "false positive using memcmp()");
universe@123 390
universe@123 391 UCX_TEST_END
universe@356 392 ucx_array_free(array);
universe@123 393 }
universe@123 394
universe@334 395 UCX_TEST(test_ucx_array_remove) {
universe@356 396 UcxArray *array = ucx_array_new(16, sizeof(int));
universe@342 397 int *elems;
universe@334 398
universe@356 399 array->size = 5;
universe@356 400 elems = array->data;
universe@342 401 elems[0] = 47;
universe@342 402 elems[1] = 11;
universe@342 403 elems[2] = 0;
universe@342 404 elems[3] = 8;
universe@342 405 elems[4] = 15;
universe@334 406
universe@334 407 UCX_TEST_BEGIN
universe@334 408
universe@356 409 ucx_array_remove(array, 2);
universe@356 410 elems = array->data;
universe@334 411 UCX_TEST_ASSERT(
universe@342 412 elems[0] == 47 &&
universe@342 413 elems[1] == 11 &&
universe@342 414 elems[2] == 8 &&
universe@342 415 elems[3] == 15,
universe@334 416 "wrong contents after remove");
universe@356 417 UCX_TEST_ASSERT(array->size == 4, "wrong size after remove");
universe@334 418
universe@356 419 ucx_array_remove_fast(array, 1);
universe@356 420 elems = array->data;
universe@334 421 UCX_TEST_ASSERT(
universe@342 422 elems[0] == 47 &&
universe@342 423 elems[1] == 15 &&
universe@342 424 elems[2] == 8,
universe@334 425 "wrong contents after fast remove");
universe@356 426 UCX_TEST_ASSERT(array->size == 3, "wrong size after fast remove");
universe@334 427
universe@334 428 UCX_TEST_END
universe@356 429 ucx_array_free(array);
universe@334 430 }
universe@334 431
universe@334 432 UCX_TEST(test_ucx_array_clone) {
universe@356 433 UcxArray array;
universe@356 434 UcxArray copy;
universe@356 435 ucx_array_init(&array, 16, sizeof(int));
universe@357 436 ucx_array_init(&copy, 4, 2*sizeof(double));
universe@342 437 int *elems;
universe@334 438
universe@334 439 array.size = 5;
universe@342 440 elems = array.data;
universe@342 441 elems[0] = 47;
universe@342 442 elems[1] = 11;
universe@342 443 elems[2] = 0;
universe@342 444 elems[3] = 8;
universe@342 445 elems[4] = 15;
universe@334 446
universe@356 447 ucx_array_clone(&copy, &array);
universe@334 448 UCX_TEST_BEGIN
universe@334 449
universe@334 450 UCX_TEST_ASSERT(array.data != copy.data, "no true copy");
universe@334 451 UCX_TEST_ASSERT(array.size == copy.size, "size mismatch");
universe@334 452 UCX_TEST_ASSERT(array.capacity == copy.capacity, "capacity mismatch");
universe@334 453 UCX_TEST_ASSERT(array.elemsize == copy.elemsize, "element size mismatch");
universe@334 454 UCX_TEST_ASSERT(array.allocator == copy.allocator, "allocator mismatch");
universe@356 455 UCX_TEST_ASSERT(ucx_array_equals(&array, &copy, ucx_cmp_int, NULL),
universe@356 456 "contents do not match after clone");
universe@334 457
universe@334 458 UCX_TEST_END
universe@334 459
universe@353 460 ucx_array_destroy(&array);
universe@353 461 ucx_array_destroy(&copy);
universe@334 462 }
universe@334 463
universe@342 464 static int ucx_cmp_int_reverse(const void* x, const void* y, void* data) {
universe@342 465 return -ucx_cmp_int(x,y,data);
universe@342 466 }
universe@342 467
universe@334 468 UCX_TEST(test_ucx_array_sort) {
universe@342 469 int *elems;
universe@342 470
universe@356 471 UcxArray *array = ucx_array_new(16, sizeof(int));
universe@356 472 array->size = 5;
universe@356 473 elems = array->data;
universe@342 474 elems[0] = 47;
universe@342 475 elems[1] = 11;
universe@342 476 elems[2] = 0;
universe@342 477 elems[3] = 8;
universe@342 478 elems[4] = 15;
universe@334 479
universe@356 480 UcxArray *expected = ucx_array_new(16, sizeof(int));
universe@356 481 expected->size = 5;
universe@356 482 elems = expected->data;
universe@342 483 elems[0] = 0;
universe@342 484 elems[1] = 8;
universe@342 485 elems[2] = 11;
universe@342 486 elems[3] = 15;
universe@342 487 elems[4] = 47;
universe@342 488
universe@356 489 UcxArray *expectedrev = ucx_array_new(16, sizeof(int));
universe@356 490 expectedrev->size = 5;
universe@356 491 elems = expectedrev->data;
universe@342 492 elems[0] = 47;
universe@342 493 elems[1] = 15;
universe@342 494 elems[2] = 11;
universe@342 495 elems[3] = 8;
universe@342 496 elems[4] = 0;
universe@334 497
universe@334 498
universe@334 499 UCX_TEST_BEGIN
universe@356 500 void* original_ptr = array->data;
universe@336 501 ucx_array_sort(array, ucx_cmp_int, NULL);
universe@336 502 UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL), "failed");
universe@356 503 UCX_TEST_ASSERT(array->size == 5, "size corrupted");
universe@356 504 UCX_TEST_ASSERT(array->data == original_ptr, "shall not reallocate");
universe@342 505
universe@342 506 ucx_array_sort(array, ucx_cmp_int_reverse, NULL);
universe@342 507 UCX_TEST_ASSERT(ucx_array_equals(array, expectedrev, NULL, NULL), "failed");
universe@342 508
universe@356 509 ucx_array_reserve(array, 32);
universe@356 510 ucx_array_reserve(expected, 32);
universe@356 511 array->size = expected->size = 32;
universe@336 512 for (size_t i = 0 ; i < 32 ; i++) {
universe@356 513 ((int*)array->data)[i]= ((i%2==0)?-1:1) * ((int) i);
universe@356 514 ((int*)expected->data)[i] = (-30+2*i) - (i > 15 ? 1 : 0);
universe@336 515 }
universe@336 516
universe@342 517 /* dummy third argument to trigger a possible fallback for qsort_s */
universe@356 518 ucx_array_sort(array, ucx_cmp_int, array->data);
universe@336 519 UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL),
universe@336 520 "failed for bigger arrays");
universe@334 521 UCX_TEST_END
universe@334 522
universe@356 523 ucx_array_free(expectedrev);
universe@356 524 ucx_array_free(expected);
universe@356 525 ucx_array_free(array);
universe@334 526 }
universe@334 527
universe@334 528 UCX_TEST(test_ucx_array_shrink) {
universe@356 529 UcxArray *array = ucx_array_new(16, sizeof(int));
universe@356 530 array->size = 4;
universe@90 531
universe@123 532 UCX_TEST_BEGIN
universe@356 533 UCX_TEST_ASSERT(!ucx_array_shrink(array), "failed");
universe@356 534 UCX_TEST_ASSERT(array->capacity == 4, "incorrect capacity after shrink");
universe@334 535 UCX_TEST_END
universe@356 536 ucx_array_free(array);
universe@334 537 }
universe@334 538
universe@334 539 UCX_TEST(test_ucx_array_resize) {
universe@356 540 UcxArray *array = ucx_array_new(16, sizeof(int));
universe@356 541 array->size = 8;
universe@123 542
universe@334 543 UCX_TEST_BEGIN
universe@334 544
universe@356 545 UCX_TEST_ASSERT(!ucx_array_resize(array, 32), "failed");
universe@356 546 UCX_TEST_ASSERT(array->capacity == 32, "incorrect capacity after resize");
universe@356 547 UCX_TEST_ASSERT(array->size == 8, "incorrect size after resize");
universe@334 548
universe@356 549 UCX_TEST_ASSERT(!ucx_array_resize(array, 4), "failed");
universe@356 550 UCX_TEST_ASSERT(array->capacity == 4, "incorrect capacity after resize");
universe@356 551 UCX_TEST_ASSERT(array->size == 4, "incorrect size after resize");
universe@90 552
universe@90 553 UCX_TEST_END
universe@356 554 ucx_array_free(array);
universe@90 555 }
universe@90 556
universe@334 557 UCX_TEST(test_ucx_array_reserve) {
universe@356 558 UcxArray *array = ucx_array_new(16, sizeof(int));
universe@27 559
universe@123 560 UCX_TEST_BEGIN
universe@334 561
universe@356 562 UCX_TEST_ASSERT(!ucx_array_reserve(array, 4), "failed");
universe@356 563 UCX_TEST_ASSERT(array->capacity == 16, "reserve shall not shrink");
universe@334 564
universe@356 565 UCX_TEST_ASSERT(!ucx_array_resize(array, 32), "failed");
universe@356 566 UCX_TEST_ASSERT(array->capacity == 32, "incorrect capacity after reserve");
olaf@162 567
universe@33 568 UCX_TEST_END
universe@356 569 ucx_array_free(array);
universe@27 570 }
universe@355 571
universe@369 572 UCX_TEST(test_ucx_array_grow) {
universe@369 573 UcxArray *array = ucx_array_new(16, sizeof(int));
universe@369 574 array->size = 12;
universe@369 575
universe@369 576 UCX_TEST_BEGIN
universe@369 577
universe@369 578 UCX_TEST_ASSERT(!ucx_array_grow(array, 4), "failed");
universe@369 579 UCX_TEST_ASSERT(array->capacity == 16, "shall be noop if contents fit");
universe@369 580 /* subsequent calls shall also be noops */
universe@369 581 UCX_TEST_ASSERT(!ucx_array_grow(array, 4), "failed");
universe@369 582 UCX_TEST_ASSERT(array->capacity == 16, "shall be noop if contents fit");
universe@369 583
universe@369 584 UCX_TEST_ASSERT(!ucx_array_grow(array, 6), "failed");
universe@369 585 UCX_TEST_ASSERT(array->capacity == 18, "incorrect capacity after grow");
universe@369 586
universe@369 587 UCX_TEST_END
universe@369 588 ucx_array_free(array);
universe@369 589 }
universe@369 590
universe@355 591 UCX_TEST(test_ucx_array_util_set) {
universe@355 592 size_t capacity = 16;
universe@355 593 int* array = malloc(sizeof(int)*capacity);
universe@369 594 int x;
universe@355 595
universe@355 596 UCX_TEST_BEGIN
universe@355 597
universe@369 598 x = 42;
universe@369 599 ucx_array_util_set(&array, &capacity, sizeof(int), 7, &x);
universe@355 600
universe@355 601 UCX_TEST_ASSERT(array[7] == 42, "failed");
universe@355 602 UCX_TEST_ASSERT(capacity == 16, "capacity changed unnecessarily");
universe@355 603
universe@369 604 x = 13;
universe@369 605 ucx_array_util_set(&array, &capacity, sizeof(int), 37, &x);
universe@369 606 x = 37;
universe@369 607 ucx_array_util_set(&array, &capacity, sizeof(int), 38, &x);
universe@355 608
universe@355 609 UCX_TEST_ASSERT(array[37] == 13, "failed");
universe@355 610 UCX_TEST_ASSERT(array[38] == 37, "failed");
universe@355 611 UCX_TEST_ASSERT(capacity == 64, "capacity not grown");
universe@355 612
universe@355 613 UCX_TEST_END
universe@355 614
universe@355 615 free(array);
universe@355 616 }
universe@369 617
universe@369 618
universe@369 619 UCX_TEST(test_ucx_array_util_setptr) {
universe@369 620 size_t capacity = 16;
universe@369 621 double** array = malloc(sizeof(double*)*capacity);
universe@369 622 double x, y, z;
universe@369 623
universe@369 624 UCX_TEST_BEGIN
universe@369 625
universe@369 626 ucx_array_util_setptr(&array, &capacity, 7, &x);
universe@369 627
universe@369 628 UCX_TEST_ASSERT(array[7] == &x, "failed");
universe@369 629 UCX_TEST_ASSERT(capacity == 16, "capacity changed unnecessarily");
universe@369 630
universe@369 631 ucx_array_util_setptr(&array, &capacity, 37, &y);
universe@369 632 ucx_array_util_setptr(&array, &capacity, 38, &z);
universe@369 633
universe@369 634 UCX_TEST_ASSERT(array[37] == &y, "failed");
universe@369 635 UCX_TEST_ASSERT(array[38] == &z, "failed");
universe@369 636 UCX_TEST_ASSERT(capacity == 64, "capacity not grown");
universe@369 637
universe@369 638 UCX_TEST_END
universe@369 639
universe@369 640 free(array);
universe@369 641 }
universe@369 642

mercurial