Skip to content

Commit 5ce3c64

Browse files
committed
fix bug in addAtIndex of DoubleLinkedLists; add tests for SingleLinkedLists and DoubleLinkedLists.
1 parent 5e4efee commit 5ce3c64

File tree

39 files changed

+4242
-39
lines changed

39 files changed

+4242
-39
lines changed

src/AbstractList.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,16 @@ class AbstractList {
7676
explicit AbstractList<T>(bool mutableList) : mutableList(mutableList) {}
7777

7878
/*!
79-
* @brief Get a pointer to the element, stored at specific index-
79+
* @brief Get a pointer to the element, stored at specific index.
8080
*
8181
* @param index The index of the element to retrieve.
8282
* @return The nullptr, if the index is out of bounds, otherwise the address
8383
* of the element.
8484
*
85-
* @note This is independent from their mutability. It will always return the
85+
* @note This is independent from the mutability of the list. It will always return the
8686
* correct address (pointer) to the element.
87+
* @note Allowed indices are 0 to getSize() -1. If the index is out of bounds,
88+
* a nullptr will be returned.
8789
*/
8890
virtual T *getPointer(int index) = 0;
8991

src/DoubleLinkedList.hpp

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,6 @@ class DoubleLinkedList : public AbstractList<T> {
4545
Entry *next = nullptr;/// Pointer to the next element of the list.
4646

4747
public:
48-
/*!
49-
* @brief Constructor of an Entry Object.
50-
*
51-
* @param value Value of the entry.
52-
*/
53-
explicit Entry(T *value) : AbstractList<T>::AbstractEntry(value) {}
54-
5548
/*!
5649
* @brief Destructor of an Entry Object.
5750
*/
@@ -94,9 +87,9 @@ class DoubleLinkedList : public AbstractList<T> {
9487

9588
protected:
9689
/*!
97-
* @copydoc AbstractList::get()
90+
* @copydoc AbstractList::getPointer()
9891
*/
99-
T *get(int index) override {
92+
T *getPointer(int index) override {
10093
if (this->isIndexOutOfBounds(index)) {
10194
return nullptr;
10295
}
@@ -115,14 +108,7 @@ class DoubleLinkedList : public AbstractList<T> {
115108
}
116109
}
117110

118-
if (this->isMutable()) {
119-
return (T *) current->getValue();
120-
} else {
121-
T *val = current->getValue();
122-
T *finalValue;
123-
createFinalValue(*val, finalValue, T);
124-
return finalValue;
125-
}
111+
return current->getValue();
126112
}
127113

128114
public:
@@ -140,6 +126,10 @@ class DoubleLinkedList : public AbstractList<T> {
140126
*/
141127
~DoubleLinkedList() { this->clear(); }
142128

129+
using AbstractList<T>::addAtIndex;///'Using' the addAtIndex method, to
130+
///prevent name hiding of the addAtIndex
131+
///method from AbstractList
132+
143133
/*!
144134
* @copydoc AbstractList::addAtIndex()
145135
*/
@@ -153,11 +143,12 @@ class DoubleLinkedList : public AbstractList<T> {
153143
Entry *entry;
154144

155145
if (this->isMutable()) {
156-
entry = new Entry(&value);
146+
// TODO: change for mutable
147+
entry = new Entry();
148+
entry->setValue(value);
157149
} else {
158-
T *finalValue;
159-
createFinalValue(value, finalValue, T);
160-
entry = new Entry(finalValue);
150+
entry = new Entry();
151+
entry->setValue(value);
161152
}
162153

163154
if (index == 0) {
@@ -187,6 +178,7 @@ class DoubleLinkedList : public AbstractList<T> {
187178
}
188179
entry->setNext(current);
189180
entry->setPrev(current->getPrev());
181+
entry->getPrev()->setNext(entry);
190182
current->setPrev(entry);
191183
} else {
192184
current = head;
@@ -207,20 +199,18 @@ class DoubleLinkedList : public AbstractList<T> {
207199
* @copydoc AbstractList::clear()
208200
*/
209201
void clear() override {
210-
if (this->getSize() > 0) {
202+
if (this->getSize() == 0) {
203+
return;
204+
}
205+
211206
Entry *current = head;
212207
Entry *next;
213208
for (int i = 0; i < this->getSize(); ++i) {
214209
next = current->getNext();
215210

216-
if (!this->isMutable()) {
217-
current->freeValue();
218-
}
219-
220211
delete current;
221212
current = next;
222213
}
223-
}
224214

225215
this->resetSize();
226216
head = nullptr;
@@ -277,9 +267,6 @@ class DoubleLinkedList : public AbstractList<T> {
277267
}
278268
}
279269

280-
if (!this->isMutable()) {
281-
toDelete->freeValue();
282-
}
283270
delete toDelete;
284271

285272
this->decreaseSize();

src/SingleLinkedList.hpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,7 @@ class SingleLinkedList : public AbstractList<T> {
6969

7070
protected:
7171
/*!
72-
* Get the pointer to the element at the specified index.
73-
*
74-
* @note Allowed indices are 0 to getSize() -1. If the index is out of bounds,
75-
* a nullptr will be returned.
76-
*
77-
* @param index
78-
* @return
72+
* @copydoc AbstractList::getPointer()
7973
*/
8074
T *getPointer(int index) override {
8175
if (this->isIndexOutOfBounds(index)) {
Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
#include <Arduino.h>
2+
3+
#include "unity.h"
4+
5+
#include <DoubleLinkedList.hpp>
6+
7+
// ---------- In different scope (ds) ---------- //
8+
9+
void add_ds_rvalue_primitive(void) {
10+
DoubleLinkedList<int> list;
11+
12+
{
13+
list + 1;
14+
}
15+
TEST_ASSERT_EQUAL_INT(1, list[0]);
16+
TEST_ASSERT_EQUAL_INT(1, list.getSize());
17+
18+
{
19+
list + 2;
20+
}
21+
TEST_ASSERT_EQUAL_INT(1, list[0]);
22+
TEST_ASSERT_EQUAL_INT(2, list[1]);
23+
TEST_ASSERT_EQUAL_INT(2, list.getSize());
24+
}
25+
26+
void add_ds_lvalue_primitive(void) {
27+
DoubleLinkedList<int> list;
28+
29+
{
30+
int a = 1;
31+
list + a;
32+
33+
TEST_ASSERT_EQUAL_INT(1, a);
34+
a = 2;
35+
}
36+
TEST_ASSERT_EQUAL_INT(1, list[0]);
37+
TEST_ASSERT_EQUAL_INT(1, list.getSize());
38+
39+
{
40+
int b = 2;
41+
list + b;
42+
43+
TEST_ASSERT_EQUAL_INT(2, b);
44+
b = 3;
45+
}
46+
TEST_ASSERT_EQUAL_INT(1, list[0]);
47+
TEST_ASSERT_EQUAL_INT(2, list[1]);
48+
TEST_ASSERT_EQUAL_INT(2, list.getSize());
49+
}
50+
51+
void add_ds_rvalue_class(void) {
52+
DoubleLinkedList<String> list;
53+
54+
{
55+
list + "1";
56+
}
57+
TEST_ASSERT_EQUAL_STRING("1", list[0].c_str());
58+
TEST_ASSERT_EQUAL_INT(1, list.getSize());
59+
60+
{
61+
list + "2";
62+
}
63+
TEST_ASSERT_EQUAL_STRING("1", list[0].c_str());
64+
TEST_ASSERT_EQUAL_STRING("2", list[1].c_str());
65+
TEST_ASSERT_EQUAL_INT(2, list.getSize());
66+
}
67+
68+
void add_ds_lvalue_class(void) {
69+
DoubleLinkedList<String> list;
70+
71+
{
72+
String a = "1";
73+
list + a;
74+
75+
TEST_ASSERT_EQUAL_STRING("1", a.c_str());
76+
a = "2";
77+
}
78+
TEST_ASSERT_EQUAL_STRING("1", list[0].c_str());
79+
TEST_ASSERT_EQUAL_INT(1, list.getSize());
80+
81+
{
82+
String b = "2";
83+
list + b;
84+
85+
TEST_ASSERT_EQUAL_STRING("2", b.c_str());
86+
b = "3";
87+
}
88+
TEST_ASSERT_EQUAL_STRING("1", list[0].c_str());
89+
TEST_ASSERT_EQUAL_STRING("2", list[1].c_str());
90+
TEST_ASSERT_EQUAL_INT(2, list.getSize());
91+
}
92+
93+
// ---------- In same scope (ss) ---------- //
94+
95+
void add_ss_rvalue_primitive(void) {
96+
DoubleLinkedList<int> list;
97+
98+
list + 1;
99+
TEST_ASSERT_EQUAL_INT(1, list[0]);
100+
TEST_ASSERT_EQUAL_INT(1, list.getSize());
101+
102+
list + 2;
103+
TEST_ASSERT_EQUAL_INT(1, list[0]);
104+
TEST_ASSERT_EQUAL_INT(2, list[1]);
105+
TEST_ASSERT_EQUAL_INT(2, list.getSize());
106+
}
107+
108+
void add_ss_lvalue_primitive(void) {
109+
DoubleLinkedList<int> list;
110+
111+
int a = 1, b = 2;
112+
113+
list + a;
114+
115+
TEST_ASSERT_EQUAL_INT(1, a);
116+
a = 2;
117+
TEST_ASSERT_EQUAL_INT(1, list[0]);
118+
TEST_ASSERT_EQUAL_INT(1, list.getSize());
119+
120+
list + b;
121+
122+
TEST_ASSERT_EQUAL_INT(2, b);
123+
b = 3;
124+
TEST_ASSERT_EQUAL_INT(1, list[0]);
125+
TEST_ASSERT_EQUAL_INT(2, list[1]);
126+
TEST_ASSERT_EQUAL_INT(2, list.getSize());
127+
}
128+
129+
void add_ss_rvalue_class(void) {
130+
DoubleLinkedList<String> list;
131+
132+
list + "1";
133+
TEST_ASSERT_EQUAL_STRING("1", list[0].c_str());
134+
TEST_ASSERT_EQUAL_INT(1, list.getSize());
135+
136+
list + "2";
137+
TEST_ASSERT_EQUAL_STRING("1", list[0].c_str());
138+
TEST_ASSERT_EQUAL_STRING("2", list[1].c_str());
139+
TEST_ASSERT_EQUAL_INT(2, list.getSize());
140+
}
141+
142+
void add_ss_lvalue_class(void) {
143+
DoubleLinkedList<String> list;
144+
145+
String a = "1", b = "2";
146+
147+
list + a;
148+
149+
TEST_ASSERT_EQUAL_STRING("1", a.c_str());
150+
a = "2";
151+
TEST_ASSERT_EQUAL_STRING("1", list[0].c_str());
152+
TEST_ASSERT_EQUAL_INT(1, list.getSize());
153+
154+
list + b;
155+
156+
TEST_ASSERT_EQUAL_STRING("2", b.c_str());
157+
b = "3";
158+
TEST_ASSERT_EQUAL_STRING("1", list[0].c_str());
159+
TEST_ASSERT_EQUAL_STRING("2", list[1].c_str());
160+
TEST_ASSERT_EQUAL_INT(2, list.getSize());
161+
}
162+
163+
// ---------- void operator+(AbstractList<T> &list) ---------- //
164+
165+
void addAll_primitive(void) {
166+
DoubleLinkedList<int> fromList;
167+
DoubleLinkedList<int> toList;
168+
169+
toList.add(1);
170+
toList.add(2);
171+
toList.add(3);
172+
fromList.add(4);
173+
fromList.add(5);
174+
fromList.add(6);
175+
176+
toList + fromList;
177+
178+
TEST_ASSERT_EQUAL_INT(4, fromList[0]);
179+
TEST_ASSERT_EQUAL_INT(5, fromList[1]);
180+
TEST_ASSERT_EQUAL_INT(6, fromList[2]);
181+
TEST_ASSERT_EQUAL_INT(3, fromList.getSize());
182+
183+
fromList.clear();
184+
185+
TEST_ASSERT_EQUAL_INT(1, toList[0]);
186+
TEST_ASSERT_EQUAL_INT(2, toList[1]);
187+
TEST_ASSERT_EQUAL_INT(3, toList[2]);
188+
TEST_ASSERT_EQUAL_INT(4, toList[3]);
189+
TEST_ASSERT_EQUAL_INT(5, toList[4]);
190+
TEST_ASSERT_EQUAL_INT(6, toList[5]);
191+
TEST_ASSERT_EQUAL_INT(6, toList.getSize());
192+
}
193+
194+
void addAll_class(void) {
195+
DoubleLinkedList<String> fromList;
196+
DoubleLinkedList<String> toList;
197+
198+
toList.add("1");
199+
toList.add("2");
200+
toList.add("3");
201+
fromList.add("4");
202+
fromList.add("5");
203+
fromList.add("6");
204+
205+
toList + fromList;
206+
207+
TEST_ASSERT_EQUAL_STRING("4", fromList[0].c_str());
208+
TEST_ASSERT_EQUAL_STRING("5", fromList[1].c_str());
209+
TEST_ASSERT_EQUAL_STRING("6", fromList[2].c_str());
210+
TEST_ASSERT_EQUAL_INT(3, fromList.getSize());
211+
212+
fromList.clear();
213+
214+
TEST_ASSERT_EQUAL_STRING("1", toList[0].c_str());
215+
TEST_ASSERT_EQUAL_STRING("2", toList[1].c_str());
216+
TEST_ASSERT_EQUAL_STRING("3", toList[2].c_str());
217+
TEST_ASSERT_EQUAL_STRING("4", toList[3].c_str());
218+
TEST_ASSERT_EQUAL_STRING("5", toList[4].c_str());
219+
TEST_ASSERT_EQUAL_STRING("6", toList[5].c_str());
220+
TEST_ASSERT_EQUAL_INT(6, toList.getSize());
221+
}
222+
223+
void setup() {
224+
UNITY_BEGIN();
225+
226+
// different scope tests
227+
RUN_TEST(add_ds_rvalue_primitive);
228+
RUN_TEST(add_ds_lvalue_primitive);
229+
RUN_TEST(add_ds_rvalue_class);
230+
RUN_TEST(add_ds_lvalue_class);
231+
232+
// same scope tests
233+
RUN_TEST(add_ss_rvalue_primitive);
234+
RUN_TEST(add_ss_lvalue_primitive);
235+
RUN_TEST(add_ss_rvalue_class);
236+
RUN_TEST(add_ss_lvalue_class);
237+
238+
// ---------- void operator+(AbstractList<T> &list) ---------- //
239+
RUN_TEST(addAll_primitive);
240+
RUN_TEST(addAll_class);
241+
242+
UNITY_END();
243+
}
244+
245+
void loop() {
246+
}

0 commit comments

Comments
 (0)