test/test_tree.cpp

Sat, 05 Nov 2022 17:50:04 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 05 Nov 2022 17:50:04 +0100
changeset 601
95ba6014041b
parent 515
6d3909bf1609
permissions
-rw-r--r--

add compare functions

/*
 * 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/tree.h"
#include <gtest/gtest.h>

struct TestNode {
    TestNode *parent = nullptr;
    TestNode *prev = nullptr;
    TestNode *next = nullptr;

    TestNode *children_begin = nullptr;
    TestNode *children_end = nullptr;
};

TEST(Tree, cx_tree_add_sibling) {
    // prepare test tree
    TestNode root, a;
    root.children_begin = &a;
    root.children_end = &a;
    a.parent = &root;

    // new test nodes
    TestNode b, c;

    // test
    cx_tree_add_sibling(&a, offsetof(TestNode, prev), offsetof(TestNode, next), offsetof(TestNode, parent), &b);
    EXPECT_EQ(b.parent, &root);
    EXPECT_EQ(b.prev, &a);
    EXPECT_EQ(b.next, nullptr);
    EXPECT_EQ(a.next, &b);

    cx_tree_add_sibling(&a, -1, offsetof(TestNode, next), -1, &c);
    EXPECT_EQ(c.parent, nullptr);
    EXPECT_EQ(c.prev, nullptr);
    EXPECT_EQ(c.next, nullptr);
    EXPECT_EQ(b.next, &c);
}

TEST(Tree, cx_tree_add_child) {
    TestNode root, a, b, c, a1;

    cx_tree_add_child(
            (void **) &root.children_begin,
            (void **) &root.children_end,
            offsetof(TestNode, prev),
            offsetof(TestNode, next),
            &a,
            offsetof(TestNode, parent),
            &root);
    EXPECT_EQ(root.children_begin, &a);
    EXPECT_EQ(root.children_end, &a);
    EXPECT_EQ(a.parent, &root);
    EXPECT_EQ(a.prev, nullptr);
    EXPECT_EQ(a.next, nullptr);

    cx_tree_add_child(
            (void **) &root.children_begin,
            (void **) &root.children_end,
            offsetof(TestNode, prev),
            offsetof(TestNode, next),
            &b,
            offsetof(TestNode, parent),
            &root);
    EXPECT_EQ(root.children_begin, &a);
    EXPECT_EQ(root.children_begin->next, &b);
    EXPECT_EQ(root.children_end, &b);
    EXPECT_EQ(b.parent, &root);
    EXPECT_EQ(b.prev, &a);

    cx_tree_add_child(
            (void **) &root.children_begin,
            nullptr,
            -1,
            offsetof(TestNode, next),
            &c,
            -1,
            &root);
    EXPECT_EQ(root.children_end, &b); // children_end unchanged
    EXPECT_EQ(b.next, &c);
    EXPECT_EQ(c.prev, nullptr);
    EXPECT_EQ(c.next, nullptr);
    EXPECT_EQ(c.parent, nullptr);

    cx_tree_add_child(
            (void **) &a.children_begin,
            (void **) &a.children_end,
            offsetof(TestNode, prev),
            offsetof(TestNode, next),
            &a1,
            offsetof(TestNode, parent),
            &a);
    EXPECT_EQ(a.children_begin, &a1);
    EXPECT_EQ(a1.parent, &a);
    EXPECT_EQ(root.children_begin, &a);
    EXPECT_EQ(root.children_begin->children_begin, &a1);
}

mercurial