tests/test_list.cpp

Tue, 09 Jan 2024 00:09:11 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 09 Jan 2024 00:09:11 +0100
changeset 799
a2a757d225b4
parent 798
7644da6e2d35
child 800
1274e46b3013
permissions
-rw-r--r--

fix some broken cleanup

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2021 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/linked_list.h"
#include "cx/array_list.h"
#include "cx/utils.h"
#include "cx/compare.h"
#include "util_allocator.h"

#include <gtest/gtest.h>
#include <array>
#include <vector>
#include <unordered_set>
#include <algorithm>


class HighLevelTest : public ::testing::Test {
    mutable std::unordered_set<CxList *> lists;
protected:
    CxTestingAllocator testingAllocator;

    void TearDown() override {
        for (auto &&l: lists) cxListDestroy(l);
        CX_TEST_ASSERT(testingAllocator.verify());
    }

    static constexpr size_t testdata_len = 250;
    int_test_data<testdata_len> testdata;

    auto autofree(CxList *list) const -> CxList * {
        if (list != NULL) lists.insert(list);
        return list;
    }

    auto linkedListFromTestData() const -> CxList * {
        auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
        cxListAddArray(list, testdata.data.data(), testdata_len);
        return list;
    }

    auto pointerLinkedListFromTestData() const -> CxList * {
        auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS));
        // note: cannot use cxListAddArray() because we don't have a list of pointers
        cx_for_n(i, testdata_len) cxListAdd(list, &testdata.data[i]);
        return list;
    }

    auto arrayListFromTestData() const -> CxList * {
        auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), testdata_len));
        cxListAddArray(list, testdata.data.data(), testdata_len);
        return list;
    }

    auto pointerArrayListFromTestData() const -> CxList * {
        auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS, 256));
        // note: cannot use cxListAddArray() because we don't have a list of pointers
        cx_for_n(i, testdata_len) cxListAdd(list, &testdata.data[i]);
        return list;
    }

    void verifyAdd(
            CxList *list,
            bool as_pointer
    ) {
        auto len = testdata_len;
        cx_for_n (i, len) EXPECT_EQ(cxListAdd(list, &testdata.data[i]), 0);
        CX_TEST_ASSERT(cxListSize(list) == len);
        cx_for_n (i, len) EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]);
        cx_for_n (i, len) ++testdata.data[i];
        if (as_pointer) {
            cx_for_n (i, len) EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]);
        } else {
            cx_for_n (i, len) EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i] - 1);
        }
    }

    static void verifyInsert(CxList *list) {
        int a = 5, b = 47, c = 13, d = 42;

        EXPECT_NE(cxListInsert(list, 1, &a), 0);
        EXPECT_EQ(cxListSize(list), 0);
        EXPECT_EQ(cxListInsert(list, 0, &a), 0);
        EXPECT_EQ(cxListSize(list), 1);
        EXPECT_EQ(cxListInsert(list, 0, &b), 0);
        EXPECT_EQ(cxListSize(list), 2);
        EXPECT_EQ(cxListInsert(list, 1, &c), 0);
        EXPECT_EQ(cxListSize(list), 3);
        EXPECT_EQ(cxListInsert(list, 3, &d), 0);

        CX_TEST_ASSERT(cxListSize(list) == 4);

        EXPECT_EQ(*(int *) cxListAt(list, 0), 47);
        EXPECT_EQ(*(int *) cxListAt(list, 1), 13);
        EXPECT_EQ(*(int *) cxListAt(list, 2), 5);
        EXPECT_EQ(*(int *) cxListAt(list, 3), 42);
    }

    static void verifyInsertArray(
            CxList *list,
            bool pointers = false
    ) {
        int a[5] = {5, 47, 11, 13, 42};
        int b[5] = {9, 18, 72, 50, 7};
        int *aptr[5];
        int *bptr[5];
        cx_for_n(i, 5) {
            aptr[i] = &a[i];
            bptr[i] = &b[i];
        }

        size_t inserted;

        if (pointers) {
            inserted = cxListInsertArray(list, 0, aptr, 5);
        } else {
            inserted = cxListInsertArray(list, 0, a, 5);
        }
        CX_TEST_ASSERT(inserted == 5);
        EXPECT_EQ(*(int *) cxListAt(list, 0), 5);
        EXPECT_EQ(*(int *) cxListAt(list, 1), 47);
        EXPECT_EQ(*(int *) cxListAt(list, 2), 11);
        EXPECT_EQ(*(int *) cxListAt(list, 3), 13);
        EXPECT_EQ(*(int *) cxListAt(list, 4), 42);
        if (pointers) {
            inserted = cxListInsertArray(list, 3, bptr, 5);
        } else {
            inserted = cxListInsertArray(list, 3, b, 5);
        }
        CX_TEST_ASSERT(inserted == 5);
        EXPECT_EQ(*(int *) cxListAt(list, 0), 5);
        EXPECT_EQ(*(int *) cxListAt(list, 1), 47);
        EXPECT_EQ(*(int *) cxListAt(list, 2), 11);
        EXPECT_EQ(*(int *) cxListAt(list, 3), 9);
        EXPECT_EQ(*(int *) cxListAt(list, 4), 18);
        EXPECT_EQ(*(int *) cxListAt(list, 5), 72);
        EXPECT_EQ(*(int *) cxListAt(list, 6), 50);
        EXPECT_EQ(*(int *) cxListAt(list, 7), 7);
        EXPECT_EQ(*(int *) cxListAt(list, 8), 13);
        EXPECT_EQ(*(int *) cxListAt(list, 9), 42);
    }

    void verifyRemove(CxList *list) const {
        EXPECT_EQ(cxListRemove(list, 2), 0);
        EXPECT_EQ(cxListRemove(list, 4), 0);
        EXPECT_EQ(cxListSize(list), testdata_len - 2);
        EXPECT_EQ(*(int *) cxListAt(list, 0), testdata.data[0]);
        EXPECT_EQ(*(int *) cxListAt(list, 1), testdata.data[1]);
        EXPECT_EQ(*(int *) cxListAt(list, 2), testdata.data[3]);
        EXPECT_EQ(*(int *) cxListAt(list, 3), testdata.data[4]);
        EXPECT_EQ(*(int *) cxListAt(list, 4), testdata.data[6]);

        EXPECT_EQ(cxListRemove(list, 0), 0);
        EXPECT_EQ(cxListSize(list), testdata_len - 3);
        EXPECT_EQ(*(int *) cxListAt(list, 0), testdata.data[1]);
        EXPECT_EQ(*(int *) cxListAt(list, 1), testdata.data[3]);

        EXPECT_NE(cxListRemove(list, testdata_len), 0);
    }

    void verifyFindRemove(CxList *list) const {
        size_t exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp)
        int val = testdata.data[exp];
        // randomly picked number could occur earlier in list - find first position
        cx_for_n (i, exp) {
            if (testdata.data[i] == val) {
                exp = i;
                break;
            }
        }
        EXPECT_EQ(cxListSize(list), testdata_len);
        EXPECT_EQ(cxListFind(list, &val), exp);
        EXPECT_EQ(cxListFindRemove(list, &val), exp);
        EXPECT_EQ(cxListSize(list), testdata_len - 1);
        EXPECT_NE(cxListFind(list, &val), exp);

        int notinlist = -1;
        EXPECT_LT(cxListFindRemove(list, &notinlist), 0);
        EXPECT_EQ(cxListSize(list), testdata_len - 1);
    }

    static void verifyClear(CxList *list) {
        cxListClear(list);
        EXPECT_EQ(0, cxListSize(list));
    }

    static unsigned destr_test_ctr;
    static int destr_last_value;

    static void simple_destr_test_fun(void *data) {
        auto ptr = (int *) data;
        destr_last_value = *ptr;
        *ptr = destr_last_value + 1;
        destr_test_ctr++;
    }

    static void advanced_destr_test_fun(
            [[maybe_unused]] void *u,
            void *data
    ) {
        simple_destr_test_fun(data);
    }

    void verifyAnyDestructor(CxList *list) {
        int off = cxListIsStoringPointers(list) ? 1 : 0;

        cxListRemove(list, 15);
        EXPECT_EQ(1, destr_test_ctr);
        EXPECT_EQ(testdata.data[15], destr_last_value + off);
        EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list));
        cxListRemove(list, 47);
        EXPECT_EQ(2, destr_test_ctr);
        EXPECT_EQ(testdata.data[48], destr_last_value + off);
        EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list));

        auto iter = cxListMutIteratorAt(list, 7);
        cxIteratorNext(iter);
        EXPECT_EQ(2, destr_test_ctr);
        EXPECT_EQ(testdata.data[48], destr_last_value + off);
        EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list));
        cxIteratorFlagRemoval(iter);
        cxIteratorNext(iter);
        EXPECT_EQ(3, destr_test_ctr);
        EXPECT_EQ(testdata.data[8], destr_last_value + off);
        EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list));

        iter = cxListMutBackwardsIteratorAt(list, 5);
        cxIteratorNext(iter);
        EXPECT_EQ(3, destr_test_ctr);
        EXPECT_EQ(testdata.data[8], destr_last_value + off);
        EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list));
        cxIteratorFlagRemoval(iter);
        cxIteratorNext(iter);
        EXPECT_EQ(4, destr_test_ctr);
        EXPECT_EQ(testdata.data[4], destr_last_value + off);
        EXPECT_EQ(testdata_len - destr_test_ctr, cxListSize(list));

        cxListClear(list);
        EXPECT_EQ(testdata_len, destr_test_ctr);
        EXPECT_EQ(testdata.data[testdata_len - 1], destr_last_value + off);
    }

    void verifySimpleDestructor(CxList *list) {
        destr_test_ctr = 0;
        list->simple_destructor = simple_destr_test_fun;
        verifyAnyDestructor(list);
    }

    void verifyAdvancedDestructor(CxList *list) {
        destr_test_ctr = 0;
        list->advanced_destructor = advanced_destr_test_fun;
        verifyAnyDestructor(list);
    }

    static void verifySwap(CxList *list) {
        CX_TEST_ASSERT(cxListSize(list) == 0);

        int original[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
        int swapped[16] = {8, 4, 14, 3, 1, 5, 9, 12, 0, 6, 11, 10, 7, 15, 2, 13};

        // we have to add the items one by one, because it could be a pointer list
        cx_for_n(i, 16) {
            cxListAdd(list, &original[i]);
        }

        int result;

        // execute the test two times with different item sizes
        result = cxListSwap(list, 1, 4);
        EXPECT_EQ(0, result);
        result = cxListSwap(list, 2, 14);
        EXPECT_EQ(0, result);
        result = cxListSwap(list, 9, 6);
        EXPECT_EQ(0, result);
        result = cxListSwap(list, 3, 3);
        EXPECT_EQ(0, result);
        result = cxListSwap(list, 10, 11);
        EXPECT_EQ(0, result);
        result = cxListSwap(list, 8, 0);
        EXPECT_EQ(0, result);
        result = cxListSwap(list, 7, 12);
        EXPECT_EQ(0, result);
        result = cxListSwap(list, 13, 15);
        EXPECT_EQ(0, result);

        result = cxListSwap(list, 5, 16);
        CX_TEST_ASSERT(0 != result);
        result = cxListSwap(list, 16, 6);
        CX_TEST_ASSERT(0 != result);
        result = cxListSwap(list, 16, 17);
        CX_TEST_ASSERT(0 != result);

        auto iter = cxListIterator(list);
        cx_foreach(int*, e, iter) {
            EXPECT_EQ(*e, swapped[iter.index]);
        }
        iter = cxListBackwardsIterator(list);
        cx_foreach(int*, e, iter) {
            EXPECT_EQ(*e, swapped[iter.index]);
        }
    }

    void verifyAt(CxList *list) const {
        auto len = testdata_len;
        EXPECT_EQ(cxListSize(list), len);
        cx_for_n (i, len) {
            EXPECT_EQ(*(int *) cxListAt(list, i), testdata.data[i]);
        }
        EXPECT_EQ(cxListAt(list, cxListSize(list)), NULL);
    }

    void verifyFind(CxList *list) const {
        cx_for_n (attempt, 25) {
            size_t exp = rand() % testdata_len; // NOLINT(cert-msc50-cpp)
            int val = testdata.data[exp];
            // randomly picked number could occur earlier in list - find first position
            cx_for_n (i, exp) {
                if (testdata.data[i] == val) {
                    exp = i;
                    break;
                }
            }
            EXPECT_EQ(cxListFind(list, &val), exp);
        }

        int notinlist = -1;
        EXPECT_LT(cxListFind(list, &notinlist), 0);
    }

    void verifySort(CxList *list) const {
        std::array<int, testdata_len> expected{};
        std::partial_sort_copy(testdata.data.begin(), testdata.data.end(), expected.begin(), expected.end());
        cxListSort(list);
        cx_for_n (i, testdata_len) CX_TEST_ASSERT(*(int *) cxListAt(list, i) == expected[i]);
    }

    void verifyIterator(CxList *list) const {
        auto iter = cxListIterator(list);
        size_t i = 0;
        cx_foreach(int*, x, iter) {
            CX_TEST_ASSERT(i == iter.index);
            EXPECT_EQ(*x, testdata.data[iter.index]);
            i++;
        }
        CX_TEST_ASSERT(i == cxListSize(list));
        iter = cxListBackwardsIterator(list);
        cx_foreach(int*, x, iter) {
            CX_TEST_ASSERT(i - 1 == iter.index);
            EXPECT_EQ(*x, testdata.data[iter.index]);
            i--;
        }
        CX_TEST_ASSERT(i == 0);
        auto len = testdata_len;
        i = len / 2;
        auto mut_iter = cxListMutIteratorAt(list, i);
        size_t j = 0;
        cx_foreach(int*, x, mut_iter) {
            CX_TEST_ASSERT(mut_iter.index == len / 2 + j / 2);
            CX_TEST_ASSERT(*x == testdata.data[i]);
            if (i % 2 == 1) cxIteratorFlagRemoval(mut_iter);
            i++;
            j++;
        }
        CX_TEST_ASSERT(i == len);
        i = len / 2;
        j = 0;
        mut_iter = cxListMutBackwardsIteratorAt(list, i - 1);
        cx_foreach(int*, x, mut_iter) {
            CX_TEST_ASSERT(mut_iter.index == len / 2 - 1 - j);
            CX_TEST_ASSERT(*x == testdata.data[i - 1]);
            if (i % 2 == 0) cxIteratorFlagRemoval(mut_iter);
            i--;
            j++;
        }
        CX_TEST_ASSERT(i == 0);
        CX_TEST_ASSERT(cxListSize(list) == len / 2);
        cx_for_n(j, len / 2) ASSERT_EQ(*(int *) cxListAt(list, j), testdata.data[j * 2]);
    }

    static void verifyInsertViaIterator(CxList *list) {
        int newdata[] = {10, 20, 30, 40, 50};

        auto iter = cxListMutIteratorAt(list, 2);
        CX_TEST_ASSERT(cxIteratorValid(iter));
        EXPECT_EQ(iter.index, 2);
        EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 2);
        cxListInsertAfter(&iter, &newdata[0]);
        CX_TEST_ASSERT(cxIteratorValid(iter));
        EXPECT_EQ(iter.index, 2);
        EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 2);
        cxListInsertBefore(&iter, &newdata[1]);
        CX_TEST_ASSERT(cxIteratorValid(iter));
        EXPECT_EQ(iter.index, 3);
        EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 2);

        iter = cxListMutIterator(list);
        cxListInsertBefore(&iter, &newdata[2]);
        CX_TEST_ASSERT(cxIteratorValid(iter));
        EXPECT_EQ(iter.index, 1);
        EXPECT_EQ(*(int *) cxIteratorCurrent(iter), 0);
        iter = cxListMutIteratorAt(list, cxListSize(list));
        cxListInsertBefore(&iter, &newdata[3]);
        CX_TEST_ASSERT(!cxIteratorValid(iter));
        EXPECT_EQ(iter.index, 9);
        iter = cxListMutIteratorAt(list, cxListSize(list));
        cxListInsertAfter(&iter, &newdata[4]);
        CX_TEST_ASSERT(!cxIteratorValid(iter));
        EXPECT_EQ(iter.index, 10);

        int expdata[] = {30, 0, 1, 20, 2, 10, 3, 4, 40, 50};
        cx_for_n (j, 10) EXPECT_EQ(*(int *) cxListAt(list, j), expdata[j]);
    }

    void verifyReverse(CxList *list) const {
        cxListReverse(list);
        cx_for_n(i, testdata_len) {
            ASSERT_EQ(*(int *) cxListAt(list, i), testdata.data[testdata_len - 1 - i]);
        }
    }

    static void verifyCompare(
            CxList *left,
            CxList *right
    ) {
        EXPECT_EQ(cxListCompare(left, right), 0);
        int x = 42;
        cxListAdd(left, &x);
        ASSERT_GT(cxListSize(left), cxListSize(right));
        EXPECT_GT(cxListCompare(left, right), 0);
        EXPECT_LT(cxListCompare(right, left), 0);
        cxListAdd(right, &x);
        CX_TEST_ASSERT(cxListSize(left) == cxListSize(right));
        EXPECT_EQ(cxListCompare(left, right), 0);
        int a = 5, b = 10;
        cxListInsert(left, 15, &a);
        cxListInsert(right, 15, &b);
        CX_TEST_ASSERT(cxListSize(left) == cxListSize(right));
        EXPECT_LT(cxListCompare(left, right), 0);
        EXPECT_GT(cxListCompare(right, left), 0);
        *(int *) cxListAt(left, 15) = 10;
        EXPECT_EQ(cxListCompare(left, right), 0);
    }
};

