-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
281 lines (243 loc) · 9.24 KB
/
main.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* @file main.cpp
* University of Illinois CS 400, MOOC 2, Week 1: Linked Lists
*
* @author Eric Huber
**/
#include <iostream>
#include <string>
#include "LinkedList.h"
#include "LinkedListExercises.h"
// -----------------------------------------------------------------------
// Function prototypes: When global functions are defined later in the
// compilation unit than they are called, then at least these function
// prototype header lines must be written before the call.
// Some informal unit tests. Defined at the bottom of main.cpp
void informalTests();
// Helper function used by informalTests. Defined at the bottom of main.cpp
void errorReaction(std::string msg);
// -----------------------------------------------------------------------
// main program: You can do whatever you want to test your code here.
// See also the test suite that you can build with "make test".
int main() {
informalTests();
return 0;
}
// ------------------------------------------------------------------------
// Choose what to do if an informal test has a big problem.
void errorReaction(std::string msg) {
// If you want to throw an exception and stop the program immediately:
// throw std::runtime_error(msg);
// If you want to display a warning and keep going:
std::cout << std::endl << "WARNING: " << msg << std::endl << std::endl;;
}
// These tests aren't very exhaustive of the student exercise code.
// They are more of a sanity check on the LinkedList class itself.
void informalTests() {
std::cout << "Running a few informal tests. Feel free to study these or edit them." << std::endl;
std::cout << "The unit tests in the tests/ subdirectory are much more exhaustive!" << std::endl;
std::cout << "You can run those by doing: make clean && make test && ./test" << std::endl;
std::cout << "Remember that when you're ready to submit, you should do: make zip" << std::endl;
std::cout << "That will package the zip file properly for submission." << std::endl;
{
std::cout << std::endl << "Testing splitHalves():" << std::endl;
LinkedList<int> l;
l.pushBack(5);
l.pushBack(6);
l.pushBack(7);
l.pushBack(8);
l.pushBack(9);
std::cout << "List: " << l << std::endl;
std::cout << "Size: " << l.size() << std::endl;
LinkedList<LinkedList<int>> halves = l.splitHalves();
std::cout << "Front half: " << halves.front()
<< " Back half: " << halves.back() << std::endl;
LinkedList<int> expectedFrontHalf;
expectedFrontHalf.pushBack(5);
expectedFrontHalf.pushBack(6);
expectedFrontHalf.pushBack(7);
std::cout << "Expected front half: " << expectedFrontHalf << std::endl;
if (expectedFrontHalf != halves.front()) errorReaction("front half is wrong");
LinkedList<int> expectedBackHalf;
expectedBackHalf.pushBack(8);
expectedBackHalf.pushBack(9);
std::cout << "Expected back half: " << expectedBackHalf << std::endl;
if (expectedBackHalf != halves.back()) errorReaction("back half is wrong");
}
{
std::cout << std::endl << "Testing explode() and iterating with pointers:" << std::endl;
LinkedList<int> l;
l.pushBack(5);
l.pushBack(6);
l.pushBack(7);
l.pushBack(8);
l.pushBack(9);
std::cout << "Original list: " << l << std::endl;
auto lists = l.explode();
std::cout << "Exploded list of lists:" << std::endl;
std::cout << lists << std::endl;
std::cout << "Iterating through lists using pointers:" << std::endl;
auto nodePtr = lists.getHeadPtr();
while (nodePtr) {
auto list = nodePtr->data;
if (list.size() != 1) errorReaction("exploded list item should have size 1");
std::cout << "List: " << list << std::endl;
// std::cout << "Size: " << list.size() << std::endl << std::endl;
nodePtr = nodePtr->next;
}
// You can also avoid dealing with pointers entirely by using the
// push and pop interface to cycle through the list contents. (This is somewhat
// less efficient because it creates and destroys copies of the data throughout
// the process of shuffling the list.)
std::cout << "Fake iteration done by popping and pushing items:" << std::endl;
for (int i=0; i<lists.size(); i++) {
auto list = lists.front();
std::cout << "List: " << list << std::endl;
// std::cout << "Size: " << list.size() << std::endl << std::endl;
lists.popFront();
lists.pushBack(list);
}
}
{
std::cout << std::endl << "Testing insertOrdered:" << std::endl;
LinkedList<int> expected;
expected.pushBack(-1);
expected.pushBack(4);
expected.pushBack(5);
expected.pushBack(6);
expected.pushBack(7);
expected.pushBack(8);
expected.pushBack(9);
expected.pushBack(10);
LinkedList<int> l;
l.pushBack(5);
l.pushBack(6);
l.pushBack(8);
l.pushBack(9);
std::cout << "List: " << l << std::endl;
l.insertOrdered(7);
l.insertOrdered(4);
l.insertOrdered(-1);
l.insertOrdered(10);
std::cout << "Inserting items now." << std::endl;
std::cout << "List: " << l << std::endl;
std::cout << "Expected: " << expected << std::endl;
if (l != expected) errorReaction("wrong result");
std::cout << "Testing copy constructor:" << std::endl;
LinkedList<int> l2(l);
std::cout << "Copy: " << l2 << std::endl;
std::cout << "Expected: " << expected << std::endl;
if (l2 != expected) errorReaction("wrong result");
std::cout << "Checking equivalency:" << std::boolalpha << (l == l2) << std::endl;
}
{
std::cout << std::endl << "Testing insertionSort:" << std::endl;
LinkedList<int> l;
l.pushBack(10);
l.pushBack(4);
l.pushBack(7);
l.pushBack(5);
l.pushBack(8);
l.pushBack(6);
l.pushBack(-1);
l.pushBack(9);
std::cout << "(Before) List: " << l << std::endl;
auto l2 = l.insertionSort();
std::cout << "(After) List: " << l2 << std::endl;
std::cout << "isSorted: " << std::boolalpha << l2.isSorted() << std::endl;
LinkedList<int> expected;
expected.pushBack(-1);
expected.pushBack(4);
expected.pushBack(5);
expected.pushBack(6);
expected.pushBack(7);
expected.pushBack(8);
expected.pushBack(9);
expected.pushBack(10);
std::cout << "Expected: " << expected << std::endl;
if (l2 != expected) errorReaction("Bug in insertionSort (check insertOrdered first)");
if (l2 == expected && !l2.isSorted()) errorReaction("Bug in isSorted");
}
{
std::cout << std::endl << "Testing merge():" << std::endl;
LinkedList<int> l;
l.pushBack(1);
l.pushBack(3);
l.pushBack(5);
LinkedList<int> r;
r.pushBack(-1);
r.pushBack(2);
r.pushBack(10);
r.pushBack(20);
LinkedList<int> expected;
expected.pushBack(-1);
expected.pushBack(1);
expected.pushBack(2);
expected.pushBack(3);
expected.pushBack(5);
expected.pushBack(10);
expected.pushBack(20);
std::cout << "Left List: " << l << " ";
std::cout << "Right List: " << r << std::endl;
auto merged = l.merge(r);
std::cout << "Merged: " << merged << std::endl;
std::cout << "Expected: " << expected << std::endl;
if (merged != expected) errorReaction("Bug in merge");
}
{
std::cout << std::endl << "Testing mergeSortRecursive:" << std::endl;
LinkedList<int> l;
l.pushBack(10);
l.pushBack(4);
l.pushBack(7);
l.pushBack(5);
l.pushBack(8);
l.pushBack(6);
l.pushBack(-1);
l.pushBack(9);
LinkedList<int> expected;
expected.pushBack(-1);
expected.pushBack(4);
expected.pushBack(5);
expected.pushBack(6);
expected.pushBack(7);
expected.pushBack(8);
expected.pushBack(9);
expected.pushBack(10);
std::cout << "(Before) List: " << l << std::endl;
l = l.mergeSortRecursive();
std::cout << "(After) List: " << l << std::endl;
std::cout << "isSorted: " << std::boolalpha << l.isSorted() << std::endl;
std::cout << "Expected: " << expected << std::endl;
if (l != expected) errorReaction("Bug in mergeSortRecursive (check merge first)");
}
{
std::cout << std::endl << "Testing mergeSortIterative:" << std::endl;
LinkedList<int> l;
l.pushBack(10);
l.pushBack(4);
l.pushBack(-1);
l.pushBack(5);
l.pushBack(8);
l.pushBack(6);
l.pushBack(7);
l.pushBack(9);
LinkedList<int> expected;
expected.pushBack(-1);
expected.pushBack(4);
expected.pushBack(5);
expected.pushBack(6);
expected.pushBack(7);
expected.pushBack(8);
expected.pushBack(9);
expected.pushBack(10);
std::cout << "(Before) List: " << l << std::endl;
l = l.mergeSortIterative();
std::cout << "(After) List: " << l << std::endl;
std::cout << "isSorted: " << std::boolalpha << l.isSorted() << std::endl;
std::cout << "Expected: " << expected << std::endl;
if (l != expected) errorReaction("Bug in mergeSortIterative (check merge first)");
}
std::cout << "\n\nInformal tests finished without crashing, but the output may have been incorrect."
<< "\nPlease scroll to the top of the output to read the diagnostics." << std::endl;
}