Skip to content

Commit 33f28f3

Browse files
committed
fix test for DOublyLinkedList
1 parent 7545c9b commit 33f28f3

File tree

2 files changed

+158
-34
lines changed

2 files changed

+158
-34
lines changed

data-structures/DoublyLinkedList/__test__/test_DoublyLinkedList.cpp

+141-10
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,154 @@ int main(int argc, char* argv[]){
66
DoublyLinkedList<int> dList;
77
assert(dList.size() == 0);
88
assert(dList.empty() == true);
9-
dList.push_back(5);
10-
dList.push_front(9);
11-
dList.push_back(2);
12-
dList.push_back(1);
13-
dList.push_front(3);
9+
try{
10+
dList.pop_front();
11+
} catch(const std::underflow_error &e){
12+
std::cout << "pop_front() throw an exception PASSED: " << e.what() << std::endl;
13+
}
14+
try{
15+
dList.pop_back();
16+
} catch(const std::underflow_error &e){
17+
std::cout << "pop_back() throw an exception PASSED: " << e.what() << std::endl;
18+
}
19+
try{
20+
dList.change_eq_first(12, 17);
21+
} catch(const std::underflow_error &e){
22+
std::cout << "change_eq_first() throw an exception PASSED: " << e.what() << std::endl;
23+
}
24+
try{
25+
dList.change_eq_last(12, 17);
26+
} catch(const std::underflow_error &e){
27+
std::cout << "change_eq_last() throw an exception PASSED: " << e.what() << std::endl;
28+
}
29+
try{
30+
dList.change_eq_all(12, 17);
31+
} catch(const std::underflow_error &e){
32+
std::cout << "change_eq_all() throw an exception PASSED: " << e.what() << std::endl;
33+
}
34+
try{
35+
dList.front();
36+
} catch(const std::underflow_error &e){
37+
std::cout << "front() throw an exception PASSED: " << e.what() << std::endl;
38+
}
39+
try{
40+
dList.back();
41+
} catch(const std::underflow_error &e){
42+
std::cout << "back() throw an exception PASSED: " << e.what() << std::endl;
43+
}
44+
try{
45+
dList.contains(12);
46+
} catch(const std::underflow_error &e){
47+
std::cout << "contains() throw an exception PASSED: " << e.what() << std::endl;
48+
}
49+
dList.print();
50+
dList.print_reverse();
51+
dList.push_front(5); // 5
52+
assert(dList.empty() == false);
53+
assert(dList.size() == 1);
54+
assert(dList.front() == 5);
55+
assert(dList.back() == 5);
56+
assert(dList.contains(5) == 0);
57+
dList.reverse();
58+
assert(dList.empty() == false);
59+
assert(dList.size() == 1);
60+
assert(dList.front() == 5);
61+
assert(dList.back() == 5);
62+
assert(dList.contains(5) == 0);
63+
dList.pop_front(); // null
64+
assert(dList.empty() == true);
65+
assert(dList.size() == 0);
66+
dList.push_back(5); // 5
67+
assert(dList.empty() == false);
68+
assert(dList.size() == 1);
69+
assert(dList.front() == 5);
70+
assert(dList.back() == 5);
71+
assert(dList.contains(5) == 0);
72+
dList.pop_back(); // null
73+
assert(dList.empty() == true);
74+
assert(dList.size() == 0);
75+
dList.push_back(5); // 5
76+
assert(dList.empty() == false);
77+
assert(dList.size() == 1);
78+
assert(dList.front() == 5);
79+
assert(dList.back() == 5);
80+
assert(dList.contains(5) == 0);
81+
dList.push_front(9); // 9 5
82+
assert(dList.empty() == false);
83+
assert(dList.size() == 2);
84+
assert(dList.front() == 9);
85+
assert(dList.back() == 5);
86+
assert(dList.contains(5) == 1);
87+
dList.push_back(2); // 9 5 2
88+
assert(dList.empty() == false);
89+
assert(dList.size() == 3);
90+
assert(dList.front() == 9);
91+
assert(dList.back() == 2);
92+
assert(dList.contains(5) == 1);
93+
dList.push_back(1); // 9 5 2 1
94+
assert(dList.empty() == false);
95+
assert(dList.size() == 4);
96+
assert(dList.front() == 9);
97+
assert(dList.back() == 1);
98+
assert(dList.contains(2) == 2);
99+
dList.push_front(3); // 3 9 5 2 1
100+
assert(dList.empty() == false);
101+
assert(dList.size() == 5);
102+
assert(dList.front() == 3);
103+
assert(dList.back() == 1);
104+
assert(dList.contains(1) == 4);
14105
dList.push_at(2, 4); // 3 9 4 5 2 1
15106
assert(dList.size() == 6);
16107
assert(dList.empty() == false);
17-
std::cout << "empty() and size() test passed!" << std::endl;
18-
dList.pop_back();
108+
try{
109+
dList.push_at(7, 19);
110+
} catch(const std::out_of_range &e){
111+
std::cout << "push_at() throw an exception PASSED: " << e.what() << std::endl;
112+
}
113+
dList.push_at(0, 13); // 13 3 9 4 5 2 1
114+
assert(dList.empty() == false);
115+
assert(dList.size() == 7);
116+
assert(dList.front() == 13);
117+
assert(dList.back() == 1);
118+
assert(dList.contains(13) == 0);
119+
dList.push_at(7, 42); // 13 3 9 4 5 2 1 42
120+
assert(dList.empty() == false);
121+
assert(dList.size() == 8);
122+
assert(dList.front() == 13);
123+
assert(dList.back() == 42);
124+
assert(dList.contains(42) == 7);
125+
try{
126+
dList.pop_at(8);
127+
} catch(const std::out_of_range &e){
128+
std::cout << "pop_at() throw an exception PASSED: " << e.what() << std::endl;
129+
}
130+
dList.pop_at(0); // 3 9 4 5 2 1 42
131+
assert(dList.empty() == false);
132+
assert(dList.size() == 7);
133+
assert(dList.front() == 3);
134+
assert(dList.back() == 42);
135+
assert(dList.contains(42) == 6);
136+
dList.pop_at(6); // 3 9 4 5 2 1
137+
assert(dList.empty() == false);
138+
assert(dList.size() == 6);
139+
assert(dList.front() == 3);
140+
assert(dList.back() == 1);
141+
assert(dList.contains(5) == 3);
142+
try{
143+
dList.change_at(7, 90);
144+
} catch(const std::out_of_range &e){
145+
std::cout << "change_at() throw an exception PASSED: " << e.what() << std::endl;
146+
}
147+
try{
148+
dList.get_at(6);
149+
} catch(const std::out_of_range& e){
150+
std::cout << "get_at() throw an exception PASSED: " << e.what() << std::endl;
151+
}
152+
dList.pop_back(); // 3 9 4 5 2
19153
dList.pop_front(); // 9 4 5 2
20154
assert(dList.size() == 4);
21155
dList.pop_at(2); // 9 4 2
22156
assert(dList.size() == 3);
23-
std::cout << "pop test passed!" << std::endl;
24157
dList.push_front(3);
25158
dList.push_back(1);
26159
dList.push_back(6);
@@ -41,11 +174,9 @@ int main(int argc, char* argv[]){
41174
assert(dList.get_at(4) == 2);
42175
assert(dList.get_at(5) == 1);
43176
assert(dList.get_at(6) == 5);
44-
std::cout << "check test passed!" << std::endl;
45177
dList.print();
46178
dList.print_reverse();
47179
dList.reverse(); // 5 1 2 7 4 9 3
48-
std::cout << "reverse test passed" << std::endl;
49180
assert(dList.get_at(0) == 5);
50181
assert(dList.get_at(1) == 1);
51182
assert(dList.get_at(2) == 2);

data-structures/DoublyLinkedList/include/DoublyLinkedList.h

+17-24
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void DoublyLinkedList<T>::push_back(const T &value){
8989
template <typename T>
9090
void DoublyLinkedList<T>::push_at(const size_t index, const T &value){
9191
if(index > _size){
92-
throw std::out_of_range("Index out of range");
92+
throw std::out_of_range("push_at(): Index out of range");
9393
}
9494
if(index == 0){
9595
push_front(value);
@@ -112,7 +112,7 @@ void DoublyLinkedList<T>::push_at(const size_t index, const T &value){
112112
template <typename T>
113113
void DoublyLinkedList<T>::pop_front(){
114114
if(empty()){
115-
throw std::underflow_error("This DoublyLinkedList is empty");
115+
throw std::underflow_error("pop_front(): This DoublyLinkedList is empty");
116116
}
117117
_Node *del = _head;
118118
if(_size == 1){
@@ -129,7 +129,7 @@ void DoublyLinkedList<T>::pop_front(){
129129
template <typename T>
130130
void DoublyLinkedList<T>::pop_back(){
131131
if(empty()){
132-
throw std::underflow_error("This DoublyLinkedList is empty");
132+
throw std::underflow_error("pop_back(): This DoublyLinkedList is empty");
133133
}
134134
_Node *del = _tail;
135135
if(_size == 1){
@@ -146,7 +146,7 @@ void DoublyLinkedList<T>::pop_back(){
146146
template <typename T>
147147
void DoublyLinkedList<T>::pop_at(const size_t index){
148148
if(index >= _size){
149-
throw std::out_of_range("Index out of range");
149+
throw std::out_of_range("pop_at(): Index out of range");
150150
}
151151
if(index == 0){
152152
pop_front();
@@ -167,7 +167,7 @@ void DoublyLinkedList<T>::pop_at(const size_t index){
167167
template <typename T>
168168
void DoublyLinkedList<T>::change_at(const size_t index, const T &new_value){
169169
if(index >= _size){
170-
throw std::out_of_range("Index out of range");
170+
throw std::out_of_range("change_at(): Index out of range");
171171
}
172172
_Node *pos = get_node(index);
173173
pos->_value = new_value;
@@ -176,38 +176,31 @@ void DoublyLinkedList<T>::change_at(const size_t index, const T &new_value){
176176
template <typename T>
177177
void DoublyLinkedList<T>::change_eq_first(const T &value, const T &new_value){
178178
if(empty()){
179-
throw std::underflow_error("This DoublyLinkedList is empty");
179+
throw std::underflow_error("change_eq_first(): This DoublyLinkedList is empty");
180180
}
181181
size_t index = search(value, true);
182-
if(index == std::numeric_limits<size_t>::max()){
183-
throw std::underflow_error("Value not found in the list");
184-
}
185-
else{
182+
if(index != std::numeric_limits<size_t>::max()){
186183
_Node *pos = get_node(index);
187184
pos->_value = new_value;
188-
}
189-
return;
185+
}
190186
}
191187

192188
template <typename T>
193189
void DoublyLinkedList<T>::change_eq_last(const T &value, const T &new_value){
194190
if(empty()){
195-
throw std::underflow_error("This DoublyLinkedList is empty");
191+
throw std::underflow_error("change_eq_last(): This DoublyLinkedList is empty");
196192
}
197193
size_t index = search(value, false);
198-
if(index == std::numeric_limits<size_t>::max()){
199-
throw std::underflow_error("Value not found in the list");
200-
} else{
194+
if(index != std::numeric_limits<size_t>::max()){
201195
_Node *pos = get_node(index);
202196
pos->_value = new_value;
203197
}
204-
return;
205198
}
206199

207200
template <typename T>
208201
void DoublyLinkedList<T>::change_eq_all(const T &value, const T &new_value){
209202
if(empty()){
210-
throw std::underflow_error("This DoublyLinkedList is empty");
203+
throw std::underflow_error("change_eq_all(): This DoublyLinkedList is empty");
211204
}
212205
_Node *curr = _head;
213206
while(curr){
@@ -240,19 +233,19 @@ void DoublyLinkedList<T>::reverse(){
240233
template <typename T>
241234
T& DoublyLinkedList<T>::front(){
242235
if(_head) {return _head->_value;}
243-
throw std::underflow_error("This DoublyLinkedList is empty");
236+
throw std::underflow_error("front(): This DoublyLinkedList is empty");
244237
}
245238

246239
template <typename T>
247240
T& DoublyLinkedList<T>::back(){
248241
if(_tail) {return _tail->_value;}
249-
throw std::underflow_error("This DoublyLinkedList is empty");
242+
throw std::underflow_error("back(): This DoublyLinkedList is empty");
250243
}
251244

252245
template <typename T>
253246
T& DoublyLinkedList<T>::get_at(const size_t index){
254247
if(index >= _size){
255-
throw std::out_of_range("Index out of range");
248+
throw std::out_of_range("get_at(): Index out of range");
256249
}
257250
_Node *pos = get_node(index);
258251
return pos->_value;
@@ -261,15 +254,15 @@ T& DoublyLinkedList<T>::get_at(const size_t index){
261254
template <typename T>
262255
size_t DoublyLinkedList<T>::contains(const T &value) const{
263256
if(empty()){
264-
throw std::underflow_error("This DoubltLinkedList is empty");
257+
throw std::underflow_error("contains(): This DoubltLinkedList is empty");
265258
}
266259
return search(value, true);
267260
}
268261

269262
template <typename T>
270263
void DoublyLinkedList<T>::print() const{
271264
if(empty()){
272-
std::cout << "This DoublyLinkedList is empty" << std::endl;
265+
std::cout << "print(): This DoublyLinkedList is empty" << std::endl;
273266
return;
274267
}
275268
_Node *curr = _head;
@@ -282,7 +275,7 @@ void DoublyLinkedList<T>::print() const{
282275
template <typename T>
283276
void DoublyLinkedList<T>::print_reverse() const{
284277
if(empty()){
285-
std::cout << "This DoublyLinkedList is empty" << std::endl;
278+
std::cout << "print_reverse(): This DoublyLinkedList is empty" << std::endl;
286279
return;
287280
}
288281
_Node *curr = _tail;

0 commit comments

Comments
 (0)