forked from MiamiOH-CSE274/03_Queue_Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
98 lines (87 loc) · 2.88 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
#include "ArrayQueue.h"
#include <iostream>
//Check to make sure that numItems returns correct result on a fresh
// instance of the ArrayQueue class
void testCtor(ArrayQueue<int>& testQueue){
if(testQueue.getNumItems() == 0){
std::cout << "SUCCESS: Fresh queue has 0 items" << std::endl;
} else {
std::cout << "ERROR: Fresh queue should have 0 items, but has " << testQueue.getNumItems() << std::endl;
}
}
//Add and remove some items, making sure they come back in the
// correct order
void testAddRemove(ArrayQueue<int>& testQueue){
testQueue.add(5);
testQueue.add(10);
testQueue.add(4);
if(testQueue.getNumItems() == 3){
std::cout << "SUCCESS: 3 items added" << std::endl;
} else {
std::cout << "ERROR: Added 3 items, but getNumItems says " << testQueue.getNumItems() << std::endl;
return;
}
int x = testQueue.remove();
int y = testQueue.remove();
int z = testQueue.remove();
if(x != 5 || y != 10 || z != 4){
std::cout << "ERROR: Expected 5, 10, 4, but got " << x <<", " << y << ", " << "z" << std::endl;
} else {
std::cout << "SUCCESS: 3 added items came back out in the correct order" << std::endl;
}
}
//Test to see if your queue still works if we do add, remove, add, remove
// many times
void testAroundTheHorn(ArrayQueue<int>& testQueue){
for(int i=0;i<1000;i++){
testQueue.add(i);
int t = testQueue.remove();
if(t != i){
std::cout << "ERROR: Added " << i << " but got back " << t << std::endl;
return;
}
}
std::cout << "SUCCESS: Added and removed 1000 items successfully" << std::endl;
}
//Test to see if your queue can grow to handle lots of items
void testGrow(ArrayQueue<int>& testQueue){
for(int i=0;i<1000;i++){
testQueue.add(i);
}
if(testQueue.getNumItems() != 1000){
std::cout << "ERROR: Should have 1000 items in queue, but only found " << testQueue.getNumItems() << std::endl;
return;
}
for(int i=0;i<1000;i++){
int t = testQueue.remove();
if(t != i){
std::cout << "ERROR: Added " << i << " but got back " << t << std::endl;
return;
}
}
std::cout << "SUCCESS: Added 1000 items, then removed 1000" << std::endl;
}
//Test to make sure you are throwing an exception if remove is
// called improperly
void testRemoveException(ArrayQueue<int>& testQueue){
try {
int t = testQueue.remove();
} catch (std::string s) {
std::cout << "SUCCESS: Caught exception: " << s << std::endl;
return;
} catch (...) {
std::cout << "ERROR: Caught an exception, but it wasn't a string type" << std::endl;
return;
}
std::cout << "ERROR: Tried to remove from an empty queue, but did not get an exception" << std::endl;
}
//A simple main function which creates a queue, and tests it.
int main(){
ArrayQueue<int> testQueue;
testCtor(testQueue);
testAddRemove(testQueue);
testAroundTheHorn(testQueue);
testGrow(testQueue);
testRemoveException(testQueue);
return 0;
}