# HG changeset patch # User Mike Becker # Date 1573054074 -3600 # Node ID 41a7cef34c1996d9285b373a28bd7f634b71ca1b # Parent 72da0e4cbf4a84e05f7d22efcc626e1a25a09eba adds testcase to uncover a bug in ucx_array_append() diff -r 72da0e4cbf4a -r 41a7cef34c19 test/array_tests.c --- a/test/array_tests.c Sun Nov 03 17:03:10 2019 +0100 +++ b/test/array_tests.c Wed Nov 06 16:27:54 2019 +0100 @@ -96,6 +96,48 @@ ucx_array_free(array); } +UCX_TEST(test_ucx_array_append_from_struct) { + struct teststruct { + unsigned long long x; + unsigned long long y; + unsigned long long z; + }; + + UcxArray *array = ucx_array_new(16, sizeof(struct teststruct)); + struct teststruct *elements; + + struct teststruct data; + data.x = 13; data.y = 37; data.z = 47; + + ucx_array_append_from(array, &data, 1); + UCX_TEST_BEGIN + + elements = array->data; + UCX_TEST_ASSERT(elements[0].x == 13, "failed"); + UCX_TEST_ASSERT(elements[0].y == 37, "failed"); + UCX_TEST_ASSERT(elements[0].z == 47, "failed"); + + data.x = 0; data.y = 8; data.z = 15; + ucx_array_append_from(array, &data, 1); + + elements = array->data; + UCX_TEST_ASSERT(array->size == 2, "incorrect size after append"); + UCX_TEST_ASSERT(elements[1].x == 0, "failed"); + UCX_TEST_ASSERT(elements[1].y == 8, "failed"); + UCX_TEST_ASSERT(elements[1].z == 15, "failed"); + + UCX_TEST_ASSERT(elements[0].x == 13, + "append corrupted previously inserted data"); + UCX_TEST_ASSERT(elements[0].y == 37, + "append corrupted previously inserted data"); + UCX_TEST_ASSERT(elements[0].z == 47, + "append corrupted previously inserted data"); + + UCX_TEST_END + + ucx_array_destroy(array); +} + UCX_TEST(test_ucx_array_prepend_from) { int *elems; UcxArray *array = ucx_array_new(16, sizeof(int)); @@ -195,6 +237,48 @@ ucx_array_destroy(array); } +UCX_TEST(test_ucx_array_append_struct) { + struct teststruct { + unsigned long long x; + unsigned long long y; + unsigned long long z; + }; + + UcxArray *array = ucx_array_new(16, sizeof(struct teststruct)); + struct teststruct *elements; + + struct teststruct data; + data.x = 13; data.y = 37; data.z = 47; + + ucx_array_append(array, data); + UCX_TEST_BEGIN + + elements = array->data; + UCX_TEST_ASSERT(elements[0].x == 13, "failed"); + UCX_TEST_ASSERT(elements[0].y == 37, "failed"); + UCX_TEST_ASSERT(elements[0].z == 47, "failed"); + + data.x = 0; data.y = 8; data.z = 15; + ucx_array_append(array, data); + + elements = array->data; + UCX_TEST_ASSERT(array->size == 2, "incorrect size after append"); + UCX_TEST_ASSERT(elements[1].x == 0, "failed"); + UCX_TEST_ASSERT(elements[1].y == 8, "failed"); + UCX_TEST_ASSERT(elements[1].z == 15, "failed"); + + UCX_TEST_ASSERT(elements[0].x == 13, + "append corrupted previously inserted data"); + UCX_TEST_ASSERT(elements[0].y == 37, + "append corrupted previously inserted data"); + UCX_TEST_ASSERT(elements[0].z == 47, + "append corrupted previously inserted data"); + + UCX_TEST_END + + ucx_array_destroy(array); +} + UCX_TEST(test_ucx_array_prepend) { int *elems; UcxArray *array = ucx_array_new(16, sizeof(int)); diff -r 72da0e4cbf4a -r 41a7cef34c19 test/array_tests.h --- a/test/array_tests.h Sun Nov 03 17:03:10 2019 +0100 +++ b/test/array_tests.h Wed Nov 06 16:27:54 2019 +0100 @@ -40,9 +40,11 @@ UCX_TEST(test_ucx_array_new); UCX_TEST(test_ucx_array_at); UCX_TEST(test_ucx_array_append_from); +UCX_TEST(test_ucx_array_append_from_struct); UCX_TEST(test_ucx_array_prepend_from); UCX_TEST(test_ucx_array_set_from); UCX_TEST(test_ucx_array_append); +UCX_TEST(test_ucx_array_append_struct); UCX_TEST(test_ucx_array_prepend); UCX_TEST(test_ucx_array_set); UCX_TEST(test_ucx_array_autogrow); diff -r 72da0e4cbf4a -r 41a7cef34c19 test/main.c --- a/test/main.c Sun Nov 03 17:03:10 2019 +0100 +++ b/test/main.c Wed Nov 06 16:27:54 2019 +0100 @@ -148,9 +148,11 @@ ucx_test_register(suite, test_ucx_array_new); ucx_test_register(suite, test_ucx_array_at); ucx_test_register(suite, test_ucx_array_append_from); + ucx_test_register(suite, test_ucx_array_append_from_struct); ucx_test_register(suite, test_ucx_array_prepend_from); ucx_test_register(suite, test_ucx_array_set_from); ucx_test_register(suite, test_ucx_array_append); + ucx_test_register(suite, test_ucx_array_append_struct); ucx_test_register(suite, test_ucx_array_prepend); ucx_test_register(suite, test_ucx_array_set); ucx_test_register(suite, test_ucx_array_autogrow);