-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject3TestHarness.cpp
More file actions
642 lines (532 loc) · 15 KB
/
project3TestHarness.cpp
File metadata and controls
642 lines (532 loc) · 15 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
#include <iostream>
#include <iomanip>
#include "Sequence.h"
//#define __GRADING // Leave this commented out for your tests
#define __CREATE_PRINT
#define __INDEP
#define __PUSH_BACK
#define __PUSH_BACK_EMPTY
#define __POP_BACK
#define __POP_BACK_EMPTY
#define __INSERT
#define __INSERT_INVALID
#define __FRONT
#define __FRONT_EMPTY
#define __BACK
#define __BACK_EMPTY
#define __EMPTY
#define __SIZE
#define __CLEAR
#define __ERASE
#define __ERASE_INVALID
#define __ASSIGNMENT
#define __COPY_CONSTRUCTOR
#define __MEMORY_LEAK_TEST
#define NUM_MEM_TESTS 1000
#define MEM_TEST_SIZE 10
#ifdef __GRADING
#include <fstream>
#define OUTSTREAM os
#else
#define OUTSTREAM cout
#endif
using namespace std;
void testCopyConstructor(Sequence, ostream&);
void memoryLeakTest();
int main()
{
#ifdef __GRADING
ofstream OUTSTREAM;
OUTSTREAM.open("eval.txt");
#endif
// Create a sequence of length four, store some values, and print
try {
OUTSTREAM << "Testing sequence creation and printing" << endl;
OUTSTREAM << "--------------------------------------" << endl;
#ifdef __CREATE_PRINT
Sequence data(4);
data[0] = 100;
data[1] = 200;
data[2] = 300;
OUTSTREAM << "Sequence: " << data << endl;
OUTSTREAM << "Should be: <100, 200, 300, ???>" << endl << endl;
#else
OUTSTREAM << "*** UNHANDLED CRASH DURING TESTING ***" << endl << endl;
#endif
}
catch (exception& e)
{
OUTSTREAM << "Exception: " << e.what() << endl << endl;
}
#ifdef __GRADING
OUTSTREAM << "Points (out of 5): 5" << endl << endl;
#endif
// Test for independent sequences
try {
OUTSTREAM << "Testing multiple sequences" << endl;
OUTSTREAM << "--------------------------" << endl;
#ifdef __INDEP
Sequence s1(3);
Sequence s2(3);
for (int i = 0; i < 3; i++) {
s1[i] = i;
s2[i] = 100 + i;
}
OUTSTREAM << "Sequence1: " << s1 << endl;
OUTSTREAM << "Sequence2: " << s2 << endl;
OUTSTREAM << "Should be: <0, 1, 2>" << endl << " <100, 101, 102>" << endl << endl;
#else
OUTSTREAM << "*** UNHANDLED CRASH DURING TESTING ***" << endl << endl;
#endif
}
catch (exception& e)
{
OUTSTREAM << "Exception: " << e.what() << endl << endl;
}
#ifdef __GRADING
OUTSTREAM << "Points (out of 3): 3" << endl << endl;
#endif
// Test push_back
try {
OUTSTREAM << "Testing push_back()" << endl;
OUTSTREAM << "-------------------" << endl;
#ifdef __PUSH_BACK
Sequence data(3);
data[0] = 100;
data[1] = 200;
data[2] = 300;
data.push_back(400);
data.push_back(500);
OUTSTREAM << "Sequence: " << data << endl;
OUTSTREAM << "Should be: <100, 200, 300, 400, 500>" << endl << endl;
#else
OUTSTREAM << "*** UNHANDLED CRASH DURING TESTING ***" << endl << endl;
#endif
}
catch (exception& e)
{
OUTSTREAM << "Exception: " << e.what() << endl << endl;
}
#ifdef __GRADING
OUTSTREAM << "Points (out of 3): 3" << endl << endl;
#endif
// Test push_back to an empty sequence
try {
OUTSTREAM << "Testing push_back() on an empty sequence" << endl;
OUTSTREAM << "-------------------" << endl;
#ifdef __PUSH_BACK_EMPTY
Sequence data(0);
data.push_back(100);
data.push_back(200);
data.push_back(300);
data.push_back(400);
data.push_back(500);
OUTSTREAM << "Sequence: " << data << endl;
OUTSTREAM << "Should be: <100, 200, 300, 400, 500>" << endl << endl;
#else
OUTSTREAM << "*** UNHANDLED CRASH DURING TESTING ***" << endl << endl;
#endif
}
catch (exception& e)
{
OUTSTREAM << "Exception: " << e.what() << endl << endl;
}
#ifdef __GRADING
OUTSTREAM << "Points (out of 1): 1" << endl << endl;
#endif
// Test pop_back
try {
OUTSTREAM << "Testing pop_back()" << endl;
OUTSTREAM << "------------------" << endl;
#ifdef __POP_BACK
Sequence data(5);
for (int i = 0; i < 5; i++) {
data[i] = (i + 1) * 100;
}
data.pop_back();
data.pop_back();
OUTSTREAM << "Sequence: " << data << endl;
OUTSTREAM << "Should be: <100, 200, 300>" << endl << endl;
#else
OUTSTREAM << "*** UNHANDLED CRASH DURING TESTING ***" << endl << endl;
#endif
}
catch (exception& e)
{
OUTSTREAM << "Exception: " << e.what() << endl << endl;
}
#ifdef __GRADING
OUTSTREAM << "Points (out of 3): 3" << endl << endl;
#endif
// Test pop_back on empty sequence
try {
OUTSTREAM << "Testing pop_back() on an empty sequence" << endl;
OUTSTREAM << "------------------" << endl;
#ifdef __POP_BACK_EMPTY
Sequence data(3);
for (int i = 0; i < 3; i++) {
data[i] = (i + 1) * 100;
}
data.pop_back();
data.pop_back();
data.pop_back();
data.pop_back();
OUTSTREAM << "ERROR: Pop_back() DID NOT throw an exception" << endl << endl;
#else
OUTSTREAM << "*** UNHANDLED CRASH DURING TESTING ***" << endl << endl;
#endif
}
catch (exception& e)
{
OUTSTREAM << "CORRECT: Threw exception: " << e.what() << endl << endl;
}
#ifdef __GRADING
OUTSTREAM << "Points (out of 1): 1" << endl << endl;
#endif
// Test insert()
try {
OUTSTREAM << "Testing insert()" << endl;
OUTSTREAM << "------------------" << endl;
#ifdef __INSERT
Sequence data(5);
for (int i = 0; i < 5; i++) {
data[i] = (i + 1) * 100;
}
data.insert(3, 999);
OUTSTREAM << "Sequence: " << data << endl;
OUTSTREAM << "Should be: <100, 200, 300, 999, 400, 500>" << endl << endl;
data.insert(0, 888);
OUTSTREAM << "Sequence: " << data << endl;
OUTSTREAM << "Should be: <888, 100, 200, 300, 999, 400, 500>" << endl << endl;
data.insert(6, 777);
OUTSTREAM << "Sequence: " << data << endl;
OUTSTREAM << "Should be: <888, 100, 200, 300, 999, 400, 777, 500>" << endl << endl;
#else
OUTSTREAM << "*** UNHANDLED CRASH DURING TESTING ***" << endl << endl;
#endif
}
catch (exception& e)
{
OUTSTREAM << "Exception: " << e.what() << endl << endl;
}
#ifdef __GRADING
OUTSTREAM << "Points (out of 3): 3" << endl << endl;
#endif
// Test insert on invalid index
try {
OUTSTREAM << "Testing insert() on an invalid index" << endl;
OUTSTREAM << "------------------" << endl;
#ifdef __INSERT_INVALID
Sequence data(3);
for (int i = 0; i < 3; i++) {
data[i] = (i + 1) * 100;
}
data.insert(5, 555);
OUTSTREAM << "ERROR: Pop_back() DID NOT throw an exception" << endl << endl;
#else
OUTSTREAM << "*** UNHANDLED CRASH DURING TESTING ***" << endl << endl;
#endif
}
catch (exception& e)
{
OUTSTREAM << "CORRECT: Threw exception: " << e.what() << endl << endl;
}
#ifdef __GRADING
OUTSTREAM << "Points (out of 1): 1" << endl << endl;
#endif
// Test front()
try {
OUTSTREAM << "Testing front()" << endl;
OUTSTREAM << "---------------" << endl;
#ifdef __FRONT
Sequence data(3);
for (int i = 0; i < 3; i++) {
data[i] = (i + 1) * 100;
}
OUTSTREAM << "Front: " << data.front() << endl;
OUTSTREAM << "Sequence: " << data << endl;
OUTSTREAM << "Should be: 100" << endl;
OUTSTREAM << " <100, 200, 300>" << endl << endl;
#else
OUTSTREAM << "*** UNHANDLED CRASH DURING TESTING ***" << endl << endl;
#endif
}
catch (exception& e)
{
OUTSTREAM << "Exception: " << e.what() << endl << endl;
}
#ifdef __GRADING
OUTSTREAM << "Points (out of 2): 2" << endl << endl;
#endif
// Test front() on empty sequence
try {
OUTSTREAM << "Testing front() on an empty sequence" << endl;
OUTSTREAM << "------------------------------------" << endl;
#ifdef __FRONT_EMPTY
Sequence data(0);
int result = data.front();
OUTSTREAM << "ERROR: front() DID NOT throw an exception" << endl << endl;
#else
OUTSTREAM << "*** UNHANDLED CRASH DURING TESTING ***" << endl << endl;
#endif
}
catch (exception& e)
{
OUTSTREAM << "CORRECT: Threw exception: " << e.what() << endl << endl;
}
#ifdef __GRADING
OUTSTREAM << "Points (out of 1): 1" << endl << endl;
#endif
// Test back()
try {
OUTSTREAM << "Testing back()" << endl;
OUTSTREAM << "---------------" << endl;
#ifdef __BACK
Sequence data(3);
for (int i = 0; i < 3; i++) {
data[i] = (i + 1) * 100;
}
OUTSTREAM << "Back: " << data.back() << endl;
OUTSTREAM << "Sequence: " << data << endl;
OUTSTREAM << "Should be: 300" << endl;
OUTSTREAM << " <100, 200, 300>" << endl << endl;
#else
OUTSTREAM << "*** UNHANDLED CRASH DURING TESTING ***" << endl << endl;
#endif
}
catch (exception& e)
{
OUTSTREAM << "Exception: " << e.what() << endl << endl;
}
#ifdef __GRADING
OUTSTREAM << "Points (out of 2): 2" << endl << endl;
#endif
// Test back() on empty sequence
try {
OUTSTREAM << "Testing back() on an empty sequence" << endl;
OUTSTREAM << "------------------------------------" << endl;
#ifdef __BACK_EMPTY
Sequence data(0);
int result = data.back();
OUTSTREAM << "ERROR: back() DID NOT throw an exception" << endl << endl;
#else
OUTSTREAM << "*** UNHANDLED CRASH DURING TESTING ***" << endl << endl;
#endif
}
catch (exception& e)
{
OUTSTREAM << "CORRECT: Threw exception: " << e.what() << endl << endl;
}
#ifdef __GRADING
OUTSTREAM << "Points (out of 1): 1" << endl << endl;
#endif
// Test empty()
try {
OUTSTREAM << "Testing empty()" << endl;
OUTSTREAM << "---------------" << endl;
#ifdef __EMPTY
Sequence empty_sequence(0);
Sequence nonempty_sequence(1);
OUTSTREAM << "Empty sequence, empty returns: " << empty_sequence.empty() << endl;
OUTSTREAM << "Non-empty sequence, empty returns: " << nonempty_sequence.empty() << endl << endl;
#else
OUTSTREAM << "*** UNHANDLED CRASH DURING TESTING ***" << endl << endl;
#endif
}
catch (exception& e)
{
OUTSTREAM << "Exception: " << e.what() << endl << endl;
}
#ifdef __GRADING
OUTSTREAM << "Points (out of 1): 1" << endl << endl;
#endif
// Test size()
try {
OUTSTREAM << "Testing size()" << endl;
OUTSTREAM << "---------------" << endl;
#ifdef __SIZE
Sequence data(7);
Sequence empty_sequence(0);
OUTSTREAM << "Sequence length 7, size returned: " << data.size() << endl;
OUTSTREAM << "Empty sequence, size returned: " << empty_sequence.size() << endl << endl;
#else
OUTSTREAM << "*** UNHANDLED CRASH DURING TESTING ***" << endl << endl;
#endif
}
catch (exception& e)
{
OUTSTREAM << "Exception: " << e.what() << endl << endl;
}
#ifdef __GRADING
OUTSTREAM << "Points (out of 2): 2" << endl << endl;
#endif
// Test clear()
try {
OUTSTREAM << "Testing clear()" << endl;
OUTSTREAM << "---------------" << endl;
#ifdef __CLEAR
Sequence data(5);
for (int i = 0; i < 5; i++) {
data[i] = (i + 1) * 100;
}
data.clear();
OUTSTREAM << "Sequence cleared, empty returned: " << data.empty() << endl;
OUTSTREAM << "Size returned: " << data.size() << endl << endl;
#else
OUTSTREAM << "*** UNHANDLED CRASH DURING TESTING ***" << endl << endl;
#endif
}
catch (exception& e)
{
OUTSTREAM << "Exception: " << e.what() << endl << endl;
}
#ifdef __GRADING
OUTSTREAM << "Points (out of 2): 2" << endl << endl;
#endif
// Test erase
try {
OUTSTREAM << "Testing erase()" << endl;
OUTSTREAM << "------------------" << endl;
#ifdef __ERASE
Sequence data(10);
for (int i = 0; i < 10; i++) {
data[i] = (i + 1) * 100;
}
data.erase(3, 4);
OUTSTREAM << "Sequence: " << data << endl;
OUTSTREAM << "Should be: <100, 200, 300, 800, 900, 1000>" << endl << endl;
data.erase(4, 2);
OUTSTREAM << "Sequence: " << data << endl;
OUTSTREAM << "Should be: <100, 200, 300, 800>" << endl << endl;
data.erase(0, 2);
OUTSTREAM << "Sequence: " << data << endl;
OUTSTREAM << "Should be: <300, 800>" << endl << endl;
#else
OUTSTREAM << "*** UNHANDLED CRASH DURING TESTING ***" << endl << endl;
#endif
}
catch (exception& e)
{
OUTSTREAM << "Exception: " << e.what() << endl << endl;
}
#ifdef __GRADING
OUTSTREAM << "Points (out of 3): 3" << endl << endl;
#endif
// Test erase with invalid parameters
try {
OUTSTREAM << "Testing erase() on invalid parameters" << endl;
OUTSTREAM << "------------------" << endl;
#ifdef __ERASE_INVALID
Sequence data(5);
for (int i = 0; i < 5; i++) {
data[i] = (i + 1) * 100;
}
data.erase(3, 5);
OUTSTREAM << "ERROR: erase() DID NOT throw an exception" << endl << endl;
#else
OUTSTREAM << "*** UNHANDLED CRASH DURING TESTING ***" << endl << endl;
#endif
}
catch (exception& e)
{
OUTSTREAM << "CORRECT: Threw exception: " << e.what() << endl << endl;
}
#ifdef __GRADING
OUTSTREAM << "Points (out of 1): 1" << endl << endl;
#endif
// Test assignment (=) operator
try {
OUTSTREAM << "Testing assignment (=) operator" << endl;
OUTSTREAM << "------------------" << endl;
#ifdef __ASSIGNMENT
Sequence data1(5);
Sequence data2(0);
for (int i = 0; i < 5; i++) {
data1[i] = (i + 1) * 100;
}
data2 = data1;
data2[0] = 1;
data2[1] = 2;
OUTSTREAM << "Data1: " << data1 << endl;
OUTSTREAM << "Data2: " << data2 << endl;
OUTSTREAM << "Should be: <100, 200, 300, 400, 500>" << endl;
OUTSTREAM << " <1, 2, 300, 400, 500>" << endl << endl;
#else
OUTSTREAM << "*** UNHANDLED CRASH DURING TESTING ***" << endl << endl;
#endif
}
catch (exception& e)
{
OUTSTREAM << "Exception: " << e.what() << endl << endl;
}
#ifdef __GRADING
OUTSTREAM << "Points (out of 3): 3" << endl << endl;
#endif
// Test copy constructor
try {
OUTSTREAM << "Testing copy constructor" << endl;
OUTSTREAM << "------------------" << endl;
#ifdef __COPY_CONSTRUCTOR
Sequence data(5);
for (int i = 0; i < 5; i++) {
data[i] = (i + 1) * 100;
}
testCopyConstructor(data, OUTSTREAM);
OUTSTREAM << "Original Sequence: " << data << endl;
OUTSTREAM << "Should be: <1, 200, 300, 400, 500>" << endl;
OUTSTREAM << " <100, 200, 300, 400, 500>" << endl << endl;
#else
OUTSTREAM << "*** UNHANDLED CRASH DURING TESTING ***" << endl << endl;
#endif
}
catch (exception& e)
{
OUTSTREAM << "Exception: " << e.what() << endl << endl;
}
#ifdef __GRADING
OUTSTREAM << "Points (out of 3): 3" << endl << endl;
#endif
// Test for memory leaks
try {
OUTSTREAM << "Testing for memory leaks" << endl;
OUTSTREAM << "------------------------" << endl;
#ifdef __MEMORY_LEAK_TEST
cout << "Pre-memory leak test..." << endl;
system("pause");
for (int i = 0; i < NUM_MEM_TESTS; i++) {
memoryLeakTest();
}
cout << "Post-memory leak test..." << endl;
system("pause");
OUTSTREAM << "No memory leak found" << endl;
OUTSTREAM << "*** Error: memory leak found" << endl << endl;
#else
OUTSTREAM << "*** UNHANDLED CRASH DURING TESTING ***" << endl << endl;
#endif
}
catch (exception& e)
{
OUTSTREAM << "Exception: " << e.what() << endl << endl;
}
#ifdef __GRADING
OUTSTREAM << "Points (out of 3): 3" << endl << endl;
#endif
#ifdef __GRADING
OUTSTREAM << "Programming style and documentation" << endl;
OUTSTREAM << "-----------------------------------" << endl;
OUTSTREAM << "Points (out of 5): 5" << endl << endl;
OUTSTREAM << "TOTAL POINTS (out of 50): 50" << endl;
OUTSTREAM.close();
#endif
} // END OF MAIN
void memoryLeakTest() {
Sequence s(MEM_TEST_SIZE);
for (int i = 0; i < MEM_TEST_SIZE; i++) {
s[i] = i;
}
}
void testCopyConstructor(Sequence s, ostream &os)
{
s[0] = 1;
os << "Copied Sequence: " << s << endl;
}