tests/test_list.cpp

Wed, 03 Jan 2024 22:17:40 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 03 Jan 2024 22:17:40 +0100
changeset 792
3ca984931e1d
parent 790
42877968260c
child 798
7644da6e2d35
permissions
-rw-r--r--

migrate more buffer tests - relates to #342

only read and write tests are remaining now

/*
 * 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>

struct node {
    node *next = NULL;
    node *prev = NULL;
    int data = 0;
};

const ptrdiff_t loc_prev = offsetof(struct node, prev);
const ptrdiff_t loc_next = offsetof(struct node, next);
const ptrdiff_t loc_data = offsetof(struct node, data);

struct node_test_data {
    node *begin = NULL;

    explicit node_test_data(node *begin) : begin(begin) {
        auto n = begin;
        while (n != NULL) {
            nodes.push_back(n);
            n = n->next;
        }
    }

    node_test_data(node_test_data &) = delete;

    node_test_data(node_test_data &&) = default;

    ~node_test_data() {
        for (auto &&n: nodes) delete n;
    }

private:
    std::vector<node *> nodes;
};

static node_test_data create_nodes_test_data(size_t len) {
    if (len == 0) return node_test_data{NULL};
    auto begin = new node;
    auto prev = begin;
    for (size_t i = 1; i < len; i++) {
        auto n = new node;
        cx_linked_list_link(prev, n, loc_prev, loc_next);
        prev = n;
    }
    return node_test_data{begin};
}

template<typename InputIter>
static node_test_data create_nodes_test_data(
        InputIter begin,
        InputIter end
) {
    if (begin == end) return node_test_data{NULL};
    node *first = new node;
    first->data = *begin;
    node *prev = first;
    begin++;
    for (; begin != end; begin++) {
        auto n = new node;
        n->data = *begin;
        cx_linked_list_link(prev, n, loc_prev, loc_next);
        prev = n;
    }
    return node_test_data{first};
}

static node_test_data create_nodes_test_data(std::initializer_list<int> data) {
    return create_nodes_test_data(data.begin(), data.end());
}

template<size_t N>
struct int_test_data {
    std::array<int, N> data;

    int_test_data() {
        cx_for_n (i, N) data[i] = ::rand(); // NOLINT(cert-msc50-cpp)
    }
};

TEST(LinkedList_LowLevel, link_unlink) {
    node a, b, c;

    cx_linked_list_link(&a, &b, loc_prev, loc_next);
    CX_TEST_ASSERT(a.prev == NULL);
    CX_TEST_ASSERT(a.next == &b);
    CX_TEST_ASSERT(b.prev == &a);
    CX_TEST_ASSERT(b.next == NULL);

    cx_linked_list_unlink(&a, &b, loc_prev, loc_next);
    CX_TEST_ASSERT(a.prev == NULL);
    CX_TEST_ASSERT(a.next == NULL);
    CX_TEST_ASSERT(b.prev == NULL);
    CX_TEST_ASSERT(b.next == NULL);

    cx_linked_list_link(&b, &c, loc_prev, loc_next);
    cx_linked_list_link(&a, &b, loc_prev, loc_next);
    cx_linked_list_unlink(&b, &c, loc_prev, loc_next);
    CX_TEST_ASSERT(a.prev == NULL);
    CX_TEST_ASSERT(a.next == &b);
    CX_TEST_ASSERT(b.prev == &a);
    CX_TEST_ASSERT(b.next == NULL);
    CX_TEST_ASSERT(c.prev == NULL);
    CX_TEST_ASSERT(c.next == NULL);
}

TEST(LinkedList_LowLevel, cx_linked_list_at) {
    node a, b, c, d;
    cx_linked_list_link(&a, &b, loc_prev, loc_next);
    cx_linked_list_link(&b, &c, loc_prev, loc_next);
    cx_linked_list_link(&c, &d, loc_prev, loc_next);

    EXPECT_EQ(cx_linked_list_at(&a, 0, loc_next, 0), &a);
    EXPECT_EQ(cx_linked_list_at(&a, 0, loc_next, 1), &b);
    EXPECT_EQ(cx_linked_list_at(&a, 0, loc_next, 2), &c);
    EXPECT_EQ(cx_linked_list_at(&a, 0, loc_next, 3), &d);
    EXPECT_EQ(cx_linked_list_at(&a, 0, loc_next, 4), NULL);

    EXPECT_EQ(cx_linked_list_at(&b, 1, loc_prev, 0), &a);
    EXPECT_EQ(cx_linked_list_at(&b, 1, loc_next, 1), &b);
    EXPECT_EQ(cx_linked_list_at(&b, 1, loc_next, 2), &c);
    EXPECT_EQ(cx_linked_list_at(&b, 1, loc_next, 3), &d);
    EXPECT_EQ(cx_linked_list_at(&b, 1, loc_next, 4), NULL);

    EXPECT_EQ(cx_linked_list_at(&d, 3, loc_prev, 0), &a);
    EXPECT_EQ(cx_linked_list_at(&d, 3, loc_prev, 1), &b);
}

TEST(LinkedList_LowLevel, cx_linked_list_find) {
    auto testdata = create_nodes_test_data({2, 4, 6, 8});
    auto list = testdata.begin;
    int s;

    s = 2;
    EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 0);
    s = 4;
    EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 1);
    s = 6;
    EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 2);
    s = 8;
    EXPECT_EQ(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 3);
    s = 10;
    EXPECT_LT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 0);
    s = -2;
    EXPECT_LT(cx_linked_list_find(list, loc_next, loc_data, cx_cmp_int, &s), 0);
}

TEST(LinkedList_LowLevel, cx_linked_list_compare) {
    auto ta = create_nodes_test_data({2, 4, 6, 8});
    auto tb = create_nodes_test_data({2, 4, 6});
    auto tc = create_nodes_test_data({2, 4, 6, 9});
    auto la = ta.begin, lb = tb.begin, lc = tc.begin;

    EXPECT_GT(cx_linked_list_compare(la, lb, loc_next, loc_data, cx_cmp_int), 0);
    EXPECT_LT(cx_linked_list_compare(lb, la, loc_next, loc_data, cx_cmp_int), 0);
    EXPECT_GT(cx_linked_list_compare(lc, la, loc_next, loc_data, cx_cmp_int), 0);
    EXPECT_LT(cx_linked_list_compare(la, lc, loc_next, loc_data, cx_cmp_int), 0);
    EXPECT_EQ(cx_linked_list_compare(la, la, loc_next, loc_data, cx_cmp_int), 0);
}

TEST(LinkedList_LowLevel, cx_linked_list_add) {
    // test with begin, end / prev, next
    {
        node nodes[4];
        void *begin = NULL, *end = NULL;

        cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[0]);
        CX_TEST_ASSERT(begin == &nodes[0]);
        CX_TEST_ASSERT(end == &nodes[0]);
        CX_TEST_ASSERT(nodes[0].prev == NULL);
        CX_TEST_ASSERT(nodes[0].next == NULL);

        cx_linked_list_add(&begin, &end, loc_prev, loc_next, &nodes[1]);
        CX_TEST_ASSERT(begin == &nodes[0]);
        CX_TEST_ASSERT(end == &nodes[1]);
        CX_TEST_ASSERT(nodes[0].next == &nodes[1]);
        CX_TEST_ASSERT(nodes[1].prev == &nodes[0]);
    }

    // test with begin only / prev, next
    {
        node nodes[4];
        void *begin = NULL;

        cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[0]);
        CX_TEST_ASSERT(begin == &nodes[0]);
        cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[1]);
        CX_TEST_ASSERT(begin == &nodes[0]);
        CX_TEST_ASSERT(nodes[0].next == &nodes[1]);
        CX_TEST_ASSERT(nodes[1].prev == &nodes[0]);

        cx_linked_list_add(&begin, NULL, loc_prev, loc_next, &nodes[2]);
        CX_TEST_ASSERT(nodes[1].next == &nodes[2]);
        CX_TEST_ASSERT(nodes[2].prev == &nodes[1]);
    }

    // test with end only / prev, next
    {
        node nodes[4];
        void *end = NULL;

        cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[0]);
        CX_TEST_ASSERT(end == &nodes[0]);
        cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[1]);
        CX_TEST_ASSERT(end == &nodes[1]);
        CX_TEST_ASSERT(nodes[0].next == &nodes[1]);
        CX_TEST_ASSERT(nodes[1].prev == &nodes[0]);

        cx_linked_list_add(NULL, &end, loc_prev, loc_next, &nodes[2]);
        CX_TEST_ASSERT(end == &nodes[2]);
        CX_TEST_ASSERT(nodes[1].next == &nodes[2]);
        CX_TEST_ASSERT(nodes[2].prev == &nodes[1]);
    }

    // test with begin, end / next
    {
        node nodes[4];
        void *begin = NULL, *end = NULL;

        cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[0]);
        CX_TEST_ASSERT(begin == &nodes[0]);
        CX_TEST_ASSERT(end == &nodes[0]);
        cx_linked_list_add(&begin, &end, -1, loc_next, &nodes[1]);
        CX_TEST_ASSERT(end == &nodes[1]);
        CX_TEST_ASSERT(nodes[0].next == &nodes[1]);
        CX_TEST_ASSERT(nodes[1].prev == NULL);
    }
}

TEST(LinkedList_LowLevel, cx_linked_list_prepend) {
    // test with begin, end / prev, next
    {
        node nodes[4];
        void *begin = NULL, *end = NULL;

        cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[0]);
        CX_TEST_ASSERT(begin == &nodes[0]);
        CX_TEST_ASSERT(end == &nodes[0]);
        CX_TEST_ASSERT(nodes[0].prev == NULL);
        CX_TEST_ASSERT(nodes[0].next == NULL);

        cx_linked_list_prepend(&begin, &end, loc_prev, loc_next, &nodes[1]);
        CX_TEST_ASSERT(begin == &nodes[1]);
        CX_TEST_ASSERT(end == &nodes[0]);
        CX_TEST_ASSERT(nodes[1].next == &nodes[0]);
        CX_TEST_ASSERT(nodes[0].prev == &nodes[1]);
    }

    // test with begin only / prev, next
    {
        node nodes[4];
        void *begin = NULL;

        cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[0]);
        CX_TEST_ASSERT(begin == &nodes[0]);
        cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[1]);
        CX_TEST_ASSERT(begin == &nodes[1]);
        CX_TEST_ASSERT(nodes[1].next == &nodes[0]);
        CX_TEST_ASSERT(nodes[0].prev == &nodes[1]);

        cx_linked_list_prepend(&begin, NULL, loc_prev, loc_next, &nodes[2]);
        CX_TEST_ASSERT(begin == &nodes[2]);
        CX_TEST_ASSERT(nodes[2].next == &nodes[1]);
        CX_TEST_ASSERT(nodes[1].prev == &nodes[2]);
    }

    // test with end only / prev, next
    {
        node nodes[4];
        void *end = NULL;

        cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[0]);
        CX_TEST_ASSERT(end == &nodes[0]);
        cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[1]);
        CX_TEST_ASSERT(end == &nodes[0]);
        CX_TEST_ASSERT(nodes[1].next == &nodes[0]);
        CX_TEST_ASSERT(nodes[0].prev == &nodes[1]);

        cx_linked_list_prepend(NULL, &end, loc_prev, loc_next, &nodes[2]);
        CX_TEST_ASSERT(end == &nodes[0]);
        CX_TEST_ASSERT(nodes[2].next == &nodes[1]);
        CX_TEST_ASSERT(nodes[1].prev == &nodes[2]);
    }

    // test with begin, end / next
    {
        node nodes[4];
        void *begin = NULL, *end = NULL;

        cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[0]);
        CX_TEST_ASSERT(begin == &nodes[0]);
        CX_TEST_ASSERT(end == &nodes[0]);
        cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[1]);
        cx_linked_list_prepend(&begin, &end, -1, loc_next, &nodes[2]);
        CX_TEST_ASSERT(begin == &nodes[2]);
        CX_TEST_ASSERT(end == &nodes[0]);
        CX_TEST_ASSERT(nodes[1].next == &nodes[0]);
        CX_TEST_ASSERT(nodes[2].next == &nodes[1]);
        CX_TEST_ASSERT(nodes[1].prev == NULL);
        CX_TEST_ASSERT(nodes[0].prev == NULL);
    }
}

TEST(LinkedList_LowLevel, cx_linked_list_insert) {
    // insert mid list
    {
        node nodes[4];
        void *begin = &nodes[0], *end = &nodes[2];

        cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
        cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);

        cx_linked_list_insert(&begin, &end, loc_prev, loc_next, &nodes[1], &nodes[3]);
        CX_TEST_ASSERT(begin == &nodes[0]);
        CX_TEST_ASSERT(end == &nodes[2]);
        CX_TEST_ASSERT(nodes[1].next == &nodes[3]);
        CX_TEST_ASSERT(nodes[2].prev == &nodes[3]);
        CX_TEST_ASSERT(nodes[3].prev == &nodes[1]);
        CX_TEST_ASSERT(nodes[3].next == &nodes[2]);
    }

    // insert end
    {
        node nodes[4];
        void *begin = &nodes[0], *end = &nodes[2];

        cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
        cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);

        cx_linked_list_insert(&begin, &end, loc_prev, loc_next, &nodes[2], &nodes[3]);
        CX_TEST_ASSERT(begin == &nodes[0]);
        CX_TEST_ASSERT(end == &nodes[3]);
        CX_TEST_ASSERT(nodes[2].next == &nodes[3]);
        CX_TEST_ASSERT(nodes[3].prev == &nodes[2]);
        CX_TEST_ASSERT(nodes[3].next == NULL);
    }

    // insert begin
    {
        node nodes[4];
        void *begin = &nodes[0], *end = &nodes[2];

        cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
        cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);

        cx_linked_list_insert(&begin, &end, loc_prev, loc_next, NULL, &nodes[3]);
        CX_TEST_ASSERT(begin == &nodes[3]);
        CX_TEST_ASSERT(end == &nodes[2]);
        CX_TEST_ASSERT(nodes[0].prev == &nodes[3]);
        CX_TEST_ASSERT(nodes[3].prev == NULL);
        CX_TEST_ASSERT(nodes[3].next == &nodes[0]);
    }
}

TEST(LinkedList_LowLevel, cx_linked_list_insert_chain) {
    // insert mid list
    {
        node nodes[5];
        void *begin = &nodes[0], *end = &nodes[2];

        cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
        cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
        cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next);

        cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, &nodes[1], &nodes[3], NULL);
        CX_TEST_ASSERT(begin == &nodes[0]);
        CX_TEST_ASSERT(end == &nodes[2]);
        CX_TEST_ASSERT(nodes[1].next == &nodes[3]);
        CX_TEST_ASSERT(nodes[2].prev == &nodes[4]);
        CX_TEST_ASSERT(nodes[3].prev == &nodes[1]);
        CX_TEST_ASSERT(nodes[4].next == &nodes[2]);
    }

    // insert end
    {
        node nodes[5];
        void *begin = &nodes[0], *end = &nodes[2];

        cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
        cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
        cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next);

        cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, &nodes[2], &nodes[3], NULL);
        CX_TEST_ASSERT(begin == &nodes[0]);
        CX_TEST_ASSERT(end == &nodes[4]);
        CX_TEST_ASSERT(nodes[2].next == &nodes[3]);
        CX_TEST_ASSERT(nodes[3].prev == &nodes[2]);
        CX_TEST_ASSERT(nodes[4].next == NULL);
    }

    // insert begin
    {
        node nodes[5];
        void *begin = &nodes[0], *end = &nodes[2];

        cx_linked_list_link(&nodes[0], &nodes[1], loc_prev, loc_next);
        cx_linked_list_link(&nodes[1], &nodes[2], loc_prev, loc_next);
        cx_linked_list_link(&nodes[3], &nodes[4], loc_prev, loc_next);

        cx_linked_list_insert_chain(&begin, &end, loc_prev, loc_next, NULL, &nodes[3], NULL);
        CX_TEST_ASSERT(begin == &nodes[3]);
        CX_TEST_ASSERT(end == &nodes[2]);
        CX_TEST_ASSERT(nodes[0].prev == &nodes[4]);
        CX_TEST_ASSERT(nodes[3].prev == NULL);
        CX_TEST_ASSERT(nodes[4].next == &nodes[0]);
    }
}

TEST(LinkedList_LowLevel, cx_linked_list_first) {
    auto testdata = create_nodes_test_data(3);
    auto begin = testdata.begin;
    EXPECT_EQ(cx_linked_list_first(begin, loc_prev), begin);
    EXPECT_EQ(cx_linked_list_first(begin->next, loc_prev), begin);
    EXPECT_EQ(cx_linked_list_first(begin->next->next, loc_prev), begin);
}

TEST(LinkedList_LowLevel, cx_linked_list_last) {
    auto testdata = create_nodes_test_data(3);
    auto begin = testdata.begin;
    auto end = begin->next->next;
    EXPECT_EQ(cx_linked_list_last(begin, loc_next), end);
    EXPECT_EQ(cx_linked_list_last(begin->next, loc_next), end);
    EXPECT_EQ(cx_linked_list_last(begin->next->next, loc_next), end);
}

TEST(LinkedList_LowLevel, cx_linked_list_prev) {
    auto testdata = create_nodes_test_data(3);
    auto begin = testdata.begin;
    EXPECT_EQ(cx_linked_list_prev(begin, loc_next, begin), NULL);
    EXPECT_EQ(cx_linked_list_prev(begin, loc_next, begin->next), begin);
    EXPECT_EQ(cx_linked_list_prev(begin, loc_next, begin->next->next), begin->next);
}

TEST(LinkedList_LowLevel, cx_linked_list_remove) {
    auto testdata = create_nodes_test_data({2, 4, 6});
    auto begin = reinterpret_cast<void *>(testdata.begin);
    auto first = testdata.begin;
    auto second = first->next;
    auto third = second->next;
    auto end = reinterpret_cast<void *>(third);

    cx_linked_list_remove(&begin, &end, loc_prev, loc_next, second);
    CX_TEST_ASSERT(begin == first);
    CX_TEST_ASSERT(end == third);
    CX_TEST_ASSERT(first->prev == NULL);
    CX_TEST_ASSERT(first->next == third);
    CX_TEST_ASSERT(third->prev == first);
    CX_TEST_ASSERT(third->next == NULL);

    cx_linked_list_remove(&begin, &end, loc_prev, loc_next, third);
    CX_TEST_ASSERT(begin == first);
    CX_TEST_ASSERT(end == first);
    CX_TEST_ASSERT(first->prev == NULL);
    CX_TEST_ASSERT(first->next == NULL);

    cx_linked_list_remove(&begin, &end, loc_prev, loc_next, first);
    CX_TEST_ASSERT(begin == NULL);
    CX_TEST_ASSERT(end == NULL);
}

TEST(LinkedList_LowLevel, cx_linked_list_size) {
    EXPECT_EQ(cx_linked_list_size(NULL, loc_next), 0);

    {
        auto testdata = create_nodes_test_data(5);
        EXPECT_EQ(cx_linked_list_size(testdata.begin, loc_next), 5);
    }

    {
        auto testdata = create_nodes_test_data(13);
        EXPECT_EQ(cx_linked_list_size(testdata.begin, loc_next), 13);
    }
}

TEST(LinkedList_LowLevel, cx_linked_list_sort_empty) {
    void *begin = NULL;
    EXPECT_NO_FATAL_FAILURE(
        cx_linked_list_sort(&begin, NULL, loc_prev, loc_next, loc_data, cx_cmp_int);
    );
}

TEST(LinkedList_LowLevel, cx_linked_list_sort) {
    int_test_data<1500> testdata;
    std::array<int, 1500> sorted{};
    std::partial_sort_copy(testdata.data.begin(), testdata.data.end(), sorted.begin(), sorted.end());

    auto scrambled = create_nodes_test_data(testdata.data.begin(), testdata.data.end());
    void *begin = scrambled.begin;
    void *end = cx_linked_list_last(begin, loc_next);

    cx_linked_list_sort(&begin, &end, loc_prev, loc_next, loc_data, cx_cmp_int);

    node *check = reinterpret_cast<node *>(begin);
    node *check_last = NULL;
    cx_for_n (i, sorted.size()) {
        CX_TEST_ASSERT(check->data == sorted[i]);
        CX_TEST_ASSERT(check->prev == check_last);
        if (i < sorted.size() - 1) {
            ASSERT_NE(check->next, NULL);
        }
        check_last = check;
        check = check->next;
    }
    CX_TEST_ASSERT(check == NULL);
    CX_TEST_ASSERT(end == check_last);
}

TEST(LinkedList_LowLevel, cx_linked_list_reverse) {
    auto testdata = create_nodes_test_data({2, 4, 6, 8});
    auto expected = create_nodes_test_data({8, 6, 4, 2});

    auto begin = reinterpret_cast<void *>(testdata.begin);
    auto end = cx_linked_list_last(begin, loc_next);
    auto orig_begin = begin, orig_end = end;

    cx_linked_list_reverse(&begin, &end, loc_prev, loc_next);
    CX_TEST_ASSERT(end == orig_begin);
    CX_TEST_ASSERT(begin == orig_end);
    EXPECT_EQ(cx_linked_list_compare(begin, expected.begin, loc_next, loc_data, cx_cmp_int), 0);
}

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