-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestVec.cc
More file actions
274 lines (255 loc) · 7.2 KB
/
testVec.cc
File metadata and controls
274 lines (255 loc) · 7.2 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
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
// Test program for the ECE4893/8893 Assignment 3
// Fall 2010
// George F. Riley
#include <iostream>
#include <sstream>
#include <stdlib.h>
// Include our Vector class definition and implementation
#include "Vector.h"
#include "Vector.cc"
#include "String.h"
using namespace std;
void Test1()
{ // Just create a vector with default constructor and add
// elements with Push_Back and Push_Front. Then retrieve
// with only the indexing operator. Allows destructor to clean up
cout << "Starting Test1" << endl;
Vector<String> v;
for (int i = 0; i < 50; ++i)
{
ostringstream oss;
oss << "Hello from string back " << i;
v.Push_Back(String(oss.str().c_str()));
}
for (int i = 0; i < 50; ++i)
{
ostringstream oss;
oss << "Hello from string front " << i;
v.Push_Front(String(oss.str().c_str()));
}
// Print out results
cout << "Test1 results" << endl;
for (size_t i = 0; i < v.Size(); ++i)
{
cout << v[i] << endl;
}
}
void Test2()
{ // Just create a vector with default constructor and add
// elements with Push_Back and Push_Front. Then retrieve
// with only the Back() function and Pop_Back. Also
// tests "Empty"
cout << "Starting Test2" << endl;
Vector<String> v;
for (int i = 0; i < 50; ++i)
{
ostringstream oss;
oss << "Hello from string back " << i;
String tmp(oss.str().c_str());
v.Push_Back(String(oss.str().c_str()));
}
for (int i = 0; i < 50; ++i)
{
ostringstream oss;
oss << "Hello from string front " << i;
v.Push_Front(String(oss.str().c_str()));
}
// Print out results
cout << "Test2 results" << endl;
while (!v.Empty())
{
String st = v.Back();
v.Pop_Back();
cout << st << endl;
}
}
void Test3()
{ // Just create a vector with default constructor and add
// elements with Push_Back and Push_Front. Then retrieve
// with only the front() function and Pop_Front. Also
// tests "Empty"
cout << "Starting Test3" << endl;
Vector<String> v;
for (int i = 0; i < 50; ++i)
{
ostringstream oss;
oss << "Hello from string back " << i;
v.Push_Back(String(oss.str().c_str()));
}
for (int i = 0; i < 50; ++i)
{
ostringstream oss;
oss << "Hello from string front " << i;
v.Push_Front(String(oss.str().c_str()));
}
// Print out results
cout << "Test3 results" << endl;
while (!v.Empty())
{
String st = v.Front();
v.Pop_Front();
cout << st << endl;
}
}
void Test4()
{ // Same as test1 except tests the copy constructor and clear
cout << "Starting Test4" << endl;
Vector<String> v;
for (int i = 0; i < 50; ++i)
{
ostringstream oss;
oss << "Hello from string back " << i;
v.Push_Back(String(oss.str().c_str()));
}
for (int i = 0; i < 50; ++i)
{
ostringstream oss;
oss << "Hello from string front " << i;
v.Push_Front(String(oss.str().c_str()));
}
// Now make a copy of the vector
Vector<String> v1(v);
// Print out results
cout << "Test4 results" << endl;
for (size_t i = 0; i < v1.Size(); ++i)
{
cout << v1[i] << endl;
}
// Clear the vector and print again
v.Clear();
cout << "Test4 results again, should be Empty" << endl;
for (size_t i = 0; i < v.Size(); ++i)
{
cout << v[i] << endl;
}
}
void Test5()
{// Create a vector the print out using iterators
cout << "Starting Test5" << endl;
Vector<String> v;
for (int i = 0; i < 50; ++i)
{
ostringstream oss;
oss << "Hello from string back " << i;
v.Push_Back(String(oss.str().c_str()));
}
for (int i = 0; i < 50; ++i)
{
ostringstream oss;
oss << "Hello from string front " << i;
v.Push_Front(String(oss.str().c_str()));
}
// Print out results
cout << "Test5 results" << endl;
VectorIterator<String> it = v.Begin();
while(it != v.End())
{
cout << *it++ << endl;
}
}
void Test6()
{ // test indexing operator on left-hand-size of assignment
cout << "Starting Test6" << endl;
Vector<String> v;
for (int i = 0; i < 50; ++i)
{
ostringstream oss;
oss << "Hello from string back " << i;
v.Push_Back(String(oss.str().c_str()));
}
// Now use indexing operator to access (and chnage) elements
for (size_t i = 0; i < v.Size(); ++i)
{
ostringstream oss;
oss << "Hello from string replaced " << i;
v[i] = String(oss.str().c_str());
}
// And print results
cout << "Test6 results" << endl;
for (size_t i = 0; i < v.Size(); ++i)
{
cout << v[i] << endl;
}
}
#ifdef GRAD_STUDENT
void Test7()
{ // Test insert and erase (grad students only)
cout << "Starting Test7" << endl;
Vector<String> v;
for (int i = 0; i < 50; ++i)
{
ostringstream oss;
oss << "Hello from string back " << i;
v.Push_Back(String(oss.str().c_str()));
}
for (int i = 0; i < 50; ++i)
{
ostringstream oss;
oss << "Hello from string front " << i;
v.Push_Front(String(oss.str().c_str()));
}
// Now find the 25th element and insert a new string before
VectorIterator<String> it;
VectorIterator<String> it1 = v.End();;
size_t i = 0;
for (it = v.Begin(); it != v.End(); ++it)
{
if (++i == 25) it1 = it; // it1 is an iterator to 25th element
}
v.Insert(String("Inserted element before 25th"), it1);
// Now print out using iterators
cout << "Test7 results" << endl;
VectorIterator<String> it2 = v.End();
size_t i2 = 0;
for (VectorIterator<String> it = v.Begin(); it != v.End(); ++it)
{
cout << *it << endl;
if (++i2 == 10) it2 = it; // Iterator for 10th element
}
// Erase 10th and print again
v.Erase(it2);
cout << "Test7 results again, should be missing 10th element" << endl;
for (VectorIterator<String> it = v.Begin(); it != v.End(); ++it)
{
cout << *it << endl;
}
}
void Test8()
{ // Grad students only, test special constructor
cout << "Starting Test8" << endl;
Vector<String> v(50, String("Replicated Constructor"));
// And print out
cout << "Test8 results" << endl;
for (size_t i = 0; i < v.Size(); ++i)
{
cout << v[i] << endl;
}
// Also test reserve
Vector<String> v1;
v1.Reserve(10);
for (int i = 0; i < 50; ++i)
{
ostringstream oss;
oss << "Hello from string back " << i;
v1.Push_Back(String(oss.str().c_str()));
}
// No need t print out; the constructor counts will verify correctness
}
#endif
int main(int argc, char** argv)
{ // If second arg specified, it is the test number to run
// if not specified, run all
int testNum = -1;
if (argc > 1) testNum = atol(argv[1]);
String::ClearCounts();
if (testNum < 0 || testNum == 1) { Test1(); String::PrintCounts();}
if (testNum < 0 || testNum == 2) { Test2(); String::PrintCounts();}
if (testNum < 0 || testNum == 3) { Test3(); String::PrintCounts();}
if (testNum < 0 || testNum == 4) { Test4(); String::PrintCounts();}
if (testNum < 0 || testNum == 5) { Test5(); String::PrintCounts();}
if (testNum < 0 || testNum == 6) { Test6(); String::PrintCounts();}
#ifdef GRAD_STUDENT
if (testNum < 0 || testNum == 7) { Test7(); String::PrintCounts();}
if (testNum < 0 || testNum == 8) { Test8(); String::PrintCounts();}
#endif
}