-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtestsymtable.c
968 lines (734 loc) · 26.6 KB
/
testsymtable.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
/*--------------------------------------------------------------------*/
/* testsymtable.c */
/* Author: Bob Dondero */
/*--------------------------------------------------------------------*/
#include "symtable.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <assert.h>
#ifndef S_SPLINT_S
#include <sys/resource.h>
#endif
/*--------------------------------------------------------------------*/
#define ASSURE(i) assure(i, __LINE__)
/*--------------------------------------------------------------------*/
/* If !iSuccessful, print a message to stdout indicating that the
test at line iLineNum failed. */
static void assure(int iSuccessful, int iLineNum)
{
if (! iSuccessful)
{
printf("Test at line %d failed.\n", iLineNum);
fflush(stdout);
}
}
/*--------------------------------------------------------------------*/
#ifndef S_SPLINT_S
/* Set the process's "CPU time" resource limit. After the CPU
time limit expires, the OS will send a SIGKILL signal to the
process. */
static void setCpuTimeLimit(void)
{
enum {CPU_TIME_LIMIT_IN_SECONDS = 300};
struct rlimit sRlimit;
sRlimit.rlim_cur = CPU_TIME_LIMIT_IN_SECONDS;
sRlimit.rlim_max = CPU_TIME_LIMIT_IN_SECONDS;
setrlimit(RLIMIT_CPU, &sRlimit);
}
#endif
/*--------------------------------------------------------------------*/
/* Write the binding whose key is pcKey and whose string value is
pvValue using format string pvExtra. */
static void printBinding(const char *pcKey, void *pvValue,
void *pvExtra)
{
assert(pcKey != NULL);
assert(pvValue != NULL);
assert(pvExtra != NULL);
printf((char*)pvExtra, pcKey, (char*)pvValue);
fflush(stdout);
}
/*--------------------------------------------------------------------*/
/* Write the binding whose key is pcKey and whose string value is
pvValue. pvExtra is unused. */
static void printBindingSimple(const char *pcKey, void *pvValue,
void *pvExtra)
{
assert(pcKey != NULL);
assert(pvValue != NULL);
assert(pvExtra == NULL);
printf("%s\t%s\n", pcKey, (char*)pvValue);
fflush(stdout);
}
/*--------------------------------------------------------------------*/
/* Test the most basic SymTable functions. */
static void testBasics(void)
{
SymTable_T oSymTable;
char acJeter[] = "Jeter";
char acMantle[] = "Mantle";
char acGehrig[] = "Gehrig";
char acRuth[] = "Ruth";
char acShortstop[] = "Shortstop";
char acCenterField[] = "Center Field";
char acFirstBase[] = "First Base";
char acRightField[] = "Right Field";
char acBrown[] = "Brown";
char *pcValue;
int iSuccessful;
int iFound;
size_t uLength;
printf("------------------------------------------------------\n");
printf("Testing the most basic SymTable functions.\n");
printf("No output should appear here:\n");
fflush(stdout);
/* Test SymTable_new(). */
oSymTable = SymTable_new();
ASSURE(oSymTable != NULL);
/* Test SymTable_put() and SymTable_getLength(). */
iSuccessful = SymTable_put(oSymTable, acJeter, acShortstop);
ASSURE(iSuccessful);
uLength = SymTable_getLength(oSymTable);
ASSURE(uLength == 1);
iSuccessful = SymTable_put(oSymTable, acMantle, acCenterField);
ASSURE(iSuccessful);
uLength = SymTable_getLength(oSymTable);
ASSURE(uLength == 2);
iSuccessful = SymTable_put(oSymTable, acGehrig, acFirstBase);
ASSURE(iSuccessful);
uLength = SymTable_getLength(oSymTable);
ASSURE(uLength == 3);
iSuccessful = SymTable_put(oSymTable, acRuth, acRightField);
ASSURE(iSuccessful);
uLength = SymTable_getLength(oSymTable);
ASSURE(uLength == 4);
/* Try to insert duplicate to first key entered */
iSuccessful = SymTable_put(oSymTable, acJeter, acCenterField);
ASSURE(! iSuccessful);
uLength = SymTable_getLength(oSymTable);
ASSURE(uLength == 4);
/* Try to insert duplicate to last key entered */
iSuccessful = SymTable_put(oSymTable, acRuth, acCenterField);
ASSURE(! iSuccessful);
uLength = SymTable_getLength(oSymTable);
ASSURE(uLength == 4);
/* Test SymTable_contains(). */
iFound = SymTable_contains(oSymTable, acJeter);
ASSURE(iFound);
iFound = SymTable_contains(oSymTable, acMantle);
ASSURE(iFound);
iFound = SymTable_contains(oSymTable, acGehrig);
ASSURE(iFound);
iFound = SymTable_contains(oSymTable, acRuth);
ASSURE(iFound);
iFound = SymTable_contains(oSymTable, "Clemens");
ASSURE(! iFound);
iFound = SymTable_contains(oSymTable, "Maris");
ASSURE(! iFound);
/* Test SymTable_get(). */
pcValue = (char*)SymTable_get(oSymTable, acJeter);
ASSURE(pcValue == acShortstop);
pcValue = (char*)SymTable_get(oSymTable, acMantle);
ASSURE(pcValue == acCenterField);
pcValue = (char*)SymTable_get(oSymTable, acGehrig);
ASSURE(pcValue == acFirstBase);
pcValue = (char*)SymTable_get(oSymTable, acRuth);
ASSURE(pcValue == acRightField);
pcValue = (char*)SymTable_get(oSymTable, "Clemens");
ASSURE(pcValue == NULL);
pcValue = (char*)SymTable_get(oSymTable, "Maris");
ASSURE(pcValue == NULL);
/* Test SymTable_replace(). */
pcValue = (char*)
SymTable_replace(oSymTable, acMantle, acFirstBase);
ASSURE(pcValue == acCenterField);
uLength = SymTable_getLength(oSymTable);
ASSURE(uLength == 4);
pcValue = (char*)SymTable_get(oSymTable, acMantle);
ASSURE(pcValue == acFirstBase);
pcValue = (char*)
SymTable_replace(oSymTable, "Clemens", acRightField);
ASSURE(pcValue == NULL);
pcValue = (char*)SymTable_replace(oSymTable, "Maris", acRightField);
ASSURE(pcValue == NULL);
uLength = SymTable_getLength(oSymTable);
ASSURE(uLength == 4);
/* Insert key with NULL value */
iSuccessful = SymTable_put(oSymTable, acBrown, NULL);
ASSURE(iSuccessful);
uLength = SymTable_getLength(oSymTable);
ASSURE(uLength == 5);
/* Try to insert duplicate of key that had NULL value */
iSuccessful = SymTable_put(oSymTable, acBrown, acShortstop);
ASSURE(! iSuccessful);
uLength = SymTable_getLength(oSymTable);
ASSURE(uLength == 5);
/* Test SymTable_free(). */
SymTable_free(oSymTable);
}
/*--------------------------------------------------------------------*/
/* Test handling of key comparisons. */
static void testKeyComparison(void)
{
SymTable_T oSymTable;
char acJeter[] = "Jeter";
char acJeter2[] = "Jeter";
char acShortstop[] = "Shortstop";
char *pcValue;
int iSuccessful;
printf("------------------------------------------------------\n");
printf("Testing key comparison.\n");
printf("No output should appear here:\n");
fflush(stdout);
oSymTable = SymTable_new();
ASSURE(oSymTable != NULL);
iSuccessful = SymTable_put(oSymTable, acJeter, acShortstop);
ASSURE(iSuccessful);
iSuccessful = SymTable_put(oSymTable, acJeter2, acShortstop);
ASSURE(! iSuccessful);
pcValue = (char*)SymTable_get(oSymTable, acJeter2);
ASSURE(pcValue == acShortstop);
SymTable_free(oSymTable);
}
/*--------------------------------------------------------------------*/
/* Test handling of key ownership. */
static void testKeyOwnership(void)
{
enum {MAX_KEY_LENGTH = 10};
SymTable_T oSymTable;
char acKey[MAX_KEY_LENGTH];
char *pcValue;
int iSuccessful;
char acCenterField[] = "CenterField";
printf("------------------------------------------------------\n");
printf("Testing key ownership.\n");
printf("No output should appear here:\n");
fflush(stdout);
oSymTable = SymTable_new();
ASSURE(oSymTable != NULL);
strcpy(acKey, "Mantle");
iSuccessful = SymTable_put(oSymTable, acKey, acCenterField);
ASSURE(iSuccessful);
strcpy(acKey, "xxx");
pcValue = (char*)SymTable_get(oSymTable, "Mantle");
ASSURE(pcValue == acCenterField);
SymTable_free(oSymTable);
}
/*--------------------------------------------------------------------*/
/* Test the SymTable_remove() function. */
static void testRemove(void)
{
SymTable_T oSymTable;
char acJeter[] = "Jeter";
char acMantle[] = "Mantle";
char acGehrig[] = "Gehrig";
char acRuth[] = "Ruth";
char acShortstop[] = "Shortstop";
char acCenterField[] = "Center Field";
char acFirstBase[] = "First Base";
char acRightField[] = "Right Field";
char *pcValue;
int iFound;
size_t uLength;
int iSuccessful;
printf("------------------------------------------------------\n");
printf("Testing the SymTable_remove() function.\n");
printf("No output should appear here:\n");
fflush(stdout);
oSymTable = SymTable_new();
ASSURE(oSymTable != NULL);
iSuccessful = SymTable_put(oSymTable, acJeter, acShortstop);
ASSURE(iSuccessful);
iSuccessful = SymTable_put(oSymTable, acMantle, acCenterField);
ASSURE(iSuccessful);
iSuccessful = SymTable_put(oSymTable, acGehrig, acFirstBase);
ASSURE(iSuccessful);
iSuccessful = SymTable_put(oSymTable, acRuth, acRightField);
ASSURE(iSuccessful);
uLength = SymTable_getLength(oSymTable);
ASSURE(uLength == 4);
pcValue = (char*)SymTable_remove(oSymTable, acJeter);
ASSURE(pcValue == acShortstop);
uLength = SymTable_getLength(oSymTable);
ASSURE(uLength == 3);
iFound = SymTable_contains(oSymTable, acJeter);
ASSURE(! iFound);
pcValue = (char*)SymTable_remove(oSymTable, acRuth);
ASSURE(pcValue == acRightField);
uLength = SymTable_getLength(oSymTable);
ASSURE(uLength == 2);
iFound = SymTable_contains(oSymTable, acRuth);
ASSURE(! iFound);
pcValue = (char*)SymTable_remove(oSymTable, "Clemens");
ASSURE(pcValue == NULL);
uLength = SymTable_getLength(oSymTable);
ASSURE(uLength == 2);
iFound = SymTable_contains(oSymTable, "Clemens");
ASSURE(! iFound);
pcValue = (char*)SymTable_remove(oSymTable, acRuth);
ASSURE(pcValue == NULL);
uLength = SymTable_getLength(oSymTable);
ASSURE(uLength == 2);
iFound = SymTable_contains(oSymTable, acRuth);
ASSURE(! iFound);
SymTable_free(oSymTable);
}
/*--------------------------------------------------------------------*/
/* Test the SymTable_map() function. */
static void testMap(void)
{
SymTable_T oSymTable;
char acJeter[] = "Jeter";
char acMantle[] = "Mantle";
char acGehrig[] = "Gehrig";
char acRuth[] = "Ruth";
char acShortstop[] = "Shortstop";
char acCenterField[] = "Center Field";
char acFirstBase[] = "First Base";
char acRightField[] = "Right Field";
int iSuccessful;
printf("------------------------------------------------------\n");
printf("Testing the SymTable_map() function.\n");
fflush(stdout);
oSymTable = SymTable_new();
ASSURE(oSymTable != NULL);
iSuccessful = SymTable_put(oSymTable, acJeter, acShortstop);
ASSURE(iSuccessful);
iSuccessful = SymTable_put(oSymTable, acMantle, acCenterField);
ASSURE(iSuccessful);
iSuccessful = SymTable_put(oSymTable, acGehrig, acFirstBase);
ASSURE(iSuccessful);
iSuccessful = SymTable_put(oSymTable, acRuth, acRightField);
ASSURE(iSuccessful);
printf("Four players and their positions should appear here:\n");
fflush(stdout);
SymTable_map(oSymTable, printBinding, "%s\t%s\n");
printf("Four players and their positions should appear here:\n");
fflush(stdout);
SymTable_map(oSymTable, printBindingSimple, NULL);
SymTable_free(oSymTable);
}
/*--------------------------------------------------------------------*/
/* Test a SymTable object that contains no bindings. */
static void testEmptyTable(void)
{
SymTable_T oSymTable;
char *pcValue;
int iFound;
size_t uLength;
printf("------------------------------------------------------\n");
printf("Testing an empty SymTable object.\n");
printf("No output should appear here:\n");
fflush(stdout);
oSymTable = SymTable_new();
ASSURE(oSymTable != NULL);
uLength = SymTable_getLength(oSymTable);
ASSURE(uLength == 0);
iFound = SymTable_contains(oSymTable, "Jeter");
ASSURE(! iFound);
pcValue = (char*)SymTable_get(oSymTable, "Jeter");
ASSURE(pcValue == NULL);
pcValue = (char*)SymTable_remove(oSymTable, "Jeter");
ASSURE(pcValue == NULL);
SymTable_map(oSymTable, printBinding, "%s\t%s\n");
SymTable_free(oSymTable);
}
/*--------------------------------------------------------------------*/
/* Test the ability of a SymTable object to contain an empty key. */
static void testEmptyKey(void)
{
SymTable_T oSymTable;
char *pcValue;
char acShortstop[] = "Shortstop";
int iFound;
size_t uLength;
int iSuccessful;
printf("------------------------------------------------------\n");
printf("Testing a SymTable object that contains an empty key.\n");
fflush(stdout);
oSymTable = SymTable_new();
ASSURE(oSymTable != NULL);
iSuccessful = SymTable_put(oSymTable, "", acShortstop);
ASSURE(iSuccessful);
uLength = SymTable_getLength(oSymTable);
ASSURE(uLength == 1);
iFound = SymTable_contains(oSymTable, "");
ASSURE(iFound);
pcValue = (char*)SymTable_get(oSymTable, "");
ASSURE(pcValue == acShortstop);
printf("An empty name and a position should appear here:\n");
fflush(stdout);
SymTable_map(oSymTable, printBinding, "%s\t%s\n");
pcValue = (char*)SymTable_remove(oSymTable, "");
ASSURE(pcValue == acShortstop);
SymTable_free(oSymTable);
}
/*--------------------------------------------------------------------*/
/* Test the ability of a SymTable object to contain NULL values. */
static void testNullValue(void)
{
SymTable_T oSymTable;
char *pcValue;
int iFound;
size_t uLength;
int iSuccessful;
char acCenterField[] = "Center Field";
printf("------------------------------------------------------\n");
printf("Testing a SymTable object that contains NULL values.\n");
printf("No output should appear here:\n");
fflush(stdout);
oSymTable = SymTable_new();
ASSURE(oSymTable != NULL);
iSuccessful = SymTable_put(oSymTable, "Jeter", NULL);
ASSURE(iSuccessful);
uLength = SymTable_getLength(oSymTable);
ASSURE(uLength == 1);
iFound = SymTable_contains(oSymTable, "Jeter");
ASSURE(iFound);
pcValue = (char*)SymTable_get(oSymTable, "Jeter");
ASSURE(pcValue == NULL);
pcValue = (char*)SymTable_remove(oSymTable, "Jeter");
ASSURE(pcValue == NULL);
iSuccessful = SymTable_put(oSymTable, "Mantle", acCenterField);
ASSURE(iSuccessful);
uLength = SymTable_getLength(oSymTable);
ASSURE(uLength == 1);
iFound = SymTable_contains(oSymTable, "Mantle");
ASSURE(iFound);
pcValue = (char*)SymTable_get(oSymTable, "Mantle");
ASSURE(pcValue == acCenterField);
pcValue = SymTable_replace(oSymTable, "Mantle", NULL);
ASSURE(pcValue == acCenterField);
iFound = SymTable_contains(oSymTable, "Mantle");
ASSURE(iFound);
pcValue = (char*)SymTable_get(oSymTable, "Mantle");
ASSURE(pcValue == NULL);
pcValue = (char*)SymTable_remove(oSymTable, "Mantle");
ASSURE(pcValue == NULL);
SymTable_free(oSymTable);
}
/*--------------------------------------------------------------------*/
/* Test the ability of a SymTable object to contain long keys. */
static void testLongKey(void)
{
enum {KEY_SIZE = 1000};
SymTable_T oSymTable;
char acKeyA[KEY_SIZE];
char acKeyB[KEY_SIZE];
char *pcValue;
char acShortstop[] = "Shortstop";
int iFound;
int i;
size_t uLength;
int iSuccessful;
printf("------------------------------------------------------\n");
printf("Testing a SymTable object that contains long keys.\n");
printf("No output should appear here:\n");
fflush(stdout);
for (i = 0; i < KEY_SIZE - 1; i++)
acKeyA[i] = 'a';
acKeyA[KEY_SIZE - 1] = '\0';
for (i = 0; i < KEY_SIZE - 1; i++)
acKeyB[i] = 'b';
acKeyB[KEY_SIZE - 1] = '\0';
oSymTable = SymTable_new();
ASSURE(oSymTable != NULL);
iSuccessful = SymTable_put(oSymTable, acKeyA, acShortstop);
ASSURE(iSuccessful);
iSuccessful = SymTable_put(oSymTable, acKeyB, acShortstop);
ASSURE(iSuccessful);
iSuccessful = SymTable_put(oSymTable, acKeyB, acShortstop);
ASSURE(! iSuccessful);
uLength = SymTable_getLength(oSymTable);
ASSURE(uLength == 2);
iFound = SymTable_contains(oSymTable, acKeyA);
ASSURE(iFound);
iFound = SymTable_contains(oSymTable, acKeyB);
ASSURE(iFound);
pcValue = (char*)SymTable_get(oSymTable, acKeyA);
ASSURE(pcValue == acShortstop);
pcValue = (char*)SymTable_get(oSymTable, acKeyB);
ASSURE(pcValue == acShortstop);
pcValue = (char*)SymTable_remove(oSymTable, acKeyB);
ASSURE(pcValue == acShortstop);
pcValue = (char*)SymTable_remove(oSymTable, acKeyB);
ASSURE(pcValue == NULL);
pcValue = (char*)SymTable_remove(oSymTable, acKeyA);
ASSURE(pcValue == acShortstop);
SymTable_free(oSymTable);
}
/*--------------------------------------------------------------------*/
/* Test the ability of SymTable object to have values that are
other SymTable objects. */
static void testTableOfTables(void)
{
SymTable_T oSymTable;
SymTable_T oSymTable1;
SymTable_T oSymTable2;
SymTable_T oSymTableRet;
char acShortstop[] = "Shortstop";
char acCenterField[] = "CenterField";
char *pcValue;
int iSuccessful;
printf("------------------------------------------------------\n");
printf("Testing a SymTable object that contains other\n");
printf("SymTable objects.\n");
printf("No output should appear here:\n");
fflush(stdout);
oSymTable1 = SymTable_new();
ASSURE(oSymTable1 != NULL);
iSuccessful = SymTable_put(oSymTable1, "Jeter", acShortstop);
ASSURE(iSuccessful);
oSymTable2 = SymTable_new();
ASSURE(oSymTable2 != NULL);
iSuccessful = SymTable_put(oSymTable2, "Mantle", acCenterField);
ASSURE(iSuccessful);
oSymTable = SymTable_new();
ASSURE(oSymTable != NULL);
iSuccessful = SymTable_put(oSymTable, "first table", oSymTable1);
ASSURE(iSuccessful);
iSuccessful = SymTable_put(oSymTable, "second table", oSymTable2);
ASSURE(iSuccessful);
oSymTableRet = (SymTable_T)SymTable_get(oSymTable, "first table");
ASSURE(oSymTableRet == oSymTable1);
pcValue = (char*)SymTable_get(oSymTableRet, "Jeter");
ASSURE(pcValue == acShortstop);
oSymTableRet = (SymTable_T)SymTable_get(oSymTable, "second table");
ASSURE(oSymTableRet == oSymTable2);
pcValue = (char*)SymTable_get(oSymTableRet, "Mantle");
ASSURE(pcValue == acCenterField);
SymTable_free(oSymTable2);
SymTable_free(oSymTable1);
SymTable_free(oSymTable);
}
/*--------------------------------------------------------------------*/
/* Test the ability of a SymTable object to handle collisions. This
test assumes that a SymTable object is implemented as a hash table,
that there are 509 buckets in the hash table, and that the
implementation uses the hash function provided in the assignment
specification. */
static void testCollisions(void)
{
SymTable_T oSymTable;
int iSuccessful;
char acCenterField[] = "pitcher";
char acCatcher[] = "catcher";
char acFirstBase[] = "first base";
char acRightField[] = "second base";
char *pcValue;
printf("------------------------------------------------------\n");
printf("Testing the collision handling of a SymTable object\n");
printf("assuming a hash table implementation, and assuming that\n");
printf("the implementation uses the hash function from the\n");
printf("assignment specification.\n");
printf("No output should appear here:\n");
fflush(stdout);
oSymTable = SymTable_new();
ASSURE(oSymTable != NULL);
/* Note that strings "250", "469", "947", "1303", and "2016" hash
to the same bucket -- bucket 123. */
iSuccessful = SymTable_put(oSymTable, "250", acCenterField);
ASSURE(iSuccessful);
iSuccessful = SymTable_put(oSymTable, "469", acCatcher);
ASSURE(iSuccessful);
iSuccessful = SymTable_put(oSymTable, "947", acFirstBase);
ASSURE(iSuccessful);
iSuccessful = SymTable_put(oSymTable, "1303", acRightField);
ASSURE(iSuccessful);
iSuccessful = SymTable_put(oSymTable, "2016", acRightField);
ASSURE(iSuccessful);
pcValue = SymTable_get(oSymTable, "250");
ASSURE(pcValue == acCenterField);
pcValue = SymTable_get(oSymTable, "469");
ASSURE(pcValue == acCatcher);
pcValue = SymTable_get(oSymTable, "947");
ASSURE(pcValue == acFirstBase);
pcValue = SymTable_get(oSymTable, "1303");
ASSURE(pcValue == acRightField);
pcValue = SymTable_get(oSymTable, "2016");
ASSURE(pcValue == acRightField);
pcValue = SymTable_remove(oSymTable, "947");
ASSURE(pcValue == acFirstBase);
pcValue = SymTable_remove(oSymTable, "2016");
ASSURE(pcValue == acRightField);
pcValue = SymTable_remove(oSymTable, "250");
ASSURE(pcValue == acCenterField);
pcValue = SymTable_get(oSymTable, "469");
ASSURE(pcValue == acCatcher);
pcValue = SymTable_get(oSymTable, "1303");
ASSURE(pcValue == acRightField);
SymTable_free(oSymTable);
}
/*--------------------------------------------------------------------*/
/* Test the ability of a SymTable object to be large, that is, to
contain iBindingCount bindings. Write the time consumed to stdout. */
static void testLargeTable(int iBindingCount)
{
enum {MAX_KEY_LENGTH = 10};
SymTable_T oSymTable;
SymTable_T oSymTableSmall;
char acKey[MAX_KEY_LENGTH];
char *pcValue;
int i;
int iSmall;
int iLarge;
int iSuccessful;
clock_t iInitialClock;
clock_t iFinalClock;
size_t uLength = 0;
size_t uLength2;
printf("------------------------------------------------------\n");
printf("Testing a potentially large SymTable object.\n");
printf("No output except CPU time consumed should appear here:\n");
fflush(stdout);
/* Note the current time. */
iInitialClock = clock();
/* Create oSymTableSmall, and put a couple of bindings into it. */
oSymTableSmall = SymTable_new();
ASSURE(oSymTableSmall != NULL);
iSuccessful = SymTable_put(oSymTableSmall, "xxx", "xxx");
ASSURE(iSuccessful);
iSuccessful = SymTable_put(oSymTableSmall, "yyy", "yyy");
ASSURE(iSuccessful);
/* Create oSymTable, the primary SymTable object. */
oSymTable = SymTable_new();
ASSURE(oSymTable != NULL);
/* Put iBindingCount new bindings into oSymTable. Each binding's
key and value contain the same characters. */
for (i = 0; i < iBindingCount; i++)
{
sprintf(acKey, "%d", i);
pcValue = (char*)malloc(sizeof(char) * (strlen(acKey) + 1));
ASSURE(pcValue != NULL);
strcpy(pcValue, acKey);
iSuccessful = SymTable_put(oSymTable, acKey, pcValue);
ASSURE(iSuccessful);
uLength = SymTable_getLength(oSymTable);
ASSURE(uLength == (size_t)(i+1));
}
/* Get each binding's value, and make sure that it contains
the same characters as its key. */
iSmall = 0;
iLarge = iBindingCount - 1;
while (iSmall < iLarge)
{
/* Get the smallest of the remaining bindings. */
sprintf(acKey, "%d", iSmall);
pcValue = (char*)SymTable_get(oSymTable, acKey);
ASSURE(pcValue != NULL);
ASSURE((pcValue != NULL) && (strcmp(pcValue, acKey) == 0));
iSmall++;
/* Get the largest of the remaining bindings. */
sprintf(acKey, "%d", iLarge);
pcValue = (char*)SymTable_get(oSymTable, acKey);
ASSURE(pcValue != NULL);
ASSURE((pcValue != NULL) && (strcmp(pcValue, acKey) == 0));
iLarge--;
}
/* Get the middle binding -- if there is one. */
if (iSmall == iLarge)
{
sprintf(acKey, "%d", iSmall);
pcValue = (char*)SymTable_get(oSymTable, acKey);
ASSURE(pcValue != NULL);
ASSURE((pcValue != NULL) && (strcmp(pcValue, acKey) == 0));
}
/* Remove each binding. Also free each binding's value. */
iSmall = 0;
iLarge = iBindingCount - 1;
while (iSmall < iLarge)
{
/* Remove the smallest of the remaining bindings. */
sprintf(acKey, "%d", iSmall);
pcValue = (char*)SymTable_remove(oSymTable, acKey);
ASSURE(pcValue != NULL);
ASSURE((pcValue != NULL) && (strcmp(pcValue, acKey) == 0));
free(pcValue);
uLength--;
uLength2 = SymTable_getLength(oSymTable);
ASSURE(uLength2 == uLength);
iSmall++;
/* Remove the largest of the remaining bindings. */
sprintf(acKey, "%d", iLarge);
pcValue = (char*)SymTable_remove(oSymTable, acKey);
ASSURE(pcValue != NULL);
ASSURE((pcValue != NULL) && (strcmp(pcValue, acKey) == 0));
free(pcValue);
uLength--;
uLength2 = SymTable_getLength(oSymTable);
ASSURE(uLength2 == uLength);
iLarge--;
}
/* Remove the middle binding -- if there is one. */
if (iSmall == iLarge)
{
sprintf(acKey, "%d", iSmall);
pcValue = (char*)SymTable_remove(oSymTable, acKey);
ASSURE(pcValue != NULL);
ASSURE((pcValue != NULL) && (strcmp(pcValue, acKey) == 0));
free(pcValue);
uLength--;
uLength2 = SymTable_getLength(oSymTable);
ASSURE(uLength2 == uLength);
}
/* Make sure oSymTableSmall hasn't been corrupted by expansion
of oSymTable. */
pcValue = (char*)SymTable_get(oSymTableSmall, "xxx");
ASSURE((pcValue != NULL) && (strcmp(pcValue, "xxx") == 0));
pcValue = (char*)SymTable_get(oSymTableSmall, "yyy");
ASSURE((pcValue != NULL) && (strcmp(pcValue, "yyy") == 0));
/* Free both SymTable objects. */
SymTable_free(oSymTable);
SymTable_free(oSymTableSmall);
/* Note the current time, and print the time consumed to stdout. */
iFinalClock = clock();
printf("CPU time (%d bindings): %f seconds\n", iBindingCount,
((double)(iFinalClock - iInitialClock)) / CLOCKS_PER_SEC);
fflush(stdout);
}
/*--------------------------------------------------------------------*/
/* Test the SymTable ADT. Write the output of the tests to stdout.
As always, argc is the command-line argument count, argv contains
the command-line arguments, and argv[0] is the name of the
executable binary file. argv[1] is the number of bindings to put
into a potentially large SymTable object. Exit with EXIT_FAILURE
if argv[1] is missing or not numeric. Otherwise return 0. */
int main(int argc, char *argv[])
{
int iBindingCount;
if (argc != 2)
{
fprintf(stderr, "Usage: %s bindingcount\n", argv[0]);
exit(EXIT_FAILURE);
}
if (sscanf(argv[1], "%d", &iBindingCount) != 1)
{
fprintf(stderr, "bindingcount must be numeric\n");
exit(EXIT_FAILURE);
}
if (iBindingCount < 0)
{
fprintf(stderr, "bindingcount cannot be negative\n");
exit(EXIT_FAILURE);
}
#ifndef S_SPLINT_S
setCpuTimeLimit();
#endif
testBasics();
testKeyComparison();
testKeyOwnership();
testRemove();
testMap();
testEmptyTable();
testEmptyKey();
testNullValue();
testLongKey();
testTableOfTables();
testCollisions();
testLargeTable(iBindingCount);
printf("------------------------------------------------------\n");
printf("End of %s.\n", argv[0]);
return 0;
}