linked_list.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #include <gtest/gtest.h>
7 
8 struct ListItem : uavcan::LinkedListNode<ListItem>
9 {
10  int value;
11 
12  ListItem(int value = 0)
13  : value(value)
14  { }
15 
17  {
18  const int compare_with;
19 
22  { }
23 
24  bool operator()(const ListItem* item) const
25  {
26  return item->value > compare_with;
27  }
28  };
29 
31  {
33  }
34 };
35 
36 TEST(LinkedList, Basic)
37 {
39 
40  /*
41  * Insert/remove
42  */
43  EXPECT_EQ(0, root.getLength());
44 
45  ListItem item1;
46  root.insert(&item1);
47  root.insert(&item1); // Insert twice - second will be ignored
48  EXPECT_EQ(1, root.getLength());
49 
50  root.remove(&item1);
51  root.remove(&item1);
52  EXPECT_EQ(0, root.getLength());
53 
54  ListItem items[3];
55  root.insert(&item1);
56  root.insert(items + 0);
57  root.insert(items + 1);
58  root.insert(items + 2);
59  EXPECT_EQ(4, root.getLength());
60 
61  /*
62  * Order persistence
63  */
64  items[0].value = 10;
65  items[1].value = 11;
66  items[2].value = 12;
67  const int expected_values[] = {12, 11, 10, 0};
68  ListItem* node = root.get();
69  for (int i = 0; i < 4; i++)
70  {
71  EXPECT_EQ(expected_values[i], node->value);
72  node = node->getNextListNode();
73  }
74 
75  root.remove(items + 0);
76  root.remove(items + 2);
77  root.remove(items + 2);
78  EXPECT_EQ(2, root.getLength());
79 
80  const int expected_values2[] = {11, 0};
81  node = root.get();
82  for (int i = 0; i < 2; i++)
83  {
84  EXPECT_EQ(expected_values2[i], node->value);
85  node = node->getNextListNode();
86  }
87 
88  root.insert(items + 2);
89  EXPECT_EQ(3, root.getLength());
90  EXPECT_EQ(12, root.get()->value);
91 
92  /*
93  * Emptying
94  */
95  root.remove(&item1);
96  root.remove(items + 0);
97  root.remove(items + 1);
98  root.remove(items + 2);
99  EXPECT_EQ(0, root.getLength());
100 }
101 
102 TEST(LinkedList, Sorting)
103 {
105  ListItem items[6];
106  for (int i = 0; i < 6; i++)
107  {
108  items[i].value = i;
109  }
110 
111  EXPECT_EQ(0, root.getLength());
112 
113  items[2].insort(root);
114  EXPECT_EQ(1, root.getLength());
115 
116  items[2].insort(root); // Making sure the duplicate will be removed
117  EXPECT_EQ(1, root.getLength());
118 
119  items[3].insort(root);
120 
121  items[0].insort(root);
122 
123  items[4].insort(root);
124  EXPECT_EQ(4, root.getLength());
125 
126  items[1].insort(root);
127  EXPECT_EQ(5, root.getLength());
128 
129  items[1].insort(root); // Another duplicate
130  EXPECT_EQ(5, root.getLength());
131 
132  items[5].insort(root);
133 
134  EXPECT_EQ(6, root.getLength());
135 
136  int prev_val = -100500;
137  const ListItem* item = root.get();
138  while (item)
139  {
140  //std::cout << item->value << std::endl;
141  EXPECT_LT(prev_val, item->value);
142  prev_val = item->value;
143  item = item->getNextListNode();
144  }
145 }
ListItem::GreaterThanComparator
Definition: linked_list.cpp:16
TEST
TEST(LinkedList, Basic)
Definition: linked_list.cpp:36
uavcan::LinkedListRoot::getLength
unsigned getLength() const
Definition: linked_list.hpp:89
ListItem::insort
void insort(uavcan::LinkedListRoot< ListItem > &root)
Definition: linked_list.cpp:30
uavcan::LinkedListRoot::get
T * get() const
Definition: linked_list.hpp:53
uavcan::LinkedListRoot::remove
void remove(const T *node)
Definition: linked_list.hpp:148
ListItem::GreaterThanComparator::GreaterThanComparator
GreaterThanComparator(int compare_with)
Definition: linked_list.cpp:20
uavcan::LinkedListRoot::insert
void insert(T *node)
Definition: linked_list.hpp:102
ListItem::ListItem
ListItem(int value=0)
Definition: linked_list.cpp:12
ListItem
Definition: linked_list.cpp:8
ListItem::value
int value
Definition: linked_list.cpp:10
linked_list.hpp
uavcan::LinkedListRoot::insertBefore
void insertBefore(T *node, Predicate predicate)
Definition: linked_list.hpp:116
uavcan::LinkedListRoot
Definition: linked_list.hpp:44
ListItem::GreaterThanComparator::compare_with
const int compare_with
Definition: linked_list.cpp:18
pyuavcan_v0.introspect.node
node
Definition: introspect.py:398
uavcan::LinkedListNode::getNextListNode
T * getNextListNode() const
Definition: linked_list.hpp:32
ListItem::GreaterThanComparator::operator()
bool operator()(const ListItem *item) const
Definition: linked_list.cpp:24
uavcan::LinkedListNode
Definition: linked_list.hpp:20


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:02