-
Notifications
You must be signed in to change notification settings - Fork 6
/
basic_test.cpp
executable file
Β·205 lines (172 loc) Β· 5.12 KB
/
basic_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "gtest/gtest.h"
#include <cmath>
#include <iostream>
#include <iomanip>
#include <set>
#include <vector>
#include <list>
//------------------------------------------------------------------------------
//Files we are testing:
#include "../../includes/node/node.h"
#include "../../includes/linked_list_functions/linked_list_functions.h"
#include "../../includes/list_iterated/list_iterated.h"
//------------------------------------------------------------------------------
using namespace std;
//------------------------------------------------------------------------------
// COPY BASIC_TEST INTO THIS FILE.
// AND THEN,
// DO NOT EDIT THIS FILE ANY FURTHER
//------------------------------------------------------------------------------
bool basic_test(bool debug = false)
{
List<int> list;
// check list size
cout << "list.size(): " << list.size() << endl;
cout << "list.empty(): " << boolalpha << list.empty() << endl << endl;
// insert items
for (int i = 1; i < 6; i++)
{
list.insert_head(i);
list.insert_after(i * 10, list.begin());
list.insert_before(i * 100, list.begin());
cout << list;
}
cout << endl;
// check copy constructor
List<int> list2(list);
for (int i=0; i<list2.size(); i++) {
if (list[i] != list2[i]) {
cout << "copy constructor is wrong" << endl << endl;
break;
}
}
cout << "copy constructor is ok" << endl << endl;
List<int>::Iterator it;
// found non-existing node
it = list.search(99);
if (it) {
cout << "found " << *it << endl;
} else {
cout << "not found 99" << endl;
}
// found existing node
it = list.search(4);
if (it) {
cout << "found " << *it << endl;
} else {
cout << "not found 4" << endl;
}
cout << endl;
// compare two iterators
List<int>::Iterator it_2 = list.search(4);
if (it == it_2) {
cout << "iterators point to same node" << endl;
} else {
cout << "iterators point to different node" << endl;
}
it_2 = list.search(300);
if (it != it_2) {
cout << "iterators point to different node" << endl;
} else {
cout << "iterators point to same node" << endl;
}
cout << endl;
// check previous node and delete it
it = list.prev(it);
cout << "previous to 4: " << *it << endl;
list.Delete(it);
cout << "deleted the prev : " << list << endl;
// check it++ and ++it
cout << "it++ is: " << *(it_2++) << endl;
cout << "++it is: " << *(++it_2) << endl;
cout << endl;
// traverse list
cout << "iterating all the nodes by *it" << endl;
for (it = list.begin(); it != list.end(); it++)
{
cout << *it << " ";
}
cout << endl;
cout << "iterating all the nodes by list[i]" << endl;
for (int i=0; i<list.size(); i++)
{
cout << list[i] << " ";
}
cout << endl << endl;
list2 = list;
for (int i=0; i<list2.size(); i++) {
if (list[i] != list2[i]) {
cout << "assign operator is wrong" << endl << endl;
break;
}
}
cout << "assign operator is ok" << endl << endl;
// check list size
cout << "list.size(): " << list.size() << endl;
cout << "list.empty(): " << boolalpha << list.empty() << endl;
cout << "\n\n--------- D O N E ----------------" << endl << endl;
return true;
}
//Lord help me!
bool debug = false;
TEST(ITERATED_LIST, TestInsertHead)
{
bool success = basic_test(debug);
EXPECT_EQ(success, true);
}
int main(int argc, char **argv)
{
if (argc>1){
debug = argv[1][0]=='t';
}
::testing::InitGoogleTest(&argc, argv);
std::cout<<"\n\n----------running basic_test.cpp---------\n\n"<<std::endl;
return RUN_ALL_TESTS();
}
/*
build git:(master) π $> tree ../includes
../includes
βββ linked_list_functions
β βββ linked_list_functions.h
βββ list_iterated
β βββ list_iterated.h
βββ node
βββ node.h
3 directories, 3 files
build git:(master) π $> ./bin/basic_test
----------running basic_test.cpp---------
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from ITERATED_LIST
[ RUN ] ITERATED_LIST.TestInsertHead
list.size(): 0
list.empty(): true
[100]->[1]->[10]->|||
[200]->[2]->[20]->[100]->[1]->[10]->|||
[300]->[3]->[30]->[200]->[2]->[20]->[100]->[1]->[10]->|||
[400]->[4]->[40]->[300]->[3]->[30]->[200]->[2]->[20]->[100]->[1]->[10]->|||
[500]->[5]->[50]->[400]->[4]->[40]->[300]->[3]->[30]->[200]->[2]->[20]->[100]->[1]->[10]->|||
copy constructor is ok
not found 99
found 4
iterators point to same node
iterators point to different node
previous to 4: 400
deleted the prev : [500]->[5]->[50]->[4]->[40]->[300]->[3]->[30]->[200]->[2]->[20]->[100]->[1]->[10]->|||
it++ is: 300
++it is: 30
iterating all the nodes by *it
500 5 50 4 40 300 3 30 200 2 20 100 1 10
iterating all the nodes by list[i]
500 5 50 4 40 300 3 30 200 2 20 100 1 10
assign operator is ok
list.size(): 14
list.empty(): false
--------- D O N E ----------------
[ OK ] ITERATED_LIST.TestInsertHead (0 ms)
[----------] 1 test from ITERATED_LIST (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[ PASSED ] 1 test.
build git:(master) π $>
*/