unsigned HighLevelTest::destr_test_ctr = 0;
int HighLevelTest::destr_last_value = 0;

class LinkedList : public HighLevelTest {
};

class PointerLinkedList : public HighLevelTest {
};

class ArrayList : public HighLevelTest {
};

class PointerArrayList : public HighLevelTest {
};

TEST_F(PointerLinkedList, cxListStorePointers) {
    auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, 47));
    CX_TEST_ASSERT(!cxListIsStoringPointers(list));
    cxListStorePointers(list);
    EXPECT_EQ(list->item_size, sizeof(void *));
    CX_TEST_ASSERT(list->cl != NULL);
    CX_TEST_ASSERT(list->climpl != NULL);
    CX_TEST_ASSERT(cxListIsStoringPointers(list));
    cxListStoreObjects(list);
    CX_TEST_ASSERT(list->cl != NULL);
    EXPECT_EQ(list->climpl, NULL);
    CX_TEST_ASSERT(!cxListIsStoringPointers(list));
}

TEST_F(LinkedList, cxLinkedListCreate) {
    CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
    ASSERT_NE(list, NULL);
    EXPECT_EQ(list->item_size, sizeof(int));
    EXPECT_EQ(list->simple_destructor, NULL);
    EXPECT_EQ(list->advanced_destructor, NULL);
    EXPECT_EQ(list->destructor_data, NULL);
    EXPECT_EQ(cxListSize(list), 0);
    EXPECT_EQ(list->allocator, &testingAllocator);
    EXPECT_EQ(list->cmpfunc, cx_cmp_int);
    CX_TEST_ASSERT(!cxListIsStoringPointers(list));
}

