From 1c4b748a508552358d5654412389636029257c29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Fri, 17 Feb 2017 11:59:11 +0100 Subject: test/core: add some more array related tests --- core/array.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'core/array.c') diff --git a/core/array.c b/core/array.c index 64f6b81..5cf19a6 100644 --- a/core/array.c +++ b/core/array.c @@ -54,6 +54,15 @@ static void test_small_objects(void) { "Re-add integer: %zu = %d", i, values[i]); } + int old, *tmp; + ok((tmp = array_get(&arr, 0)) && (old = *tmp) && array_set(&arr, 0, NULL) && + array_get(&arr, 0) == tmp && *tmp == 0 && array_set(&arr, 0, &old) && + array_get(&arr, 0) == tmp && *tmp == old, "Set array element NULL"); + ok(!array_set(&arr, array_length(&arr), &values[0]) && errno == EINVAL, "Get past end of array"); + ok(!array_get(&arr, array_length(&arr)) && errno == EINVAL, "Get past end of array"); + + ok(!array_remove(&arr, array_length(&arr)) && errno == EINVAL, "Remove past end of array"); + int *v; size_t len_before = array_length(&arr); @@ -124,6 +133,20 @@ static void test_large_objects(void) { static void test_pointers(void) { Array arr; + + array_init_sized(&arr, 1); + ok(array_length(&arr) == 0 && array_get_ptr(&arr, 0) == NULL && errno == ENOTSUP, + "Initialization with size 1"); + + ok(!array_add_ptr(&arr, &arr) && errno == ENOTSUP && array_get_ptr(&arr, 0) == NULL, + "Add pointer to non-pointer array"); + + errno = 0; + char byte = '_', *ptr; + ok(array_add(&arr, &byte) && (ptr = array_get(&arr, 0)) && *ptr == byte, + "Add byte element"); + ok(!array_get_ptr(&arr, 0) && errno == ENOTSUP, "Get pointer from non-pointer array"); + array_init(&arr); ok(array_length(&arr) == 0 && array_get_ptr(&arr, 0) == NULL && errno == EINVAL, "Initialization"); @@ -156,6 +179,13 @@ static void test_pointers(void) { "Get item: %zu = %p", i, (void*)item); } + Item *tmp; + ok((tmp = array_get_ptr(&arr, 0)) && array_set_ptr(&arr, 0, NULL) && + array_get_ptr(&arr, 0) == NULL && array_set_ptr(&arr, 0, tmp) && + array_get_ptr(&arr, 0) == tmp, "Set pointer NULL"); + ok(!array_set_ptr(&arr, array_length(&arr), items[0]) && errno == EINVAL, "Set pointer past end of array"); + ok(!array_get_ptr(&arr, array_length(&arr)) && errno == EINVAL, "Get pointer past end of array"); + array_clear(&arr); ok(array_length(&arr) == 0 && array_get_ptr(&arr, 0) == NULL && errno == EINVAL, "Clear"); -- cgit v1.2.3