/* Print n strings that are hashed to 0 by universal_hash seeded with seed */ void collide_clever(unsigned int size, unsigned int seed, int n) { char string[MAXSTRLEN] = { '\0' }; int coeffs[MAXSTRLEN]; int len, c; if (size > next_prime(UCHAR_MAX)) { fprintf(stderr, "%d exceeds limit: %d\n", size, next_prime(UCHAR_MAX)); exit(EXIT_FAILURE); } print_universal(MAXSTRLEN, seed, size, coeffs); // Find the position of the first non-zero universal coefficient for (c = 0; !coeffs[c]; c++); for (int i = 0; i < n; i++) { // Generate a random string at least MINLEN long len = rand() % (MAXSTRLEN - MINLEN) + MINLEN; string[len] = '\0'; // Constrain the characters to between A to z for(int j = 0; j < len; j++) string[j] = rand() % (MAXCHAR - MINCHAR) + MINCHAR; // Find a value of string[c] so that string hashes to zero string[c] = hash_zero(coeffs, string, size, c); printf("%s\n", string); } }
int main(int argc, char **argv) { hash_t h; elt *elts, *p; int nelts = 0; char *c; int i; do { hash_zero(&h); if( argc>1 ) { nelts = strtoul(argv[1], &c, 0); } if( nelts == 0 ) { nelts = 1000; } elts = (elt*)calloc(nelts*sizeof(*elts),1); test_string(nelts); hash_init(&h, hash_hash_int, hash_cmp_int, 0, 0, 1); // put everything in for(i=0, p=elts; i<nelts; i++, p++) { p->in_hash = 1; hash_put(&h, p, p); } verify(&h, elts, nelts, "put"); verify(&h, elts, nelts, "put"); // delete every even element for(i=0, p=elts; i<nelts; i+=2, p+=2) { p->in_hash = 0; hash_free(&h, hash_get(&h, p)); } verify(&h, elts, nelts, "delete even"); // delete every odd element for(i=1, p=elts+1; i<nelts; i+=2, p+=2) { p->in_hash = 0; hash_free(&h, hash_get(&h, p)); } verify(&h, elts, nelts, "delete odd"); // randomly insert and delete stuff for(i=0; i<3*nelts; i++) { int j = (int)(1.0*nelts*rand()/RAND_MAX); p = elts + j; if( (int)(10.0*rand()/RAND_MAX)+1 > 1 ) { p->in_hash = 1; hash_put(&h, p, p); } else { p->in_hash = 0; hash_free(&h, hash_get(&h, p)); } } verify(&h, elts, nelts, "random insert/delete"); hash_clear(&h); /* loop forever looking for memory leak */ if( 0 ) { hash_init(&h, hash_hash_int, hash_cmp_int, 0, 0, 1); for(i=0; ;i++) { hash_node_t *node; hash_remove(&h, (void*)(intptr_t)(i-1)); hash_put(&h, (void*)(intptr_t)i, (void*)(intptr_t)i); node = hash_get(&h, (void*)(intptr_t)(i-1)); assertb(!node); node = hash_get(&h, (void*)(intptr_t)i); assertb(node); assertb((intptr_t)node->node_val == i); } } } while(0); return 0; }