TEST_F(LinkedList, cxLinkedListCreateSimple) {
    CxList *list = autofree(cxLinkedListCreateSimple(sizeof(int)));
    ASSERT_NE(list, NULL);
    EXPECT_EQ(list->item_size, sizeof(int));
    EXPECT_EQ(list->cmpfunc, NULL);
    EXPECT_EQ(list->allocator, cxDefaultAllocator);
    EXPECT_EQ(list->simple_destructor, NULL);
    EXPECT_EQ(list->advanced_destructor, NULL);
    EXPECT_EQ(list->destructor_data, NULL);
    EXPECT_EQ(cxListSize(list), 0);
    CX_TEST_ASSERT(!cxListIsStoringPointers(list));
}

TEST_F(PointerLinkedList, cxLinkedListCreateSimpleForPointers) {
    CxList *list = autofree(cxLinkedListCreateSimple(CX_STORE_POINTERS));
    ASSERT_NE(list, NULL);
    EXPECT_EQ(list->item_size, sizeof(void *));
    EXPECT_EQ(list->cmpfunc, cx_cmp_ptr);
    EXPECT_EQ(list->allocator, cxDefaultAllocator);
    EXPECT_EQ(list->simple_destructor, NULL);
    EXPECT_EQ(list->advanced_destructor, NULL);
    EXPECT_EQ(list->destructor_data, NULL);
    EXPECT_EQ(cxListSize(list), 0);
    CX_TEST_ASSERT(cxListIsStoringPointers(list));
}

