/
test_libDict.c
188 lines (164 loc) · 4.43 KB
/
test_libDict.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
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include "libDict.h"
#define BUFF_LEN 1024
int test_hash(){
printf("BEGIN: %s\n", __FUNCTION__);
char buff[BUFF_LEN];
int numKeys = 1<<20;
int first=1;
for(int m=1<<8;m<1<<15;m=m<<1){
int h[m]; // to keep collisions
for(int i=0;i<m;i++)h[i]=0; // clear collisions
for(int i=0;i<numKeys;i++){
sprintf(buff, "key %d", i);
// printf("hash(%s,%d)=%d\n", buff,m,dictHash(buff,m));
h[dictHash(buff, m)]++;
}
/**
* Lets say hash is good if the distribution essentially
* looks uniform. All entries should be within (numKeys/m)/2 and (numKeys/m)*2
*/
int collision_min = (numKeys/m)/2, collision_max = (numKeys/m)*2;
int numBadRows=0;
for(int i=0;i<m;i++){
if(! (collision_min<h[i] && h[i]<collision_max) ){
numBadRows++;
}
}
printf("HASH: numBadRows/numRows = %d/%d\n", numBadRows, m);
/**
if(first){
first = !first;
for(int i=0;i<m;i++)printf("h[%d]=%d\n", i,h[i]);
}
**/
}
printf("END : %s\n", __FUNCTION__);
}
int test_dictNew(){
printf("BEGIN: %s\n", __FUNCTION__);
Dict *d=dictNew(10);
dictPrint(d,2);
dictFree(d);
printf("END : %s\n", __FUNCTION__);
}
int test_dictGet_empty(){
printf("BEGIN: %s\n", __FUNCTION__);
Dict *d=dictNew(10);
char *keys[]={ "", "a", "aa", "ab", "this", "that", "this is a very long string with lots of characters"};
int keysLen = sizeof(keys)/sizeof(keys[0]); // aka sizeof(keys)/sizeof(char *);
for(int i=0;i<keysLen;i++){
DictEntry *de = dictGet(d,keys[i]);
if(de==NULL){
// printf("OK : searched for %s on empty Dict and result was NULL\n", keys[i]);
}else{
printf("ERROR: searched for %s on empty Dict and result was NOT NULL\n", keys[i]);
}
}
dictFree(d);
printf("END : %s\n", __FUNCTION__);
}
/**
* Check that words at index%m==t are exactly the words
* in the dictionary
*/
int checkWords(Dict *d, FILE *fp, int numWords, int m, int t){
printf("BEGIN: %s\n", __FUNCTION__);
int numWordsProcessed = 0;
char buff[BUFF_LEN];
rewind(fp);
while(fgets(buff, BUFF_LEN, fp)!=NULL){
buff[strcspn(buff, "\r\n")] = '\0'; // remove trailing newline
DictEntry *de=dictGet(d,buff);
if(numWordsProcessed%m==t){
if(de==NULL){
printf("ERROR: Key %s not found\n", buff);
continue;
}
char *key=de->key, *value=de->value;
if(strcmp(buff, de->value)!=0){
printf("ERROR: %s found, but value incorrect\n", buff);
continue;
}
} else {
if(de!=NULL){
printf("ERROR: Deleted key %s still in Dict\n", buff);
continue;
}
}
numWordsProcessed++;
if(numWordsProcessed==numWords)break;
}
printf("END : %s\n", __FUNCTION__);
}
/**
* Add words at index%m==t to the dictionary
*/
int addWords(Dict *d, FILE *fp, int numWords, int m, int t){
printf("BEGIN: %s\n", __FUNCTION__);
int numWordsProcessed = 0;
char buff[BUFF_LEN];
rewind(fp);
numWordsProcessed = 0;
while(fgets(buff, BUFF_LEN, fp)!=NULL){
buff[strcspn(buff, "\r\n")] = '\0'; // remove trailing newline
if(numWordsProcessed%m==t){
dictPut(d,buff,strdup(buff));
}
numWordsProcessed++;
if(numWordsProcessed==numWords)break;
}
printf("END : %s\n", __FUNCTION__);
}
/**
* Delete words at index%m==t from the dictionary
*/
int delWords(Dict *d, FILE *fp, int numWords, int m, int t){
printf("BEGIN: %s\n", __FUNCTION__);
int numWordsProcessed = 0;
char buff[BUFF_LEN];
rewind(fp);
while(fgets(buff, BUFF_LEN, fp)!=NULL){
buff[strcspn(buff, "\r\n")] = '\0'; // remove trailing newline
if(numWordsProcessed%m==t){
DictEntry *de=dictGet(d,buff);
if(de!=NULL){
char *key=de->key, *value=de->value;
if(!dictDel(d,buff))printf("ERROR (delete): Can't delete %s\n", buff);
free(value);
}
}
numWordsProcessed++;
if(numWordsProcessed==numWords)break;
}
printf("END : %s\n", __FUNCTION__);
}
int test_stressTest(int numRows, int numWords){
printf("BEGIN: %s\n", __FUNCTION__);
int numWordsProcessed = 0;
char buff[BUFF_LEN];
Dict *d=dictNew(numRows);
char *fileName = "/usr/share/dict/words";
FILE *fp = fopen(fileName,"r");
if(fp==NULL){
perror(fileName);
exit(1);
}
addWords(d,fp,numWords,1,0);
checkWords(d,fp,numWords,1,0);
delWords(d,fp,numWords,2,1);
checkWords(d,fp,numWords,2,0);
delWords(d,fp,numWords,2,0);
checkWords(d,fp,numWords,2,3); // No words should be present
fclose(fp);
dictFree(d);
printf("END : %s\n", __FUNCTION__);
}
int main(int argc, char **argv){
test_dictNew();
test_hash();
test_dictGet_empty();
test_stressTest(11,150);
}