generated from CSUEB-CS301/UnsortedList-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestDriver.cpp
More file actions
146 lines (130 loc) · 3.44 KB
/
TestDriver.cpp
File metadata and controls
146 lines (130 loc) · 3.44 KB
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
// Test driver
#include <iostream>
#include <fstream>
#include <string>
#include "UnsortedList.h"
using namespace std;
void PrintList(ofstream& outFile, UnsortedList<int>& list);
int main()
{
ifstream inFile; // file containing operations
ofstream outFile; // file containing output
string inFileName; // input file external name
string outFileName; // output file external name
string outputLabel;
string command; // operation to be executed
int number;
int item;
UnsortedList<int> list;
bool found;
// Prompt for file names, read file names, and prepare files
cout << "Enter name of input command file: ";
cin >> inFileName;
inFile.open(inFileName);
cout << "Enter name of output file: ";
cin >> outFileName;
outFile.open(outFileName);
cout << "Enter name of test run: ";
cin >> outputLabel;
outFile << outputLabel << endl;
if (!inFile)
{
cout << "File not found." << endl;
exit(2);
}
inFile >> command;
//cin >> command; // use cin instead of inFile if you want to do command line testing instead
int numCommands = 0;
while (command != "Quit")
{
//cout << "Command: " << command;
if (command == "PutItem")
{
inFile >> item;
//cin >> item;
list.PutItem(item);
cout << item;
cout << " is in list." << endl;
outFile << item;
outFile << " is in list." << endl;
}
else if (command == "DeleteItem")
{
inFile >> item;
//cin >> item;
list.DeleteItem(item);
cout << item;
cout << " is deleted." << endl;
outFile << item;
outFile << " is deleted." << endl;
}
else if (command == "Contains")
{
inFile >> item;
//cin >> item;
if (list.Contains(item)) {
cout << item << " found in list." << endl;
outFile << item << " found in list." << endl;
}
else {
cout << item << " not in list." << endl;
outFile << item << " not in list." << endl;
}
}
else if (command == "GetLength") {
cout << "Length is " << list.GetLength() << endl;
outFile << "Length is " << list.GetLength() << endl;
}
else if (command == "IsFull") {
if (list.IsFull()) {
cout << "List is full." << endl;
outFile << "List is full." << endl;
} else {
cout << "List is not full." << endl;
outFile << "List is not full." << endl;
}
}
else if (command == "MakeEmpty") {
cout << "Make list empty." << endl;
outFile << "Make list empty." << endl;
list.MakeEmpty();
}
else if (command == "PrintList") {
PrintList(outFile, list);
}
else {
cout << " Command not recognized." << endl;
}
numCommands++;
inFile >> command;
//cin >> command;
};
cout << "Quit" << endl << "Testing completed." << endl;
inFile.close();
outFile.close();
return 0;
};
void PrintList(ofstream& dataFile, UnsortedList<int>& list)
// Pre: list has been initialized.
// dataFile is open for writing.
// Post: Each component in list has been written to dataFile.
// dataFile is still open.
{
int length;
int item;
dataFile << "PrintList" << endl;
cout << "PrintList: ";
list.ResetIterator(); // Sets currentPos = -1
length = list.GetLength();
if (length == 0)
dataFile << "List is empty.";
else
for (int counter = 0; counter < list.GetLength(); counter++)
{
item = list.GetNextItem();
dataFile << item << " ";
cout << item << " ";
}
dataFile << endl;
cout << endl;
}