TEST_F(ArrayList, cxArrayListCreate) {
    CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8));
    ASSERT_NE(list, NULL);
    EXPECT_EQ(list->item_size, sizeof(int));
    EXPECT_EQ(list->simple_destructor, NULL);
    EXPECT_EQ(list->advanced_destructor, NULL);
    EXPECT_EQ(list->destructor_data, NULL);
    EXPECT_EQ(cxListSize(list), 0);
    EXPECT_EQ(list->allocator, &testingAllocator);
    EXPECT_EQ(list->cmpfunc, cx_cmp_int);
    CX_TEST_ASSERT(!cxListIsStoringPointers(list));
}

TEST_F(ArrayList, cxArrayListCreateSimple) {
    CxList *list = autofree(cxArrayListCreateSimple(sizeof(int), 8));
    ASSERT_NE(list, NULL);
    EXPECT_EQ(list->cmpfunc, NULL);
    EXPECT_EQ(list->allocator, cxDefaultAllocator);
    EXPECT_EQ(list->item_size, sizeof(int));
    EXPECT_EQ(list->simple_destructor, NULL);
    EXPECT_EQ(list->advanced_destructor, NULL);
    EXPECT_EQ(list->destructor_data, NULL);
    EXPECT_EQ(cxListSize(list), 0);
    CX_TEST_ASSERT(!cxListIsStoringPointers(list));
}

