-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
954 lines (798 loc) · 40.2 KB
/
Copy pathmain.cpp
File metadata and controls
954 lines (798 loc) · 40.2 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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
#include "ConcatStringList.h"
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#ifndef __MAIN_H__
#define __MAIN_H__
#endif
#ifdef _DEBUG
#ifndef DBG_NEW
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#define new DBG_NEW
#endif
#endif
void tc1() {
ConcatStringList s1("Hello,_world");
// test length
cout << "s1's length: " << s1.length() << endl;
// test get
cout << "char at index 2: " << s1.get(2) << endl;
try {
cout << "char at index 22: " << s1.get(22) << endl;
}
catch (out_of_range & ofr) {
cout << "Exception out_of_range: " << ofr.what() << endl;
}
// test indexOf
cout << "in s1, index of 'o': " << s1.indexOf('o') << endl;
cout << "in s1, index of 'a': " << s1.indexOf('a') << endl;
// test toString
cout << "s1 toString: " << s1.toString() << endl;
}
void tc2() {
ConcatStringList s1("Hello,");
ConcatStringList s2("_this_is");
ConcatStringList s3("_an_assignment");
// test concat
/*ConcatStringList s4 = s1.concat(s2);
cout << "s4: " << s4.toString() << endl;*/
ConcatStringList s5 = s1.concat(s2).concat(s3);
cout << "s5: " << s5.toString() << endl;
// test subString
ConcatStringList s6 = s5.subString(5, 15);
cout << "s6: " << s6.toString() << endl;
try {
ConcatStringList s6 = s5.subString(25, 15);
cout << "s6: " << s6.toString() << endl;
}
catch (exception& ofr) {
cout << "Pekowide Error: " << ofr.what() << endl;
}
// test reverse
ConcatStringList s7 = s5.reverse();
cout << "s7: " << s7.toString() << endl;
}
void tc3() {
ConcatStringList * s1 = new ConcatStringList("Hello");
ConcatStringList * s2 = new ConcatStringList("_world");
ConcatStringList * s3 = new ConcatStringList(s1->concat(*s2));
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << "---After deleting s1---" << endl;
delete s1;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << "---After deleting s3---" << endl;
delete s3;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << "---After deleting s2---" << endl;
delete s2;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
}
void tc30() {
cout << endl;
ConcatStringList* s0 = new ConcatStringList("XYZ");
cout << "s0: " << s0->toString() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s1 = new ConcatStringList("ABC");
cout << "s1: " << s1->toString() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s2 = new ConcatStringList("DEF");
cout << "s2: " << s2->toString() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s3 = new ConcatStringList("GHI");
cout << "s3: " << s3->toString() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s4 = new ConcatStringList(s1->concat(*s2));
cout << "s4: " << s4->toString() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s5 = new ConcatStringList(s4->reverse().concat(*s3));
cout << "s5: " << s5->toString() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s6 = new ConcatStringList(s5->reverse().reverse());
cout << "s6: " << s6->toString() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s7 = new ConcatStringList(s5->subString(1, 8));
cout << "s7: " << s7->toString() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s8 = new ConcatStringList(s5->subString(1, 8).subString(1, 5).subString(2, 3));
cout << "s8: " << s8->toString() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s9 = new ConcatStringList(s6->concat(s5->reverse().subString(4, 5)).reverse());
cout << "s9: " << s9->toString() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s10 = new ConcatStringList(s0->reverse().concat((s0->reverse().concat(s0->reverse()).reverse())));
cout << "s10: " << s10->toString() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << "---After deleting s0---" << endl;
delete s0;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
cout << "---After deleting s1---" << endl;
delete s1;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
cout << "---After deleting s3---" << endl;
delete s3;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
cout << "---After deleting s2---" << endl;
delete s2;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
cout << "---After deleting s4---" << endl;
delete s4;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
cout << "---After deleting s5---" << endl;
delete s5;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
cout << "---After deleting s6---" << endl;
delete s6;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
cout << "---After deleting s7---" << endl;
delete s7;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
cout << "---After deleting s8---" << endl;
delete s8;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
cout << "---After deleting s9---" << endl;
delete s9;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
cout << "---After deleting s10---" << endl;
delete s10;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
}
void tc39() {
cout << endl;
ConcatStringList* s0 = new ConcatStringList("");
cout << "s0: " << s0->toString() << " , length =" << s0->length() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s1 = new ConcatStringList("");
cout << "s1: " << s1->toString() << " , length =" << s1->length() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s2 = new ConcatStringList("");
cout << "s2: " << s2->toString() << " , length =" << s2->length() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s3 = new ConcatStringList("123456789");
cout << "s3: " << s3->toString() << " , length =" << s3->length() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s4 = new ConcatStringList(s0->concat(*s1));
cout << "s4: " << s4->toString() << " , length =" << s4->length() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s5 = new ConcatStringList(s0->reverse());
//s5 is "" so head and tail is the same
cout << "s5: " << s5->toString() << " , length =" << s5->length() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s6 = new ConcatStringList(s4->reverse());
//s6 is "" -> "" so there are different head and tail
cout << "s6: " << s6->toString() << " , length =" << s6->length() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s7 = new ConcatStringList(s2->concat(*s3));
//s7 is "" -> "123456789" so head is "" and tail is "123456789"
cout << "s7: " << s7->toString() << " , length =" << s7->length() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s8 = new ConcatStringList(s7->reverse());
//s8 is "987654321" -> "" so head is "987654321" and tail is ""
cout << "s8: " << s8->toString() << " , length =" << s8->length() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s9 = new ConcatStringList(s7->subString(0, 9));
//s9 still get the empty string so ""->"123456789"
cout << "s9: " << s9->toString() << " , length =" << s9->length() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s10 = new ConcatStringList(s9->reverse());
//try reverse it why not "987654321" ->""
cout << "s10: " << s10->toString() << " , length =" << s10->length() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s11 = new ConcatStringList(s8->subString(0, 9));
//s11 still get the empty string so "987654321" -> ""
cout << "s11: " << s11->toString() << " , length =" << s11->length() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s12 = new ConcatStringList(s11->reverse());
//reverse it why not ""->"123456789"
cout << "s12: " << s12->toString() << " , length =" << s12->length() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s13 = new ConcatStringList(s12->concat(*s10));
//s13 is "" -> "123456789" -> "987654321" -> ""
cout << "s13: " << s13->toString() << " , length =" << s13->length() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s14 = new ConcatStringList(s13->subString(0, 18));
//s14 is "" -> "123456789" -> "987654321" -> ""
cout << "s14: " << s14->toString() << " , length =" << s14->length() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << "s14: '9' is at " << s14->indexOf('9') << " index and at index 9 is " << s14->get(9) << " , so they are equal " << (s14->indexOf(s14->get(9)) == 9) << endl;
cout << endl;
ConcatStringList* s15 = new ConcatStringList(s13->reverse());
//s14 is "987654321" -> "" -> "" -> "123456789"
cout << "s15: " << s15->toString() << " , length =" << s15->length() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << "s15: '1' is at " << s15->indexOf('1') << " index and at index 1 is " << s15->get(1) << " , so they are equal " << (s15->indexOf(s15->get(1)) == 1) << endl;
cout << "---After deleting s15---" << endl;
delete s15;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << "---After deleting s14---" << endl;
delete s14;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << "---After deleting s13---" << endl;
delete s13;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << "---After deleting s12---" << endl;
delete s12;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << "---After deleting s11---" << endl;
delete s11;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << "---After deleting s10---" << endl;
delete s10;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << "---After deleting s9---" << endl;
delete s9;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << "---After deleting s8---" << endl;
delete s8;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << "---After deleting s7---" << endl;
delete s7;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << "---After deleting s6---" << endl;
delete s6;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << "---After deleting s0---" << endl;
delete s0;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
cout << "---After deleting s1---" << endl;
delete s1;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
cout << "---After deleting s3---" << endl;
delete s3;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
cout << "---After deleting s2---" << endl;
delete s2;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
cout << "---After deleting s4---" << endl;
delete s4;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
cout << "---After deleting s5---" << endl;
delete s5;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
}
void tc47() {
cout << endl;
ConcatStringList* s0 = new ConcatStringList("0123456789");
cout << "s0: " << s0->toString() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
int sum = 0;
for (int i = 9; i >= 0; i--) {
sum += s0->indexOf((char)'0' + i);
}
cout << "SUM = " << sum << endl;
for (int i = 9; i >= 0; i--) {
cout << s0->get(i);
}
cout << endl;
cout << endl;
ConcatStringList* s2 = new ConcatStringList
(s0->subString(0, 1).concat(
(s0->subString(1, 2).concat(
(s0->subString(2, 3).concat(
(s0->subString(3, 4).concat(
(s0->subString(4, 5).concat(
(s0->subString(5, 6).concat(
(s0->subString(6, 7).concat(
(s0->subString(7, 8).concat(
(s0->subString(8, 9).concat(
(s0->subString(9, 10)))
.subString(0, 2)))
.subString(0, 3)))
.subString(0, 4)))
.subString(0, 5)))
.subString(0, 6)))
.subString(0, 7)))
.subString(0, 8)))
.subString(0, 9)))
.subString(0, 10));
//Literally s0 == s2 = "0123456789" but take extra effort for no reason
cout << "s2: " << s2->toString() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
sum = 0;
for (int i = 9; i >= 0; i--) {
sum += s2->indexOf((char)'0' + i);
}
cout << "SUM = " << sum << endl;
for (int i = 9; i >= 0; i--) {
cout << s2->get(i);
}
cout << endl;
cout << "AT 10th index" << s2->indexOf(10) << endl;
cout << endl;
ConcatStringList* s3 = new ConcatStringList(s2->reverse());
cout << "s3: " << s3->toString() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
sum = 0;
for (int i = 9; i >= 0; i--) {
sum += s3->indexOf((char)'0' + i);
}
cout << "SUM = " << sum << endl;
for (int i = 9; i >= 0; i--) {
cout << s3->get(i);
}
cout << endl;
cout << endl;
cout << "---After deleting 3---" << endl;
delete s3;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
cout << "---After deleting 0---" << endl;
delete s0;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
//_CrtDumpMemoryLeaks();
cout << endl;
cout << "---After deleting s2---" << endl;
delete s2;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
//_CrtDumpMemoryLeaks();
}
void tc69()
{
///////////////////////////////////////////////////
/// HOW WELL CAN YOU HANDLE MULTIPLE CONCAT ??? ///
///////////////////////////////////////////////////
cout << "Test 1: " << '\n' << '\n';
ConcatStringList* s1 = new ConcatStringList("A");
ConcatStringList* s2 = new ConcatStringList("B");
ConcatStringList* s3 = new ConcatStringList("C");
ConcatStringList* s4 = new ConcatStringList(s1->concat(*s2).concat(*s3));
cout << "s4: " << s4->toString() << '\n';
cout << "refList size: " << ConcatStringList::refList.size() << '\n';
cout << ConcatStringList::refList.refCountsString();
cout << '\n';
cout << "delStrList size: " << ConcatStringList::delStrList.size() << '\n';
cout << ConcatStringList::delStrList.totalRefCountsString();
cout << '\n' << '\n';
cout << "After deleting s3:" << '\n';
delete s3;
cout << "s4: " << s4->toString() << '\n';
cout << "refList size: " << ConcatStringList::refList.size() << '\n';
cout << ConcatStringList::refList.refCountsString();
cout << '\n';
cout << "delStrList size: " << ConcatStringList::delStrList.size() << '\n';
cout << ConcatStringList::delStrList.totalRefCountsString();
cout << '\n' << '\n';
cout << "After deleting s1:" << '\n';
delete s1;
cout << "s2: " << s2->toString() << '\n';
cout << "refList size: " << ConcatStringList::refList.size() << '\n';
cout << ConcatStringList::refList.refCountsString();
cout << '\n';
cout << "delStrList size: " << ConcatStringList::delStrList.size() << '\n';
cout << ConcatStringList::delStrList.totalRefCountsString();
cout << '\n' << '\n';
cout << "After deleting s4:" << '\n';
delete s4;
cout << "refList size: " << ConcatStringList::refList.size() << '\n';
cout << ConcatStringList::refList.refCountsString();
cout << '\n';
cout << "delStrList size: " << ConcatStringList::delStrList.size() << '\n';
cout << ConcatStringList::delStrList.totalRefCountsString();
cout << '\n' << '\n';
cout << "After deleting s2:" << '\n';
delete s2;
cout << "refList size: " << ConcatStringList::refList.size() << '\n';
cout << ConcatStringList::refList.refCountsString();
cout << '\n';
cout << "delStrList size: " << ConcatStringList::delStrList.size() << '\n';
cout << ConcatStringList::delStrList.totalRefCountsString();
cout << '\n' << '\n';
//////////////////////////////////////////////////
/// HOW ABOUT WE ADD SOME REVERSE-CEPTIONS ??? ///
//////////////////////////////////////////////////
cout << "Test 2: " << '\n' << '\n';
s1 = new ConcatStringList("A");
s2 = new ConcatStringList("B");
s3 = new ConcatStringList("C");
s4 = new ConcatStringList("D");
ConcatStringList* s5 = new ConcatStringList("E");
ConcatStringList* s6 = new ConcatStringList("F");
ConcatStringList *s7 = new ConcatStringList(s1->concat(*s2).reverse());
ConcatStringList *s8 = new ConcatStringList(s3->concat(*s4).reverse());
ConcatStringList *s9 = new ConcatStringList(s5->concat(*s6).reverse());
cout << "s7: " << s7->toString() << '\n';
cout << "s8: " << s8->toString() << '\n';
cout << "s9: " << s9->toString() << '\n';
ConcatStringList* s10 = new ConcatStringList(s7->reverse().concat(s8->reverse()).concat(s9->reverse()).reverse());
cout << "s10: " << s10->toString() << '\n';
cout << '\n';
cout << "After deleting s9:" << '\n';
delete s9;
cout << "s10: " << s10->toString() << '\n';
cout << "refList size: " << ConcatStringList::refList.size() << '\n';
cout << ConcatStringList::refList.refCountsString();
cout << '\n';
cout << "delStrList size: " << ConcatStringList::delStrList.size() << '\n';
cout << ConcatStringList::delStrList.totalRefCountsString();
cout << '\n' << '\n';
cout << "After deleting s7:" << '\n';
delete s7;
cout << "s10: " << s10->toString() << '\n';
cout << "refList size: " << ConcatStringList::refList.size() << '\n';
cout << ConcatStringList::refList.refCountsString();
cout << '\n';
cout << "delStrList size: " << ConcatStringList::delStrList.size() << '\n';
cout << ConcatStringList::delStrList.totalRefCountsString();
cout << '\n' << '\n';
cout << "After deleting s1:" << '\n';
delete s1;
cout << "s10: " << s10->toString() << '\n';
cout << "refList size: " << ConcatStringList::refList.size() << '\n';
cout << ConcatStringList::refList.refCountsString();
cout << '\n';
cout << "delStrList size: " << ConcatStringList::delStrList.size() << '\n';
cout << ConcatStringList::delStrList.totalRefCountsString();
cout << '\n' << '\n';
cout << "After deleting s2:" << '\n';
delete s2;
cout << "s10: " << s10->toString() << '\n';
cout << "refList size: " << ConcatStringList::refList.size() << '\n';
cout << ConcatStringList::refList.refCountsString();
cout << '\n';
cout << "delStrList size: " << ConcatStringList::delStrList.size() << '\n';
cout << ConcatStringList::delStrList.totalRefCountsString();
cout << '\n' << '\n';
cout << "After deleting s3:" << '\n';
delete s3;
cout << "s10: " << s10->toString() << '\n';
cout << "refList size: " << ConcatStringList::refList.size() << '\n';
cout << ConcatStringList::refList.refCountsString();
cout << '\n';
cout << "delStrList size: " << ConcatStringList::delStrList.size() << '\n';
cout << ConcatStringList::delStrList.totalRefCountsString();
cout << '\n' << '\n';
cout << "After deleting s4:" << '\n';
delete s4;
cout << "s10: " << s10->toString() << '\n';
cout << "refList size: " << ConcatStringList::refList.size() << '\n';
cout << ConcatStringList::refList.refCountsString();
cout << '\n';
cout << "delStrList size: " << ConcatStringList::delStrList.size() << '\n';
cout << ConcatStringList::delStrList.totalRefCountsString();
cout << '\n' << '\n';
cout << "After deleting s5:" << '\n';
delete s5;
cout << "s10: " << s10->toString() << '\n';
cout << "refList size: " << ConcatStringList::refList.size() << '\n';
cout << ConcatStringList::refList.refCountsString();
cout << '\n';
cout << "delStrList size: " << ConcatStringList::delStrList.size() << '\n';
cout << ConcatStringList::delStrList.totalRefCountsString();
cout << '\n' << '\n';
cout << "After deleting s6:" << '\n';
delete s6;
cout << "s10: " << s10->toString() << '\n';
cout << "refList size: " << ConcatStringList::refList.size() << '\n';
cout << ConcatStringList::refList.refCountsString();
cout << '\n';
cout << "delStrList size: " << ConcatStringList::delStrList.size() << '\n';
cout << ConcatStringList::delStrList.totalRefCountsString();
cout << '\n' << '\n';
/////////////////////////////////////////////////////////////////////////////////
/// LET'S PLAY A LITTLE GAME OF SUBSTRING BEFORE ENDING THE TEST SHALL WE ??? ///
/////////////////////////////////////////////////////////////////////////////////
ConcatStringList* s11 = new ConcatStringList(s10->subString(3, 6).concat(s10->subString(0, 3)));
cout << "s11: " << s11->toString() << '\n';
cout << '\n';
cout << "After deleting s10:" << '\n';
delete s10;
cout << "refList size: " << ConcatStringList::refList.size() << '\n';
cout << ConcatStringList::refList.refCountsString();
cout << '\n';
cout << "delStrList size: " << ConcatStringList::delStrList.size() << '\n';
cout << ConcatStringList::delStrList.totalRefCountsString();
cout << '\n' << '\n';
cout << "After deleting s8:" << '\n';
delete s8;
cout << "refList size: " << ConcatStringList::refList.size() << '\n';
cout << ConcatStringList::refList.refCountsString();
cout << '\n';
cout << "delStrList size: " << ConcatStringList::delStrList.size() << '\n';
cout << ConcatStringList::delStrList.totalRefCountsString();
cout << '\n' << '\n';
cout << "After deleting s11:" << '\n';
delete s11;
cout << "refList size: " << ConcatStringList::refList.size() << '\n';
cout << ConcatStringList::refList.refCountsString();
cout << '\n';
cout << "delStrList size: " << ConcatStringList::delStrList.size() << '\n';
cout << ConcatStringList::delStrList.totalRefCountsString();
cout << '\n' << '\n';
///////////////////////////////////////////////////
/// THANKS FOR TESTING !!! HERE'S MY ANSWER !!! ///
///////////////////////////////////////////////////
/*
Test 1:
s4: ABC
refList size: 3
RefCounts[2,3,3]
delStrList size: 1
TotalRefCounts[5]
After deleting s3:
s4: ABC
refList size: 3
RefCounts[1,2,3]
delStrList size: 2
TotalRefCounts[5,1]
After deleting s1:
s2: B
refList size: 3
RefCounts[1,1,2]
delStrList size: 3
TotalRefCounts[3,1,1]
After deleting s4:
refList size: 3
RefCounts[2,0,0]
delStrList size: 1
TotalRefCounts[2]
After deleting s2:
refList size: 0
RefCounts[]
delStrList size: 0
TotalRefCounts[]
Test 2:
s7: BA
s8: DC
s9: FE
s10: FEDCBA
After deleting s9:
s10: FEDCBA
refList size: 20
RefCounts[1,1,1,1,1,1,2,2,2,2,2,2,0,0,0,0,0,0,0,0]
delStrList size: 3
TotalRefCounts[4,4,4]
After deleting s7:
s10: FEDCBA
refList size: 20
RefCounts[1,1,1,1,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0]
delStrList size: 3
TotalRefCounts[4,4,4]
After deleting s1:
s10: FEDCBA
refList size: 20
RefCounts[1,1,1,1,2,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0]
delStrList size: 3
TotalRefCounts[2,4,4]
After deleting s2:
s10: FEDCBA
refList size: 20
RefCounts[1,1,1,1,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0]
delStrList size: 2
TotalRefCounts[4,4]
After deleting s3:
s10: FEDCBA
refList size: 20
RefCounts[1,1,1,1,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0]
delStrList size: 2
TotalRefCounts[2,4]
After deleting s4:
s10: FEDCBA
refList size: 20
RefCounts[1,1,1,1,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
delStrList size: 1
TotalRefCounts[4]
After deleting s5:
s10: FEDCBA
refList size: 20
RefCounts[1,1,1,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
delStrList size: 1
TotalRefCounts[2]
After deleting s6:
s10: FEDCBA
refList size: 20
RefCounts[1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
delStrList size: 0
TotalRefCounts[]
s11: CBAFED
After deleting s10:
refList size: 24
RefCounts[1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
delStrList size: 2
TotalRefCounts[1,1]
After deleting s8:
refList size: 24
RefCounts[1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
delStrList size: 2
TotalRefCounts[1,1]
After deleting s11:
refList size: 0
RefCounts[]
delStrList size: 0
TotalRefCounts[]
*/
}
void tc()
{
cout << endl;
ConcatStringList* s0 = new ConcatStringList("XYZ");
cout << "s0: " << s0->toString() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
cout << endl;
ConcatStringList* s10 = new ConcatStringList(s0->reverse().concat((s0->reverse().concat(s0->reverse()).reverse())));
cout << "s10: " << s10->toString() << endl;
cout << "refList size: " << ConcatStringList::refList.size() << ", delStrList size: " << ConcatStringList::delStrList.size() << endl;
cout << ConcatStringList::refList.refCountsString() << endl;
cout << ConcatStringList::delStrList.totalRefCountsString() << endl;
}
int main() {
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDOUT);
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT);
_CrtMemState s1, s2, s3;
_CrtMemCheckpoint(&s1);
tc69();
_CrtMemCheckpoint(&s2);
if (_CrtMemDifference(&s3, &s1, &s2))
{
_CrtMemDumpStatistics(&s3);
}
return 0;
}