-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
126 lines (104 loc) · 4.64 KB
/
test.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "FlatChainedHashTable.h"
#define SAMPLE_SIZE 10000
uint64_t samples[SAMPLE_SIZE];
int main(){
int i,j;
//kvpair_t *ppair;
//element_t v;
int r = EXIT_SUCCESS;
map_t *map = map_create(0);
time_t s = 1666869947; //time(0);
srand(s);
printf("Seed = %ld\n",s);
//srand(0);
fprintf(stdout, "\n Buckets\tCount\tLoad Factor\n");
fprintf(stdout, " -------\t-----\t-----------");
for(int n = 1; n <= 3; n++){
fprintf(stdout, "\nIteration #%d:\n",n);
fprintf(stdout, "-------------\n");
for(i = 0; i < SAMPLE_SIZE; i++){
do{
samples[i] = (uint32_t)rand();
for(j = 0; samples[j] != samples[i]; j++);
}while(j < i);
}
fprintf(stdout, "Initial State: %lu\t%lu,\t%f\n", map->hashtable.capacity, map->hashtable.count, map_load_factor(map));
//fprintf(stdout, "Initial State: %lu\t%lu,\t%f\n", 1ul << map->bits, map->count, map_load_factor(map));
for(i = 0; i < SAMPLE_SIZE; i++){
//v = map_put(&map, samples[i], (element_t)samples[i]);
if(!map_put(&map, samples[i], (element_t)samples[i])){ //, samples[i])){
fprintf(stderr, "Insertion failed [%d] %lu\n", i, samples[i]);
r = EXIT_FAILURE;
goto end_test;
}
//v.u64 = samples[i];
if(map->hashtable.count != (uint64_t)i+1){
fprintf(stderr, "Size is incorrect [%d] [%lu]\n", i+1, map->hashtable.count);
r = EXIT_FAILURE;
goto end_test;
}
for(j = 0; j < i; j++){
//printf("i = [%d] j = [%d]\n",i,j);
element_t *x = map_get(map, samples[j]);
if(!x){
fprintf(stderr, "Lookup failed! [%d] [%d] %lu\n", i, j, samples[j]);
goto end_test;
}
if(x->u64 != samples[j]){
fprintf(stderr, "Lookup value value not correct [%d] %lu [%d] %lu\n", i, x->u64, j, samples[j]);
goto end_test;
}
}
}
fprintf(stdout, "After Insert: %lu,\t%lu,\t%f\n", map->hashtable.capacity, map->hashtable.count, map_load_factor(map));
//fprintf(stdout, "After Insert: %lu,\t%lu,\t%f\n", 1ul << map->bits, map->count, map_load_factor(map));
for(i = 0; i < SAMPLE_SIZE; i++){
map_del(&map, samples[i]);
/*if(!buff){
fprintf(stderr, "Removal failed [%lu] %lu\n", i, samples[i]);
r = EXIT_FAILURE;
goto end_test;
}
if(buff != samples[i]){
fprintf(stderr, "Removal value incorrect %lu [%lu] %lu\n", buff, i, samples[i]);
r = EXIT_FAILURE;
goto end_test;
}*/
if(map->hashtable.count != (uint64_t)SAMPLE_SIZE - (i+1)){
//fprintf(stderr, "Size incorrect after removal %lu [%lu] %lu\n", *buff, i, samples[i]);
fprintf(stderr, "Size incorrect after removal [%d] %lu\n", i, samples[i]);
r = EXIT_FAILURE;
goto end_test;
}
element_t *x = map_get(map, samples[i]);
if(x != NULL){
fprintf(stderr, "Failed to remove [%d] [%d] %lu\n", i, j, samples[j]);
r = EXIT_FAILURE;
goto end_test;
}
for(j = i + 1; j < SAMPLE_SIZE; j++){
x = map_get(map, samples[j]);
if(!x){
fprintf(stderr, "Lookup failed after removal [%d] [%d] %lu\n", i, j, samples[j]);
r = EXIT_FAILURE;
goto end_test;
}
if(x->u64 != samples[j]){
fprintf(stderr, "Lookup value incorrect after removal %lu [%d] %lu\n", x->u64, j, samples[j]);
r = EXIT_FAILURE;
goto end_test;
}
}
}
fprintf(stdout, "After Remove: %lu,\t%lu,\t%f\n", map->hashtable.capacity, map->hashtable.count, map_load_factor(map));
//fprintf(stdout, "After Remove: %lu,\t%lu,\t%f\n", 1ul << map->bits, map->count, map_load_factor(map));
}
end_test:
fprintf(stdout, "\nEnd State: %lu,\t%lu,\t%f\n\n", map->hashtable.capacity, map->hashtable.count, map_load_factor(map));
//fprintf(stdout, "\nEnd State: %lu,\t%lu,\t%f\n\n", 1ul << map->bits, map->count, map_load_factor(map));
map_destroy(&map);
return r;
}