Commit f806ffb7 authored by Chris Müller's avatar Chris Müller

test: add testcases for pointer arrays

parent f9456069
#include "unittest.h"
#include "structures/array.h"
#include <assert.h>
#include <stdio.h>
static void
test_array_allocation(const_pointer data)
{
char text[] = { 'A', 'B', 'C', 'D', 'E' };
struct CryArray* array = cry_array_new(2, 4);
assert(cry_array_size(array) == 0);
assert(cry_array_alloc(array) == 4);
cry_array_append(array, text, 2);
assert(cry_array_size(array) == 2);
assert(cry_array_alloc(array) == 4);
cry_array_append(array, text + 2, 3);
assert(cry_array_size(array) == 5);
assert(cry_array_alloc(array) == 8);
cry_array_free(array);
}
#include <string.h>
static void
test_array_operations(const_pointer data)
{
char text[] = { 'A', 'B', 'C', 'D', 'E' };
struct CryArray* array = cry_array_new(2, 4);
struct CryArray* array = cry_array_new(4);
assert(cry_array_size(array) == 0);
assert(cry_array_alloc(array) == 4);
assert(cry_array_capacity(array) == 4);
cry_array_append(array, text, 5);
cry_array_append(array, "\0", 1);
assert(cry_array_size(array) == 6);
assert(cry_array_alloc(array) == 8);
assert(cry_array_capacity(array) > 4);
assert(strcmp(cry_array_get(array, 0), "ABCDE") == 0);
assert(cry_array_remove(array, 0, 2) == CRY_OKAY);
assert(cry_array_size(array) == 4);
assert(cry_array_alloc(array) == 8);
assert(cry_array_capacity(array) > 4);
assert(cry_array_get(array, 0) != 0);
......@@ -56,19 +32,67 @@ test_array_operations(const_pointer data)
assert(cry_array_insert(array, 1, "A", 1) == CRY_OKAY);
assert(cry_array_size(array) == 5);
assert(cry_array_alloc(array) == 8);
assert(cry_array_capacity(array) > 4);
assert(cry_array_get(array, 0) != 0);
assert(strcmp(cry_array_get(array, 0), "CADE") == 0);
cry_array_free(array);
assert(cry_array_trim(array) == cry_array_size(array));
cry_array_free(array);
}
static void
test_ptr_array_operations(const_pointer data)
{
struct CryPtrArray* array = cry_ptr_array_new(4);
assert(cry_ptr_array_size(array) == 0);
assert(cry_ptr_array_capacity(array) == 4);
cry_ptr_array_append(array, "1");
cry_ptr_array_append(array, "2");
cry_ptr_array_append(array, "3");
cry_ptr_array_append(array, "4");
cry_ptr_array_append(array, "5");
assert(cry_ptr_array_size(array) == 5);
assert(cry_ptr_array_size(array) > 4);
assert(strcmp(cry_cast(const char*, cry_ptr_array_get(array, 0)), "1") == 0);
assert(strcmp(cry_cast(const char*, cry_ptr_array_get(array, 1)), "2") == 0);
assert(strcmp(cry_cast(const char*, cry_ptr_array_get(array, 2)), "3") == 0);
assert(strcmp(cry_cast(const char*, cry_ptr_array_get(array, 3)), "4") == 0);
assert(strcmp(cry_cast(const char*, cry_ptr_array_get(array, 4)), "5") == 0);
cry_ptr_array_remove(array, 0);
cry_ptr_array_remove(array, 1);
assert(cry_ptr_array_size(array) == 3);
assert(strcmp(cry_cast(const char*, cry_ptr_array_get(array, 0)), "2") == 0);
assert(strcmp(cry_cast(const char*, cry_ptr_array_get(array, 1)), "4") == 0);
assert(strcmp(cry_cast(const char*, cry_ptr_array_get(array, 2)), "5") == 0);
assert(cry_ptr_array_insert(array, 0, "0") == CRY_OKAY);
assert(strcmp(cry_cast(const char*, cry_ptr_array_get(array, 0)), "0") == 0);
assert(strcmp(cry_cast(const char*, cry_ptr_array_get(array, 1)), "2") == 0);
assert(strcmp(cry_cast(const char*, cry_ptr_array_get(array, 2)), "4") == 0);
assert(strcmp(cry_cast(const char*, cry_ptr_array_get(array, 3)), "5") == 0);
assert(cry_ptr_array_size(array) == 4);
assert(cry_ptr_array_trim(array) == cry_ptr_array_size(array));
cry_ptr_array_free(array);
}
void
cry_test_arrays(void)
{
cry_unittest_run("structures.array.allocation", test_array_allocation, 0, 10);
cry_unittest_run("structures.array.operations", test_array_operations, 0, 10);
cry_unittest_run("structures.array", test_array_operations, 0, 10);
cry_unittest_run("structures.ptr_array", test_ptr_array_operations, 0, 10);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment