Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
snodenc.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 <string>
39# include <utility>
40
41# include <gtest/gtest.h>
42
43# include <htlist.H>
44
45using namespace testing;
46using namespace Aleph;
47
48// An explicit instantiation compiles every non-template member, including
49// those that no test calls; otherwise an ill-formed one goes unnoticed.
50template class Aleph::Snodenc<int>;
51template class Aleph::Snodenc<std::string>;
52
53template <class Node>
54concept Has_Get_First = requires (Node & node) { node.get_first(); };
55
56static_assert(Has_Get_First<Snodenc<int>>);
57// The result allows changing the link, so a constant node must not give it
58static_assert(not Has_Get_First<const Snodenc<int>>);
59static_assert(noexcept(std::declval<Snodenc<int> &>().get_first()));
60
61struct List_of_5_nodes : public Test
62{
65 {
66 list.insert(new Snodenc<int>(5));
67 list.insert(new Snodenc<int>(4));
68 list.insert(new Snodenc<int>(3));
69 list.insert(new Snodenc<int>(2));
70 list.insert(new Snodenc<int>(1));
71 }
73 {
74 delete list.remove_next();
75 delete list.remove_next();
76 delete list.remove_next();
77 delete list.remove_next();
78 delete list.remove_next();
79 }
80};
81
82TEST(Snodenc, conversion_from_slinknc)
83{
84 {
85 Snodenc<int> node = 10;
86 Slinknc * ptr = &node;
87 ASSERT_EQ(ptr->to_snodenc<int>(), &node);
88 ASSERT_EQ(ptr->to_data<int>(), 10);
89 }
90
91 {
92 const Snodenc<int> node = 10;
93 const Slinknc * ptr = &node;
94 ASSERT_EQ(ptr->to_snodenc<int>(), &node);
95 ASSERT_EQ(ptr->to_data<int>(), 10);
96 }
97}
98
99TEST_F(List_of_5_nodes, Basic_operations)
100{
101 ASSERT_EQ(list.get_next()->get_data(), 1);
102
103 int i = 1;
104 for (Snodenc<int>::Iterator it = list; it.has_curr(); it.next())
105 {
106 auto ptr = static_cast<Snodenc<int>*>(it.get_curr());
107 EXPECT_EQ(ptr->get_data(), i++);
108 }
109
110 auto ptr = list.remove_first();
111 EXPECT_EQ(ptr->get_data(), 1);
112 EXPECT_TRUE(ptr->is_empty());
113 list.insert(ptr);
114 EXPECT_EQ(list.get_next()->get_data(), 1);
115}
116
117TEST(Snodenc, get_first_of_a_lone_node_is_null)
118{
119 Snodenc<int> list;
120 EXPECT_EQ(list.get_first(), nullptr);
121}
122
123TEST(Snodenc, get_first_returns_the_following_node)
124{
125 Snodenc<std::string> list, a("a"), b("b");
126
127 list.insert(&a);
128 EXPECT_EQ(list.get_first(), &a);
129 EXPECT_EQ(a.get_first(), nullptr);
130
131 list.insert(&b);
132 EXPECT_EQ(list.get_first(), &b);
133 EXPECT_EQ(list.get_first()->get_data(), "b");
134 EXPECT_EQ(b.get_first(), &a);
135
136 EXPECT_EQ(list.remove_first(), &b);
137 EXPECT_EQ(list.get_first(), &a);
138 EXPECT_EQ(b.get_first(), nullptr);
139
140 EXPECT_EQ(list.remove_first(), &a);
141 EXPECT_EQ(list.get_first(), nullptr);
142}
143
144TEST(Snodenc, get_first_is_a_reference_to_the_link)
145{
146 Snodenc<int> list, a(1);
147 ASSERT_EQ(&list.get_first(), &list.get_next());
148
149 list.get_first() = &a;
150 EXPECT_EQ(list.get_next(), &a);
151 EXPECT_FALSE(list.is_empty());
152
153 list.get_first() = nullptr;
154 EXPECT_TRUE(list.is_empty());
155}
WeightedDigraph::Node Node
Link of a single linked list non-circular and without header node.
Definition htlist.H:95
Slinknc * next
Definition htlist.H:96
T & to_data() noexcept
Return the item of the Snodenc<T> that this is.
Definition htlist.H:382
constexpr bool is_empty() const noexcept
Return true if this is empty.
Definition htlist.H:102
Snodenc< T > * to_snodenc() noexcept
Convert this to a Snodenc<T>.
Definition htlist.H:370
void insert(Slinknc *p) noexcept
insert(p) inserts the node pointed by p after this.
Definition htlist.H:146
Snodenc *& get_first() noexcept
Return the first node of the list whose header is this.
Definition htlist.H:363
Snodenc *& get_next() noexcept
Return the node following to this
Definition htlist.H:342
T & get_data() noexcept
Return a modifiable reference to the data.
Definition htlist.H:295
Snodenc * remove_first() noexcept
See remove_next().
Definition htlist.H:350
#define TEST(name)
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
TEST_F(List_of_5_nodes, Basic_operations)
Definition snodenc.cc:99
Snodenc< int > list
Definition snodenc.cc:63