tests/test_iterator.c

Mon, 15 Dec 2025 19:00:51 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 15 Dec 2025 19:00:51 +0100
changeset 1608
46d8a8305948
parent 1607
0ecb13118cac
permissions
-rw-r--r--

complete refactoring of low-level array list functions - relates to #619

now only the documentation needs to be updated

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2024 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 "cx/test.h"

#include "cx/iterator.h"

CX_TEST(test_iterator_create) {
    unsigned array[20];
    size_t size = cx_nmemb(array);
    for (unsigned i = 0 ; i < size ; i++) array[i] = i;

    CxIterator iter = cxIterator(array, sizeof(unsigned), size);
    CX_TEST_DO {
        CX_TEST_ASSERT(iter.index == 0);
        CX_TEST_ASSERT(iter.elem_size == sizeof(unsigned));
        CX_TEST_ASSERT(iter.elem_count == size);
        CX_TEST_ASSERT(iter.src_handle == array);
        CX_TEST_ASSERT(iter.elem_handle == &array[0]);
        CX_TEST_ASSERT(cxIteratorValid(iter));
    }
}

CX_TEST(test_iterator_create_null) {
    CxIterator iter = cxIterator(NULL, sizeof(unsigned), 47);
    CX_TEST_DO {
        CX_TEST_ASSERT(iter.index == 0);
        CX_TEST_ASSERT(iter.elem_size == sizeof(unsigned));
        CX_TEST_ASSERT(iter.elem_count == 0);
        CX_TEST_ASSERT(iter.src_handle == NULL);
        CX_TEST_ASSERT(iter.elem_handle == NULL);
        CX_TEST_ASSERT(!cxIteratorValid(iter));
    }
}

CX_TEST(test_iterator_iterate) {
    unsigned array[20];
    size_t size = cx_nmemb(array);
    for (unsigned i = 0 ; i < size ; i++) array[i] = i;

    CxIterator iter = cxIterator(array, sizeof(unsigned), size);
    CX_TEST_DO {
        CX_TEST_ASSERT(iter.elem_size == sizeof(unsigned));
        CX_TEST_ASSERT(iter.elem_count == size);
        CX_TEST_ASSERT(iter.src_handle == array);
        unsigned expected = 0;
        cx_foreach(unsigned *, e, iter) {
            CX_TEST_ASSERT(iter.index == expected);
            CX_TEST_ASSERT(*e == expected);
            CX_TEST_ASSERT(iter.elem_handle == &array[expected]);
            expected++;
        }
        CX_TEST_ASSERT(expected == size);
    }
}

CX_TEST(test_iterator_iterate_pointers) {
    unsigned array[20];
    unsigned* ptr_array[20];
    size_t size = cx_nmemb(array);
    for (unsigned i = 0 ; i < size ; i++) {
        array[i] = 3*i;
        ptr_array[i] = &array[i];
    }

    CxIterator iter = cxIteratorPtr(ptr_array, size);
    CX_TEST_DO {
        CX_TEST_ASSERT(iter.elem_size == sizeof(void*));
        CX_TEST_ASSERT(iter.elem_count == size);
        CX_TEST_ASSERT(iter.src_handle == ptr_array);
        unsigned idx = 0;
        cx_foreach(unsigned *, e, iter) {
            CX_TEST_ASSERT(iter.index == idx);
            CX_TEST_ASSERT(*e == array[idx]);
            CX_TEST_ASSERT(iter.elem_handle == &ptr_array[idx]);
            idx++;
        }
        CX_TEST_ASSERT(idx == size);
    }
}

CxTestSuite *cx_test_suite_iterator(void) {
    CxTestSuite *suite = cx_test_suite_new("iterator");

    cx_test_register(suite, test_iterator_create);
    cx_test_register(suite, test_iterator_iterate);
    cx_test_register(suite, test_iterator_iterate_pointers);

    return suite;
}

mercurial