~donmcc/ood

2cad5cb517396f3e0e9f1267a98dc29fe2dfcbfb — Don McCaughey 28 days ago b1579e4 main
Add `ood_ptr_array_append()` function.

Unlike `ood_ptr_array_add()`, which will fill any `NULL` spot in the
array of item pointers, the "append" function will always place the new
item at the end of the items array, or fail if there is no unused
capacity left at the end of the array.
3 files changed, 69 insertions(+), 0 deletions(-)

M src/libood/ood/ptr_array.c
M src/libood/ood/ptr_array.h
M src/libood/ood/ptr_array_test.c
M src/libood/ood/ptr_array.c => src/libood/ood/ptr_array.c +13 -0
@@ 96,6 96,19 @@ ood_ptr_array_realloc_add(struct ood_ptr_array *array, void *item)


enum ood_result
ood_ptr_array_append(struct ood_ptr_array *array, void *item)
{
    int i = array->capacity - 1;
    if (array->items[i]) return ood_error;
    skip_empty_indices_reverse(array, &i);
    ++i;
    array->items[i] = item;
    ++array->count;
    return ood_okay;
}


enum ood_result
ood_ptr_array_clear(struct ood_ptr_array *array, void *item)
{
    for (int i = 0; i < array->capacity; ++i) {

M src/libood/ood/ptr_array.h => src/libood/ood/ptr_array.h +3 -0
@@ 29,6 29,9 @@ enum ood_result
ood_ptr_array_realloc_add(struct ood_ptr_array *array, void *item);

enum ood_result
ood_ptr_array_append(struct ood_ptr_array *array, void *item);

enum ood_result
ood_ptr_array_clear(struct ood_ptr_array *array, void *item);

void *

M src/libood/ood/ptr_array_test.c => src/libood/ood/ptr_array_test.c +53 -0
@@ 118,6 118,58 @@ test_ptr_array_realloc_add(void) {
    ood_ptr_array_free(array);
}


static void
test_ptr_array_append(void)
{
    char *one = "one";
    char *two = "two";
    char *three = "three";
    char *four = "four";
    struct ood_ptr_array *array = ood_ptr_array_alloc(3);
    assert(3 == array->capacity);

    // append three items
    enum ood_result result = ood_ptr_array_append(array, one);
    assert(ood_okay == result);
    assert(1 == array->count);

    result = ood_ptr_array_append(array, two);
    assert(ood_okay == result);
    assert(2 == array->count);

    result = ood_ptr_array_append(array, three);
    assert(ood_okay == result);
    assert(3 == array->count);

    // array is full
    result = ood_ptr_array_append(array, four);
    assert(ood_error == result);

    // remove middle item
    result = ood_ptr_array_clear(array, two);
    assert(ood_okay == result);
    assert(2 == array->count);

    // try to append one item
    result = ood_ptr_array_append(array, four);
    assert(ood_error == result);

    // remove last item
    result = ood_ptr_array_clear(array, three);
    assert(ood_okay == result);
    assert(1 == array->count);

    // append one item
    result = ood_ptr_array_append(array, four);
    assert(ood_okay == result);
    assert(2 == array->count);
    assert(four == ood_ptr_array_last(array));

    ood_ptr_array_free(array);
}


static void
test_ptr_array_next(void)
{


@@ 397,6 449,7 @@ ptr_array_test(void)
    test_ptr_array_add_and_clear();

    test_ptr_array_realloc_add();
    test_ptr_array_append();

    test_ptr_array_next();
    test_ptr_array_next_when_empty();