Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
slist.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 <tpl_slist.H>
41
42using namespace Aleph;
43using namespace testing;
44
46{
47 Slist<int> list;
48 auto * n1 = new Snode<int>(1);
49 auto * n2 = new Snode<int>(2);
50
51 list.insert_first(n1);
52 EXPECT_EQ(list.get_first(), n1);
53
54 list.insert_first(n2);
55 EXPECT_EQ(list.get_first(), n2);
56
57 auto removed = list.remove_first();
58 EXPECT_EQ(removed, n2);
60 delete removed;
61
62 removed = list.remove_first();
63 EXPECT_EQ(removed, n1);
64 delete removed;
65
66 EXPECT_TRUE(list.is_empty());
67 EXPECT_THROW(list.remove_first(), std::underflow_error);
68}
69
71{
72 Slist<int> list;
73 auto * n1 = new Snode<int>(1);
74 auto * n2 = new Snode<int>(2);
75
76 list.insert_first(n1);
77 list.insert_first(n2);
78
79 ASSERT_FALSE(list.is_empty());
80 EXPECT_EQ(list.get_first_ne(), n2);
81
82 auto * removed = list.remove_first_ne();
83 EXPECT_EQ(removed, n2);
85 delete removed;
86
87 EXPECT_EQ(list.get_first_ne(), n1);
88 removed = list.remove_first_ne();
89 EXPECT_EQ(removed, n1);
90 delete removed;
91
92 EXPECT_TRUE(list.is_empty());
93}
94
96{
97 Slist<int> list;
98 Slist<int>::Iterator it(list);
100 EXPECT_THROW(it.get_curr(), std::overflow_error);
101 EXPECT_THROW(it.next(), std::overflow_error);
102
103 list.insert_first(new Snode<int>(1));
104 Slist<int>::Iterator it2(list);
105 EXPECT_TRUE(it2.has_curr());
106 ASSERT_NE(it2.get_curr(), nullptr);
107 it2.next();
108 EXPECT_FALSE(it2.has_curr());
109 EXPECT_THROW(it2.get_curr(), std::overflow_error);
110 EXPECT_THROW(it2.next(), std::overflow_error);
111
112 while (not list.is_empty())
113 delete list.remove_first();
114}
115
117{
118 Slist<int> list;
119 for (int i = 0; i < 5; ++i)
120 list.insert_first(new Snode<int>(i));
121
122 int count = 0;
123 for (Slist<int>::Iterator it(list); it.has_curr(); it.next())
124 {
125 ASSERT_NE(it.get_curr(), nullptr);
126 ++count;
127 }
128 EXPECT_EQ(count, 5);
129
130 while (not list.is_empty())
131 delete list.remove_first();
132}
constexpr bool is_empty() const noexcept
Return true if list is empty.
Definition htlist.H:523
Iterator over singly linked nodes.
Definition tpl_slist.H:174
Node * get_curr()
Return the current node.
Definition tpl_slist.H:205
void next()
Advance the iterator to the next node.
Definition tpl_slist.H:215
bool has_curr() const noexcept
Return true if the iterator currently points to a node.
Definition tpl_slist.H:198
Singly linked list of nodes that store values of type T.
Definition tpl_slist.H:102
Node * remove_first_ne() noexcept
Remove and return the first node without checking emptiness.
Definition tpl_slist.H:124
Node * get_first_ne() noexcept
Return the first node without checking emptiness.
Definition tpl_slist.H:158
Node * get_first()
Return a pointer to the first node; throw if the list is empty.
Definition tpl_slist.H:142
void insert_first(Node *node)
Insert node right after this sentinel (at the beginning).
Definition tpl_slist.H:115
Node * remove_first()
Remove the first node of the list.
Definition tpl_slist.H:134
Singly linked node that stores data of type T.
Definition tpl_snode.H:66
#define TEST(name)
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
Singly linked list with typed nodes.