test/array_tests.c

Thu, 04 Jul 2019 20:07:31 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 04 Jul 2019 20:07:31 +0200
branch
feature/array
changeset 334
bc81faa9afda
parent 323
test/list_tests.c@b8c49e7a1dba
child 336
6d7aa8a1a3b3
permissions
-rw-r--r--

adds array interface and tests

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@334 32 UCX_TEST(test_ucx_array_free) {
universe@334 33 UcxArray array = ucx_array_new(16, sizeof(int));
universe@334 34
universe@334 35 UCX_TEST_BEGIN
universe@334 36 ucx_array_free(&array);
universe@334 37 UCX_TEST_ASSERT(array.data == NULL, "data pointer not NULL after free");
universe@334 38 UCX_TEST_ASSERT(array.size == 0, "size not zero after free");
universe@334 39 UCX_TEST_ASSERT(array.capacity == 0, "capacity not zero after free");
universe@334 40 UCX_TEST_ASSERT(array.allocator == ucx_default_allocator(),
universe@334 41 "allocator corrupted during free");
universe@334 42 UCX_TEST_END
universe@334 43 }
universe@334 44
universe@334 45 UCX_TEST(test_ucx_array_new) {
universe@334 46 UcxArray array = ucx_array_new(16, 47);
universe@334 47
universe@334 48 UCX_TEST_BEGIN
universe@334 49 UCX_TEST_ASSERT(array.data, "no memory allocated");
universe@334 50 UCX_TEST_ASSERT(array.size == 0, "size not initially zero");
universe@334 51 UCX_TEST_ASSERT(array.capacity == 16, "capacity not as requested");
universe@334 52 UCX_TEST_ASSERT(array.elemsize == 47, "element size not as requested");
universe@334 53 UCX_TEST_ASSERT(array.allocator == ucx_default_allocator(),
universe@334 54 "array not using the default allocator");
universe@334 55 UCX_TEST_END
universe@334 56 ucx_array_free(&array);
universe@334 57 }
universe@334 58
universe@334 59 UCX_TEST(test_ucx_array_append) {
universe@334 60 UcxArray array = ucx_array_new(16, sizeof(int));
universe@334 61
universe@334 62 int x = 42;
universe@334 63 ucx_array_append(&array, &x);
universe@33 64 UCX_TEST_BEGIN
universe@27 65
universe@334 66 UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, "failed");
universe@27 67
universe@334 68 x = 13;
universe@334 69 ucx_array_append(&array, &x);
universe@27 70
universe@334 71 UCX_TEST_ASSERT(array.size == 2, "incorrect size after append");
universe@334 72 UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13, "failed");
universe@334 73 UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42,
universe@334 74 "append corrupted previously inserted data");
universe@334 75
universe@334 76 ucx_array_append(&array, NULL);
universe@334 77
universe@334 78 UCX_TEST_ASSERT(array.size == 3, "incorrect size after NULL append");
universe@334 79 UCX_TEST_ASSERT(ucx_array_at_int(array, 2) == 0, "element is not zeroed");
universe@334 80 UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42,
universe@334 81 "NULL append corrupted previously inserted data");
universe@334 82 UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13,
universe@334 83 "NULL append corrupted previously inserted data");
universe@334 84
universe@33 85 UCX_TEST_END
universe@27 86
universe@334 87 ucx_array_free(&array);
universe@24 88 }
universe@24 89
universe@334 90 UCX_TEST(test_ucx_array_prepend) {
universe@334 91 UcxArray array = ucx_array_new(16, sizeof(int));
universe@334 92
universe@334 93 int x = 42;
universe@334 94 ucx_array_prepend(&array, &x);
universe@33 95 UCX_TEST_BEGIN
universe@27 96
universe@334 97 UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, "failed");
universe@334 98
universe@334 99 x = 13;
universe@334 100 ucx_array_prepend(&array, &x);
universe@334 101
universe@334 102 UCX_TEST_ASSERT(array.size == 2, "incorrect size after prepend");
universe@334 103 UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 13, "failed");
universe@334 104 UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 42,
universe@334 105 "prepend corrupted previously inserted data");
universe@334 106
universe@334 107 ucx_array_prepend(&array, NULL);
universe@334 108
universe@334 109 UCX_TEST_ASSERT(array.size == 3, "incorrect size after NULL prepend");
universe@334 110 UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 0, "element is not zeroed");
universe@334 111 UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13,
universe@334 112 "NULL prepend corrupted previously inserted data");
universe@334 113 UCX_TEST_ASSERT(ucx_array_at_int(array, 2) == 42,
universe@334 114 "NULL prepend corrupted previously inserted data");
universe@27 115
universe@33 116 UCX_TEST_END
universe@334 117
universe@334 118 ucx_array_free(&array);
universe@18 119 }
universe@18 120
universe@334 121 UCX_TEST(test_ucx_array_equals) {
universe@334 122 UcxArray a1 = ucx_array_new(16, sizeof(int));
universe@334 123 UcxArray a2 = ucx_array_new(16, sizeof(int));
universe@334 124 UcxArray a3 = ucx_array_new(16, sizeof(long int));
universe@334 125 UcxArray a4 = ucx_array_new(16, sizeof(int));
universe@27 126
universe@334 127 a1.size = 5;
universe@334 128 ucx_array_at_int(a1, 0) = 47;
universe@334 129 ucx_array_at_int(a1, 1) = 11;
universe@334 130 ucx_array_at_int(a1, 2) = 0;
universe@334 131 ucx_array_at_int(a1, 3) = 8;
universe@334 132 ucx_array_at_int(a1, 4) = 15;
universe@334 133 a2.size = 5;
universe@334 134 ucx_array_at_int(a2, 0) = 47;
universe@334 135 ucx_array_at_int(a2, 1) = 11;
universe@334 136 ucx_array_at_int(a2, 2) = 0;
universe@334 137 ucx_array_at_int(a2, 3) = 8;
universe@334 138 ucx_array_at_int(a2, 4) = 15;
universe@334 139 a3.size = 5;
universe@334 140 ucx_array_at_longint(a3, 0) = 47;
universe@334 141 ucx_array_at_longint(a3, 1) = 11;
universe@334 142 ucx_array_at_longint(a3, 2) = 0;
universe@334 143 ucx_array_at_longint(a3, 3) = 8;
universe@334 144 ucx_array_at_longint(a3, 4) = 15;
universe@334 145 a4.size = 5;
universe@334 146 ucx_array_at_int(a4, 0) = 47;
universe@334 147 ucx_array_at_int(a4, 1) = 11;
universe@334 148 ucx_array_at_int(a4, 2) = -6;
universe@334 149 ucx_array_at_int(a4, 3) = 8;
universe@334 150 ucx_array_at_int(a4, 4) = 15;
universe@27 151
universe@123 152 UCX_TEST_BEGIN
universe@123 153
universe@334 154 UCX_TEST_ASSERT(ucx_array_equals(a1, a2, ucx_cmp_int, NULL) == 0, "failed");
universe@334 155 UCX_TEST_ASSERT(ucx_array_equals(a1, a4, ucx_cmp_int, NULL) > 0, "failed");
universe@334 156 UCX_TEST_ASSERT(ucx_array_equals(a4, a1, ucx_cmp_int, NULL) < 0, "failed");
universe@334 157 UCX_TEST_ASSERT(ucx_array_equals(a1, a3, ucx_cmp_int, NULL) < 0,
universe@334 158 "comparing arrays of different element size failed");
universe@334 159 UCX_TEST_ASSERT(ucx_array_equals(a3, a1, ucx_cmp_int, NULL) > 0,
universe@334 160 "comparing arrays of different element size failed");
universe@334 161
universe@334 162 UCX_TEST_ASSERT(ucx_array_equals(a1, a2, NULL, NULL) == 0,
universe@334 163 "compare using memcmp() failed");
universe@334 164 UCX_TEST_ASSERT(ucx_array_equals(a1, a4, NULL, NULL) > 0,
universe@334 165 "compare using memcmp() failed");
universe@334 166 UCX_TEST_ASSERT(ucx_array_equals(a4, a1, NULL, NULL) < 0,
universe@334 167 "compare using memcmp() failed");
universe@27 168
universe@33 169 UCX_TEST_END
universe@334 170 ucx_array_free(&a1);
universe@334 171 ucx_array_free(&a2);
universe@334 172 ucx_array_free(&a3);
universe@334 173 ucx_array_free(&a4);
olaf@9 174 }
olaf@11 175
universe@334 176 UCX_TEST(test_ucx_array_concat) {
universe@334 177 UcxArray a1 = ucx_array_new(16, sizeof(int));
universe@334 178 UcxArray a2 = ucx_array_new(16, sizeof(int));
universe@334 179
universe@334 180 a1.size = 2;
universe@334 181 ucx_array_at_int(a1, 0) = 47;
universe@334 182 ucx_array_at_int(a1, 1) = 11;
universe@334 183 a2.size = 3;
universe@334 184 ucx_array_at_int(a2, 0) = 0;
universe@334 185 ucx_array_at_int(a2, 1) = 8;
universe@334 186 ucx_array_at_int(a2, 2) = 15;
universe@27 187
universe@123 188 UCX_TEST_BEGIN
universe@123 189
universe@334 190 UCX_TEST_ASSERT(!ucx_array_concat(&a1, &a2), "failed");
universe@334 191 UCX_TEST_ASSERT(a1.size == 5, "failed");
universe@334 192 UCX_TEST_ASSERT(ucx_array_at_int(a1, 0) == 47, "failed");
universe@334 193 UCX_TEST_ASSERT(ucx_array_at_int(a1, 1) == 11, "failed");
universe@334 194 UCX_TEST_ASSERT(ucx_array_at_int(a1, 2) == 0, "failed");
universe@334 195 UCX_TEST_ASSERT(ucx_array_at_int(a1, 3) == 8, "failed");
universe@334 196 UCX_TEST_ASSERT(ucx_array_at_int(a1, 4) == 15, "failed");
universe@27 197
universe@334 198 a1.elemsize *= 2;
universe@334 199 UCX_TEST_ASSERT(ucx_array_concat(&a1, &a2),
universe@334 200 "arrays of different element size must not be concatenated");
universe@334 201 UCX_TEST_ASSERT(a1.size == 5,
universe@334 202 "arrays of different element size must not be concatenated");
universe@27 203
universe@33 204 UCX_TEST_END
universe@334 205 ucx_array_free(&a1);
universe@334 206 ucx_array_free(&a2);
universe@27 207 }
universe@27 208
universe@334 209 UCX_TEST(test_ucx_array_at) {
universe@334 210 UcxArray array = ucx_array_new(16, sizeof(int));
universe@334 211
universe@334 212 int x = 42;
universe@334 213 ucx_array_append(&array, &x);
universe@334 214 x = 13;
universe@334 215 ucx_array_append(&array, &x);
universe@334 216 x = 5;
universe@334 217 ucx_array_append(&array, &x);
universe@27 218
universe@123 219 UCX_TEST_BEGIN
universe@123 220
universe@334 221 UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13, "failed");
universe@334 222 ucx_array_at_int(array, 1) = 80;
universe@334 223 UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 80, "assignment failed");
universe@27 224
universe@334 225
universe@334 226 UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, "corrupted data");
universe@334 227 UCX_TEST_ASSERT(ucx_array_at_int(array, 2) == 5, "corrupted data");
universe@27 228
universe@33 229 UCX_TEST_END
universe@334 230
universe@334 231 ucx_array_free(&array);
universe@27 232 }
universe@27 233
universe@334 234 UCX_TEST(test_ucx_array_find) {
universe@334 235 UcxArray array = ucx_array_new(16, sizeof(int));
universe@334 236
universe@334 237 array.size = 5;
universe@334 238 ucx_array_at_int(array, 0) = 47;
universe@334 239 ucx_array_at_int(array, 1) = 11;
universe@334 240 ucx_array_at_int(array, 2) = 0;
universe@334 241 ucx_array_at_int(array, 3) = 8;
universe@334 242 ucx_array_at_int(array, 4) = 15;
universe@334 243
universe@334 244 int x = 8;
universe@334 245 int y = 90;
universe@27 246
universe@123 247 UCX_TEST_BEGIN
universe@123 248
universe@334 249 UCX_TEST_ASSERT(ucx_array_find(array,(void*)&x,ucx_cmp_int,NULL) == 3,
universe@334 250 "doesn't find element");
universe@334 251 UCX_TEST_ASSERT(ucx_array_find(array,(void*)&y,ucx_cmp_int,NULL) == 5,
universe@334 252 "finds non-existing element");
universe@27 253
universe@334 254 UCX_TEST_ASSERT(ucx_array_find(array,(void*)&x,NULL,NULL) == 3,
universe@334 255 "failed using memcmp()");
universe@334 256 UCX_TEST_ASSERT(ucx_array_find(array,(void*)&y,NULL,NULL) == 5,
universe@334 257 "failed using memcmp()");
universe@27 258
universe@33 259 UCX_TEST_END
universe@334 260 ucx_array_free(&array);
universe@27 261 }
universe@27 262
universe@334 263 UCX_TEST(test_ucx_array_contains) {
universe@334 264 UcxArray array = ucx_array_new(16, sizeof(int));
universe@334 265
universe@334 266 array.size = 5;
universe@334 267 ucx_array_at_int(array, 0) = 47;
universe@334 268 ucx_array_at_int(array, 1) = 11;
universe@334 269 ucx_array_at_int(array, 2) = 0;
universe@334 270 ucx_array_at_int(array, 3) = 8;
universe@334 271 ucx_array_at_int(array, 4) = 15;
universe@334 272
universe@334 273 int x = 8;
universe@334 274 int y = 90;
universe@123 275
universe@123 276 UCX_TEST_BEGIN
universe@123 277
universe@334 278 UCX_TEST_ASSERT(ucx_array_contains(array,(void*)&x,ucx_cmp_int,NULL),
universe@334 279 "false negative");
universe@334 280 UCX_TEST_ASSERT(!ucx_array_contains(array,(void*)&y,ucx_cmp_int,NULL),
universe@334 281 "false positive");
universe@123 282
universe@334 283 UCX_TEST_ASSERT(ucx_array_contains(array,(void*)&x,NULL,NULL),
universe@334 284 "false negative using memcmp()");
universe@334 285 UCX_TEST_ASSERT(!ucx_array_contains(array,(void*)&y,NULL,NULL),
universe@334 286 "false positive using memcmp()");
universe@123 287
universe@123 288 UCX_TEST_END
universe@334 289 ucx_array_free(&array);
universe@123 290 }
universe@123 291
universe@334 292 UCX_TEST(test_ucx_array_remove) {
universe@334 293 UcxArray array = ucx_array_new(16, sizeof(int));
universe@334 294
universe@334 295 array.size = 5;
universe@334 296 ucx_array_at_int(array, 0) = 47;
universe@334 297 ucx_array_at_int(array, 1) = 11;
universe@334 298 ucx_array_at_int(array, 2) = 0;
universe@334 299 ucx_array_at_int(array, 3) = 8;
universe@334 300 ucx_array_at_int(array, 4) = 15;
universe@334 301
universe@334 302 UCX_TEST_BEGIN
universe@334 303
universe@334 304 ucx_array_remove(&array, 2);
universe@334 305 UCX_TEST_ASSERT(
universe@334 306 ucx_array_at_int(array, 0) == 47 &&
universe@334 307 ucx_array_at_int(array, 1) == 11 &&
universe@334 308 ucx_array_at_int(array, 2) == 8 &&
universe@334 309 ucx_array_at_int(array, 3) == 15,
universe@334 310 "wrong contents after remove");
universe@334 311 UCX_TEST_ASSERT(array.size == 4, "wrong size after remove");
universe@334 312
universe@334 313 ucx_array_remove_fast(&array, 1);
universe@334 314 UCX_TEST_ASSERT(
universe@334 315 ucx_array_at_int(array, 0) == 47 &&
universe@334 316 ucx_array_at_int(array, 1) == 15 &&
universe@334 317 ucx_array_at_int(array, 2) == 8,
universe@334 318 "wrong contents after fast remove");
universe@334 319 UCX_TEST_ASSERT(array.size == 3, "wrong size after fast remove");
universe@334 320
universe@334 321 UCX_TEST_END
universe@334 322 ucx_array_free(&array);
universe@334 323 }
universe@334 324
universe@334 325 UCX_TEST(test_ucx_array_clone) {
universe@334 326
universe@334 327 UcxArray array = ucx_array_new(16, sizeof(int));
universe@334 328
universe@334 329 array.size = 5;
universe@334 330 ucx_array_at_int(array, 0) = 47;
universe@334 331 ucx_array_at_int(array, 1) = 11;
universe@334 332 ucx_array_at_int(array, 2) = 0;
universe@334 333 ucx_array_at_int(array, 3) = 8;
universe@334 334 ucx_array_at_int(array, 4) = 15;
universe@334 335
universe@334 336 UcxArray copy = ucx_array_clone(array);
universe@334 337 UCX_TEST_BEGIN
universe@334 338
universe@334 339 UCX_TEST_ASSERT(array.data != copy.data, "no true copy");
universe@334 340 UCX_TEST_ASSERT(ucx_array_equals(array, copy, ucx_cmp_int, NULL), "failed");
universe@334 341 UCX_TEST_ASSERT(array.size == copy.size, "size mismatch");
universe@334 342 UCX_TEST_ASSERT(array.capacity == copy.capacity, "capacity mismatch");
universe@334 343 UCX_TEST_ASSERT(array.elemsize == copy.elemsize, "element size mismatch");
universe@334 344 UCX_TEST_ASSERT(array.allocator == copy.allocator, "allocator mismatch");
universe@334 345
universe@334 346 UCX_TEST_END
universe@334 347
universe@334 348 ucx_array_free(&array);
universe@334 349 ucx_array_free(&copy);
universe@334 350 }
universe@334 351
universe@334 352 UCX_TEST(test_ucx_array_sort) {
universe@334 353 UcxArray array = ucx_array_new(16, sizeof(int));
universe@334 354 array.size = 5;
universe@334 355 ucx_array_at_int(array, 0) = 47;
universe@334 356 ucx_array_at_int(array, 1) = 11;
universe@334 357 ucx_array_at_int(array, 2) = 0;
universe@334 358 ucx_array_at_int(array, 3) = 8;
universe@334 359 ucx_array_at_int(array, 4) = 15;
universe@334 360
universe@334 361 UcxArray expected = ucx_array_new(16, sizeof(int));
universe@334 362 expected.size = 5;
universe@334 363 ucx_array_at_int(expected, 0) = 0;
universe@334 364 ucx_array_at_int(expected, 1) = 8;
universe@334 365 ucx_array_at_int(expected, 2) = 11;
universe@334 366 ucx_array_at_int(expected, 3) = 15;
universe@334 367 ucx_array_at_int(expected, 4) = 47;
universe@334 368
universe@334 369
universe@334 370 UCX_TEST_BEGIN
universe@334 371 void* original_ptr = array.data;
universe@334 372 UCX_TEST_ASSERT(!ucx_array_sort(array, ucx_cmp_int, NULL), "failed");
universe@334 373 UCX_TEST_ASSERT(!ucx_array_equals(array, expected, NULL, NULL), "failed");
universe@334 374 UCX_TEST_ASSERT(array.size == 5, "size corrupted");
universe@334 375 UCX_TEST_ASSERT(array.data == original_ptr, "shall not reallocate");
universe@334 376 UCX_TEST_END
universe@334 377
universe@334 378 ucx_array_free(&expected);
universe@334 379 ucx_array_free(&array);
universe@334 380 }
universe@334 381
universe@334 382 UCX_TEST(test_ucx_array_autogrow) {
universe@334 383 UcxArray array = ucx_array_new(4, sizeof(int));
universe@334 384 array.size = 3;
universe@334 385 ucx_array_at_int(array, 0) = 47;
universe@334 386 ucx_array_at_int(array, 1) = 11;
universe@334 387 int x = 5;
universe@123 388
universe@123 389 UCX_TEST_BEGIN
universe@334 390
universe@334 391 void* oldptr = array.data;
universe@123 392
universe@334 393 ucx_array_append(&array, &x);
universe@334 394 UCX_TEST_ASSERT(array.capacity == 4 && array.data == oldptr,
universe@334 395 "array should not grow too early");
universe@334 396 ucx_array_append(&array, &x);
universe@334 397 UCX_TEST_ASSERT(array.capacity == 8, "array did not grow");
universe@334 398 UCX_TEST_ASSERT(array.size == 5, "incorrect size after grow");
universe@334 399 UCX_TEST_ASSERT(ucx_array_at_int(array, 3) == 5 &&
universe@334 400 ucx_array_at_int(array, 3) == 5, "corrupt data");
universe@172 401
universe@123 402 UCX_TEST_END
universe@334 403 ucx_array_free(&array);
universe@123 404 }
universe@123 405
universe@334 406 UCX_TEST(test_ucx_array_shrink) {
universe@334 407 UcxArray array = ucx_array_new(16, sizeof(int));
universe@334 408 array.size = 4;
universe@90 409
universe@123 410 UCX_TEST_BEGIN
universe@334 411 UCX_TEST_ASSERT(!ucx_array_shrink(&array), "failed");
universe@334 412 UCX_TEST_ASSERT(array.capacity == 4, "incorrect capacity after shrink");
universe@334 413 UCX_TEST_END
universe@334 414 ucx_array_free(&array);
universe@334 415 }
universe@334 416
universe@334 417 UCX_TEST(test_ucx_array_resize) {
universe@334 418 UcxArray array = ucx_array_new(16, sizeof(int));
universe@334 419 array.size = 8;
universe@123 420
universe@334 421 UCX_TEST_BEGIN
universe@334 422
universe@334 423 UCX_TEST_ASSERT(!ucx_array_resize(&array, 32), "failed");
universe@334 424 UCX_TEST_ASSERT(array.capacity == 32, "incorrect capacity after resize");
universe@334 425 UCX_TEST_ASSERT(array.size == 8, "incorrect size after resize");
universe@334 426
universe@334 427 UCX_TEST_ASSERT(!ucx_array_resize(&array, 4), "failed");
universe@334 428 UCX_TEST_ASSERT(array.capacity == 4, "incorrect capacity after resize");
universe@334 429 UCX_TEST_ASSERT(array.size == 4, "incorrect size after resize");
universe@90 430
universe@90 431 UCX_TEST_END
universe@334 432 ucx_array_free(&array);
universe@90 433 }
universe@90 434
universe@334 435 UCX_TEST(test_ucx_array_reserve) {
universe@334 436 UcxArray array = ucx_array_new(16, sizeof(int));
universe@27 437
universe@123 438 UCX_TEST_BEGIN
universe@334 439
universe@334 440 UCX_TEST_ASSERT(!ucx_array_reserve(&array, 4), "failed");
universe@334 441 UCX_TEST_ASSERT(array.capacity == 16, "reserve shall not shrink");
universe@334 442
universe@334 443 UCX_TEST_ASSERT(!ucx_array_resize(&array, 32), "failed");
universe@334 444 UCX_TEST_ASSERT(array.capacity == 32, "incorrect capacity after reserve");
olaf@162 445
universe@33 446 UCX_TEST_END
universe@334 447 ucx_array_free(&array);
universe@27 448 }

mercurial