Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
dnode.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 <cstring>
41# include <new>
42
43# include <tpl_dnode.H>
44
45using namespace testing;
46using namespace Aleph;
47
48// The item of a node built by default, as the header of a list, is T():
49// for a scalar it was left indeterminate, and reading it, even to copy it,
50// was undefined behavior. The node is built over memory with a pattern,
51// which its item must not keep
52TEST(Dnode, default_item_is_value_initialized)
53{
54 alignas(Dnode<long>) unsigned char buf[sizeof(Dnode<long>)];
55 std::memset(buf, 0x5a, sizeof(buf));
56 auto * p = ::new (static_cast<void *>(buf)) Dnode<long>;
57 EXPECT_EQ(p->get_data(), 0);
58 EXPECT_TRUE(p->is_empty());
59 p->~Dnode<long>();
60
62 EXPECT_EQ(q.get_data(), nullptr);
63}
64
65TEST(Dnode, conversion_from_slinknc)
66{
67 {
68 Dnode<int> node = 10;
69 Dlink * ptr = &node;
70 EXPECT_EQ(ptr->to_dnode<int>(), &node);
71 EXPECT_EQ(ptr->to_data<int>(), 10);
72 }
73
74 {
75 const Dnode<int> node = 10;
76 const Dlink * ptr = &node;
77 EXPECT_EQ(ptr->to_dnode<int>(), &node);
78 EXPECT_EQ(ptr->to_data<int>(), 10);
79 }
80}
81
82struct List_of_5_nodes : public Test
83{
86 {
87 list.insert(new Dnode<int>(5));
88 list.insert(new Dnode<int>(4));
89 list.insert(new Dnode<int>(3));
90 list.insert(new Dnode<int>(2));
91 list.insert(new Dnode<int>(1));
92 }
94 {
95 delete list.remove_next();
96 delete list.remove_next();
97 delete list.remove_next();
98 delete list.remove_next();
99 delete list.remove_next();
100 }
101};
102
103TEST_F(List_of_5_nodes, Basic_operations)
104{
105 EXPECT_EQ(list.get_next()->get_data(), 1);
106 EXPECT_EQ(list.get_prev()->get_data(), 5);
107
108 int i = 1;
109 for (Dnode<int>::Iterator it = list; it.has_curr(); it.next(), ++i)
110 EXPECT_EQ(it.get_curr()->to_data<int>(), i);
111
112 auto ptr = list.remove_first();
113 EXPECT_EQ(ptr->get_data(), 1);
114 EXPECT_TRUE(ptr->is_empty());
115 list.insert(ptr);
116 EXPECT_EQ(list.get_next()->get_data(), 1);
117}
Iterator on a list of Dnode objects.
Definition tpl_dnode.H:294
Node belonging to a double circular linked list with header node.
Definition tpl_dnode.H:106
#define TEST(name)
TEST_F(List_of_5_nodes, Basic_operations)
Definition dnode.cc:103
Main namespace for Aleph-w library functions.
Dnode< int > list
Definition dnode.cc:84
Doubly linked list node with typed data.