-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicArray.TestDriver.cpp
More file actions
373 lines (308 loc) · 11.7 KB
/
Copy pathDynamicArray.TestDriver.cpp
File metadata and controls
373 lines (308 loc) · 11.7 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// DynamicArray.TestDriver.cpp : This is a test driver for DynamicArray.
//
//Programmer: Jiefeng Yang
//Programmer's ID: 1791121
#include <iostream>
#include <iomanip>
using namespace std;
#include <cassert>
#include "DynamicArray.h"
#include "DynamicArray.h" // ifndef test
void testInt();
void testDouble();
void testChar();
void testString();
/*int main()
{
// Do tests with int , double , and char .
// Also do tests with an object, like string
testInt();
testDouble();
testChar();
testString();
return 0;
}*/
void testInt()
{
// test code for test driver.
// Code Refrence from course section "Apps And Test Drivers, Reading"
// and "Dynamic Memory And Test Drivers, Reading"
DynamicArray<int> a; //capacity =2 (default constructor)
// Array::capacity
cout << "\nTesting Data Type Int\n";
cout << "\nTesting Array::capacity\n";
cout << "EXPECTED: 2\n";
cout << "ACTUAL: " << a.capacity() << endl;
assert(2 == a.capacity());
cout << "\nTesting change the capacity\n";
a.capacity(4);
cout << "Change capacity to 4,EXPECTED: 4\n";
cout << "ACTUAL: " << a.capacity() << endl;
assert(4 == a.capacity());
// Array::operator[ ] setter
cout << "\nTesting the Array::operator[ ] setter\n";
a[0] = 6456;
cout << "\nTesting Array::operator[ ] setter\n";
cout << "AEXPECTED: 6456 for a[0]\n";
cout << "ACTUAL: " << a[0] << endl;
assert(6456 == a[0]);
a[6] = 31231; //apply capacity auto-adjusting for the setter if out of range high.
a[7] = 5222;
cout << "Auto-adjusting when out of range high. EXPECTED: 31231 for a[6]\n";
cout << "ACTUAL: " << a[6] << endl;
assert(31231 == a[6]);
cout << "EXPECTED: 5222 for a[7]\n";
cout << "ACTUAL: " << a[7] << endl;
assert(5222 == a[7]);
a[-1000] = int();
cout << "Index range-checking, EXPECTED: 0 for a[-1000]\n";
cout << "ACTUAL: " << a[-1000] << endl;
assert(int() == a[-6]); // any out-of-range uses dummy
assert(int() == a[100]); // checks upper end of range
cout << "\nTesting the Array::operator[ ] getter\n";
const DynamicArray<int> b = a;
for (int i = 0; i < 10; i++)
assert(a[i] == b[i]);
// const object test
cout << "\nConst object test\n";
const DynamicArray<int> c(100); // if this compiles, Array::Array main constructor exists
assert(c.capacity() == 100); // if this compiles, Array::capacity is a getter
assert(c[0] == c[0]); // if this compiles, there is an Array::operator[ ] getter
assert(c[-1] == c[-1]); // tests the getter's range checking
cout << "\nTesting the copy constructor Function: Object copy test\n";
const DynamicArray<int> d = a; // making a copy
assert(a.capacity() == d.capacity());
for (int i = 0; i < a.capacity(); i++)
assert(a[i] == d[i]); // uses the setter version for both a and d
// change the content in the original array to verify it's not a shallow copy
for (int i = 0; i < a.capacity(); i++)
{
a[i]++;
assert(a[i] != d[i]);
}
// object assignment test
cout << "\nTesting Object assignment\n";
DynamicArray<int> e; e = a;
assert(a.capacity() == e.capacity());
for (int i = 0; i < a.capacity(); i++)
assert(a[i] == e[i]);
// change the content in the original array to verify it's not a shallow copy
for (int i = 0; i < a.capacity(); i++)
{
a[i]++;
assert(a[i] != e[i]);
}
}
void testDouble()
{
// test code for test driver.
// Code Refrence from course section "Apps And Test Drivers, Reading"
// and "Dynamic Memory And Test Drivers, Reading"
DynamicArray<double> a; //capacity =2 (default constructor)
// Array::capacity
cout << "\nTesting Data Type Double\n";
cout << "\nTesting Array::capacity\n";
cout << "EXPECTED: 2\n";
cout << "ACTUAL: " << a.capacity() << endl;
assert(2 == a.capacity());
cout << "\nTesting change the capacity\n";
a.capacity(4);
cout << "Change capacity to 4,EXPECTED: 4\n";
cout << "ACTUAL: " << a.capacity() << endl;
assert(4 == a.capacity());
// Array::operator[ ] setter
cout << "\nTesting the Array::operator[ ] setter\n";
a[0] = 6456.4;
cout << "\nTesting Array::operator[ ] setter\n";
cout << "AEXPECTED: 6456.4 for a[0]\n";
cout << "ACTUAL: " << a[0] << endl;
assert(6456.4 == a[0]);
a[6] = 31231.43; //apply capacity auto-adjusting for the setter if out of range high.
a[7] = 5222.82;
cout << "Auto-adjusting when out of range high. EXPECTED: 31231.43 for a[6]\n";
cout << "ACTUAL: " << a[6] << endl;
assert(31231.43 == a[6]);
cout << "EXPECTED: 5222.82 for a[7]\n";
cout << "ACTUAL: " << a[7] << endl;
assert(5222.82 == a[7]);
a[-1000] = double();
cout << "Index range-checking, EXPECTED: 0 for a[-1000]\n";
cout << "ACTUAL: " << a[-1000] << endl;
assert(double() == a[-6]); // any out-of-range uses dummy
assert(double() == a[100]); // checks upper end of range
cout << "\nTesting the Array::operator[ ] getter\n";
const DynamicArray<double> b = a;
for (int i = 0; i < 10; i++)
assert(a[i] == b[i]);
// const object test
cout << "\nConst object test\n";
const DynamicArray<double> c(100); // if this compiles, Array::Array main constructor exists
assert(c.capacity() == 100); // if this compiles, Array::capacity is a getter
assert(c[0] == c[0]); // if this compiles, there is an Array::operator[ ] getter
assert(c[-1] == c[-1]); // tests the getter's range checking
cout << "\nTesting the copy constructor Function: Object copy test\n";
const DynamicArray<double> d = a; // making a copy
assert(a.capacity() == d.capacity());
for (int i = 0; i < a.capacity(); i++)
assert(a[i] == d[i]); // uses the setter version for both a and d
// change the content in the original array to verify it's not a shallow copy
for (int i = 0; i < a.capacity(); i++)
{
a[i]++;
assert(a[i] != d[i]);
}
// object assignment test
cout << "\nTesting Object assignment\n";
DynamicArray<double> e; e = a;
assert(a.capacity() == e.capacity());
for (int i = 0; i < a.capacity(); i++)
assert(a[i] == e[i]);
// change the content in the original array to verify it's not a shallow copy
for (int i = 0; i < a.capacity(); i++)
{
a[i]++;
assert(a[i] != e[i]);
}
}
void testChar()
{
// test code for test driver.
// Code Refrence from course section "Apps And Test Drivers, Reading"
// and "Dynamic Memory And Test Drivers, Reading"
DynamicArray<char> a; //capacity =2 (default constructor)
// Array::capacity
cout << "\nTesting Data Type char\n";
cout << "\nTesting Array::capacity\n";
cout << "EXPECTED: 2\n";
cout << "ACTUAL: " << a.capacity() << endl;
assert(2 == a.capacity());
cout << "\nTesting change the capacity\n";
a.capacity(4);
cout << "Change capacity to 4,EXPECTED: 4\n";
cout << "ACTUAL: " << a.capacity() << endl;
assert(4 == a.capacity());
// Array::operator[ ] setter
cout << "\nTesting the Array::operator[ ] setter\n";
a[0] = 'c';
cout << "\nTesting Array::operator[ ] setter\n";
cout << "AEXPECTED: c for a[0]\n";
cout << "ACTUAL: " << a[0] << endl;
assert('c' == a[0]);
a[6] = 'a'; //apply capacity auto-adjusting for the setter if out of range high.
a[7] = 'b';
cout << "Auto-adjusting when out of range high. EXPECTED: a for a[6]\n";
cout << "ACTUAL: " << a[6] << endl;
assert('a' == a[6]);
cout << "EXPECTED: b for a[7]\n";
cout << "ACTUAL: " << a[7] << endl;
assert('b' == a[7]);
a[-1000] = char();
cout << "Index range-checking, EXPECTED: '' for a[-1000]\n";
cout << "ACTUAL: " << a[-1000] << endl;
assert(char() == a[-6]); // any out-of-range uses dummy
assert(char() == a[100]); // checks upper end of range
cout << "\nTesting the Array::operator[ ] getter\n";
const DynamicArray<char> b = a;
for (int i = 0; i < 10; i++)
assert(a[i] == b[i]);
// const object test
cout << "\nConst object test\n";
const DynamicArray<char> c(100); // if this compiles, Array::Array main constructor exists
assert(c.capacity() == 100); // if this compiles, Array::capacity is a getter
assert(c[0] == c[0]); // if this compiles, there is an Array::operator[ ] getter
assert(c[-1] == c[-1]); // tests the getter's range checking
cout << "\nTesting the copy constructor Function: Object copy test\n";
const DynamicArray<char> d = a; // making a copy
assert(a.capacity() == d.capacity());
for (int i = 0; i < a.capacity(); i++)
assert(a[i] == d[i]); // uses the setter version for both a and d
// change the content in the original array to verify it's not a shallow copy
for (int i = 0; i < a.capacity(); i++)
{
a[i]++;
assert(a[i] != d[i]);
}
// object assignment test
cout << "\nTesting Object assignment\n";
DynamicArray<char> e; e = a;
assert(a.capacity() == e.capacity());
for (int i = 0; i < a.capacity(); i++)
assert(a[i] == e[i]);
// change the content in the original array to verify it's not a shallow copy
for (int i = 0; i < a.capacity(); i++)
{
a[i]++;
assert(a[i] != e[i]);
}
}
void testString()
{
// test code for test driver.
// Code Refrence from course section "Apps And Test Drivers, Reading"
// and "Dynamic Memory And Test Drivers, Reading"
DynamicArray<string> a; //capacity =2 (default constructor)
// Array::capacity
cout << "\nTesting Data Type string\n";
cout << "\nTesting Array::capacity\n";
cout << "EXPECTED: 2\n";
cout << "ACTUAL: " << a.capacity() << endl;
assert(2 == a.capacity());
cout << "\nTesting change the capacity\n";
a.capacity(4);
cout << "Change capacity to 4,EXPECTED: 4\n";
cout << "ACTUAL: " << a.capacity() << endl;
assert(4 == a.capacity());
// Array::operator[ ] setter
cout << "\nTesting the Array::operator[ ] setter\n";
a[0] = "ced";
cout << "\nTesting Array::operator[ ] setter\n";
cout << "AEXPECTED: ced for a[0]\n";
cout << "ACTUAL: " << a[0] << endl;
assert("ced" == a[0]);
a[6] = "abcef"; //apply capacity auto-adjusting for the setter if out of range high.
a[7] = "higk";
cout << "Auto-adjusting when out of range high. EXPECTED: abcef for a[6]\n";
cout << "ACTUAL: " << a[6] << endl;
assert("abcef" == a[6]);
cout << "EXPECTED: higk for a[7]\n";
cout << "ACTUAL: " << a[7] << endl;
assert("higk" == a[7]);
a[-1000] = string();
cout << "Index range-checking, EXPECTED: for a[-1000]\n";
cout << "ACTUAL: " << a[-1000] << endl;
assert(string() == a[-6]); // any out-of-range uses dummy
assert(string() == a[100]); // checks upper end of range
cout << "\nTesting the Array::operator[ ] getter\n";
const DynamicArray<string> b = a;
for (int i = 0; i < 10; i++)
assert(a[i] == b[i]);
// const object test
cout << "\nConst object test\n";
const DynamicArray<string> c(100); // if this compiles, Array::Array main constructor exists
assert(c.capacity() == 100); // if this compiles, Array::capacity is a getter
assert(c[0] == c[0]); // if this compiles, there is an Array::operator[ ] getter
assert(c[-1] == c[-1]); // tests the getter's range checking
cout << "\nTesting the copy constructor Function: Object copy test\n";
const DynamicArray<string> d = a; // making a copy
assert(a.capacity() == d.capacity());
for (int i = 0; i < a.capacity(); i++)
assert(a[i] == d[i]); // uses the setter version for both a and d
// change the content in the original array to verify it's not a shallow copy
for (int i = 0; i < a.capacity(); i++)
{
a[i] += "d";
assert(a[i] != d[i]);
}
// object assignment test
cout << "\nTesting Object assignment\n";
DynamicArray<string> e; e = a;
assert(a.capacity() == e.capacity());
for (int i = 0; i < a.capacity(); i++)
assert(a[i] == e[i]);
// change the content in the original array to verify it's not a shallow copy
for (int i = 0; i < a.capacity(); i++)
{
a[i] += "a";
assert(a[i] != e[i]);
}
}