Test the hashing function provided as callback and return the * number of collisions found. */
| 50 | /* Test the hashing function provided as callback and return the |
| 51 | * number of collisions found. */ |
| 52 | unsigned long testHashingFunction(uint64_t (*hash)(char *, size_t)) { |
| 53 | unsigned long collisions = 0; |
| 54 | memset(Table,0,sizeof(Table)); |
| 55 | char *prefixes[] = {"object", "message", "user", NULL}; |
| 56 | for (int i = 0; prefixes[i] != NULL; i++) { |
| 57 | for (int j = 0; j < TABLE_SIZE/2; j++) { |
| 58 | char keyname[128]; |
| 59 | size_t keylen = snprintf(keyname,sizeof(keyname),"%s:%d", |
| 60 | prefixes[i],j); |
| 61 | uint64_t bucket = hash(keyname,keylen) % TABLE_SIZE; |
| 62 | if (Table[bucket]) { |
| 63 | collisions++; |
| 64 | } else { |
| 65 | Table[bucket] = 1; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | return collisions; |
| 70 | } |
| 71 | |
| 72 | int main(void) { |
| 73 | printf("SHA1 : %lu\n", testHashingFunction(sha1Hash)); |