TEST_F(PointerArrayList, cxArrayListCreateSimpleForPointers) {
    CxList *list = autofree(cxArrayListCreateSimple(CX_STORE_POINTERS, 8));
    ASSERT_NE(list, NULL);
    EXPECT_EQ(list->cmpfunc, cx_cmp_ptr);
    EXPECT_EQ(list->allocator, cxDefaultAllocator);
    EXPECT_EQ(list->item_size, sizeof(void *));
    CX_TEST_ASSERT(cxListIsStoringPointers(list));
}

TEST_F(LinkedList, cxListAdd) {
    auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
    verifyAdd(list, false);
}

TEST_F(PointerLinkedList, cxListAdd) {
    auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS));
    verifyAdd(list, true);
}

TEST_F(ArrayList, cxListAdd) {
    auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8));
    verifyAdd(list, false);
}

TEST_F(PointerArrayList, cxListAdd) {
    auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS, 8));
    verifyAdd(list, true);
}

TEST_F(LinkedList, cxListInsert) {
    verifyInsert(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
}

TEST_F(PointerLinkedList, cxListInsert) {
    verifyInsert(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS)));
}

TEST_F(ArrayList, cxListInsert) {
    verifyInsert(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 2)));
}

TEST_F(PointerArrayList, cxListInsert) {
    verifyInsert(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS, 2)));
}

TEST_F(LinkedList, cxListInsertArray) {
    verifyInsertArray(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
}

TEST_F(PointerLinkedList, cxListInsertArray) {
    verifyInsertArray(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS)), true);
}

TEST_F(ArrayList, cxListInsertArray) {
    verifyInsertArray(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4)));
}

TEST_F(PointerArrayList, cxListInsertArray) {
    verifyInsertArray(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS, 4)), true);
}

TEST_F(LinkedList, cxListRemove) {
    verifyRemove(linkedListFromTestData());
}

TEST_F(PointerLinkedList, cxListRemove) {
    verifyRemove(pointerLinkedListFromTestData());
}

TEST_F(ArrayList, cxListRemove) {
    verifyRemove(arrayListFromTestData());
}

