-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_def.c
2062 lines (1800 loc) · 52.6 KB
/
test_def.c
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
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file contains all the test definitions for the unit test driver
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "unit_test.h"
#include "my_string.h"
Status MZ_test_init_default_returns_nonNULL(char* buffer, int length)
{
MY_STRING hString = NULL;
Status status;
hString = my_string_init_default();
if (hString == NULL){
strncpy(buffer, "my_string_init_default returns NULL", length);
status = FAILURE;
}
else{
hString->destroy(&hString);
strncpy(buffer, "my_string_init_default returns non-Null", length);
status = SUCCESS;
}
return status;
}
Status MZ_test_get_size_on_init_default_returns_0(char* buffer, int length)
{
MY_STRING hString = my_string_init_default();
Status status;
if (hString->get_size(hString) != 0){
status = FAILURE;
printf("Expected a size of 0, but got %d\n", hString->get_size(hString));
strncpy(buffer, "my_string_init_default did not receive 0 from get_size function", length);
}
else{
status = SUCCESS;
strncpy(buffer, "my_string_init_default receives 0 from get_size function", length);
}
hString->destroy(&hString);
return status;
}
Status MZ_test_get_capacity_on_init_default_returns_8(char* buffer, int length)
{
MY_STRING hString = my_string_init_default();
Status status;
if (hString->get_capacity(hString) != 8){
status = FAILURE;
printf("Expected a capacity of 8, but got %d\n", hString->get_capacity(hString));
hString->get_capacity(hString);
strncpy(buffer, "my_sting_init_default did not receive 0 from get_capacity function", length);
}
else{
status = SUCCESS;
strncpy(buffer, "my_string_init_default receives 8 from get_capacity function", length);
}
hString->destroy(&hString);
return status;
}
Status MZ_test_init_c_str_returns_nonNULL(char* buffer, int length)
{
MY_STRING hString = NULL;
Status status;
hString = my_string_init_c_str("Test");
if (hString == NULL){
status = FAILURE;
strncpy(buffer, "my_string_init_c_str returns NULL given a test case", length);
}
else{
status = SUCCESS;
strncpy(buffer, "my_string_init_c_str returns non-NULL given a test case", length);
hString->destroy(&hString);
}
return status;
}
Status MZ_test_get_size_on_init_c_str_returns_correct(char* buffer, int length)
{
MY_STRING hString = my_string_init_c_str("Test");
Status status;
if (hString->get_size(hString) == 4){
status = SUCCESS;
strncpy(buffer, "get_size on my_string_init_c_str with a test case\n"
"\treturns the correct size of the string", length);
}
else{
status = FAILURE;
strncpy(buffer, "get_size on my_string_init_c_str does not return the correct size", length);
}
hString->destroy(&hString);
return status;
}
Status MZ_test_get_capacity_on_init_c_str_returns_correct(char* buffer, int length)
{
MY_STRING hString = my_string_init_c_str("Test");
Status status;
if (hString->get_capacity(hString) == 5){
status = SUCCESS;
strncpy(buffer, "get_capacity on my_string_init_c_str with a test case\n"
"\treturns the correct capacity of the string", length);
}
else{
status = FAILURE;
strncpy(buffer, "get_capacity on my_string_init_c_str does\n"
"\tnot return the correct capacity", length);
}
hString->destroy(&hString);
return status;
}
Status MZ_test_destroy_handle_points_to_NULL(char* buffer, int length)
{
MY_STRING hString = my_string_init_default();
Status status;
hString->destroy(&hString);
if (hString != NULL){
status = FAILURE;
strncpy(buffer, "my_string_destroy has not set either the handle to My_string to NULL", length);
}
else{
status = SUCCESS;
strncpy(buffer, "my_string_destroy has deallocated the memory (allegedly)\n"
"\tand set the handle to NULL", length);
}
return status;
}
Status MZ_test_pushback_non_full_increases_size(char* buffer, int length)
{
MY_STRING hString = my_string_init_default();
Status status;
hString->push_back(hString, 't');
if (hString->get_size(hString) == 1){
status = SUCCESS;
strncpy(buffer, "my_string_pushback has successfully inserted a character\n"
"\tand increased the size to 1 for a vector not at capacity", length);
}
else{
status = FAILURE;
strncpy(buffer, "my_string_pushback has failed to increase the size of the vector", length);
}
hString->destroy(&hString);
return status;
}
Status MZ_test_pushback_non_full_stores_char(char* buffer, int length)
{
MY_STRING hString = my_string_init_default();
Status status;
hString->push_back(hString, 'T');
hString->push_back(hString, 'e');
if (*hString->at(hString, 0) == 'T' && *hString->at(hString, 1) == 'e'){
status = SUCCESS;
strncpy(buffer, "my_string_pushback successfully stores\n"
"\tcorrect character at the correct index", length);
}
else{
status = FAILURE;
strncpy(buffer, "my_string_pushback fails to store correct character", length);
}
hString->destroy(&hString);
return status;
}
Status MZ_test_pushback_full_increases_capacity(char* buffer, int length)
{
MY_STRING hString = my_string_init_default();
Status status;
int i, before, after;
for (i = 0; i < hString->get_capacity(hString); i++){
hString->push_back(hString, 'T');
}
before = hString->get_capacity(hString);
hString->push_back(hString, 'T');
after = hString->get_capacity(hString);
if (before == 8 && after == 16){
status = SUCCESS;
strncpy(buffer, "my_string_pushback on a vector at capacity successfully doubles\n"
"\tthe capacity", length);
}
else{
status = SUCCESS;
strncpy(buffer, "my_string_pushback on a vector at capacity failed to double\n"
"\tthe original capacity", length);
}
hString->destroy(&hString);
return status;
}
Status MZ_test_pushback_full_increases_size(char* buffer, int length)
{
MY_STRING hString = my_string_init_default();
Status status;
int i, before, after;
for (i = 0; i < hString->get_capacity(hString); i++){
hString->push_back(hString, 'T');
}
before = hString->get_size(hString);
hString->push_back(hString, 'T');
after = hString->get_size(hString);
if (before == 8 && after == 9){
status = SUCCESS;
strncpy(buffer, "my_string_pushback on a vector at capacity successfully\n"
"\tincrements the size by 1", length);
}
else{
status = FAILURE;
strncpy(buffer, "my_string_pushback on a vector at capacity fails to\n"
"\tincrement the size by 1", length);
}
hString->destroy(&hString);
return status;
}
Status MZ_test_pushback_full_stores_char(char* buffer, int length)
{
MY_STRING hString = my_string_init_default();
Status status;
int i;
for (i = 0; i < hString->get_capacity(hString); i++){
hString->push_back(hString, 'T');
}
hString->push_back(hString, 'e');
if (*hString->at(hString, 8) == 'e'){
status = SUCCESS;
strncpy(buffer, "my_string_pushback on a vector at capacity successfully\n"
"\tstores the character in the newly created array", length);
}
else{
status = FAILURE;
strncpy(buffer, "my_string_pushback on a vector at capacity fails to\n"
"\tstore the character in the newly created array", length);
}
hString->destroy(&hString);
return status;
}
Status MZ_test_at_returns_NULL_given_empty_vector(char* buffer, int length)
{
MY_STRING hString = my_string_init_default();
Status status;
if (hString->at(hString, 0) == NULL && hString->get_size(hString) == 0){
status = SUCCESS;
strncpy(buffer, "my_string_at returns NULL when given an empty vector", length);
}
else{
status = FAILURE;
printf("Expected NULL, instead got '%c'\n", *hString->at(hString, 0));
strncpy(buffer, "my_string_at did not return NULL when given an empty vector", length);
}
hString->destroy(&hString);
return status;
}
Status MZ_test_at_returns_NULL_given_invalid_index(char* buffer, int length)
{
MY_STRING hString = my_string_init_default();
Status status;
hString->push_back(hString, 'C');
if (hString->at(hString, 1) != NULL){
status = FAILURE;
printf("Expected NULL, but instead got '%c'\n", *hString->at(hString, 1));
strncpy(buffer, "my_string_at did not return NULL when given an invalid index", length);
}
else{
status = SUCCESS;
strncpy(buffer, "my_string_at returns NULL given an invalid index", length);
}
hString->destroy(&hString);
return status;
}
Status MZ_test_at_returns_correct_characters_after_pushback(char* buffer, int length)
{
MY_STRING hString = my_string_init_default();
Status status;
hString->push_back(hString, 'T');
hString->push_back(hString, 'e');
hString->push_back(hString, 's');
hString->push_back(hString, 't');
if (*hString->at(hString, 0) == 'T' && *hString->at(hString, 1) == 'e' &&
*hString->at(hString, 2) == 's' && *hString->at(hString, 3) == 't'){
status = SUCCESS;
strncpy(buffer, "my_string_at returns the correct character after a\n"
"\tseries of pushback operations", length);
}
else{
status = FAILURE;
printf("expected char 'T' 'e' 's' 't' but instead,\n"
"\t my_string_at returned '%c' '%c' '%c' '%c'\n",
*hString->at(hString, 0), *hString->at(hString, 1),
*hString->at(hString, 2), *hString->at(hString, 3));
strncpy(buffer, "my_string_at did not return the corrent character", length);
}
hString->destroy(&hString);
return status;
}
Status MZ_test_popback_reduces_size_by_1(char* buffer, int length)
{
MY_STRING hString = NULL;
Status status;
int before, after;
hString = my_string_init_default();
hString->push_back(hString, 'c');
before = hString->get_size(hString);
hString->pop_back(hString);
after = hString->get_size(hString);
if (before == after + 1){
status = SUCCESS;
strncpy(buffer, "my_string_popback has successfully decremented the size by 1", length);
}
else{
status = FAILURE;
printf("The size before is %d, and after the popback, the size is %d\n", before, after);
strncpy(buffer, "my_string_popback has not decremented the size", length);
}
hString->destroy(&hString);
return status;
}
Status MZ_test_popback_returns_FAILURE_on_empty_vector(char*buffer, int length)
{
MY_STRING hString = my_string_init_default();
Status status;
if (hString->pop_back(hString) == FAILURE){
status = SUCCESS;
strncpy(buffer, "my_string_popback successfully returns FAILURE when given\n"
"\tan empty vector immediately after initialization", length);
}
else{
status = FAILURE;
strncpy(buffer, "my_string_popback returns a value other than FAILURE when given\n"
"\tan empty vector immediately after initialization", length);
}
hString->destroy(&hString);
return status;
}
Status MZ_test_compare_returns_0_given_same_strings(char* buffer, int length)
{
MY_STRING hString1 = my_string_init_c_str("Test");
MY_STRING hString2 = my_string_init_c_str("Test");
Status status;
if (hString1->string_compare(hString1, hString2) == 0){
status = SUCCESS;
strncpy(buffer, "my_string_compare returns 0 given two identical test strings", length);
}
else{
status = FAILURE;
strncpy(buffer, "my_string_compare fails to return 0 given two identical test strings", length);
}
hString1->destroy(&hString1);
hString2->destroy(&hString2);
return status;
}
Status MZ_test_compare_returns_1_given_different_size_strings(char* buffer, int length)
{
MY_STRING hString1 = my_string_init_c_str("Testing");
MY_STRING hString2 = my_string_init_c_str("Test");
Status status;
if (hString1->string_compare(hString1, hString2) == 1){
status = SUCCESS;
strncpy(buffer, "my_string_compare returns 1 given two test strings of different\n"
"\tlength, where string1 > string2 in size, while all other chars match", length);
}
else{
status = FAILURE;
strncpy(buffer, "my_string_compare fails to return 1 given two test strings\n"
"\tof different length, where string 1 > string 2 in size", length);
}
hString1->destroy(&hString1);
hString2->destroy(&hString2);
return status;
}
Status MZ_test_compare_returns_1_given_different_strings_of_same_size(char* buffer, int length)
{
MY_STRING hString1 = my_string_init_c_str("TEST");
MY_STRING hString2 = my_string_init_c_str("TESS");
Status status;
if (hString1->string_compare(hString1, hString2) == 1){
status = SUCCESS;
strncpy(buffer, "my_string_compare returns 1 given two different test strings\n"
"\tof the same size, differing on the final character", length);
}
else{
status = FAILURE;
strncpy(buffer, "my_string_compare fails to return 1 given two different test strings\n"
"\tof the same size, differing on the final character", length);
}
hString1->destroy(&hString1);
hString2->destroy(&hString2);
return status;
}
Status MZ_test_c_string_returns_NULL_terminated_string(char* buffer, int length)
{
MY_STRING hString = my_string_init_c_str("TEST");
Status status;
char* check_terminator;
check_terminator = hString->c_str(hString);
if (check_terminator[hString->get_size(hString)] == '\0'){
status = SUCCESS;
strncpy(buffer, "my_c_string returns a null terminated string at index size\n"
"\tgiven a test string", length);
}
else{
status = FAILURE;
strncpy(buffer, "my_c_string fails to return a null terminated string\n"
"\tgiven a test string", length);
}
hString->destroy(&hString);
return status;
}
Status MZ_test_c_string_resizes_full_string_to_append_NULL_terminator(char* buffer, int length)
{
MY_STRING hString = my_string_init_default();
Status status;
int i, before, after;
char* check_terminator;
for (i = 0; i < hString->get_capacity(hString); i++){
hString->push_back(hString, 'T');
}
before = hString->get_capacity(hString);
check_terminator = hString->c_str(hString);
after = hString->get_capacity(hString);
if (before == 8 && after == 16 && check_terminator[hString->get_size(hString)] == 0){
status = SUCCESS;
strncpy(buffer, "my_c_string successfully resizes a full string to double its original\n"
"\tcapacity and appends the null terminator at index size of hString", length);
}
else{
status = FAILURE;
strncpy(buffer, "my_c_string has failed to resize a full string. Either the original\n"
"\tcapacity, the new capacity, or the null terminator are wrong", length);
}
hString->destroy(&hString);
return status;
}
Status MZ_test_concatenate_returns_correct_size_of_destination(char* buffer, int length)
{
MY_STRING destination = my_string_init_c_str("TE");
MY_STRING add = my_string_init_c_str("ST");
int before, after;
Status status;
before = destination->get_size(destination);
if (destination->concat(destination, add) == SUCCESS){
after = destination->get_size(destination);
if (before == 2 && after == 4){
status = SUCCESS;
strncpy(buffer, "my_string_concatenate returns SUCCESS and properly changes\n"
"\tthe destination string to its new size", length);
}
else{
status = FAILURE;
strncpy(buffer, "my_string_concatenate FAILED to properly change\n"
"\tthe destination string to its new size", length);
}
}
else{
status = FAILURE;
strncpy(buffer, "my_string_concatenate returned FAILURE, need further diagnostic testing", length);
}
destination->destroy(&destination);
return status;
}
Status MZ_test_concatenate_properly_resizes_destination_when_full(char* buffer, int length)
{
MY_STRING destination = my_string_init_c_str("TE");
MY_STRING add = my_string_init_c_str("ST");
Status status;
int before, after;
before = destination->get_capacity(destination);
destination->concat(destination, add);
after = destination->get_capacity(destination);
if (before == 3 && after == 6){
status = SUCCESS;
strncpy(buffer, "my_string_concatenate properly resizes the destination string according\n"
"\tto the pushback function's definition. The new capacity is double\n"
"\tthe capacityof the original string", length);
}
else{
status = FAILURE;
strncpy(buffer, "my_string_concatenate faile to resize the destination string when\n"
"\tit ran out of space during the appending of the add string", length);
}
destination->destroy(&destination);
return status;
}
//JEREMY
Status test_init_default_returns_nonNULL(char* buffer, int length)
{
MY_STRING hString = NULL;
Status status;
hString = my_string_init_default();
if (hString == NULL)
{
status = FAILURE;
strncpy(buffer, "test_init_default returns nonNULL\n"
"\tmy_string_init_default returns NULL\n", length);
}
else
{
status = SUCCESS;
strncpy(buffer, "test_init_default_returns_nonNULL\n", length);
}
hString->destroy(&hString);
return status;
}
Status test_get_size_on_init_default_returns_0(char* buffer, int length)
{
MY_STRING hString = NULL;
Status status;
hString = my_string_init_default();
if (hString->get_size(hString) != 0)
{
status = FAILURE;
printf("Expected a size of 0 but got %d\n", hString->get_size(hString));
strncpy(buffer, "test_get_size_on_init_default_returns_0\n"
"\tDid not receive 0 from get_size after init_default\n", length);
}
else
{
status = SUCCESS;
strncpy(buffer, "test_get_size_on_init_default_returns_0\n", length);
}
hString->destroy(&hString);
return status;
}
Status test_get_capacity_on_init_default_returns_8(char* buffer, int length){
MY_STRING hString = NULL;
Status status;
int capacity;
hString = my_string_init_default();
capacity = hString->get_capacity(hString);
if (capacity != 8)
{
status = FAILURE;
printf("Expected a size of 8 but got %d\n", capacity);
strncpy(buffer, "test_get_capacity_on_init_default_returns_8\n"
"\tDid not receive 8 from get_capacity after init_default\n", length);
}
else
{
status = SUCCESS;
strncpy(buffer, "test_get_capacity_on_init_default_returns_8\n", length);
}
hString->destroy(&hString);
return status;
}
Status test_destroy_returns_null(char* buffer, int length)
{
MY_STRING hString = my_string_init_default();
Status status;
hString->destroy(&hString);
if (hString == NULL)
{
status = SUCCESS;
strncpy(buffer, "test_destroy_returns_null\n", length);
}
else
{
status = FAILURE;
printf("Destroy function ineffective, memory leak likely\n");
strncpy(buffer, "test_destroy_returns_null\n"
"\tThe pointer still contains the address of the freed space", length);
}
return status;
}
Status test_string_compare_returns_neg_one_when_string1_is_smaller(char* buffer, int length)
{
MY_STRING hString1 = NULL;
MY_STRING hString2 = NULL;
Status status;
hString1 = my_string_init_c_str("Test");
hString2 = my_string_init_c_str("Test1");
if (hString1->string_compare(hString1, hString2) != -1)
{
status = FAILURE;
printf("Expected string1 to be smaller\n");
strncpy(buffer, "test_string_compare_returns_neg_one_when_string1_is_smaller\n"
"\tDid not return -1 when String1 was smaller than String2\n", length);
}
else
{
status = SUCCESS;
strncpy(buffer, "test_string_compare_returns_neg_one_when_string1_is_smaller\n", length);
}
hString1->destroy(&hString1); hString2->destroy(&hString2);
return status;
}
Status test_string_compare_returns_0_when_strings_are_equal(char* buffer, int length)
{
MY_STRING hString1 = NULL;
MY_STRING hString2 = NULL;
Status status;
hString1 = my_string_init_c_str("Test1");
hString2 = my_string_init_c_str("Test1");
if (hString1->string_compare(hString1, hString2) != 0)
{
status = FAILURE;
printf("Expected strings to be equal\n");
strncpy(buffer, "test_string_compare_returns_0_when_strings_are_equal\n"
"\tDid not return 0 when String1 was equal to String2\n", length);
}
else
{
status = SUCCESS;
strncpy(buffer, "test_string_compare_returns_0_when_strings_are_equal\n", length);
}
hString1->destroy(&hString1); hString2->destroy(&hString2);
return status;
}
Status test_string_compare_returns_1_when_string1_is_greater(char* buffer, int length)
{
MY_STRING hString1 = NULL;
MY_STRING hString2 = NULL;
Status status;
hString1 = my_string_init_c_str("Test1");
hString2 = my_string_init_c_str("Test");
if (hString1->string_compare(hString1, hString2) != 1)
{
status = FAILURE;
printf("Expected string1 to be larger\n");
strncpy(buffer, "test_string_compare_returns_1_when_string1_is_greater\n"
"\tDid not return 1 when String1 was larger than String2\n", length);
}
else
{
status = SUCCESS;
strncpy(buffer, "test_string_compare_returns_1_when_string1_is_greater\n", length);
}
hString1->destroy(&hString1); hString2->destroy(&hString2);
return status;
}
Status test_init_c_str_capacity_equals_word_size(char* buffer, int length)
{
MY_STRING hString = my_string_init_c_str("test1");
Status status;
int capacity;
capacity = hString->get_capacity(hString);
if (capacity < 5)
{
status = FAILURE;
printf("Given a string of 5 characters, capacity should have been 5, but instead was %d\n", capacity);
strncpy(buffer, "test_init_c_str_capacity_equals_word_size\n"
"\tCapacity was incorrect for the length of the string given.\n", length);
}
else
{
status = SUCCESS;
strncpy(buffer, "test_init_c_str_capacity_equals_word_size\n", length);
}
return status;
}
Status test_push_back_doubles_capacity_of_c_str_as_needed(char* buffer, int length)
{
Status status;
MY_STRING hString = my_string_init_c_str("testing");
int capacity;
hString->push_back(hString, 'a');
hString->push_back(hString, 'a');
capacity = hString->get_capacity(hString);
if (capacity != 16)
{
status = FAILURE;
printf("Given a c_str 7 characters long, expected capacity to double to 14, but it was instead: %d\n", capacity);
strncpy(buffer, "test_push_back_doubles_capacity_when_added_to_a_c_str\n"
"\tPush function did not properly change the capacity\n", length);
}
else
{
hString->push_back(hString, 'a');
capacity = hString->get_capacity(hString);
if (capacity != 16)
{
status = FAILURE;
printf("While capacity was at 14, pushing on a character to a string of size 8 caused capacity to change\n");
strncpy(buffer, "test_push_back_doubles_capacity_when_added_to_a_c_str\n"
"\tPush function did not properly change the capacity\n", length);
}
else
{
status = SUCCESS;
strncpy(buffer, "test_push_back_doubles_capacity_when_added_to_a_c_str\n", length);
}
}
hString->destroy(&hString);
return status;
}
Status test_push_back_doubles_capacity_of_default_str_as_needed(char* buffer, int length)
{
Status status;
int start_cap, index;
MY_STRING hString;
hString = my_string_init_default();
start_cap = hString->get_capacity(hString);
for (index = 0; index < start_cap; index++)
{
hString->push_back(hString, 'a');
}
if (hString->get_capacity(hString) != start_cap)
{
status = FAILURE;
printf("Push function changed capacity before it was supposed to\n");
strncpy(buffer, "test_push_back_doubles_capacity_of_default_str_as_needed\n"
"\tPush function did not properly change the capacity\n", length);
}
else
{
hString->push_back(hString, 'a');
if (hString->get_capacity(hString) != start_cap * 2)
{
status = FAILURE;
printf("Push function did not change capacity when needed\n");
strncpy(buffer, "test_push_back_doubles_capacity_of_default_str_as_needed\n"
"\tPush function did not properly change the capacity\n", length);
}
else
{
status = SUCCESS;
strncpy(buffer, "test_push_back_doubles_capacity_of_default_str_as_needed\n", length);
}
}
hString->destroy(&hString);
return status;
}
Status test_pop_back_removes_a_single_element_per_iteration(char* buffer, int length)
{
Status status;
int size;
MY_STRING hString;
hString = my_string_init_c_str("Test1");
size = hString->get_size(hString);
hString->pop_back(hString);
/*size -1 for the correct index, and -1 for the removed element*/
if (*hString->at(hString, size - 2) != 't')
{
status = FAILURE;
printf("Given the string 'Test1', the last element after pop back should be 't' but was not\n");
strncpy(buffer, "test_pop_back_removes_a_single_element_per_iteration\n"
"\tPop function does not remove elements from the given string correctly\n", length);
}
else
{
status = SUCCESS;
strncpy(buffer, "test_pop_back_removes_a_single_element_per_iteration\n", length);
}
hString->destroy(&hString);
return status;
}
Status test_pop_back_returns_failure_when_size_0(char* buffer, int length)
{
MY_STRING hString = my_string_init_default();
Status status, test;
test = hString->pop_back(hString);
if (test == FAILURE)
{
status = SUCCESS;
strncpy(buffer, "test_pop_back_returns_failure_when_size_0\n", length);
}
else
{
status = FAILURE;
printf("Pop_back does not fail when size is equal to zero\n");
strncpy(buffer, "test_pop_back_returns_failure_when_size_0\n"
"\tpop_back does not fail when necessary", length);
}
return status;
}
Status test_pop_back_size_decrease(char* buffer, int length)
{
MY_STRING hString = my_string_init_c_str("Test1");
Status status;
int number;
number = hString->get_size(hString);
hString->pop_back(hString);
if (hString->get_size(hString) == (number - 1))
{
status = SUCCESS;
strncpy(buffer, "test_pop_back_size_decrease\n", length);
}
else
{
status = FAILURE;
printf("Pop_back fails to decrease the size of the vector\n");
strncpy(buffer, "test_pop_back_size_decrease\n"
"\tpop_back did not successfully decrease size", length);
}
hString->destroy(&hString);
return status;
}
Status test_at_returns_the_correct_element(char* buffer, int length)
{
Status status;
int i;
char alpha[] = "abc";
MY_STRING hString;
hString = my_string_init_c_str("abc");
for (i = 0; i < 3 && *hString->at(hString, i) == alpha[i]; i++);
if (i == 3)
{
status = SUCCESS;
strncpy(buffer, "test_at_returns_the_correct_element\n", length);
}
else
{
status = FAILURE;
printf("Given the string 'abc', the at function did not correctly return each element\n");
strncpy(buffer, "test_at_returns_the_correct_element\n"
"\tThe at function does not correctly return the requested element\n", length);
}
hString->destroy(&hString);
return status;
}
Status test_at_cannot_return_an_element_that_is_out_of_range(char* buffer, int length)
{
Status status;
int i;
MY_STRING hString;
hString = my_string_init_c_str("Test1");
i = hString->get_size(hString);
if (hString->at(hString, i) != NULL)
{
status = FAILURE;
printf("At function attempted to return the value within an element that was out of range rather than NULL\n");
strncpy(buffer, "test_at_cannot_return_an_element_that_is_out_of_range\n"
"\tThe at function does not correctly handle requests that are out of range\n", length);
}
else
{
status = SUCCESS;
strncpy(buffer, "test_at_cannot_return_an_element_that_is_out_of_range\n", length);
}
hString->destroy(&hString);
return status;
}
Status test_concatenate_creates_enough_space_to_combine_characters(char* buffer, int length)
{
Status status;
int size;
char concat[] = "Test String";
MY_STRING hString1, hString2;
hString1 = my_string_init_c_str("Test");
hString2 = my_string_init_c_str("String");
size = (hString1->get_size(hString1) + hString2->get_size(hString2) + 1);
hString1->concat(hString1, hString2);
if (size < hString1->get_size(hString1))
{
status = FAILURE;
printf("Concatenate function failed to create enough space to combine the two strings\n");
strncpy(buffer, "test_concatenate_creates_enough_space_to_combine_characters\n"
"Concatenate function does not properly allocate additional space\n", length);
}
else
{
status = SUCCESS;
strncpy(buffer, "test_concatenate_creates_enough_space_to_combine_characters\n", length);
}
hString1->destroy(&hString1);
hString2->destroy(&hString2);
return status;
}
Status test_concatenate_combines_two_strings_in_the_correct_order(char* buffer, int length)
{
Status status;
int i, size;
char concat[] = "TestString";
MY_STRING hString1, hString2;
hString1 = my_string_init_c_str("Test");
hString2 = my_string_init_c_str("String");
size = (hString1->get_size(hString1) + hString2->get_size(hString2));
hString1->concat(hString1, hString2);
if (*hString1->at(hString1, 0) == 'S')
{
status = FAILURE;
printf("Concatenate function combines the two strings in the wrong order\n");
strncpy(buffer, "test_concatenate_combines_two_strings_in_the_correct_order\n"
"Concatenate function does not properly combine two strings", length);
}
else
{
for (i = 0; i < size && *hString1->at(hString1, i) == concat[i]; i++);
if (i != size)
{