-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtester.cpp
630 lines (597 loc) · 15 KB
/
tester.cpp
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
#include "NonPerishable.h"
#include "Perishable.h"
using namespace ict;
using namespace std;
const char nonPerFile[] = "nonper.txt";
const char PerFile[] = "per.txt";
void piv(const char* sku, const char* name, const char* price = "",
const char* taxed = "", const char* qty = "", const char* date = ""){
cout
<< "Enter the following: " << endl
<< "Sku: " << sku << endl
<< "Name:" << endl << name << endl;
if (price[0]) cout << "Price: " << price << endl;
if (taxed[0]) cout << "Taxed: " << taxed << endl;
if (qty[0]) cout << "Quantity: " << qty << endl;
if (date[0]) cout << "Expiry date: " << date << endl;
cout << endl;
}
void dumpFile(const char* fname){
ifstream f(fname);
char ch;
while (!f.get(ch).fail()){
cout.put(ch);
}
f.clear();
f.close();
}
void _pause(){
cout << "****press enter to continue...";
cin.ignore(1000, '\n');
}
bool yes(){
char ch;
cin >> ch;
cin.ignore(1000, '\n');
return (ch == 'y' || ch == 'Y');
}
int main(){
NonPerishable np;
Perishable pr;
bool ok = true;
cout << "Tester 1 ---------------------------------------------------------------------" << endl
<< "--NonPerishable Error Handling test:" << endl;
cout << "----Price validation test:" << endl;
piv("abc", "abc", "abc");
cin >> np;
if (cin.fail()){
cin.clear();
cin.ignore(2000, '\n');
cout << "Passed " << np << endl;
}
else{
ok = false;
cout << "Price validation failed" << endl;
_pause();
}
if (ok){
cout << "----Taxed validation test:" << endl;
piv("abc", "abc", "10", "abc");
cin >> np;
if (cin.fail()){
cin.clear();
cin.ignore(2000, '\n');
cout << "Passed " << np << endl;
}
else{
ok = false;
cout << "Taxed validaton failed" << endl;
_pause();
}
}
if (ok){
cout << "----Quantity validation test:" << endl;
piv("abc", "abc", "10", "y", "abc");
cin >> np;
if (cin.fail()){
cin.clear();
cin.ignore(2000, '\n');
cout << "Passed " << np << endl;
}
else{
ok = false;
cout << "Quantity validaton failed" << endl;
_pause();
}
}
if (ok){
cout << "Tester 2 ---------------------------------------------------------------------" << endl
<< "----Display test, the output of the Program and yours must match:" << endl;
piv("1234", "Box", "123.45", "y", "2");
cin >> np;
cin.ignore(1000, '\n');
cout << "--Linear------------" << endl;
cout << "Program: 1234 |Box | 123.45| TN| 2| 279.00|" << endl;
cout << " Yours: " << np << endl;
cout << "--Form Display------" << endl;
cout << "--Program: " << endl;
cout << "Name:" << endl << "Box" << endl;
cout << "Sku: 1234" << endl;
cout << "Price: 123.45" << endl;
cout << "Price after tax: 139.50" << endl;
cout << "Quantity: 2" << endl;
cout << "Total Cost: 279.00" << endl;
cout << "--Yours: " << endl;
np.write(cout, false);
cout << endl << "Did the program and your outputs match? (y)es/(n)o: ";
ok = yes();
}
if (ok){
cout << "Tester 3 ---------------------------------------------------------------------" << endl;
fstream npfile(nonPerFile, ios::out);
cout << "----Storage and loading test, the output of the Program and yours must match:" << endl;
NonPerishable tnp;
np.taxed(false);
np.save(npfile) << endl;
np.save(npfile) << endl;
npfile.close();
cout << "--Save NonPerishable, program: " << endl
<< "N,1234,Box,123.45,0,2" << endl
<< "N,1234,Box,123.45,0,2" << endl;
cout << "--Save NonPerishable, yours: " << endl;
dumpFile(nonPerFile);
cout << "--Load NonPerishable: " << endl;
npfile.open(nonPerFile, ios::in);
npfile.ignore(2);
tnp.load(npfile);
cout << "Program: 1234 |Box | 123.45| N| 2| 246.90|" << endl;
cout << " Yours: " << tnp << endl;
npfile.clear();
npfile.close();
cout << endl << "Did the program and your outputs match? (y)es/(n)o: ";
ok = yes();
}
if (ok){
cout << "Tester 4 ---------------------------------------------------------------------" << endl;
cout << "--Perishable Error Handling test:" << endl;
cout << "Each test must display the error message related to the test." << endl << endl;
cout << "----Price validation test:" << endl;
piv("abc", "abc", "abc");
cin >> pr;
if (cin.fail()){
cin.clear();
cin.ignore(2000, '\n');
cout << "Error: " << pr << endl;
}
else{
ok = false;
cout << "Price validation failed" << endl;
_pause();
}
if (ok){
cout << "----Taxed validation test:" << endl;
piv("abc", "abc", "10", "abc");
cin >> pr;
if (cin.fail()){
cin.clear();
cin.ignore(2000, '\n');
cout << "Error: " << pr << endl;
}
else{
ok = false;
cout << "Taxed validaton failed" << endl;
_pause();
}
}
if (ok){
cout << "----Quantity validation test:" << endl;
piv("abc", "abc", "10", "y", "abc");
cin >> pr;
if (cin.fail()){
cin.clear();
cin.ignore(2000, '\n');
cout << "Error: " << pr << endl;
}
else{
ok = false;
cout << "Quantity validaton failed" << endl;
_pause();
}
}
}
if (ok){
cout << "Tester 5 ---------------------------------------------------------------------" << endl;
cout << "--Perishable Expiry Date Error Handling test:" << endl;
cout << "Each test must display the error message related to the test." << endl << endl;
if (ok){
cout << "----Invalid Date Entry validation test:" << endl;
piv("a", "a", "1", "y", "1", "a");
cin >> pr;
if (cin.fail()){
cin.clear();
cin.ignore(2000, '\n');
cout << "Error: " << pr << endl;
}
else{
ok = false;
cout << "Date validaton failed" << endl;
_pause();
}
}
if (ok){
cout << "----Invalid Year Entry validation test:" << endl;
piv("a", "a", "1", "y", "1", "10/10/10");
cin >> pr;
if (cin.fail()){
cin.clear();
cin.ignore(2000, '\n');
cout << "Error: " << pr << endl;
}
else{
ok = false;
cout << "Year validaton failed" << endl;
_pause();
}
}
if (ok){
cout << "----Invalid Month Entry validation test:" << endl;
piv("a", "a", "1", "y", "1", "2000/13/10");
cin >> pr;
if (cin.fail()){
cin.clear();
cin.ignore(2000, '\n');
cout << "Error: " << pr << endl;
}
else{
ok = false;
cout << "Month validaton failed" << endl;
_pause();
}
}
if (ok){
cout << "----Invalid Day Entry validation test:" << endl;
piv("a", "a", "1", "y", "1", "2000/10/0");
cin >> pr;
if (cin.fail()){
cin.clear();
cin.ignore(2000, '\n');
cout << "Error: " << pr << endl;
}
else{
ok = false;
cout << "Year validaton failed" << endl;
_pause();
}
}
}
if (ok){
cout << "Tester 6 ---------------------------------------------------------------------" << endl;
cout << "----Display test, the output of the Program and yours must match:" << endl;
piv("1234", "Box", "123.45", "y", "2", "2015/11/26");
cin >> pr;
cin.ignore(1000, '\n');
cout << "--Linear------------" << endl;
cout << "Program: 1234 |Box | 123.45| TP| 2| 279.00|" << endl;
cout << " Yours: " << pr << endl;
cout << "--Form Display------" << endl;
cout << "--Program: " << endl;
cout << "Name:" << endl << "Box" << endl;
cout << "Sku: 1234" << endl;
cout << "Price: 123.45" << endl;
cout << "Price after tax: 139.50" << endl;
cout << "Quantity: 2" << endl;
cout << "Total Cost: 279.00" << endl;
cout << "Expiry date: 2015/11/26" << endl;
cout << "--Yours: " << endl;
pr.write(cout, false);
cout << endl << "If the program outputs and your outputs match, the test is passed." << endl;
cout << endl << "Did the program and your outputs match? (y)es/(n)o: ";
ok = yes();
}
if (ok){
cout << "Tester 7 ---------------------------------------------------------------------" << endl;
fstream prfile(PerFile, ios::out);
cout << "----Storage and loading test, the output of the Program and yours must match:" << endl;
Perishable tpr;
pr.taxed(false);
pr.save(prfile) << endl;
pr.save(prfile) << endl;
prfile.close();
cout << "--Save NonPerishable, program: " << endl
<< "P,1234,Box,123.45,0,2,2015/11/26" << endl
<< "P,1234,Box,123.45,0,2,2015/11/26" << endl;
cout << "--Save Perishable, yours: " << endl;
dumpFile(PerFile);
cout << "--Load Perishable: " << endl;
prfile.open(PerFile, ios::in);
prfile.ignore(2);
tpr.load(prfile);
cout << "--Program: " << endl;
cout << "Name:" << endl << "Box" << endl;
cout << "Sku: 1234" << endl;
cout << "Price: 123.45" << endl;
cout << "Price after tax: N/A" << endl;
cout << "Quantity: 2" << endl;
cout << "Total Cost: 246.90" << endl;
cout << "Expiry date: 2015/11/26" << endl;
cout << "--Yours: " << endl;
pr.write(cout, false);
prfile.clear();
prfile.close();
cout << endl << "Did the program and your outputs match? (y)es/(n)o: ";
ok = yes();
}
if (ok){
cout << "It seems you passed all the tests" << endl << "If this program is running within the submission script, type exit to have the outputs tested." << endl;
}
else{
cout << "Did not pass all the tests, please fix the problems and try again." << endl;
}
}
/*
Tester 1 ---------------------------------------------------------------------
--NonPerishable Error Handling test:
----Price validation test:
Enter the following:
Sku: abc
Name:
abc
Price: abc
Item Entry:
Sku: abc
Name:
abc
Price: abc
Passed Invalid Price Entry
----Taxed validation test:
Enter the following:
Sku: abc
Name:
abc
Price: 10
Taxed: abc
Item Entry:
Sku: abc
Name:
abc
Price: 10
Taxed: abc
Passed Invalid Taxed Entry, (y)es or (n)o
----Quantity validation test:
Enter the following:
Sku: abc
Name:
abc
Price: 10
Taxed: y
Quantity: abc
Item Entry:
Sku: abc
Name:
abc
Price: 10
Taxed: y
Quantity: abc
Passed Invalid Quantity Entry
Tester 2 ---------------------------------------------------------------------
----Display test, the output of the Program and yours must match:
Enter the following:
Sku: 1234
Name:
Box
Price: 123.45
Taxed: y
Quantity: 2
Item Entry:
Sku: 1234
Name:
Box
Price: 123.45
Taxed: y
Quantity: 2
--Linear------------
Program: 1234 |Box | 123.45| TN| 2| 279.00|
Yours: 1234 |Box | 123.45| TN| 2| 279.00|
--Form Display------
--Program:
Name:
Box
Sku: 1234
Price: 123.45
Price after tax: 139.50
Quantity: 2
Total Cost: 279.00
--Yours:
Name:
Box
Sku: 1234
Price: 123.45
Price after tax: 139.50
Quantity: 2
Total Cost: 279.00
Did the program and your outputs match? (y)es/(n)o: y
Tester 3 ---------------------------------------------------------------------
----Storage and loading test, the output of the Program and yours must match:
--Save NonPerishable, program:
N,1234,Box,123.45,0,2
N,1234,Box,123.45,0,2
--Save NonPerishable, yours:
N,1234,Box,123.45,0,2
N,1234,Box,123.45,0,2
--Load NonPerishable:
Program: 1234 |Box | 123.45| N| 2| 246.90|
Yours: 1234 |Box | 123.45| N| 2| 246.90|
Did the program and your outputs match? (y)es/(n)o: y
Tester 4 ---------------------------------------------------------------------
--Perishable Error Handling test:
Each test must display the error message related to the test.
----Price validation test:
Enter the following:
Sku: abc
Name:
abc
Price: abc
Perishable Item Entry:
Sku: abc
Name:
abc
Price: abc
Error: Invalid Price Entry
----Taxed validation test:
Enter the following:
Sku: abc
Name:
abc
Price: 10
Taxed: abc
Perishable Item Entry:
Sku: abc
Name:
abc
Price: 10
Taxed: abc
Error: Invalid Taxed Entry, (y)es or (n)o
----Quantity validation test:
Enter the following:
Sku: abc
Name:
abc
Price: 10
Taxed: y
Quantity: abc
Perishable Item Entry:
Sku: abc
Name:
abc
Price: 10
Taxed: y
Quantity: abc
Error: Invalid Quantity Entry
Tester 5 ---------------------------------------------------------------------
--Perishable Expiry Date Error Handling test:
Each test must display the error message related to the test.
----Invalid Date Entry validation test:
Enter the following:
Sku: a
Name:
a
Price: 1
Taxed: y
Quantity: 1
Expiry date: a
Perishable Item Entry:
Sku: a
Name:
a
Price: 1
Taxed: y
Quantity: 1
Expiry date (YYYY/MM/DD): a
Error: Invalid Date Entry
----Invalid Year Entry validation test:
Enter the following:
Sku: a
Name:
a
Price: 1
Taxed: y
Quantity: 1
Expiry date: 10/10/10
Perishable Item Entry:
Sku: a
Name:
a
Price: 1
Taxed: y
Quantity: 1
Expiry date (YYYY/MM/DD): 10/10/10
Error: Invalid Year in Date Entry
----Invalid Month Entry validation test:
Enter the following:
Sku: a
Name:
a
Price: 1
Taxed: y
Quantity: 1
Expiry date: 2000/13/10
Perishable Item Entry:
Sku: a
Name:
a
Price: 1
Taxed: y
Quantity: 1
Expiry date (YYYY/MM/DD): 2000/13/10
Error: Invalid Month in Date Entry
----Invalid Day Entry validation test:
Enter the following:
Sku: a
Name:
a
Price: 1
Taxed: y
Quantity: 1
Expiry date: 2000/10/0
Perishable Item Entry:
Sku: a
Name:
a
Price: 1
Taxed: y
Quantity: 1
Expiry date (YYYY/MM/DD): 2000/10/0
Error: Invalid Day in Date Entry
Tester 6 ---------------------------------------------------------------------
----Display test, the output of the Program and yours must match:
Enter the following:
Sku: 1234
Name:
Box
Price: 123.45
Taxed: y
Quantity: 2
Expiry date: 2015/11/26
Perishable Item Entry:
Sku: 1234
Name:
Box
Price: 123.45
Taxed: y
Quantity: 2
Expiry date (YYYY/MM/DD): 2015/11/26
--Linear------------
Program: 1234 |Box | 123.45| TP| 2| 279.00|
Yours: 1234 |Box | 123.45| TP| 2| 279.00|
--Form Display------
--Program:
Name:
Box
Sku: 1234
Price: 123.45
Price after tax: 139.50
Quantity: 2
Total Cost: 279.00
Expiry date: 2015/11/26
--Yours:
Name:
Box
Sku: 1234
Price: 123.45
Price after tax: 139.50
Quantity: 2
Total Cost: 279.00
Expiry date: 2015/11/26
If the program outputs and your outputs match, the test is passed.
Did the program and your outputs match? (y)es/(n)o: y
Tester 7 ---------------------------------------------------------------------
----Storage and loading test, the output of the Program and yours must match:
--Save NonPerishable, program:
P,1234,Box,123.45,0,2,2015/11/26
P,1234,Box,123.45,0,2,2015/11/26
--Save Perishable, yours:
P,1234,Box,123.45,0,2,2015/11/26
P,1234,Box,123.45,0,2,2015/11/26
--Load Perishable:
--Program:
Name:
Box
Sku: 1234
Price: 123.45
Price after tax: N/A
Quantity: 2
Total Cost: 246.90
Expiry date: 2015/11/26
--Yours:
Name:
Box
Sku: 1234
Price: 123.45
Price after tax: N/A
Quantity: 2
Total Cost: 246.90
Expiry date: 2015/11/26
Did the program and your outputs match? (y)es/(n)o: y
It seems you passed all the tests
If this program is running within the submission script, type exit to have the outputs tested.
*/