TEST_F(PointerArrayList, cxListRemove) {
    verifyRemove(pointerArrayListFromTestData());
}

TEST_F(LinkedList, cxListFindRemove) {
    verifyFindRemove(linkedListFromTestData());
}

TEST_F(PointerLinkedList, cxListFindRemove) {
    verifyFindRemove(pointerLinkedListFromTestData());
}

TEST_F(ArrayList, cxListFindRemove) {
    verifyFindRemove(arrayListFromTestData());
}

TEST_F(PointerArrayList, cxListFindRemove) {
    verifyFindRemove(pointerArrayListFromTestData());
}

TEST_F(LinkedList, cxListClear) {
    verifyClear(linkedListFromTestData());
}

TEST_F(PointerLinkedList, cxListClear) {
    verifyClear(pointerLinkedListFromTestData());
}

TEST_F(ArrayList, cxListClear) {
    verifyClear(arrayListFromTestData());
}

TEST_F(PointerArrayList, cxListClear) {
    verifyClear(pointerArrayListFromTestData());
}

TEST_F(LinkedList, cxListSwap) {
    verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
}

TEST_F(PointerLinkedList, cxListSwap) {
    verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS)));
}

TEST_F(ArrayList, cxListSwap) {
    verifySwap(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 16)));
}

TEST_F(PointerArrayList, cxListSwap) {
    verifySwap(autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS, 16)));
}

TEST_F(LinkedList, cxListSwapNoSBO) {
    CX_DISABLE_LINKED_LIST_SWAP_SBO = true;
    verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int))));
    CX_DISABLE_LINKED_LIST_SWAP_SBO = false;
}

TEST_F(PointerLinkedList, cxListSwapNoSBO) {
    CX_DISABLE_LINKED_LIST_SWAP_SBO = true;
    verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS)));
    CX_DISABLE_LINKED_LIST_SWAP_SBO = false;
}

TEST_F(LinkedList, cxListAt) {
    verifyAt(linkedListFromTestData());
}

TEST_F(PointerLinkedList, cxListAt) {
    verifyAt(pointerLinkedListFromTestData());
}

TEST_F(ArrayList, cxListAt) {
    verifyAt(arrayListFromTestData());
}

TEST_F(PointerArrayList, cxListAt) {
    verifyAt(pointerArrayListFromTestData());
}

TEST_F(LinkedList, cxListFind) {
    verifyFind(linkedListFromTestData());
}

TEST_F(PointerLinkedList, cxListFind) {
    verifyFind(pointerLinkedListFromTestData());
}

TEST_F(ArrayList, cxListFind) {
    verifyFind(arrayListFromTestData());
}

TEST_F(PointerArrayList, cxListFind) {
    verifyFind(pointerArrayListFromTestData());
}

TEST_F(LinkedList, cxListSort) {
    verifySort(linkedListFromTestData());
}

TEST_F(PointerLinkedList, cxListSort) {
    verifySort(pointerLinkedListFromTestData());
}

TEST_F(ArrayList, cxListSort) {
    verifySort(arrayListFromTestData());
}

TEST_F(PointerArrayList, cxListSort) {
    verifySort(pointerArrayListFromTestData());
}

TEST_F(LinkedList, Iterator) {
    verifyIterator(linkedListFromTestData());
}

TEST_F(PointerLinkedList, Iterator) {
    verifyIterator(pointerLinkedListFromTestData());
}

TEST_F(ArrayList, Iterator) {
    verifyIterator(arrayListFromTestData());
}

TEST_F(PointerArrayList, Iterator) {
    verifyIterator(pointerArrayListFromTestData());
}

TEST_F(LinkedList, InsertViaIterator) {
    int fivenums[] = {0, 1, 2, 3, 4, 5};
    CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
    cxListAddArray(list, fivenums, 5);
    verifyInsertViaIterator(list);
}

TEST_F(PointerLinkedList, InsertViaIterator) {
    int fivenums[] = {0, 1, 2, 3, 4, 5};
    auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS));
    // note: cannot use cxListAddArray() because we don't have a list of pointers
    cx_for_n(i, 5) cxListAdd(list, &fivenums[i]);
    verifyInsertViaIterator(list);
}

TEST_F(ArrayList, InsertViaIterator) {
    int fivenums[] = {0, 1, 2, 3, 4, 5};
    auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4));
    cxListAddArray(list, fivenums, 5);
    verifyInsertViaIterator(list);
}

TEST_F(PointerArrayList, InsertViaIterator) {
    int fivenums[] = {0, 1, 2, 3, 4, 5};
    auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS, 4));
    // note: cannot use cxListAddArray() because we don't have a list of pointers
    cx_for_n(i, 5) cxListAdd(list, &fivenums[i]);
    verifyInsertViaIterator(list);
}

TEST_F(LinkedList, cxListReverse) {
    verifyReverse(linkedListFromTestData());
}

