/
Driver.cs
262 lines (217 loc) · 9.39 KB
/
Driver.cs
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
// Console Test Driver
//
// Test Program for Lab3
//
// Created by Jim Bailey on 4/20/17.
// Licensed under a Creative Commons Attribution 4.0 International License.
//
//
// Transpiled into C# by Katie Strauss 11/4/2019
using LinkedList;
using System;
using System.Linq;
using static LinkedList.StudentList;
using static LinkedList.TextClass;
namespace Lab3LinkedList
{
class Driver
{
static void Main(string[] args)
{
// uncomment function to select a given test
TextClassTest();
IterTest();
AppendTest();
StudentListTest();
Console.Write("\nAll done");
Console.Write("\nPress Enter to exit console");
Console.Read();
}
// testing TextClass
static void TextClassTest()
{
// testing TextClass
Console.Write("\nTesting TextClass\n\n");
MyLinkedList textList1 = new MyLinkedList();
// check for add and delete head
textList1.InsertHead('a');
textList1.InsertHead('b');
textList1.InsertHead('c');
Console.Write("\nTesting Insert and delete head, output s/b cba -- is: ");
Console.Write(textList1.DeleteHead());
Console.Write(textList1.DeleteHead());
Console.Write(textList1.DeleteHead() + "\n");
Console.Write("Testing delete head from an empty list: ");
try
{
textList1.DeleteHead();
Console.Write("Failed to throw exception\n");
}
catch (InvalidOperationException err) {
Console.Write("Caught error: " + err.Message + "\n");
}
catch (Exception) {
Console.Write("Caught something other than InvalidOperation\n");
}
// check for add and delete tail
textList1.InsertTail('x');
textList1.InsertTail('y');
textList1.InsertTail('z');
Console.Write("\nTesting Insert and delete tail, output s/b zyx -- is: ");
Console.Write(textList1.DeleteTail());
Console.Write(textList1.DeleteTail());
Console.Write(textList1.DeleteTail() + "\n");
Console.Write("Testing delete tail from an empty list: ");
try
{
textList1.DeleteTail();
Console.Write("Failed to throw exception\n");
}
catch (InvalidOperationException err) {
Console.Write("Caught error: " + err.Message + "\n");
}
catch (Exception) {
Console.Write("Caught something other than InvalidOperation\n");
}
// check for add tail, delete head
textList1.InsertTail('x');
textList1.InsertTail('y');
textList1.InsertTail('z');
Console.Write("\nTesting Insert tail and delete head, output s/b xyz -- is: ");
Console.Write(textList1.DeleteHead());
Console.Write(textList1.DeleteHead());
Console.Write(textList1.DeleteHead() + "\n");
}
static void IterTest()
{
MyLinkedList textList2 = new MyLinkedList();
Console.Write("\nTesting Iter methods \n\n");
// testing iter methods
textList2.InsertTail('0');
textList2.InsertTail('3');
textList2.InsertTail('5');
textList2.InsertTail('3');
// iter find
Console.Write("\nTesting Iter find, should find 3 and 5 and not find 4\n");
Console.Write(((textList2.FindKey('3')) ? " found 3" : " did not find 3") + "\n");
Console.Write(((textList2.FindKey('4')) ? " found 4" : " did not find 4") + "\n");
Console.Write(((textList2.FindKey('5')) ? " found 5" : " did not find 5") + "\n");
// iter Insert
Console.Write("\nTesting Insert, starting string should be 0353, is: " + textList2.DisplayList() + "\n");
textList2.FindKey('3');
textList2.InsertKey('1');
textList2.FindKey('3');
textList2.InsertKey('6');
textList2.FindKey('3');
textList2.InsertKey('2');
textList2.FindKey('4');
textList2.InsertKey('7');
Console.Write("Ending string should be 0123563, is: " + textList2.DisplayList() + "\n");
//iter delete
textList2.FindKey('5');
textList2.DeleteIter();
Console.Write("\nTesting DeleteIter, ending string should be 012363, is: " + textList2.DisplayList() + "\n");
// delete key
textList2.DeleteKey('6');
Console.Write("\nTesting DeleteKey, ending string should be 01233, is: " + textList2.DisplayList() + "\n");
//changing a string
MyLinkedList stringList = new MyLinkedList();
string stringOne = "That is a test";
int lengthOne = stringOne.Count();
for (int i = 0; i < lengthOne; i++)
{
stringList.InsertTail(stringOne[i]);
}
Console.Write("\nString list starts as: " + stringList.DisplayList() + "\n");
stringList.FindKey('i');
stringList.InsertKey('w');
stringList.InsertKey('a');
stringList.DeleteIter();
Console.Write("After changing 'i' to 'wa', list now reads: " + stringList.DisplayList() + "\n");
}
static void AppendTest()
{
//Testing append
Console.Write("\nTesting append\n\n");
MyLinkedList catList = new MyLinkedList();
string stringCat = "This is a cat";
int lengthCat = stringCat.Count();
for (int i = 0; i < lengthCat; i++)
{
catList.InsertTail(stringCat[i]);
}
Console.Write("Cat list starts as: " + catList.DisplayList() + "\n");
MyLinkedList dogList = new MyLinkedList();
string stringDog = "That is a dog";
int lengthDog = stringDog.Count();
for (int i = 0; i < lengthDog; i++)
{
dogList.InsertTail(stringDog[i]);
}
Console.Write("Dog list starts as: " + dogList.DisplayList() + "\n");
//catList.AppendList(dogList);
MyLinkedList.AppendList(catList,ref dogList);
Console.Write("\nAfter append, cat list is: " + catList.DisplayList() + "\n");
catList.FindKey('T');
catList.FindKey('T');
catList.InsertKey(' ');
catList.InsertKey('a');
catList.InsertKey('n');
catList.InsertKey('d');
catList.InsertKey(' ');
catList.InsertKey('t');
catList.DeleteIter();
Console.Write("After changes, cat list is: " + catList.DisplayList() + "\n");
Console.Write("\nDog list should be unchanged and is: " + dogList.DisplayList() + "\n");
}
static void StudentListTest()
{
//Testing student list functions
Console.Write("\nTesting StudentList\n\n");
// create a list to play with
StudentList sList = new StudentList();
// create some students
Student frodo = new Student("Frodo", 50);
Student bilbo = new Student("Bilbo", 111);
Student gandalf = new Student("Gandalf", 500);
Student pippen = new Student("Pippen", 30);
Student sam = new Student("Samwise", 40);
// load the list
sList.InsertHead(frodo);
sList.InsertTail(bilbo);
sList.InsertHead(gandalf);
sList.InsertTail(pippen);
sList.InsertHead(sam);
// check empty
Console.Write("Checking IsEmpty, should not be empty, is: " + (sList.IsEmpty() ? "empty" : "not empty") + "\n");
// check find
Console.Write("Checking find with Sam, should find: " + (sList.FindKey("Samwise") ? "found" : "not found") + "\n");
Console.Write("Checking find with Merry, should not find: " + (sList.FindKey("Merry") ? "found" : "not found") + "\n");
// check DeleteKey
Console.Write("Checking DeleteKey with Pippen, should find: " + (sList.DeleteKey("Pippen") ? "found" : "not found") + "\n");
Console.Write("Checking DeleteKey with Merry, should not find: " + (sList.DeleteKey("Merry") ? "found" : "not found") + "\n");
// check DeleteHead
Console.Write("Checking delete head, should show in order: Samwise Gandalf Frodo Bilbo\n");
Console.Write("Actually showed: ");
while (true)
{
try
{
Student temp = sList.DeleteHead();
Console.Write(temp.Name + " ");
}
catch (InvalidOperationException err ) {
Console.Write("\nCaught error: " + err.Message + "\n");
break;
}
catch (Exception)
{
Console.Write("\nCaught something other than underflow\n");
break;
}
}
// checkikng empty again
Console.Write("Checking IsEmpty again, should now be empty, is: " + (sList.IsEmpty() ? "empty" : "not empty") + "\n");
}
}
}