forked from ZenanZha/btp500-f16
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha1q2main.cpp
152 lines (133 loc) · 3.49 KB
/
a1q2main.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
/*********************************************************************/
/* */
/* Tester for DSA555 assignment 1, question 2 */
/* Version 1.0 */
/* To compile: */
/* */
/* g++ a1q2main.cpp -std=c++0x */
/* */
/*********************************************************************/
#include "stack.h"
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std;
struct Record{
char word_[30];
int count_;
};
ostream& operator<<(ostream& os, const Record rec){
os << rec.word_;
return os;
}
bool operator==(const Record& a,const Record& b){
bool rc=false;
if(strcmp(a.word_,b.word_)==0 && a.count_==b.count_){
rc=true;
}
return rc;
}
bool operator!=(const Record& a,const Record& b){
return !(a==b);
}
bool operator <(const Record& a, const Record& b){
bool rc=false;
if(strcmp(a.word_,b.word_) < 0){
rc=true;
}
return rc;
}
template <typename T>
void addBack(const T& data,T arr[],int sz);
int main(void){
Record data[20]={
{"the",1},
{"quick",2},
{"brown ",3},
{"fox",4},
{"jumped",5},
{"over",6},
{"lazy",7},
{"dog",8},
{"Calvin",9},
{"and",10},
{"Hobbes",11},
{"night",12},
{"watch",13},
{"captain",14},
{"carrot",15},
{"lilac",16},
{"lavender",17},
{"lily",18},
{"coffee",19},
{"tea",20}
};
//these array will mirror what happens to LL
Record mirror[20];
Stack<Record> theStack;
bool passtest=true;
int numPassed=0;
int mirrorCount=0;
/* Test constructors, begin and end functions*/
cout << "test 1: create stack and check that it is empty" << endl;
if(passtest){
if(!theStack.isEmpty()){
passtest=false;
cout << "Error 1: isEmpty() not returning true on empty stack" << endl;
}
}
if(passtest){
numPassed++;
cout << "test 2: perform some push() calls, and check the top()" << endl;
for(int i=0;i<5;i++){
theStack.push(data[i]);
addBack(data[i],mirror,i);
mirrorCount++;
if(theStack.isEmpty() || theStack.top() != mirror[i] ){
passtest=false;
cout << "Error 2: data at top does not match most recently pushed data" << endl;
}
}
}
if(passtest){
numPassed++;
cout << "test 3: Test the pop() function" << endl;
for(int i=0;passtest && i<4;i++){
mirrorCount--;
theStack.pop();
if(theStack.isEmpty() || theStack.top() != mirror[mirrorCount-1]){
passtest=false;
cout << "Error 3: data at top does not match most recently pushed data" << endl;
cout << "what should be at top: "<< endl;
cout << mirror[mirrorCount-1];
cout << "what you have at the top: " << endl;
cout << theStack.top();
}
}
}
if(passtest){
numPassed++;
cout << "test 4: remove last item and ensure list is empty" << endl;
mirrorCount--;
theStack.pop();
if(!theStack.isEmpty()){
passtest=false;
cout << "Error 4: list should be empty but it is not" << endl;
}
}
if(passtest){
numPassed++;
}
if(numPassed == 4){
cout << "Testing for Assignment 1 Question 2 completed successfully" << endl;
}
else{
cout << numPassed << " / 4 tests passed. Looks like you still" << endl;
cout << "have some work to do" << endl;
}
return 0;
}
template <typename T>
void addBack(const T& data,T arr[],int sz){
arr[sz]=data;
}