18 #include <my_global.h>
23 #define MAX_RECORDS 100000
26 static int get_options(
int argc,
char *argv[]);
28 static int rnd(
int max_value);
30 static uint testflag=0,recant=10000,reclength=37;
31 static uint16 key1[1000];
34 #define hash_check(A) 0
36 my_bool hash_check(
HASH *hash);
39 void free_record(
void *
record);
41 static uchar *hash2_key(
const uchar *
rec,uint *length,
42 my_bool not_used __attribute__((unused)))
44 *length=(uint) (uchar) rec[reclength-1];
50 int main(
int argc,
char *argv[])
53 DBUG_PROCESS(argv[0]);
55 get_options(argc,argv);
64 uint write_count,update,
delete;
66 unsigned long key_check;
67 char *
record,*recpos,oldrecord[120],key[10];
69 DBUG_ENTER(
"do_test");
71 write_count=update=
delete=0;
73 memset(key1, 0,
sizeof(key1[0])*1000);
75 printf(
"- Creating hash\n");
76 if (hash_init(&hash, default_charset_info, recant/2, 0, 6, 0, free_record, 0))
78 printf(
"- Writing records:\n");
80 for (i=0 ; i < recant ; i++)
82 n1=rnd(1000); n2=rnd(100); n3=rnd(min(recant*5,MAX_RECORDS));
83 record= (
char*) my_malloc(reclength,MYF(MY_FAE));
84 sprintf(record,
"%6d:%4d:%8d:Pos: %4d ",n1,n2,n3,write_count);
85 if (my_hash_insert(&hash,record))
87 printf(
"Error: %d in write at record: %d\n",my_errno,i);
94 if (hash_check(&hash))
96 puts(
"Heap keys crashed");
100 for (i=0 ; i < write_count/10 ; i++)
102 for (j=rnd(1000) ; j>0 && key1[j] == 0 ; j--) ;
105 sprintf(key,
"%6d",j);
106 if (!(recpos=hash_search(&hash,key,0)))
108 printf(
"can't find key1: \"%s\"\n",key);
111 key1[atoi(recpos)]--;
112 key_check-=atoi(recpos);
113 memcpy(oldrecord,recpos,reclength);
114 if (hash_delete(&hash,recpos))
116 printf(
"error: %d; can't delete record: \"%s\"\n", my_errno,oldrecord);
120 if (testflag == 2 && hash_check(&hash))
122 puts(
"Heap keys crashed");
127 if (hash_check(&hash))
129 puts(
"Hash keys crashed");
133 printf(
"- Update\n");
134 for (i=0 ; i < write_count/10 ; i++)
136 n1=rnd(1000); n2=rnd(100); n3=rnd(min(recant*2,MAX_RECORDS));
137 for (j=rnd(1000) ; j>0 && key1[j] == 0 ; j--) ;
140 sprintf(key,
"%6d",j);
141 if (!(recpos=hash_search(&hash,key,0)))
143 printf(
"can't find key1: \"%s\"\n",key);
146 key1[atoi(recpos)]--;
147 key_check=key_check-atoi(recpos)+n1;
149 sprintf(recpos,
"%6d:%4d:%8d:XXX: %4d ",n1,n2,n3,update);
151 if (hash_update(&hash,recpos,key,0))
153 printf(
"can't update key1: \"%s\"\n",key);
156 if (testflag == 3 && hash_check(&hash))
158 printf(
"Heap keys crashed for %d update\n",update);
163 if (hash_check(&hash))
165 puts(
"Heap keys crashed");
169 for (j=0 ; j < 1000 ; j++)
174 HASH_SEARCH_STATE state;
175 printf(
"- Testing identical read\n");
176 sprintf(key,
"%6d",j);
178 if (!(recpos= hash_first(&hash, key, 0, &state)))
180 printf(
"can't find key1: \"%s\"\n",key);
183 while (hash_next(&hash, key, 0, &state) && pos < (ulong) (key1[j]+10))
185 if (pos != (ulong) key1[j])
187 printf(
"Found %ld copies of key: %s. Should be %d",pos,key,key1[j]);
191 printf(
"- Creating output heap-file 2\n");
192 if (hash_init(&hash2, default_charset_info, hash.records, 0, 0, hash2_key, free_record,0))
195 printf(
"- Copying and removing records\n");
197 while ((recpos=hash_element(&hash,0)))
199 record=(uchar*) my_malloc(reclength,MYF(MY_FAE));
200 memcpy(record,recpos,reclength);
201 record[reclength-1]=rnd(5)+1;
202 if (my_hash_insert(&hash2,record))
204 printf(
"Got error when inserting record: %*s",reclength,record);
207 key_check-=atoi(record);
209 if (hash_delete(&hash,recpos))
211 printf(
"Got error when deleting record: %*s",reclength,recpos);
216 if (hash_check(&hash) || hash_check(&hash2))
218 puts(
"Hash keys crashed");
224 if (hash_check(&hash) || hash_check(&hash2))
226 puts(
"Hash keys crashed");
231 printf(
"Key check didn't get to 0 (%ld)\n",key_check);
234 printf(
"\nFollowing test have been made:\n");
235 printf(
"Write records: %d\nUpdate records: %d\nDelete records: %d\n", write_count,
237 hash_free(&hash); hash_free(&hash2);
238 my_end(MY_GIVE_INFO);
241 printf(
"Got error: %d when using hashing\n",my_errno);
249 static int get_options(
int argc,
char **argv)
255 while (--argc >0 && *(pos = *(++argv)) ==
'-' ) {
261 testflag=atoi(++pos);
266 printf(
"%s Ver 1.0 for %s at %s\n",progname,SYSTEM_TYPE,MACHINE_TYPE);
267 printf(
"MySQL AB, by Monty\n\n");
268 printf(
"Usage: %s [-?ABIKLWv] [-m#] [-t#]\n",progname);
281 static int rnd(
int max_value)
283 return (
int) ((rand() & 32767)/32767.0*max_value);
287 void free_record(
void *record)