-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
executable file
·733 lines (624 loc) · 18.4 KB
/
Main.cpp
File metadata and controls
executable file
·733 lines (624 loc) · 18.4 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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
//****************************************************************************************
//
// INCLUDE FILES
//
//****************************************************************************************
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "Roster.h"
using namespace std;
//****************************************************************************************
//
// CONSTANT DEFINITIONS
//
//****************************************************************************************
//****************************************************************************************
//
// CLASSES, TYPEDEFS AND STRUCTURES
//
//****************************************************************************************
typedef vector<string> StringVector;
//****************************************************************************************
//
// PUBLIC DATA
//
//****************************************************************************************
//****************************************************************************************
//
// PRIVATE DATA
//
//****************************************************************************************
static uint32_t failureCount;
static StringVector expectedFullRoster =
{
"1000\tI\tJack Jones\t1200 South Central Ave\tSunset Dunes\tCA\t99456",
"2000\tH\tJohn Smith\t100 West Shady Lane\tHemet\tCA\t99123",
"2000\tP\tJoan Smith",
"2000\tC\tSally Smith",
"2000\tC\tBillie Smith",
"2000\tC\tBobbie Smith",
"300\tH\tFred Brown\t700 North Ocean Drive\tLong Beach\tCA\t94327",
"300\tP\tMarcia Jones",
"4000\tH\tPaul Sims\t400 East Grand Blvd\tWalnut\tCA\t92155",
"4000\tC\tMary Sims",
"4000\tC\tDoug Sims"
};
static StringVector expectedMember2000 =
{
"2000\tH\tJohn Smith\t100 West Shady Lane\tHemet\tCA\t99123",
"2000\tP\tJoan Smith",
"2000\tC\tSally Smith",
"2000\tC\tBillie Smith",
"2000\tC\tBobbie Smith"
};
static StringVector expectedRemove300Roster =
{
"1000\tI\tJack Jones\t1200 South Central Ave\tSunset Dunes\tCA\t99456",
"2000\tH\tJohn Smith\t100 West Shady Lane\tHemet\tCA\t99123",
"2000\tP\tJoan Smith",
"2000\tC\tSally Smith",
"2000\tC\tBillie Smith",
"2000\tC\tBobbie Smith",
"4000\tH\tPaul Sims\t400 East Grand Blvd\tWalnut\tCA\t92155",
"4000\tC\tMary Sims",
"4000\tC\tDoug Sims"
};
//****************************************************************************************
//
// FUNCTION PROTOTYPES
//
//****************************************************************************************
void Test1(Roster& roster);
void Test2(Roster& roster);
void Test3(Roster& roster);
void Test4(Roster& roster);
void Test5(Roster& roster);
void Test6(Roster& roster);
void Test7(Roster& roster);
void Test8(Roster& roster);
void Test9(Roster& roster);
void Test10(Roster& roster);
string VerifyRosterFile(const string& fileName, const StringVector& expectedText);
bool VerifyRosterText(const string& actualText, const StringVector& expectedText);
//****************************************************************************************
//
// MAIN PROGRAM
//
//****************************************************************************************
int main (int argc, char * const argv[])
{
//************************************************************************************
// LOCAL DATA
Roster roster;
//************************************************************************************
// EXECUTABLE STATEMENTS
failureCount = 0;
Test1(roster);
Test2(roster);
Test3(roster);
Test4(roster);
Test5(roster);
Test6(roster);
Test7(roster);
Test8(roster);
Test9(roster);
Test10(roster);
cout << endl;
if (failureCount == 0)
{
cout << "All tests passed." << endl;
}
else if (failureCount == 1)
{
cout << "1 test failed." << endl;
}
else
{
cout << failureCount << " tests failed." << endl;
}
cout << endl << "** Finished **" << endl;
return(0);
}
//****************************************************************************************
//
// Test1: ShowAllMemberships, before a roster has been loaded.
//
// The function should write "empty".
//
//****************************************************************************************
void Test1(Roster& roster)
{
//************************************************************************************
// LOCAL DATA
stringstream stream;
string text;
//************************************************************************************
// EXECUTABLE STATEMENTS
cout << "Test 1: ShowAllMemberships (empty roster)" << endl;
// Call the function being tested and check its results.
roster.ShowAllMemberships(stream);
text = stream.str();
if (text == "empty")
{
cout << " Pass" << endl;
}
else
{
cout << " Fail" << endl;
++failureCount;
}
return;
}
//****************************************************************************************
//
// Test2: ShowOneMembership, before a roster has been loaded.
//
// The function should return false.
//
//****************************************************************************************
void Test2(Roster& roster)
{
//************************************************************************************
// LOCAL DATA
stringstream stream;
bool success;
//************************************************************************************
// EXECUTABLE STATEMENTS
cout << "Test 2: ShowOneMembership (empty roster)" << endl;
// Call the function being tested and check its return status.
success = roster.ShowOneMembership(stream, "1000");
if (!success)
{
cout << " Pass" << endl;
}
else
{
cout << " Fail" << endl;
++failureCount;
}
return;
}
//****************************************************************************************
//
// Test3: RemoveMembership, before a roster has been loaded.
//
// The function should return false.
//
//****************************************************************************************
void Test3(Roster& roster)
{
//************************************************************************************
// LOCAL DATA
stringstream stream;
bool success;
//************************************************************************************
// EXECUTABLE STATEMENTS
cout << "Test 3: RemoveMembership (empty roster)" << endl;
// Call the function being tested and check its return status.
success = roster.RemoveMembership("1000");
if (!success)
{
cout << " Pass" << endl;
}
else
{
cout << " Fail" << endl;
++failureCount;
}
return;
}
//****************************************************************************************
//
// Test4: LoadRoster
//
// The function should succeed and the roster should match the expected elements.
//
//****************************************************************************************
void Test4(Roster& roster)
{
//************************************************************************************
// LOCAL DATA
stringstream stream;
bool success;
//************************************************************************************
// EXECUTABLE STATEMENTS
cout << "Test 4: LoadRoster" << endl;
// Call the function being tested and check its return status.
success = roster.LoadMemberships("roster_in.txt");
if (success)
{
// Show the roster and verify it against the expected elements.
roster.ShowAllMemberships(stream);
success = VerifyRosterText(stream.str(), expectedFullRoster);
if (success)
{
cout << " Pass" << endl;
}
else
{
cout << " Fail (roster not as expected)" << endl;
++failureCount;
}
}
else
{
cout << " Fail (couldn't load file." << endl;
++failureCount;
}
return;
}
//****************************************************************************************
//
// Test5: ShowAllMemberships, after a roster has been loaded.
//
// The text returned should match its expected contents.
//
//****************************************************************************************
void Test5(Roster& roster)
{
//************************************************************************************
// LOCAL DATA
stringstream stream;
bool success;
//************************************************************************************
// EXECUTABLE STATEMENTS
cout << "Test 5: ShowAllMemberships (after Load)" << endl;
// Show the roster and verify it against the expected elements.
roster.ShowAllMemberships(stream);
success = VerifyRosterText(stream.str(), expectedFullRoster);
if (success)
{
cout << " Pass" << endl;
}
else
{
cout << " Fail" << endl;
++failureCount;
}
return;
}
//****************************************************************************************
//
// Test6: ShowOneMember, with a known id
//
// The function should succeed and the text returned should match its expected contents.
//
//****************************************************************************************
void Test6(Roster& roster)
{
//************************************************************************************
// LOCAL DATA
stringstream stream;
bool success;
//************************************************************************************
// EXECUTABLE STATEMENTS
cout << "Test 6: ShowOneMembership (after Load, known id)" << endl;
// Call the function being tested and check its return status.
success = roster.ShowOneMembership(stream, "2000");
if (success)
{
// It shuold succedd' the id is known.
success = VerifyRosterText(stream.str(), expectedMember2000);
if (success)
{
cout << " Pass" << endl;
}
else
{
cout << " Fail (membership not as expected)" << endl;
++failureCount;
}
}
else
{
cout << " Fail (membership not found)" << endl;
++failureCount;
}
return;
}
//****************************************************************************************
//
// Test7: ShowOneMembership, with an unknow id.
//
// The function should return false.
//
//****************************************************************************************
void Test7(Roster& roster)
{
//************************************************************************************
// LOCAL DATA
stringstream stream;
bool success;
//************************************************************************************
// EXECUTABLE STATEMENTS
cout << "Test 7: ShowOneMembership (after Load, unknown id)" << endl;
// Call the function being tested and check its return status.
success = roster.RemoveMembership("9999");
if (!success)
{
cout << " Pass" << endl;
}
else
{
cout << " Fail" << endl;
++failureCount;
}
return;
}
//****************************************************************************************
//
// Test8: RemoveMembership, known id.
//
// The fucntion should succeed and the modified roster should match its expected contents.
//
//****************************************************************************************
void Test8(Roster& roster)
{
//************************************************************************************
// LOCAL DATA
stringstream stream;
bool success;
//************************************************************************************
// EXECUTABLE STATEMENTS
cout << "Test 8: RemoveMembership (after Load, known id)" << endl;
// Call the function being tested and check its return status.
success = roster.RemoveMembership("300");
if (success)
{
// Show the modified roster and verify its actual contents against
// the expected contents.
roster.ShowAllMemberships(stream);
success = VerifyRosterText(stream.str(), expectedRemove300Roster);
if (success)
{
cout << " Pass" << endl;
}
else
{
cout << " Fail (roster after Remove not as expected)" << endl;
++failureCount;
}
}
else
{
cout << " Fail (can't remove membership 300" << endl;
++failureCount;
}
return;
}
//****************************************************************************************
//
// Test9: RemoveMembership, unknown id.
//
// The function shoiuld return false.
//
//****************************************************************************************
void Test9(Roster& roster)
{
//************************************************************************************
// LOCAL DATA
stringstream stream;
bool success;
//************************************************************************************
// EXECUTABLE STATEMENTS
cout << "Test 9: RemoveMembership (after Load, unknown id)" << endl;
// Call the function being tested and check its return status.
success = roster.RemoveMembership("9999");
if (!success)
{
// It was unsuccessful as expected.
cout << " Pass" << endl;
}
else
{
// It should have been unsuccessful.
cout << " Fail" << endl;
++failureCount;
}
return;
}
//****************************************************************************************
//
// Test10
//
// The function should succeed and the output file should match its expected contents.
//
//****************************************************************************************
void Test10(Roster& roster)
{
//************************************************************************************
// LOCAL DATA
string result;
stringstream stream;
bool success;
//************************************************************************************
// EXECUTABLE STATEMENTS
cout << "Test 10: StoreRoster" << endl;
// Call the function being tested and check its return status.
success = roster.StoreMemberships("roster_out.txt");
if (success)
{
// No errors during Store. Verify the output file.
result = VerifyRosterFile("roster_out.txt", expectedRemove300Roster);
if (result.empty())
{
cout << " Pass" << endl;
}
else
{
cout << " " << result << endl;
++failureCount;
}
}
else
{
cout << " Fail (can't store into file)" << endl;
++failureCount;
}
return;
}
//****************************************************************************************
//
// VerifyRosterFile
//
// Compares the stored roster with its expected contents.
// Returns a text string containing the result.
// 1. "": no errors
// 2. "Fail (contents don't match)"
// 3. "Fail (file shorter than expected)": the end of the file was reached
// before the end of the expected text.
// 4. "Fail (file longer than expected)":the end of the expected text was reached
// before the end of the file.
// In cases 3 and 4, the shorter of the file and the expected text matched; that is,
// the shorter of the two is a prefix of the longer. This is still a mismatch but
// not of the characters in the file.
//
//****************************************************************************************
string VerifyRosterFile(const string& fileName, const StringVector& expectedTextVector)
{
//************************************************************************************
// LOCAL DATA
StringVector actualFieldVector;
string actualText;
bool endOfFile;
string expectedText;
MembershipFile file;
uint32_t lineIndex;
bool mismatch;
string result;
bool success;
//************************************************************************************
// EXECUTABLE STATEMENTS
// Open the file and check for success.
success = file.Open(fileName);
if (success)
{
// Set the two "reasons for leaving the compare" loop.
endOfFile = false;
mismatch = false;
// Initialize the expected line index.
lineIndex = 0;
while (true)
{
// Get a file line and check for end-of-file.
// Set indicator and leave loop.
success = file.Read(actualFieldVector);
if (!success)
{
endOfFile = true;
break;
}
// Form an actual text contiguous string,
// because that's what's in the rows of
// the expected text vector.
uint32_t i = 0;
actualText.erase();
while (true)
{
actualText += actualFieldVector[i];
++i;
if (i < actualFieldVector.size())
{
actualText += '\t';
}
else
{
break;
}
}
// Compare actual and expected text.
// On mismatch, set indicator and leave loop.
expectedText = expectedTextVector[lineIndex];
if (actualText != expectedTextVector[lineIndex])
{
mismatch = true;
break;
}
// Advance to next expected line and check for "end of expected lines."
++lineIndex;
if (lineIndex == expectedTextVector.size())
{
// There are no more expected lines but the end of the file
// hasn't been reached. There's one legitimate reason: the
// last line of the file ends with a endl. It doesn't have
// to but it's not wrong to do so. One more read should see
// and end-of-file; see if it does and set the indicator.
success = file.Read(actualFieldVector);
if (!success)
{
endOfFile = true;
}
break;
}
}
// We've left the loop; check the reason.
if (mismatch)
{
// Mismatch, return the appropriate indicator
result = "Fail (contents don't match)";
}
else
{
// No mismatch; did we reach both the end of the file and
// the end of the expected lines?
if (endOfFile && (lineIndex == expectedTextVector.size()))
{
// Yes, indicate a successful verification.
result = "";
}
else
{
// No, which end did we reach? Return an appropriate indicator.
if (endOfFile)
{
result = "Fail (file shorter than expected)";
}
else
{
result = "Fail (file longer than expected)";
}
}
}
}
else
{
result = "Fail (can't open file)";
}
return(result);
}
//****************************************************************************************
//
// VerifyRosterText
//
// Compares the actual text returned by a Show... function with the expected text.
// Returns true if they match, false if they don't.
//
//****************************************************************************************
bool VerifyRosterText(const string& actualText, const StringVector& expectedTextVector)
{
//************************************************************************************
// LOCAL DATA
stringstream stream;
string expectedText;
//************************************************************************************
// EXECUTABLE STATEMENTS
// Form a stringstream from the expected text lines.
for (uint32_t i = 0; i < expectedTextVector.size(); ++i)
{
stream << expectedTextVector[i] << endl;
}
// Get a single expected text string.
expectedText = stream.str();
// Compare the strings and return the appropriate status.
if (actualText == expectedText)
{
return(true);
}
else
{
return(false);
}
}