Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
slink.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
38#include <gtest/gtest.h>
39
40#include <slink.H>
41
42using namespace Aleph;
43
44namespace {
45
46struct TestNode
47{
48 int value;
49 Slink link;
50 SLINK_TO_TYPE(TestNode, link);
51};
52
53void push_front(Slink & head, TestNode & node)
54{
55 node.link.reset();
56 head.insert_next(&node.link);
57}
58
59} // namespace
60
62{
63 Slink head;
64 TestNode n1{1, {}};
65 TestNode n2{2, {}};
66
67 ASSERT_TRUE(head.is_empty());
68 ASSERT_TRUE(n1.link.is_empty());
69
70 push_front(head, n1);
71 EXPECT_FALSE(head.is_empty());
72 EXPECT_EQ(head.get_next(), &n1.link);
73
74 push_front(head, n2);
75 EXPECT_EQ(head.get_next(), &n2.link);
76 EXPECT_EQ(n2.link.get_next(), &n1.link);
77
78 auto removed = head.remove_next();
79 EXPECT_EQ(removed, &n2.link);
80 EXPECT_TRUE(n2.link.is_empty());
81 EXPECT_EQ(head.get_next(), &n1.link);
82
83 removed = head.remove_next();
84 EXPECT_EQ(removed, &n1.link);
85 EXPECT_TRUE(head.is_empty());
86}
87
89{
90 Slink head;
91 TestNode nodes[] = {{10, {}}, {20, {}}, {30, {}}};
92
93 for (auto & node : nodes)
94 push_front(head, node);
95
96 int expected[] = {30, 20, 10};
97 size_t idx = 0;
98 for (Slink * it = head.get_next(); it != &head; it = it->get_next())
99 {
100 auto * owner = TestNode::slink_to_type(it);
101 ASSERT_NE(owner, nullptr);
102 EXPECT_EQ(owner->value, expected[idx++]);
103 }
104 EXPECT_EQ(idx, std::size(expected));
105
106 while (not head.is_empty())
107 {
108 auto * removed = head.remove_next();
109 auto * owner = TestNode::slink_to_type(removed);
110 owner->link.reset();
111 EXPECT_TRUE(owner->link.is_empty());
112 }
113 EXPECT_TRUE(head.is_empty());
114}
115
117{
118 Slink head;
119 TestNode node{42, {}};
120 push_front(head, node);
121
122 const Slink & cref = head;
123 EXPECT_EQ(cref.get_next(), head.get_next());
124}
constexpr bool is_empty() const noexcept
Return true if list is empty.
Definition htlist.H:523
void reset() noexcept
Definition htlist.H:516
#define TEST(name)
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
Graph_Anode< Empty_Class > TestNode