Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
slinknc.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
33
38# include <gtest/gtest.h>
39
40# include <htlist.H>
41
42using namespace std;
43using namespace testing;
44using namespace Aleph;
45
47{
48 Slinknc l;
49 ASSERT_EQ(l.get_next(), nullptr);
50}
51
53{
54 Slinknc l;
56 ASSERT_EQ(l.get_next(), nullptr);
57
58 Slinknc n1;
59 l.insert(&n1);
60 ASSERT_EQ(l.get_next(), &n1);
61
62 Slinknc c1(l);
63 ASSERT_EQ(c1.get_next(), nullptr);
64
65 Slinknc c2;
66 ASSERT_EQ(c2.get_next(), nullptr);
67 c2 = l;
68 ASSERT_EQ(c2.get_next(), nullptr);
69
70 Slinknc n2;
71 l.insert(&n2);
72 ASSERT_EQ(l.get_next(), &n2);
73 ASSERT_EQ(l.remove_next(), &n2);
74 ASSERT_EQ(l.get_next(), &n1);
75 ASSERT_EQ(l.remove_next(), &n1);
76 ASSERT_EQ(l.get_next(), nullptr);
77}
78
79struct Slinknc_of_5_items : public testing::Test
80{
84 {
85 list.insert(&n5);
86 list.insert(&n4);
87 list.insert(&n3);
88 list.insert(&n2);
89 list.insert(&n1);
90 }
91};
92
94{
95 EXPECT_FALSE(list.is_empty());
96 ASSERT_EQ(list.get_next(), &n1);
97 ASSERT_EQ(list.get_next()->get_next(), &n2);
98 ASSERT_EQ(list.get_next()->get_next()->get_next(), &n3);
99 ASSERT_EQ(list.get_next()->get_next()->get_next()->get_next(), &n4);
100 ASSERT_EQ(list.get_next()->get_next()->get_next()->get_next()->get_next(), &n5);
101
102 Slinknc laux = list;
103 EXPECT_TRUE(laux.is_empty());
104 ASSERT_EQ(laux.get_next(), nullptr);
105 laux = list;
106 ASSERT_EQ(laux.get_next(), nullptr);
107
108 Slinknc n6;
109 list.insert(&n6);
110 ASSERT_EQ(list.get_next(), &n6);
111 EXPECT_FALSE(list.is_empty());
112
113 ASSERT_EQ(list.remove_next(), &n6);
114 EXPECT_TRUE(n6.is_empty());
115 EXPECT_FALSE(list.is_empty());
116
117 ASSERT_EQ(list.remove_next(), &n1);
118 EXPECT_TRUE(n1.is_empty());
119 EXPECT_FALSE(list.is_empty());
120
121 ASSERT_EQ(list.remove_next(), &n2);
122 EXPECT_TRUE(n2.is_empty());
123 EXPECT_FALSE(list.is_empty());
124
125 ASSERT_EQ(list.remove_next(), &n3);
126 EXPECT_TRUE(n3.is_empty());
127 EXPECT_FALSE(list.is_empty());
128
129 ASSERT_EQ(list.remove_next(), &n4);
130 EXPECT_TRUE(n4.is_empty());
131 EXPECT_FALSE(list.is_empty());
132
133 ASSERT_EQ(list.remove_next(), &n5);
134 EXPECT_TRUE(n5.is_empty());
135 EXPECT_TRUE(list.is_empty());
136}
137
139{
140 Slinknc l;
142 ASSERT_EQ(it.has_curr(), false);
143 EXPECT_THROW(it.get_curr(), std::overflow_error);
144}
145
147{
148 Slinknc::Iterator it = list;
149
150 EXPECT_TRUE(it.has_curr());
151 ASSERT_EQ(it.get_curr(), &n1);
152 it.next();
153
154 it.reset_first();
155 EXPECT_TRUE(it.has_curr());
156 ASSERT_EQ(it.get_curr(), &n1);
157 it.next();
158
159 EXPECT_TRUE(it.has_curr());
160 ASSERT_EQ(it.get_curr(), &n2);
161 it.next();
162
163 EXPECT_TRUE(it.has_curr());
164 ASSERT_EQ(it.get_curr(), &n3);
165 it.next();
166
167 EXPECT_TRUE(it.has_curr());
168 ASSERT_EQ(it.get_curr(), &n4);
169 it.next();
170
171 EXPECT_TRUE(it.has_curr());
172 ASSERT_EQ(it.get_curr(), &n5);
173 it.next();
174
176 EXPECT_THROW(it.get_curr(), std::overflow_error);
177}
178
T & insert(const T &item)
Definition htlist.H:1220
constexpr bool is_empty() const noexcept
Definition htlist.H:419
void reset_first() noexcept
Reset the iterator to the first link on the list.
Definition htlist.H:263
bool has_curr() const noexcept
Return true if the iterator is positioned on a valid link.
Definition htlist.H:225
Slinknc * get_curr() const
Definition htlist.H:235
Link of a single linked list non-circular and without header node.
Definition htlist.H:95
constexpr bool is_empty() const noexcept
Return true if this is empty.
Definition htlist.H:103
void insert(Slinknc *p) noexcept
insert(p) inserts the node pointed by p after this.
Definition htlist.H:143
#define TEST(name)
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
STL namespace.
TEST_F(Slinknc_of_5_items, Complex_operations)
Definition slinknc.cc:93
DynList< int > l