TEST_F(PointerLinkedList, cxListReverse) {
    verifyReverse(pointerLinkedListFromTestData());
}

TEST_F(ArrayList, cxListReverse) {
    verifyReverse(arrayListFromTestData());
}

TEST_F(PointerArrayList, cxListReverse) {
    verifyReverse(pointerArrayListFromTestData());
}

TEST_F(LinkedList, cxListCompare) {
    auto left = linkedListFromTestData();
    auto right = linkedListFromTestData();
    verifyCompare(left, right);
}

TEST_F(LinkedList, cxListCompareWithPtrList) {
    auto left = linkedListFromTestData();
    auto right = pointerLinkedListFromTestData();
    verifyCompare(left, right);
}

TEST_F(LinkedList, cxListCompareWithArrayList) {
    auto left = linkedListFromTestData();
    auto right = arrayListFromTestData();
    verifyCompare(left, right);
}

TEST_F(LinkedList, cxListCompareWithPtrArrayList) {
    auto left = linkedListFromTestData();
    auto right = pointerArrayListFromTestData();
    verifyCompare(left, right);
}

TEST_F(PointerLinkedList, cxListCompare) {
    auto left = pointerLinkedListFromTestData();
    auto right = pointerLinkedListFromTestData();
    verifyCompare(left, right);
}

TEST_F(PointerLinkedList, cxListCompareWithNormalList) {
    auto left = pointerLinkedListFromTestData();
    auto right = linkedListFromTestData();
    verifyCompare(left, right);
}

TEST_F(PointerLinkedList, cxListCompareWithArrayList) {
    auto left = pointerLinkedListFromTestData();
    auto right = arrayListFromTestData();
    verifyCompare(left, right);
}

TEST_F(PointerLinkedList, cxListCompareWithPtrArrayList) {
    auto left = pointerLinkedListFromTestData();
    auto right = pointerArrayListFromTestData();
    verifyCompare(left, right);
}

TEST_F(ArrayList, cxListCompare) {
    auto left = arrayListFromTestData();
    auto right = arrayListFromTestData();
    verifyCompare(left, right);
}

TEST_F(ArrayList, cxListCompareWithPtrList) {
    auto left = arrayListFromTestData();
    auto right = pointerLinkedListFromTestData();
    verifyCompare(left, right);
}

TEST_F(ArrayList, cxListCompareWithNormalList) {
    auto left = arrayListFromTestData();
    auto right = linkedListFromTestData();
    verifyCompare(left, right);
}

TEST_F(ArrayList, cxListCompareWithPtrArrayList) {
    auto left = arrayListFromTestData();
    auto right = pointerArrayListFromTestData();
    verifyCompare(left, right);
}

TEST_F(PointerArrayList, cxListCompare) {
    auto left = pointerArrayListFromTestData();
    auto right = pointerArrayListFromTestData();
    verifyCompare(left, right);
}

TEST_F(PointerArrayList, cxListCompareWithPtrList) {
    auto left = pointerArrayListFromTestData();
    auto right = pointerLinkedListFromTestData();
    verifyCompare(left, right);
}

TEST_F(PointerArrayList, cxListCompareWithNormalList) {
    auto left = pointerArrayListFromTestData();
    auto right = linkedListFromTestData();
    verifyCompare(left, right);
}

TEST_F(PointerArrayList, cxListCompareWithNormalArrayList) {
    auto left = pointerArrayListFromTestData();
    auto right = arrayListFromTestData();
    verifyCompare(left, right);
}

TEST_F(LinkedList, SimpleDestructor) {
    verifySimpleDestructor(linkedListFromTestData());
}

TEST_F(PointerLinkedList, SimpleDestructor) {
    verifySimpleDestructor(pointerLinkedListFromTestData());
}

TEST_F(ArrayList, SimpleDestructor) {
    verifySimpleDestructor(arrayListFromTestData());
}

TEST_F(PointerArrayList, SimpleDestructor) {
    verifySimpleDestructor(pointerArrayListFromTestData());
}

TEST_F(LinkedList, AdvancedDestructor) {
    verifyAdvancedDestructor(linkedListFromTestData());
}

TEST_F(PointerLinkedList, AdvancedDestructor) {
    verifyAdvancedDestructor(pointerLinkedListFromTestData());
}

TEST_F(ArrayList, AdvancedDestructor) {
    verifyAdvancedDestructor(arrayListFromTestData());
}

TEST_F(PointerArrayList, AdvancedDestructor) {
    verifyAdvancedDestructor(pointerArrayListFromTestData());
}

TEST_F(PointerLinkedList, DestroyNoDestructor) {
    void *item = cxMalloc(&testingAllocator, sizeof(int));
    auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
    cxListAdd(list, item);
    CX_TEST_ASSERT(!testingAllocator.verify());
    cxListDestroy(list);
    CX_TEST_ASSERT(!testingAllocator.verify());
    cxFree(&testingAllocator, item);
    CX_TEST_ASSERT(testingAllocator.verify());
}

