1
/* Copyright (C) 2000 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
/* Test of hash library: big test */
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];
33
void free_record(void *record);
35
static uchar *hash2_key(const uchar *rec,uint *length,
36
my_bool not_used __attribute__((unused)))
38
*length=(uint) (uchar) rec[reclength-1];
44
int main(int argc,char *argv[])
48
get_options(argc,argv);
57
uint write_count,update,delete;
59
unsigned long key_check;
60
char *record,*recpos,oldrecord[120],key[10];
63
write_count=update=delete=0;
65
memset((char*) key1, 0, sizeof(key1[0])*1000);
67
printf("- Creating hash\n");
68
if (hash_init(&hash, default_charset_info, recant/2, 0, 6, 0, free_record, 0))
70
printf("- Writing records:\n");
72
for (i=0 ; i < recant ; i++)
74
n1=rnd(1000); n2=rnd(100); n3=rnd(min(recant*5,MAX_RECORDS));
75
record= (char*) my_malloc(reclength,MYF(MY_FAE));
76
sprintf(record,"%6d:%4d:%8d:Pos: %4d ",n1,n2,n3,write_count);
77
if (my_hash_insert(&hash,record))
79
printf("Error: %d in write at record: %d\n",my_errno,i);
87
for (i=0 ; i < write_count/10 ; i++)
89
for (j=rnd(1000) ; j>0 && key1[j] == 0 ; j--) ;
93
if (!(recpos=hash_search(&hash,key,0)))
95
printf("can't find key1: \"%s\"\n",key);
99
key_check-=atoi(recpos);
100
memcpy(oldrecord,recpos,reclength);
101
if (hash_delete(&hash,recpos))
103
printf("error: %d; can't delete record: \"%s\"\n", my_errno,oldrecord);
109
puts("Heap keys crashed");
115
printf("- Update\n");
116
for (i=0 ; i < write_count/10 ; i++)
118
n1=rnd(1000); n2=rnd(100); n3=rnd(min(recant*2,MAX_RECORDS));
119
for (j=rnd(1000) ; j>0 && key1[j] == 0 ; j--) ;
122
sprintf(key,"%6d",j);
123
if (!(recpos=hash_search(&hash,key,0)))
125
printf("can't find key1: \"%s\"\n",key);
128
key1[atoi(recpos)]--;
129
key_check=key_check-atoi(recpos)+n1;
131
sprintf(recpos,"%6d:%4d:%8d:XXX: %4d ",n1,n2,n3,update);
133
if (hash_update(&hash,recpos,key,0))
135
printf("can't update key1: \"%s\"\n",key);
140
printf("Heap keys crashed for %d update\n",update);
146
for (j=0 ; j < 1000 ; j++)
151
HASH_SEARCH_STATE state;
152
printf("- Testing identical read\n");
153
sprintf(key,"%6d",j);
155
if (!(recpos= hash_first(&hash, key, 0, &state)))
157
printf("can't find key1: \"%s\"\n",key);
160
while (hash_next(&hash, key, 0, &state) && pos < (ulong) (key1[j]+10))
162
if (pos != (ulong) key1[j])
164
printf("Found %ld copies of key: %s. Should be %d",pos,key,key1[j]);
168
printf("- Creating output heap-file 2\n");
169
if (hash_init(&hash2, default_charset_info, hash.records, 0, 0, hash2_key, free_record,0))
172
printf("- Copying and removing records\n");
174
while ((recpos=hash_element(&hash,0)))
176
record=(uchar*) my_malloc(reclength,MYF(MY_FAE));
177
memcpy(record,recpos,reclength);
178
record[reclength-1]=rnd(5)+1;
179
if (my_hash_insert(&hash2,record))
181
printf("Got error when inserting record: %*s",reclength,record);
184
key_check-=atoi(record);
186
if (hash_delete(&hash,recpos))
188
printf("Got error when deleting record: %*s",reclength,recpos);
195
printf("Key check didn't get to 0 (%ld)\n",key_check);
198
printf("\nFollowing test have been made:\n");
199
printf("Write records: %d\nUpdate records: %d\nDelete records: %d\n", write_count,
201
hash_free(&hash); hash_free(&hash2);
202
my_end(MY_GIVE_INFO);
205
printf("Got error: %d when using hashing\n",my_errno);
212
static int get_options(int argc, char **argv)
218
while (--argc >0 && *(pos = *(++argv)) == '-' ) {
220
case 'm': /* records */
224
testflag=atoi(++pos); /* testmod */
229
printf("%s Ver 1.0 for %s at %s\n",progname,SYSTEM_TYPE,MACHINE_TYPE);
230
printf("MySQL AB, by Monty\n\n");
231
printf("Usage: %s [-?ABIKLWv] [-m#] [-t#]\n",progname);
239
/* Get a random number in the interval 0 <= x <= n */
241
static int rnd(int max_value)
243
return (int) ((rand() & 32767)/32767.0*max_value);
247
void free_record(void *record)
249
my_free(record,MYF(0));