Fri, 05 Jul 2019 15:07:43 +0200
fixes doxygen doc
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "array_tests.h" #include <ucx/utils.h> UCX_TEST(test_ucx_array_free) { UcxArray array = ucx_array_new(16, sizeof(int)); UCX_TEST_BEGIN ucx_array_free(&array); UCX_TEST_ASSERT(array.data == NULL, "data pointer not NULL after free"); UCX_TEST_ASSERT(array.size == 0, "size not zero after free"); UCX_TEST_ASSERT(array.capacity == 0, "capacity not zero after free"); UCX_TEST_ASSERT(array.allocator == ucx_default_allocator(), "allocator corrupted during free"); UCX_TEST_END } UCX_TEST(test_ucx_array_new) { UcxArray array = ucx_array_new(16, 47); UCX_TEST_BEGIN UCX_TEST_ASSERT(array.data, "no memory allocated"); UCX_TEST_ASSERT(array.size == 0, "size not initially zero"); UCX_TEST_ASSERT(array.capacity == 16, "capacity not as requested"); UCX_TEST_ASSERT(array.elemsize == 47, "element size not as requested"); UCX_TEST_ASSERT(array.allocator == ucx_default_allocator(), "array not using the default allocator"); UCX_TEST_END ucx_array_free(&array); } UCX_TEST(test_ucx_array_append) { UcxArray array = ucx_array_new(16, sizeof(int)); int x = 42; ucx_array_append(&array, &x); UCX_TEST_BEGIN UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, "failed"); x = 13; ucx_array_append(&array, &x); UCX_TEST_ASSERT(array.size == 2, "incorrect size after append"); UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13, "failed"); UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, "append corrupted previously inserted data"); ucx_array_append(&array, NULL); UCX_TEST_ASSERT(array.size == 3, "incorrect size after NULL append"); UCX_TEST_ASSERT(ucx_array_at_int(array, 2) == 0, "element is not zeroed"); UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, "NULL append corrupted previously inserted data"); UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13, "NULL append corrupted previously inserted data"); UCX_TEST_END ucx_array_free(&array); } UCX_TEST(test_ucx_array_prepend) { UcxArray array = ucx_array_new(16, sizeof(int)); int x = 42; ucx_array_prepend(&array, &x); UCX_TEST_BEGIN UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, "failed"); x = 13; ucx_array_prepend(&array, &x); UCX_TEST_ASSERT(array.size == 2, "incorrect size after prepend"); UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 13, "failed"); UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 42, "prepend corrupted previously inserted data"); ucx_array_prepend(&array, NULL); UCX_TEST_ASSERT(array.size == 3, "incorrect size after NULL prepend"); UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 0, "element is not zeroed"); UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13, "NULL prepend corrupted previously inserted data"); UCX_TEST_ASSERT(ucx_array_at_int(array, 2) == 42, "NULL prepend corrupted previously inserted data"); UCX_TEST_END ucx_array_free(&array); } UCX_TEST(test_ucx_array_set) { UcxArray array = ucx_array_new(16, sizeof(int)); int x = 42; UCX_TEST_BEGIN ucx_array_set(&array, 7, &x); UCX_TEST_ASSERT(ucx_array_at_int(array, 7) == 42, "failed"); UCX_TEST_ASSERT(array.size >= 8, "array not resized on set"); UCX_TEST_ASSERT(array.capacity == 16, "capacity changed unnecessarily"); x = 13; ucx_array_set(&array, 27, &x); UCX_TEST_ASSERT(ucx_array_at_int(array, 27) == 13, "failed"); UCX_TEST_ASSERT(array.size == 28, "array not resized on set"); UCX_TEST_ASSERT(array.capacity == 28, "capacity not grown"); ucx_array_set(&array, 7, NULL); UCX_TEST_ASSERT(ucx_array_at_int(array, 7) == 0, "not zeroed on NULL set"); UCX_TEST_END ucx_array_free(&array); } UCX_TEST(test_ucx_array_equals) { UcxArray a1 = ucx_array_new(16, sizeof(int)); UcxArray a2 = ucx_array_new(16, sizeof(int)); UcxArray a3 = ucx_array_new(16, sizeof(long int)); UcxArray a4 = ucx_array_new(16, sizeof(int)); a1.size = 5; ucx_array_at_int(a1, 0) = 47; ucx_array_at_int(a1, 1) = 11; ucx_array_at_int(a1, 2) = 0; ucx_array_at_int(a1, 3) = 8; ucx_array_at_int(a1, 4) = 15; a2.size = 5; ucx_array_at_int(a2, 0) = 47; ucx_array_at_int(a2, 1) = 11; ucx_array_at_int(a2, 2) = 0; ucx_array_at_int(a2, 3) = 8; ucx_array_at_int(a2, 4) = 15; a3.size = 5; ucx_array_at_longint(a3, 0) = 47; ucx_array_at_longint(a3, 1) = 11; ucx_array_at_longint(a3, 2) = 0; ucx_array_at_longint(a3, 3) = 8; ucx_array_at_longint(a3, 4) = 15; a4.size = 5; ucx_array_at_int(a4, 0) = 47; ucx_array_at_int(a4, 1) = 11; ucx_array_at_int(a4, 2) = -6; ucx_array_at_int(a4, 3) = 8; ucx_array_at_int(a4, 4) = 15; UCX_TEST_BEGIN UCX_TEST_ASSERT(ucx_array_equals(a1, a2, ucx_cmp_int, NULL), "failed"); UCX_TEST_ASSERT(!ucx_array_equals(a1, a4, ucx_cmp_int, NULL), "failed"); UCX_TEST_ASSERT(!ucx_array_equals(a4, a1, ucx_cmp_int, NULL), "failed"); UCX_TEST_ASSERT(!ucx_array_equals(a1, a3, ucx_cmp_int, NULL), "comparing arrays of different element size shall fail"); UCX_TEST_ASSERT(!ucx_array_equals(a3, a1, ucx_cmp_int, NULL), "comparing arrays of different element size shall fail"); UCX_TEST_ASSERT(ucx_array_equals(a1, a2, NULL, NULL), "compare using memcmp() failed"); UCX_TEST_ASSERT(!ucx_array_equals(a1, a4, NULL, NULL), "compare using memcmp() failed"); UCX_TEST_END ucx_array_free(&a1); ucx_array_free(&a2); ucx_array_free(&a3); ucx_array_free(&a4); } UCX_TEST(test_ucx_array_concat) { UcxArray a1 = ucx_array_new(16, sizeof(int)); UcxArray a2 = ucx_array_new(16, sizeof(int)); a1.size = 2; ucx_array_at_int(a1, 0) = 47; ucx_array_at_int(a1, 1) = 11; a2.size = 3; ucx_array_at_int(a2, 0) = 0; ucx_array_at_int(a2, 1) = 8; ucx_array_at_int(a2, 2) = 15; UCX_TEST_BEGIN UCX_TEST_ASSERT(!ucx_array_concat(&a1, &a2), "failed"); UCX_TEST_ASSERT(a1.size == 5, "failed"); UCX_TEST_ASSERT(ucx_array_at_int(a1, 0) == 47, "failed"); UCX_TEST_ASSERT(ucx_array_at_int(a1, 1) == 11, "failed"); UCX_TEST_ASSERT(ucx_array_at_int(a1, 2) == 0, "failed"); UCX_TEST_ASSERT(ucx_array_at_int(a1, 3) == 8, "failed"); UCX_TEST_ASSERT(ucx_array_at_int(a1, 4) == 15, "failed"); a1.elemsize *= 2; UCX_TEST_ASSERT(ucx_array_concat(&a1, &a2), "arrays of different element size must not be concatenated"); UCX_TEST_ASSERT(a1.size == 5, "arrays of different element size must not be concatenated"); UCX_TEST_END ucx_array_free(&a1); ucx_array_free(&a2); } UCX_TEST(test_ucx_array_at) { UcxArray array = ucx_array_new(16, sizeof(int)); int x = 42; ucx_array_append(&array, &x); x = 13; ucx_array_append(&array, &x); x = 5; ucx_array_append(&array, &x); UCX_TEST_BEGIN UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 13, "failed"); ucx_array_at_int(array, 1) = 80; UCX_TEST_ASSERT(ucx_array_at_int(array, 1) == 80, "assignment failed"); UCX_TEST_ASSERT(ucx_array_at_int(array, 0) == 42, "corrupted data"); UCX_TEST_ASSERT(ucx_array_at_int(array, 2) == 5, "corrupted data"); UCX_TEST_END ucx_array_free(&array); } UCX_TEST(test_ucx_array_find) { UcxArray array = ucx_array_new(16, sizeof(int)); array.size = 5; ucx_array_at_int(array, 0) = 47; ucx_array_at_int(array, 1) = 11; ucx_array_at_int(array, 2) = 0; ucx_array_at_int(array, 3) = 8; ucx_array_at_int(array, 4) = 15; int x = 8; int y = 90; UCX_TEST_BEGIN UCX_TEST_ASSERT(ucx_array_find(array,(void*)&x,ucx_cmp_int,NULL) == 3, "doesn't find element"); UCX_TEST_ASSERT(ucx_array_find(array,(void*)&y,ucx_cmp_int,NULL) == 5, "finds non-existing element"); UCX_TEST_ASSERT(ucx_array_find(array,(void*)&x,NULL,NULL) == 3, "failed using memcmp()"); UCX_TEST_ASSERT(ucx_array_find(array,(void*)&y,NULL,NULL) == 5, "failed using memcmp()"); UCX_TEST_END ucx_array_free(&array); } UCX_TEST(test_ucx_array_contains) { UcxArray array = ucx_array_new(16, sizeof(int)); array.size = 5; ucx_array_at_int(array, 0) = 47; ucx_array_at_int(array, 1) = 11; ucx_array_at_int(array, 2) = 0; ucx_array_at_int(array, 3) = 8; ucx_array_at_int(array, 4) = 15; int x = 8; int y = 90; UCX_TEST_BEGIN UCX_TEST_ASSERT(ucx_array_contains(array,(void*)&x,ucx_cmp_int,NULL), "false negative"); UCX_TEST_ASSERT(!ucx_array_contains(array,(void*)&y,ucx_cmp_int,NULL), "false positive"); UCX_TEST_ASSERT(ucx_array_contains(array,(void*)&x,NULL,NULL), "false negative using memcmp()"); UCX_TEST_ASSERT(!ucx_array_contains(array,(void*)&y,NULL,NULL), "false positive using memcmp()"); UCX_TEST_END ucx_array_free(&array); } UCX_TEST(test_ucx_array_remove) { UcxArray array = ucx_array_new(16, sizeof(int)); array.size = 5; ucx_array_at_int(array, 0) = 47; ucx_array_at_int(array, 1) = 11; ucx_array_at_int(array, 2) = 0; ucx_array_at_int(array, 3) = 8; ucx_array_at_int(array, 4) = 15; UCX_TEST_BEGIN ucx_array_remove(&array, 2); UCX_TEST_ASSERT( ucx_array_at_int(array, 0) == 47 && ucx_array_at_int(array, 1) == 11 && ucx_array_at_int(array, 2) == 8 && ucx_array_at_int(array, 3) == 15, "wrong contents after remove"); UCX_TEST_ASSERT(array.size == 4, "wrong size after remove"); ucx_array_remove_fast(&array, 1); UCX_TEST_ASSERT( ucx_array_at_int(array, 0) == 47 && ucx_array_at_int(array, 1) == 15 && ucx_array_at_int(array, 2) == 8, "wrong contents after fast remove"); UCX_TEST_ASSERT(array.size == 3, "wrong size after fast remove"); UCX_TEST_END ucx_array_free(&array); } UCX_TEST(test_ucx_array_clone) { UcxArray array = ucx_array_new(16, sizeof(int)); array.size = 5; ucx_array_at_int(array, 0) = 47; ucx_array_at_int(array, 1) = 11; ucx_array_at_int(array, 2) = 0; ucx_array_at_int(array, 3) = 8; ucx_array_at_int(array, 4) = 15; UcxArray copy = ucx_array_clone(array); UCX_TEST_BEGIN UCX_TEST_ASSERT(array.data != copy.data, "no true copy"); UCX_TEST_ASSERT(array.size == copy.size, "size mismatch"); UCX_TEST_ASSERT(array.capacity == copy.capacity, "capacity mismatch"); UCX_TEST_ASSERT(array.elemsize == copy.elemsize, "element size mismatch"); UCX_TEST_ASSERT(array.allocator == copy.allocator, "allocator mismatch"); UCX_TEST_ASSERT(ucx_array_equals(array, copy, ucx_cmp_int, NULL), "failed"); UCX_TEST_END ucx_array_free(&array); ucx_array_free(©); } UCX_TEST(test_ucx_array_sort) { UcxArray array = ucx_array_new(16, sizeof(int)); array.size = 5; ucx_array_at_int(array, 0) = 47; ucx_array_at_int(array, 1) = 11; ucx_array_at_int(array, 2) = 0; ucx_array_at_int(array, 3) = 8; ucx_array_at_int(array, 4) = 15; UcxArray expected = ucx_array_new(16, sizeof(int)); expected.size = 5; ucx_array_at_int(expected, 0) = 0; ucx_array_at_int(expected, 1) = 8; ucx_array_at_int(expected, 2) = 11; ucx_array_at_int(expected, 3) = 15; ucx_array_at_int(expected, 4) = 47; UCX_TEST_BEGIN void* original_ptr = array.data; ucx_array_sort(array, ucx_cmp_int, NULL); UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL), "failed"); UCX_TEST_ASSERT(array.size == 5, "size corrupted"); UCX_TEST_ASSERT(array.data == original_ptr, "shall not reallocate"); ucx_array_reserve(&array, 32); ucx_array_reserve(&expected, 32); array.size = expected.size = 32; for (size_t i = 0 ; i < 32 ; i++) { ucx_array_at_int(array, i) = ((i%2==0)?-1:1) * ((int) i); ucx_array_at_int(expected, i) = (-30+2*i) - (i > 15 ? 1 : 0); } ucx_array_sort(array, ucx_cmp_int, NULL); UCX_TEST_ASSERT(ucx_array_equals(array, expected, NULL, NULL), "failed for bigger arrays"); UCX_TEST_END ucx_array_free(&expected); ucx_array_free(&array); } UCX_TEST(test_ucx_array_autogrow) { UcxArray array = ucx_array_new(4, sizeof(int)); array.size = 3; ucx_array_at_int(array, 0) = 47; ucx_array_at_int(array, 1) = 11; int x = 5; UCX_TEST_BEGIN void* oldptr = array.data; ucx_array_append(&array, &x); UCX_TEST_ASSERT(array.capacity == 4 && array.data == oldptr, "array should not grow too early"); ucx_array_append(&array, &x); UCX_TEST_ASSERT(array.capacity == 8, "array did not grow"); UCX_TEST_ASSERT(array.size == 5, "incorrect size after grow"); UCX_TEST_ASSERT(ucx_array_at_int(array, 3) == 5 && ucx_array_at_int(array, 3) == 5, "corrupt data"); UCX_TEST_END ucx_array_free(&array); } UCX_TEST(test_ucx_array_shrink) { UcxArray array = ucx_array_new(16, sizeof(int)); array.size = 4; UCX_TEST_BEGIN UCX_TEST_ASSERT(!ucx_array_shrink(&array), "failed"); UCX_TEST_ASSERT(array.capacity == 4, "incorrect capacity after shrink"); UCX_TEST_END ucx_array_free(&array); } UCX_TEST(test_ucx_array_resize) { UcxArray array = ucx_array_new(16, sizeof(int)); array.size = 8; UCX_TEST_BEGIN UCX_TEST_ASSERT(!ucx_array_resize(&array, 32), "failed"); UCX_TEST_ASSERT(array.capacity == 32, "incorrect capacity after resize"); UCX_TEST_ASSERT(array.size == 8, "incorrect size after resize"); UCX_TEST_ASSERT(!ucx_array_resize(&array, 4), "failed"); UCX_TEST_ASSERT(array.capacity == 4, "incorrect capacity after resize"); UCX_TEST_ASSERT(array.size == 4, "incorrect size after resize"); UCX_TEST_END ucx_array_free(&array); } UCX_TEST(test_ucx_array_reserve) { UcxArray array = ucx_array_new(16, sizeof(int)); UCX_TEST_BEGIN UCX_TEST_ASSERT(!ucx_array_reserve(&array, 4), "failed"); UCX_TEST_ASSERT(array.capacity == 16, "reserve shall not shrink"); UCX_TEST_ASSERT(!ucx_array_resize(&array, 32), "failed"); UCX_TEST_ASSERT(array.capacity == 32, "incorrect capacity after reserve"); UCX_TEST_END ucx_array_free(&array); }