TEST_F(PointerLinkedList, DestroySimpleDestructor) {
    int item = 0;
    auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
    list->simple_destructor = [](void *elem) { *(int *) elem = 42; };
    cxListAdd(list, &item);
    cxListDestroy(list);
    EXPECT_EQ(item, 42);
}

TEST_F(PointerLinkedList, DestroyAdvancedDestructor) {
    void *item = cxMalloc(&testingAllocator, sizeof(int));
    auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
    list->destructor_data = &testingAllocator;
    list->advanced_destructor = (cx_destructor_func2) cxFree;
    cxListAdd(list, item);
    CX_TEST_ASSERT(!testingAllocator.verify());
    cxListDestroy(list);
    CX_TEST_ASSERT(testingAllocator.verify());
}

TEST_F(PointerArrayList, DestroyNoDestructor) {
    void *item = cxMalloc(&testingAllocator, sizeof(int));
    auto list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4);
    cxListAdd(list, item);
    CX_TEST_ASSERT(!testingAllocator.verify());
    cxListDestroy(list);
    CX_TEST_ASSERT(!testingAllocator.verify());
    cxFree(&testingAllocator, item);
    CX_TEST_ASSERT(testingAllocator.verify());
}

TEST_F(PointerArrayList, DestroySimpleDestructor) {
    int item = 0;
    auto list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4);
    list->simple_destructor = [](void *elem) { *(int *) elem = 42; };
    cxListAdd(list, &item);
    cxListDestroy(list);
    EXPECT_EQ(item, 42);
}

TEST_F(PointerArrayList, DestroyAdvancedDestructor) {
    void *item = cxMalloc(&testingAllocator, sizeof(int));
    auto list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4);
    list->destructor_data = &testingAllocator;
    list->advanced_destructor = (cx_destructor_func2) cxFree;
    cxListAdd(list, item);
    CX_TEST_ASSERT(!testingAllocator.verify());
    cxListDestroy(list);
    CX_TEST_ASSERT(testingAllocator.verify());
}

TEST(EmptyList, Size) {
    auto list = cxEmptyList;

    EXPECT_EQ(list->size, 0);
    EXPECT_EQ(cxListSize(list), 0);
}

TEST(EmptyList, Iterator) {
    auto list = cxEmptyList;

    auto it1 = cxListIterator(list);
    auto it2 = cxListBackwardsIterator(list);
    auto it3 = cxListMutIterator(list);
    auto it4 = cxListMutBackwardsIterator(list);

    CX_TEST_ASSERT(!cxIteratorValid(it1));
    CX_TEST_ASSERT(!cxIteratorValid(it2));
    CX_TEST_ASSERT(!cxIteratorValid(it3));
    CX_TEST_ASSERT(!cxIteratorValid(it4));

    int c = 0;
    cx_foreach(void*, data, it1) c++;
    cx_foreach(void*, data, it2) c++;
    cx_foreach(void*, data, it3) c++;
    cx_foreach(void*, data, it4) c++;
    EXPECT_EQ(c, 0);
}

TEST(EmptyList, NoOps) {
    auto list = cxEmptyList;

    ASSERT_NO_FATAL_FAILURE(cxListSort(list));
    ASSERT_NO_FATAL_FAILURE(cxListClear(list));
    ASSERT_NO_FATAL_FAILURE(cxListDestroy(list));
}

TEST(EmptyList, At) {
    auto list = cxEmptyList;

    EXPECT_EQ(cxListAt(list, 0), NULL);
    EXPECT_EQ(cxListAt(list, 1), NULL);
}

TEST(EmptyList, Find) {
    auto list = cxEmptyList;

    int x = 42, y = 1337;

    EXPECT_LT(cxListFind(list, &x), 0);
    EXPECT_LT(cxListFind(list, &y), 0);
}

TEST(EmptyList, Compare) {
    auto empty = cxEmptyList;

    auto ll = cxLinkedListCreateSimple(sizeof(int));
    auto al = cxArrayListCreateSimple(sizeof(int), 8);

    int x = 5;

    EXPECT_EQ(cxListCompare(empty, cxEmptyList), 0);
    EXPECT_EQ(cxListCompare(ll, cxEmptyList), 0);
    EXPECT_EQ(cxListCompare(al, cxEmptyList), 0);
    EXPECT_EQ(cxListCompare(cxEmptyList, ll), 0);
    EXPECT_EQ(cxListCompare(cxEmptyList, al), 0);

    cxListAdd(ll, &x);
    cxListAdd(al, &x);

    EXPECT_GT(cxListCompare(ll, cxEmptyList), 0);
    EXPECT_GT(cxListCompare(al, cxEmptyList), 0);
    EXPECT_LT(cxListCompare(cxEmptyList, ll), 0);
    EXPECT_LT(cxListCompare(cxEmptyList, al), 0);

    cxListDestroy(ll);
    cxListDestroy(al);
}

mercurial