olaf@9: /* universe@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@103: * universe@334: * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved. universe@103: * universe@103: * Redistribution and use in source and binary forms, with or without universe@103: * modification, are permitted provided that the following conditions are met: universe@103: * universe@103: * 1. Redistributions of source code must retain the above copyright universe@103: * notice, this list of conditions and the following disclaimer. universe@103: * universe@103: * 2. Redistributions in binary form must reproduce the above copyright universe@103: * notice, this list of conditions and the following disclaimer in the universe@103: * documentation and/or other materials provided with the distribution. universe@103: * universe@103: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@103: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@103: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@103: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@103: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@103: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@103: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@103: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@103: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@103: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@103: * POSSIBILITY OF SUCH DAMAGE. olaf@9: */ olaf@9: universe@334: #include "array_tests.h" universe@251: #include olaf@9: universe@356: UCX_TEST(test_ucx_array_destroy) { universe@356: UcxArray array; universe@356: ucx_array_init(&array, 16, sizeof(int)); universe@334: universe@334: UCX_TEST_BEGIN universe@353: ucx_array_destroy(&array); universe@356: UCX_TEST_ASSERT(array.data == NULL, "data pointer not NULL after destroy"); universe@356: UCX_TEST_ASSERT(array.size == 0, "size not zero after destroy"); universe@356: UCX_TEST_ASSERT(array.capacity == 0, "capacity not zero after destroy"); universe@334: UCX_TEST_ASSERT(array.allocator == ucx_default_allocator(), universe@356: "allocator corrupted during destroy"); universe@334: UCX_TEST_END universe@334: } universe@334: universe@334: UCX_TEST(test_ucx_array_new) { universe@356: UcxArray* array = ucx_array_new(16, 47); universe@334: universe@334: UCX_TEST_BEGIN universe@356: UCX_TEST_ASSERT(array->data, "no memory allocated"); universe@356: UCX_TEST_ASSERT(array->size == 0, "size not initially zero"); universe@356: UCX_TEST_ASSERT(array->capacity == 16, "capacity not as requested"); universe@356: UCX_TEST_ASSERT(array->elemsize == 47, "element size not as requested"); universe@356: UCX_TEST_ASSERT(array->allocator == ucx_default_allocator(), universe@334: "array not using the default allocator"); universe@334: UCX_TEST_END universe@356: ucx_array_free(array); universe@334: } universe@334: universe@354: UCX_TEST(test_ucx_array_append_from) { universe@356: UcxArray *array = ucx_array_new(16, sizeof(int)); universe@342: int *elements; universe@334: universe@334: int x = 42; universe@356: ucx_array_append_from(array, &x, 1); universe@33: UCX_TEST_BEGIN universe@27: universe@356: elements = array->data; universe@342: UCX_TEST_ASSERT(elements[0] == 42, "failed"); universe@334: universe@354: int y[2] = {13, 37}; universe@356: ucx_array_append_from(array, y, 2); universe@27: universe@356: elements = array->data; universe@356: UCX_TEST_ASSERT(array->size == 3, "incorrect size after append"); universe@342: UCX_TEST_ASSERT(elements[1] == 13, "failed"); universe@354: UCX_TEST_ASSERT(elements[2] == 37, "failed"); universe@342: UCX_TEST_ASSERT(elements[0] == 42, universe@334: "append corrupted previously inserted data"); universe@27: universe@356: ucx_array_append_from(array, NULL, 2); universe@334: universe@356: elements = array->data; universe@356: UCX_TEST_ASSERT(array->size == 5, "incorrect size after NULL append"); universe@354: UCX_TEST_ASSERT(elements[3] == 0, "element is not zeroed"); universe@354: UCX_TEST_ASSERT(elements[4] == 0, "element is not zeroed"); universe@342: UCX_TEST_ASSERT(elements[0] == 42, universe@334: "NULL append corrupted previously inserted data"); universe@342: UCX_TEST_ASSERT(elements[1] == 13, universe@334: "NULL append corrupted previously inserted data"); universe@354: UCX_TEST_ASSERT(elements[2] == 37, universe@354: "NULL append corrupted previously inserted data"); universe@354: universe@354: UCX_TEST_END universe@354: universe@356: ucx_array_free(array); universe@354: } universe@354: universe@354: UCX_TEST(test_ucx_array_prepend_from) { universe@354: int *elems; universe@356: UcxArray *array = ucx_array_new(16, sizeof(int)); universe@354: universe@354: int x = 42; universe@356: ucx_array_prepend_from(array, &x, 1); universe@354: UCX_TEST_BEGIN universe@354: universe@356: elems = array->data; universe@354: UCX_TEST_ASSERT(elems[0] == 42, "failed"); universe@354: universe@354: int y[2] = {13, 37}; universe@356: ucx_array_prepend_from(array, y, 2); universe@354: universe@356: elems = array->data; universe@356: UCX_TEST_ASSERT(array->size == 3, "incorrect size after prepend"); universe@354: UCX_TEST_ASSERT(elems[0] == 13, "failed"); universe@354: UCX_TEST_ASSERT(elems[1] == 37, "failed"); universe@354: UCX_TEST_ASSERT(elems[2] == 42, universe@354: "prepend corrupted previously inserted data"); universe@354: universe@356: ucx_array_prepend_from(array, NULL, 2); universe@354: universe@356: elems = array->data; universe@356: UCX_TEST_ASSERT(array->size == 5, "incorrect size after NULL prepend"); universe@354: UCX_TEST_ASSERT(elems[0] == 0, "element is not zeroed"); universe@354: UCX_TEST_ASSERT(elems[1] == 0, "element is not zeroed"); universe@354: UCX_TEST_ASSERT(elems[2] == 13, universe@354: "NULL prepend corrupted previously inserted data"); universe@354: UCX_TEST_ASSERT(elems[3] == 37, universe@354: "NULL prepend corrupted previously inserted data"); universe@354: UCX_TEST_ASSERT(elems[4] == 42, universe@354: "NULL prepend corrupted previously inserted data"); universe@354: universe@354: UCX_TEST_END universe@354: universe@356: ucx_array_free(array); universe@354: } universe@354: universe@354: UCX_TEST(test_ucx_array_set_from) { universe@354: int *elems; universe@356: UcxArray *array = ucx_array_new(16, sizeof(int)); universe@354: universe@354: int x = 42; universe@354: universe@354: UCX_TEST_BEGIN universe@354: universe@356: ucx_array_set_from(array, 7, &x, 1); universe@354: universe@356: elems = array->data; universe@354: UCX_TEST_ASSERT(elems[7] == 42, "failed"); universe@356: UCX_TEST_ASSERT(array->size >= 8, "array not resized on set"); universe@356: UCX_TEST_ASSERT(array->capacity == 16, "capacity changed unnecessarily"); universe@354: universe@354: int y[2] = {13, 37}; universe@356: ucx_array_set_from(array, 27, y, 2); universe@354: universe@356: elems = array->data; universe@354: UCX_TEST_ASSERT(elems[27] == 13, "failed"); universe@354: UCX_TEST_ASSERT(elems[28] == 37, "failed"); universe@356: UCX_TEST_ASSERT(array->size == 29, "array not resized on set"); universe@356: UCX_TEST_ASSERT(array->capacity == 32, "capacity not grown"); universe@354: universe@356: ucx_array_set_from(array, 7, NULL, 2); universe@354: universe@356: elems = array->data; universe@354: UCX_TEST_ASSERT(elems[7] == 0, "not zeroed on NULL set"); universe@354: UCX_TEST_ASSERT(elems[8] == 0, "not zeroed on NULL set"); universe@354: universe@354: UCX_TEST_END universe@354: universe@356: ucx_array_free(array); universe@354: } universe@354: universe@354: UCX_TEST(test_ucx_array_append) { universe@356: UcxArray *array = ucx_array_new(16, sizeof(int)); universe@354: int *elements; universe@354: universe@356: ucx_array_append(array, 42); universe@354: UCX_TEST_BEGIN universe@354: universe@356: elements = array->data; universe@354: UCX_TEST_ASSERT(elements[0] == 42, "failed"); universe@354: universe@356: ucx_array_append(array, 13); universe@356: ucx_array_append(array, 37); universe@354: universe@356: elements = array->data; universe@356: UCX_TEST_ASSERT(array->size == 3, "incorrect size after append"); universe@354: UCX_TEST_ASSERT(elements[1] == 13, "failed"); universe@354: UCX_TEST_ASSERT(elements[2] == 37, "failed"); universe@354: UCX_TEST_ASSERT(elements[0] == 42, universe@354: "append corrupted previously inserted data"); universe@334: universe@33: UCX_TEST_END universe@27: universe@356: ucx_array_destroy(array); universe@24: } universe@24: universe@334: UCX_TEST(test_ucx_array_prepend) { universe@342: int *elems; universe@356: UcxArray *array = ucx_array_new(16, sizeof(int)); universe@334: universe@356: ucx_array_prepend(array, 42); universe@33: UCX_TEST_BEGIN universe@27: universe@356: elems = array->data; universe@342: UCX_TEST_ASSERT(elems[0] == 42, "failed"); universe@334: universe@356: ucx_array_prepend(array, 37); universe@356: ucx_array_prepend(array, 13); universe@334: universe@356: elems = array->data; universe@356: UCX_TEST_ASSERT(array->size == 3, "incorrect size after prepend"); universe@342: UCX_TEST_ASSERT(elems[0] == 13, "failed"); universe@354: UCX_TEST_ASSERT(elems[1] == 37, "failed"); universe@354: UCX_TEST_ASSERT(elems[2] == 42, universe@334: "prepend corrupted previously inserted data"); universe@334: universe@33: UCX_TEST_END universe@334: universe@356: ucx_array_free(array); universe@18: } universe@18: universe@337: UCX_TEST(test_ucx_array_set) { universe@342: int *elems; universe@356: UcxArray *array = ucx_array_new(16, sizeof(int)); universe@337: universe@337: UCX_TEST_BEGIN universe@337: universe@356: ucx_array_set(array, 7, 42); universe@342: universe@356: elems = array->data; universe@342: UCX_TEST_ASSERT(elems[7] == 42, "failed"); universe@356: UCX_TEST_ASSERT(array->size == 8, "array not resized on set"); universe@356: UCX_TEST_ASSERT(array->capacity == 16, "capacity changed unnecessarily"); universe@337: universe@356: ucx_array_set(array, 27, 13); universe@356: ucx_array_set(array, 28, 37); universe@337: universe@356: elems = array->data; universe@342: UCX_TEST_ASSERT(elems[27] == 13, "failed"); universe@354: UCX_TEST_ASSERT(elems[28] == 37, "failed"); universe@356: UCX_TEST_ASSERT(array->size == 29, "array not resized on set"); universe@356: UCX_TEST_ASSERT(array->capacity == 32, "capacity not grown"); universe@354: universe@337: UCX_TEST_END universe@337: universe@356: ucx_array_free(array); universe@337: } universe@337: universe@334: UCX_TEST(test_ucx_array_equals) { universe@356: UcxArray *a1 = ucx_array_new(16, sizeof(int32_t)); universe@356: UcxArray *a2 = ucx_array_new(16, sizeof(int32_t)); universe@356: UcxArray *a3 = ucx_array_new(16, sizeof(int64_t)); universe@356: UcxArray *a4 = ucx_array_new(16, sizeof(int32_t)); universe@27: universe@350: int32_t *intelems; universe@350: int64_t *longintelems; universe@342: universe@356: a1->size = 5; universe@356: intelems = a1->data; universe@342: intelems[0] = 47; universe@342: intelems[1] = 11; universe@342: intelems[2] = 0; universe@342: intelems[3] = 8; universe@342: intelems[4] = 15; universe@356: a2->size = 5; universe@356: intelems = a2->data; universe@342: intelems[0] = 47; universe@342: intelems[1] = 11; universe@342: intelems[2] = 0; universe@342: intelems[3] = 8; universe@342: intelems[4] = 15; universe@356: a3->size = 5; universe@356: longintelems = a3->data; universe@342: longintelems[0] = 47; universe@342: longintelems[1] = 11; universe@342: longintelems[2] = 0; universe@342: longintelems[3] = 8; universe@342: longintelems[4] = 15; universe@356: a4->size = 5; universe@356: intelems = a4->data; universe@342: intelems[0] = 47; universe@342: intelems[1] = 11; universe@342: intelems[2] = -6; universe@342: intelems[3] = 8; universe@342: intelems[4] = 15; universe@27: universe@123: UCX_TEST_BEGIN universe@123: universe@350: UCX_TEST_ASSERT(ucx_array_equals(a1, a2, ucx_cmp_int32, NULL), "failed"); universe@350: UCX_TEST_ASSERT(!ucx_array_equals(a1, a4, ucx_cmp_int32, NULL), "failed"); universe@350: UCX_TEST_ASSERT(!ucx_array_equals(a4, a1, ucx_cmp_int32, NULL), "failed"); universe@350: UCX_TEST_ASSERT(!ucx_array_equals(a1, a3, ucx_cmp_int64, NULL), universe@336: "comparing arrays of different element size shall fail"); universe@350: UCX_TEST_ASSERT(!ucx_array_equals(a3, a1, ucx_cmp_int64, NULL), universe@336: "comparing arrays of different element size shall fail"); universe@334: universe@336: UCX_TEST_ASSERT(ucx_array_equals(a1, a2, NULL, NULL), universe@334: "compare using memcmp() failed"); universe@336: UCX_TEST_ASSERT(!ucx_array_equals(a1, a4, NULL, NULL), universe@334: "compare using memcmp() failed"); universe@27: universe@33: UCX_TEST_END universe@356: ucx_array_free(a1); universe@356: ucx_array_free(a2); universe@356: ucx_array_free(a3); universe@356: ucx_array_free(a4); olaf@9: } olaf@9: universe@334: UCX_TEST(test_ucx_array_concat) { universe@356: UcxArray *a1 = ucx_array_new(16, sizeof(int)); universe@356: UcxArray *a2 = ucx_array_new(16, sizeof(int)); universe@342: int *elems; universe@27: universe@356: a1->size = 2; universe@356: elems = a1->data; universe@342: elems[0] = 47; universe@342: elems[1] = 11; universe@356: a2->size = 3; universe@356: elems = a2->data; universe@342: elems[0] = 0; universe@342: elems[1] = 8; universe@342: elems[2] = 15; universe@27: universe@123: UCX_TEST_BEGIN universe@123: universe@356: UCX_TEST_ASSERT(!ucx_array_concat(a1, a2), "failed"); universe@356: UCX_TEST_ASSERT(a1->size == 5, "failed"); universe@356: elems = a1->data; universe@342: UCX_TEST_ASSERT(elems[0] == 47, "failed"); universe@342: UCX_TEST_ASSERT(elems[1] == 11, "failed"); universe@342: UCX_TEST_ASSERT(elems[2] == 0, "failed"); universe@342: UCX_TEST_ASSERT(elems[3] == 8, "failed"); universe@342: UCX_TEST_ASSERT(elems[4] == 15, "failed"); universe@27: universe@356: a1->elemsize *= 2; universe@356: UCX_TEST_ASSERT(ucx_array_concat(a1, a2), universe@334: "arrays of different element size must not be concatenated"); universe@356: UCX_TEST_ASSERT(a1->size == 5, universe@334: "arrays of different element size must not be concatenated"); universe@27: universe@33: UCX_TEST_END universe@356: ucx_array_free(a1); universe@356: ucx_array_free(a2); universe@27: } universe@24: universe@344: UCX_TEST(test_ucx_array_at) { universe@356: UcxArray *array = ucx_array_new(16, sizeof(int)); universe@344: universe@354: int x[3] = {42, 13, 5}; universe@356: ucx_array_append_from(array, x, 3); universe@344: universe@344: UCX_TEST_BEGIN universe@344: universe@344: UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 1) == 13, "failed"); universe@344: *(int*)ucx_array_at(array, 1) = 80; universe@344: UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 1) == 80, "assignment failed"); universe@344: universe@344: UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 0) == 42, "corrupted data"); universe@344: UCX_TEST_ASSERT(*(int*)ucx_array_at(array, 2) == 5, "corrupted data"); universe@344: universe@344: UCX_TEST_END universe@344: universe@356: ucx_array_free(array); universe@344: } universe@344: universe@334: UCX_TEST(test_ucx_array_find) { universe@356: UcxArray *array = ucx_array_new(16, sizeof(int)); universe@342: int *elems; universe@334: universe@356: array->size = 5; universe@356: elems = array->data; universe@342: elems[0] = 47; universe@342: elems[1] = 11; universe@342: elems[2] = 0; universe@342: elems[3] = 8; universe@342: elems[4] = 15; universe@334: universe@334: int x = 8; universe@334: int y = 90; universe@123: universe@123: UCX_TEST_BEGIN universe@123: universe@334: UCX_TEST_ASSERT(ucx_array_find(array,(void*)&x,ucx_cmp_int,NULL) == 3, universe@334: "doesn't find element"); universe@334: UCX_TEST_ASSERT(ucx_array_find(array,(void*)&y,ucx_cmp_int,NULL) == 5, universe@334: "finds non-existing element"); universe@123: universe@334: UCX_TEST_ASSERT(ucx_array_find(array,(void*)&x,NULL,NULL) == 3, universe@334: "failed using memcmp()"); universe@334: UCX_TEST_ASSERT(ucx_array_find(array,(void*)&y,NULL,NULL) == 5, universe@334: "failed using memcmp()"); universe@172: universe@123: UCX_TEST_END universe@356: ucx_array_free(array); universe@123: } universe@123: universe@334: UCX_TEST(test_ucx_array_contains) { universe@356: UcxArray *array = ucx_array_new(16, sizeof(int)); universe@342: int *elems; universe@90: universe@356: array->size = 5; universe@356: elems = array->data; universe@342: elems[0] = 47; universe@342: elems[1] = 11; universe@342: elems[2] = 0; universe@342: elems[3] = 8; universe@342: elems[4] = 15; olaf@162: universe@334: int x = 8; universe@334: int y = 90; universe@27: universe@123: UCX_TEST_BEGIN universe@123: universe@334: UCX_TEST_ASSERT(ucx_array_contains(array,(void*)&x,ucx_cmp_int,NULL), universe@334: "false negative"); universe@334: UCX_TEST_ASSERT(!ucx_array_contains(array,(void*)&y,ucx_cmp_int,NULL), universe@334: "false positive"); olaf@162: universe@334: UCX_TEST_ASSERT(ucx_array_contains(array,(void*)&x,NULL,NULL), universe@334: "false negative using memcmp()"); universe@334: UCX_TEST_ASSERT(!ucx_array_contains(array,(void*)&y,NULL,NULL), universe@334: "false positive using memcmp()"); olaf@162: universe@33: UCX_TEST_END universe@356: ucx_array_free(array); universe@27: } universe@27: universe@334: UCX_TEST(test_ucx_array_remove) { universe@356: UcxArray *array = ucx_array_new(16, sizeof(int)); universe@342: int *elems; universe@334: universe@356: array->size = 5; universe@356: elems = array->data; universe@342: elems[0] = 47; universe@342: elems[1] = 11; universe@342: elems[2] = 0; universe@342: elems[3] = 8; universe@342: elems[4] = 15; universe@334: universe@334: UCX_TEST_BEGIN universe@334: universe@356: ucx_array_remove(array, 2); universe@356: elems = array->data; universe@334: UCX_TEST_ASSERT( universe@342: elems[0] == 47 && universe@342: elems[1] == 11 && universe@342: elems[2] == 8 && universe@342: elems[3] == 15, universe@334: "wrong contents after remove"); universe@356: UCX_TEST_ASSERT(array->size == 4, "wrong size after remove"); universe@27: universe@356: ucx_array_remove_fast(array, 1); universe@356: elems = array->data; universe@334: UCX_TEST_ASSERT( universe@342: elems[0] == 47 && universe@342: elems[1] == 15 && universe@342: elems[2] == 8, universe@334: "wrong contents after fast remove"); universe@356: UCX_TEST_ASSERT(array->size == 3, "wrong size after fast remove"); universe@27: universe@334: UCX_TEST_END universe@356: ucx_array_free(array); universe@334: } universe@334: universe@334: UCX_TEST(test_ucx_array_clone) { universe@356: UcxArray array; universe@356: UcxArray copy; universe@356: ucx_array_init(&array, 16, sizeof(int)); universe@357: ucx_array_init(©, 4, 2*sizeof(double)); universe@342: int *elems; universe@27: universe@334: array.size = 5; universe@342: elems = array.data; universe@342: elems[0] = 47; universe@342: elems[1] = 11; universe@342: elems[2] = 0; universe@342: elems[3] = 8; universe@342: elems[4] = 15; universe@334: universe@356: ucx_array_clone(©, &array); universe@33: UCX_TEST_BEGIN universe@27: universe@334: UCX_TEST_ASSERT(array.data != copy.data, "no true copy"); universe@334: UCX_TEST_ASSERT(array.size == copy.size, "size mismatch"); universe@334: UCX_TEST_ASSERT(array.capacity == copy.capacity, "capacity mismatch"); universe@334: UCX_TEST_ASSERT(array.elemsize == copy.elemsize, "element size mismatch"); universe@334: UCX_TEST_ASSERT(array.allocator == copy.allocator, "allocator mismatch"); universe@356: UCX_TEST_ASSERT(ucx_array_equals(&array, ©, ucx_cmp_int, NULL), universe@356: "contents do not match after clone"); universe@211: universe@334: UCX_TEST_END universe@334: universe@353: ucx_array_destroy(&array); universe@353: ucx_array_destroy(©); universe@334: } universe@27: universe@342: static int ucx_cmp_int_reverse(const void* x, const void* y, void* data) { universe@342: return -ucx_cmp_int(x,y,data); universe@342: } universe@342: universe@334: UCX_TEST(test_ucx_array_sort) { universe@342: int *elems; universe@342: universe@356: UcxArray *array = ucx_array_new(16, sizeof(int)); universe@356: array->size = 5; universe@356: elems = array->data; universe@342: elems[0] = 47; universe@342: elems[1] = 11; universe@342: elems[2] = 0; universe@342: elems[3] = 8; universe@342: elems[4] = 15; universe@334: universe@356: UcxArray *expected = ucx_array_new(16, sizeof(int)); universe@356: expected->size = 5; universe@356: elems = expected->data; universe@342: elems[0] = 0; universe@342: elems[1] = 8; universe@342: elems[2] = 11; universe@342: elems[3] = 15; universe@342: elems[4] = 47; universe@342: universe@356: UcxArray *expectedrev = ucx_array_new(16, sizeof(int)); universe@356: expectedrev->size = 5; universe@356: elems = expectedrev->data; universe@342: elems[0] = 47; universe@342: elems[1] = 15; universe@342: elems[2] = 11; universe@342: elems[3] = 8; universe@342: elems[4] = 0; universe@334: universe@334: universe@334: UCX_TEST_BEGIN universe@356: void* original_ptr = array->data; universe@336: ucx_array_sort(array, ucx_cmp_int, NULL); universe@336: UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL), "failed"); universe@356: UCX_TEST_ASSERT(array->size == 5, "size corrupted"); universe@356: UCX_TEST_ASSERT(array->data == original_ptr, "shall not reallocate"); universe@342: universe@342: ucx_array_sort(array, ucx_cmp_int_reverse, NULL); universe@342: UCX_TEST_ASSERT(ucx_array_equals(array, expectedrev, NULL, NULL), "failed"); universe@342: universe@356: ucx_array_reserve(array, 32); universe@356: ucx_array_reserve(expected, 32); universe@356: array->size = expected->size = 32; universe@336: for (size_t i = 0 ; i < 32 ; i++) { universe@356: ((int*)array->data)[i]= ((i%2==0)?-1:1) * ((int) i); universe@356: ((int*)expected->data)[i] = (-30+2*i) - (i > 15 ? 1 : 0); universe@336: } universe@336: universe@342: /* dummy third argument to trigger a possible fallback for qsort_s */ universe@356: ucx_array_sort(array, ucx_cmp_int, array->data); universe@336: UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL), universe@336: "failed for bigger arrays"); universe@334: UCX_TEST_END universe@334: universe@356: ucx_array_free(expectedrev); universe@356: ucx_array_free(expected); universe@356: ucx_array_free(array); universe@27: } universe@35: universe@334: UCX_TEST(test_ucx_array_autogrow) { universe@342: int *elems; universe@356: UcxArray *array = ucx_array_new(4, sizeof(int)); universe@356: array->size = 3; universe@356: elems = array->data; universe@342: elems[0] = 47; universe@342: elems[1] = 11; universe@334: int x = 5; universe@334: universe@334: UCX_TEST_BEGIN universe@334: universe@356: void* oldptr = array->data; universe@334: universe@356: ucx_array_append(array, 5); universe@356: UCX_TEST_ASSERT(array->capacity == 4 && array->data == oldptr, universe@334: "array should not grow too early"); universe@356: ucx_array_append(array, 5); universe@356: elems = array->data; universe@356: UCX_TEST_ASSERT(array->capacity == 8, "array did not grow"); universe@356: UCX_TEST_ASSERT(array->size == 5, "incorrect size after grow"); universe@342: UCX_TEST_ASSERT(elems[3] == 5 && elems[4] == 5, "corrupt data"); universe@334: universe@334: UCX_TEST_END universe@356: ucx_array_free(array); universe@334: } universe@35: universe@334: UCX_TEST(test_ucx_array_shrink) { universe@356: UcxArray *array = ucx_array_new(16, sizeof(int)); universe@356: array->size = 4; universe@334: universe@334: UCX_TEST_BEGIN universe@356: UCX_TEST_ASSERT(!ucx_array_shrink(array), "failed"); universe@356: UCX_TEST_ASSERT(array->capacity == 4, "incorrect capacity after shrink"); universe@334: UCX_TEST_END universe@356: ucx_array_free(array); universe@334: } universe@334: universe@334: UCX_TEST(test_ucx_array_resize) { universe@356: UcxArray *array = ucx_array_new(16, sizeof(int)); universe@356: array->size = 8; universe@334: universe@334: UCX_TEST_BEGIN universe@35: universe@356: UCX_TEST_ASSERT(!ucx_array_resize(array, 32), "failed"); universe@356: UCX_TEST_ASSERT(array->capacity == 32, "incorrect capacity after resize"); universe@356: UCX_TEST_ASSERT(array->size == 8, "incorrect size after resize"); universe@334: universe@356: UCX_TEST_ASSERT(!ucx_array_resize(array, 4), "failed"); universe@356: UCX_TEST_ASSERT(array->capacity == 4, "incorrect capacity after resize"); universe@356: UCX_TEST_ASSERT(array->size == 4, "incorrect size after resize"); universe@334: universe@334: UCX_TEST_END universe@356: ucx_array_free(array); universe@334: } universe@35: universe@334: UCX_TEST(test_ucx_array_reserve) { universe@356: UcxArray *array = ucx_array_new(16, sizeof(int)); universe@334: universe@35: UCX_TEST_BEGIN universe@334: universe@356: UCX_TEST_ASSERT(!ucx_array_reserve(array, 4), "failed"); universe@356: UCX_TEST_ASSERT(array->capacity == 16, "reserve shall not shrink"); universe@334: universe@356: UCX_TEST_ASSERT(!ucx_array_resize(array, 32), "failed"); universe@356: UCX_TEST_ASSERT(array->capacity == 32, "incorrect capacity after reserve"); universe@334: universe@35: UCX_TEST_END universe@356: ucx_array_free(array); universe@35: } universe@355: universe@355: UCX_TEST(test_ucx_array_util_set) { universe@355: size_t capacity = 16; universe@355: int* array = malloc(sizeof(int)*capacity); universe@355: universe@355: UCX_TEST_BEGIN universe@355: universe@355: UCX_ARRAY_UTIL_SET(&array, &capacity, 7, 42); universe@355: universe@355: UCX_TEST_ASSERT(array[7] == 42, "failed"); universe@355: UCX_TEST_ASSERT(capacity == 16, "capacity changed unnecessarily"); universe@355: universe@355: UCX_ARRAY_UTIL_SET(&array, &capacity, 37, 13); universe@355: UCX_ARRAY_UTIL_SET(&array, &capacity, 38, 37); universe@355: universe@355: UCX_TEST_ASSERT(array[37] == 13, "failed"); universe@355: UCX_TEST_ASSERT(array[38] == 37, "failed"); universe@355: UCX_TEST_ASSERT(capacity == 64, "capacity not grown"); universe@355: universe@355: UCX_TEST_END universe@355: universe@355: free(array); universe@355: }