-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathautotest.hpp
666 lines (577 loc) · 23.3 KB
/
autotest.hpp
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
//
// autotest.hpp
// PA3
//
// Created by rick gessner on 1/22/22.
//
#ifndef autotest_h
#define autotest_h
#include <map>
#include "Tracker.hpp"
#include "BufferManager.hpp"
#include "String.hpp"
#include <string>
#include "Timer.hpp"
#include "Testable.hpp"
bool assertTrue(const char* aMessage, bool aValue,
std::ostream& anOutput = std::cout) {
static const char* theMsgs[] = { "OK\n","FAIL\n" };
anOutput << "\t" << aMessage << " " << theMsgs[!aValue];
return aValue;
}
bool assertFalse(const char* aMessage, bool aValue,
std::ostream& anOutput = std::cout) {
static const char* theMsgs[] = { "OK\n","FAIL\n" };
anOutput << "\t" << aMessage << " " << theMsgs[aValue];
return false == aValue;
}
std::string getWords(size_t aCount) {
std::vector<std::string> kWords={
"hello","world","goodbye","yellow","brick",
"road","what","you","won't","do","there",
"apricot","silly","songs","glass","keys",
"apple","bananna","pear","grape","orange"};
size_t theSize=kWords.size();
const char *thePrefix="";
std::string theResult;
for(size_t i=0;i<aCount;i++) {
theResult+=thePrefix;
theResult+=kWords[rand() % theSize];
thePrefix=" ";
}
return theResult;
}
class BufferManagerAutoTests : public ECE141::Testable {
//If this won't compile, your Buffer Managerclass may not be ready.
//If this crashes, check how your Buffer Managerwrites to a stream.
public:
BufferManagerAutoTests(): ECE141::Testable(){
count = vList.size();
}
using Callable = bool (BufferManagerAutoTests::*)(std::ostream &anOutput);
std::map<std::string, Callable> vList{
{"OCFTest",&BufferManagerAutoTests::doBufferManagerOCFTests},
{"ExpandTest",&BufferManagerAutoTests::doBufferManagerExpandTests},
{"CompactTest", &BufferManagerAutoTests::doBufferManagerCompactTests}
};
/*
* The getTestName funciton used thorough the testable classes
*/
OptString getTestName(size_t anIndex) const override{
size_t thePos{0};
for (auto const thePair : vList){
if(anIndex==thePos++){
return thePair.first;
}
}
return std::nullopt;
};
// calling operator overloaded
bool operator()(const std::string &aName) override{
return vList.count(aName) != 0 && (this->*vList[aName])(std::cout);
}
bool operator()(const std::string &aName, std::ostream &aStream) override{
return vList.count(aName) != 0 && (this->*vList[aName])(aStream);
}
private:
bool doBufferManagerOCFTests(std::ostream &anOutput) {
auto &theTracker=Tracker::instance();
theTracker.enable(true).reset();
{
ECE141::BufferManager<char> theBuf1(100);
ECE141::BufferManager<char> theBuf2(theBuf1);
if(100>theBuf1.getCapacity()) {
anOutput << "ctor copy failed\n";
return false;
}
if(theBuf1.getCapacity()!=theBuf2.getCapacity()) {
anOutput << "ctor copy failed\n";
return false;
}
}
theTracker.reportLeaks(anOutput);
return true;
}
bool doBufferManagerExpandTests(std::ostream &anOutput) {
auto& theTracker = Tracker::instance();
theTracker.enable(true).reset();
{
ECE141::BufferManager<char> theBuf1;
theBuf1.willExpand(100);
if (100 > theBuf1.getCapacity()) {
anOutput << "expand failed\n";
return false;
}
theBuf1.willExpand(200);
if (200 > theBuf1.getCapacity()) {
anOutput << "expand failed\n";
return false;
}
}
theTracker.reportLeaks(anOutput);
return true;
}
bool doBufferManagerCompactTests(std::ostream &anOutput) {
auto& theTracker = Tracker::instance();
theTracker.enable(true).reset();
{
ECE141::BufferManager<char> theBuf1(100);
if(100>theBuf1.getCapacity()) {
anOutput << "expand failed\n";
return false;
}
theBuf1.willCompact(50);
if(50>theBuf1.getCapacity()) {
anOutput << "expand failed\n";
return false;
}
}
theTracker.reportLeaks(anOutput);
return true;
}
};
class StringAutoTests : public ECE141::Testable {
//If this won't compile, your Buffer Managerclass may not be ready.
//If this crashes, check how your Buffer Managerwrites to a stream.
public:
StringAutoTests() : ECE141::Testable(){
count = vList.size();
}
using Callable = bool (StringAutoTests::*)(std::ostream &anOutput);
std::map<std::string, Callable> vList{
{"OCFTest",&StringAutoTests::doOCFTests},
{"InsertTest",&StringAutoTests::doInsertTests},
{"AppendTest",&StringAutoTests::doAppendTests},
{"EraseTest",&StringAutoTests::doEraseTests},
{"ReplaceTest",&StringAutoTests::doReplaceTests},
{"SearchTest",&StringAutoTests::doSearchTests},
{"CompareTest",&StringAutoTests::doCompareTests},
{"SpeedTest",&StringAutoTests::doSpeedTest}
};
OptString getTestName(size_t anIndex) const override{
size_t thePos{0};
for (auto const thePair : vList){
if(anIndex==thePos++){
return thePair.first;
}
}
return std::nullopt;
};
bool operator()(const std::string &aName) override{
return vList.count(aName) != 0 && (this->*vList[aName])(std::cout);
}
bool operator()(const std::string &aName, std::ostream &aStream) override{
return vList.count(aName) != 0 && (this->*vList[aName])(aStream);
}
private:
bool doOCFTests(std::ostream &anOutput) {
auto &theTracker=Tracker::instance();
theTracker.enable(true).reset();
{
ECE141::BufferManager<char> theBuf1(100);
ECE141::BufferManager<char> theBuf2(theBuf1);
auto theTestStr1=getWords(1);
ECE141::String theECEString1(theTestStr1.c_str());
if (theTestStr1!=theECEString1.getBuffer()) {
anOutput << "ctor failed\n";
return false;
}
//string copy-ctor test...
ECE141::String theECEString2(theECEString1);
if (theTestStr1!=theECEString2.getBuffer()) {
anOutput << "copy ctor() failed\n";
return false;
}
auto theTestStr2=getWords(2);
theECEString1 = theTestStr2.c_str();
if (theTestStr2!=theECEString1.getBuffer()) {
anOutput << "operator=char* failed\n";
return false;
}
theECEString2 = theECEString1;
auto theBuffer=theECEString1.getBuffer();
auto theBuffer2=theECEString2.getBuffer();
if (std::strcmp(theBuffer,theBuffer2)) {
anOutput << "operator=string failed\n";
return false;
}
theTestStr2=getWords(2);
std::string theSTDString1(theTestStr2.c_str());
ECE141::String theECEString5(theTestStr2.c_str());
if (theECEString5[4]!=theSTDString1[4]) {
anOutput << "operator[] failed\n";
return false;
}
theTestStr2=getWords(1);
ECE141::String theECEString6(theTestStr2.c_str());
std::string theSTDString6(theTestStr2);
ECE141::String theECEString7;
std::string theSTDString7;
theTestStr2=getWords(1);
theSTDString7=theSTDString6+theTestStr2.c_str();
theECEString7=theECEString6+theTestStr2.c_str();
auto theBuf=theECEString7.getBuffer();
if(theSTDString7!=theECEString7.getBuffer()) {
anOutput << "operator+(const String& lhs, const char* rhs) failed\n";
return false;
}
theTestStr2=getWords(1);
ECE141::String theECEString8(theTestStr2.c_str());
std::string theSTDString8(theTestStr2.c_str());
theECEString7=theECEString6+theECEString8;
theSTDString7=theSTDString6+theSTDString8;
theBuf=theECEString7.getBuffer();
if (strcmp(theSTDString7.c_str(),theBuf)) {
anOutput << "operator+(const String& lhs, const String& rhs) failed\n";
return false;
}
}
theTracker.reportLeaks(anOutput);
return true;
}
bool doInsertTests(std::ostream &anOutput) {
auto& theTracker = Tracker::instance();
theTracker.enable(true).reset();
{
std::string theSTDString1(getWords(2));
ECE141::String theECEString1(theSTDString1.c_str());
std::string temp(getWords(3));
ECE141::String theECEString2(temp.c_str());
theECEString1.insert(5, theECEString2, 0, temp.size());
theSTDString1.insert(5, temp, 0, temp.size());
if (theSTDString1!=theECEString1.getBuffer()) {
anOutput << "insert(size_t anIndex, const String &aStr, size_t aStrIndex, size_t aStrCount) failed\n";
return false;
}
temp=getWords(2);
theSTDString1.insert(8, temp.c_str(), 3, 6);
theECEString1.insert(8, temp.c_str(), 3, 6);
if (theSTDString1.compare(theECEString1.getBuffer()) != 0) {
anOutput << "insert(size_t anIndex, const char* aBuffer, size_t aStrIndex, size_t aStrCount) failed\n";
return false;
}
theSTDString1.insert(10, 1, '?');
theECEString1.insert(10, '?'); //WE DONT PASS COUNT!
if (theSTDString1.compare(theECEString1.getBuffer()) != 0) {
anOutput << "insert(size_t anIndex, char ch) failed\n";
return false;
}
for (int i = 0; i < 100; ++i) {
theECEString1.insert(i%theECEString1.size(), temp.c_str(), 0, temp.size());
theSTDString1.insert(i%theSTDString1.length(), temp.c_str(), 0, temp.size());
}
if (theSTDString1.compare(theECEString1.getBuffer()) != 0) {
anOutput << "insert(size_t anIndex, const char* aBuffer, size_t aStrIndex, size_t aStrCount) failed 2\n";
return false;
}
}
theTracker.reportLeaks(anOutput);
return true;
}
bool doAppendTests(std::ostream &anOutput) {
auto& theTracker = Tracker::instance();
theTracker.enable(true).reset();
{
std::string theSTDString1("hello world");
ECE141::String theECEString1("hello world");
std::string theSTDString2("hello there world");
ECE141::String theECEString2("hello there world");
theSTDString1 += theSTDString2;
theECEString1 += theECEString2;
if (theSTDString1!=theECEString1.getBuffer()) {
anOutput << "operator+=(const String &aString) \failed\n";
return false;
}
theSTDString1 += "testing";
theECEString1 += "testing";
if (theSTDString1!=theECEString1.getBuffer()) {
anOutput << "operator+=(const char *aBuffer) \failed\n";
return false;
}
}
theTracker.reportLeaks(anOutput);
return true;
}
bool doEraseTests(std::ostream &anOutput) {
auto& theTracker = Tracker::instance();
theTracker.enable(true).reset();
{
auto theWords=getWords(3);
std::string theSTDString1(theWords.c_str());
ECE141::String theECEString1(theWords.c_str());
theSTDString1.erase(3, 5);
theECEString1.erase(3, 5);
auto theBuffer=theECEString1.getBuffer();
if (std::strcmp(theBuffer,theSTDString1.c_str())) {
anOutput << "erase(size_t aPos, size_t aLength) failed\n";
return false;
}
auto theWords2 = getWords(5);
std::string theSTDString2(theWords2.c_str());
ECE141::String theECEString2(theWords2.c_str());
theSTDString2.erase(0, 1);
theECEString2.erase(0, 1);
auto theBuffer2 = theECEString2.getBuffer();
if (std::strcmp(theBuffer2, theSTDString2.c_str())) {
anOutput << "erase(size_t aPos, size_t aLength) failed\n";
return false;
}
auto theWords3 = getWords(8);
std::string theSTDString3(theWords3.c_str());
ECE141::String theECEString3(theWords3.c_str());
theSTDString3.erase(theSTDString3.length()-2, theSTDString3.length()-1);
theECEString3.erase(theECEString3.size()-2, theECEString3.size()-1);
auto theBuffer3 = theECEString3.getBuffer();
if (std::strcmp(theBuffer3, theSTDString3.c_str())) {
anOutput << "erase(size_t aPos, size_t aLength) failed\n";
return false;
}
theWords3 = getWords(5);
std::string theSTDString4(theWords3.c_str());
ECE141::String theECEString4(theWords3.c_str());
theSTDString4.erase(12, 100);
theECEString4.erase(12, 100); //truncate
auto theBuffer4=theECEString4.getBuffer();
if (std::strcmp(theBuffer4,theSTDString4.c_str())) {
anOutput << "erase(12,100) (truncate) failed\n";
return false;
}
}
theTracker.reportLeaks(anOutput);
return true;
}
bool doReplaceTests(std::ostream &anOutput) {
auto& theTracker = Tracker::instance();
theTracker.enable(true).reset();
{
auto temp=getWords(3);
std::string theSTDString1(temp.c_str());
ECE141::String theECEString1(temp.c_str());
std::string theRep=getWords(2);
std::string theSTDRep1(theRep.c_str());
ECE141::String theECERep1(theRep.c_str());
theSTDString1.replace(2,2, theSTDRep1);
theECEString1.replace(2,2, theECERep1);
if (!(theECEString1.getBuffer() == theSTDString1)) {
anOutput << "replace(size_t pos, size_t len, const String& aString)\n";
return false;
}
//replace all...
theRep=getWords(2);
theSTDString1=theRep.c_str();
theECEString1=theRep.c_str();
theRep=getWords(3);
theSTDString1.replace(0, theSTDString1.size(), theSTDRep1);
theECEString1.replace(0, theECEString1.size(), theECERep1);
if (!(theECEString1.getBuffer()==theSTDString1)) {
anOutput << "replace(0, size(), string)\n";
return false;
}
auto theWords=getWords(2);
std::string STDString1(theWords);
ECE141::String ECEString5(theWords.c_str());
theWords=getWords(3);
std::string STDString2(theWords);
ECE141::String ECEString6(theWords.c_str());
STDString1.replace(5, 8, STDString2);
ECEString5.replace(5, 8, ECEString6);
if (!(ECEString5.getBuffer() == STDString1)) {
anOutput << "replace(size_t pos, size_t len, const String& aString)\n";
return false;
}
STDString1.replace(3, 2, STDString2);
ECEString5.replace(3, 2, ECEString6);
if (!(ECEString5.getBuffer() == STDString1)) {
anOutput << "replace(size_t pos, size_t len, const String& aString)\n";
return false;
}
STDString1.replace(5, 6, STDString2);
ECEString5.replace(5, 6, ECEString6);
if (!(ECEString5.getBuffer() == STDString1)) {
anOutput << "replace(size_t pos, size_t len, const String& aString)\n";
return false;
}
STDString1.replace(7, 8, STDString2);
ECEString5.replace(7, 8, ECEString6);
if (!(ECEString5.getBuffer() == STDString1)) {
anOutput << "replace(size_t pos, size_t len, const String& aString)\n";
return false;
}
theWords=getWords(1);
STDString1.replace(3, 9, theWords.c_str());
ECEString5.replace(3, 9, theWords.c_str());
if (!(ECEString5.getBuffer() == STDString1)) {
anOutput << "replace(size_t pos, size_t len, const String& aString)\n";
return false;
}
theWords=getWords(1);
STDString1.replace(5, 4, theWords.c_str());
ECEString5.replace(5, 4, theWords.c_str());
if (!(ECEString5.getBuffer() == STDString1)) {
anOutput << "replace(size_t pos, size_t len, const String& aString)\n";
return false;
}
theWords=getWords(1);
STDString1.replace(0, 0, theWords.c_str());
ECEString5.replace(0, 0, theWords.c_str());
if (!(ECEString5.getBuffer() == STDString1)) {
anOutput << "replace(size_t pos, size_t len, const String& aString)\n";
return false;
}
}
theTracker.reportLeaks(anOutput);
return true;
}
bool doSearchTests(std::ostream &anOutput) {
std::string STDString1("hello there world");
ECE141::String ECEString1("hello there world");
if (!(ECEString1.find("e", 5)==(int)STDString1.find("e", 5))) {
anOutput << "find(const char *aBuffer, size_t anOffset=0)\n";
return false;
}
if (!(ECEString1.find("ere")==(int)STDString1.find("ere"))) {
anOutput << "find(const char *aBuffer, size_t anOffset=0)\n";
return false;
}
std::string STDString2("world");
ECE141::String ECEString2("world");
if(!(ECEString1.find(ECEString2)==(int)STDString1.find(STDString2))) {
anOutput << "find(const T &aTarget, size_t anOffset=0)\n";
return false;
}
std::string STDString3("hello world");
ECE141::String ECEString3("hello world");
if (!(ECEString3[4]==STDString3[4])) {
anOutput << "operator[]\n";
return false;
}
return true;
}
bool doCompareTests(std::ostream &anOutput) {
auto theWords=getWords(2);
const char* theBuf=theWords.c_str();
ECE141::String theString1(theBuf);
std::string theSTLString1(theBuf);
//first -- test operator== (comparison)...
if (!(theSTLString1==theBuf && theString1==theBuf)) {
anOutput << "operator==(const char *aBuffer) fail\n";
return false;
}
ECE141::String theString2(theBuf);
std::string theSTLString2(theBuf);
if (!(theString1==theString2 && theSTLString1==theSTLString2)) {
anOutput << "operator==(const T &aString)\n";
return false;
}
if (theString1!=theBuf && theSTLString1!=theBuf) {
anOutput << "operator!=(const char *aBuffer) fail\n";
return false;
}
ECE141::String theString3(theBuf);
std::string theSTLString3(theBuf);
if (theString1!=theString3 && theSTLString1!=theSTLString3) {
anOutput << "operator!=(const T &aString) fail\n";
return false;
}
const char* temp="zoologists rock";
theWords=getWords(2);
theBuf=theWords.c_str();
ECE141::String theString4(theBuf);
std::string theSTLString4(theBuf);
if (!(theSTLString4<temp && theString4<temp)) {
anOutput << "operator<(const T* aBuffer) fail\n";
return false;
}
theSTLString1=temp;
theString1=temp;
if (!(theString4<theString1 && theSTLString4<theSTLString1)) {
anOutput << "operator<(const T* aBuffer) fail\n";
return false;
}
if (!(theSTLString4<=temp && theString4<=temp)) {
anOutput << "operator<=(const char *aBuffer)\n";
return false;
}
if (!(theString4<=theString1 && theSTLString4<=theSTLString1)) {
anOutput << "operator<=(const T &aString)\n";
return false;
}
if (!(theString4<=theString4 && theSTLString4<=theSTLString4)) {
anOutput << "operator<=(const T &aString)\n";
return false;
}
std::string str1("able and ready");
theWords=getWords(2);
theBuf=theWords.c_str();
std::string theSTLString5(theBuf);
ECE141::String theString5(theBuf);
if (!(theString5>str1.c_str() && theSTLString5>str1.c_str())) {
anOutput << "operator>(const char *aBuffer)\n";
return false;
}
theString1=str1.c_str();
if (!(theSTLString5>str1 && theString5>theString1)) {
anOutput << "operator>(const T &aString)\n";
return false;
}
if (!(theSTLString5>=theSTLString5 && theString5>=theString5)) {
anOutput << "operator>=(const T &aString)\n";
return false;
}
if (!(theString5>=theBuf && theSTLString5>=theBuf)) {
anOutput << "operator>=(const char *aBuffer)\n";
return false;
}
return true;
}
bool doSpeedTest(std::ostream& anOutput) {
double ECE141StringTime = 0;
double stdStringTime = 0;
{
ECE141::Timer theTimer;
auto theWords=getWords(2);
/*. UNCOMMENT HIS WHEN YOUR STRING IS WORKING...
theTimer.start();
ECE141::String temp("this is a block that gets reused");
anOutput << "\nTesting ECE141::String class performance...\n";
for (int i = 0; i < 90000; i++) {
ECE141::String theString(theWords.c_str());
theString += temp;
theString += temp;
for (int j = 0; j < 20; j++) {
theString.insert(10, temp, 0, temp.size());
theString += (temp);
theString.erase(30, 10);
}
ECE141::String theOther(theString);
}
theTimer.stop();
ECE141StringTime = theTimer.elapsed();
*/
theTimer.start();
std::string temp1("this is a block that gets reused");
anOutput << "\nTesting std::string class performance...\n";
for (int i = 0; i < 90000; i++) {
std::string theString(theWords.c_str());
theString += temp1;
theString += temp1;
for (int j = 0; j < 20; j++) {
theString.insert(10, temp1, 0, temp1.size());
theString += (temp1);
theString.erase(30, 10);
}
std::string theOther(theString);
}
theTimer.stop();
stdStringTime = theTimer.elapsed();
anOutput << "std::string " << stdStringTime
<< " Your string " << ECE141StringTime << "\n";
}
if (ECE141StringTime > (stdStringTime * 2.0)) {
anOutput << "your string implementation is too slow!\n";
return false;
}
return true;
}
};
#endif /* autotest_h */