static int test12(void) { unsigned int i; char *keys[] = { "100", "200", "300" }; char *values[] = { "100", "200", "300" }; struct hashtbl *h; h = hashtbl_create(ht_size, HASHTBL_MAX_LOAD_FACTOR, 1, hashtbl_string_hash, hashtbl_string_equals, NULL, NULL, NULL, NULL); CUT_ASSERT_NOT_NULL(h); CUT_ASSERT_EQUAL(0, hashtbl_count(h)); for (i = 0; i < NELEMENTS(keys); i++) { CUT_ASSERT_EQUAL(0, hashtbl_insert(h, keys[i], values[i])); CUT_ASSERT_NOT_NULL(hashtbl_lookup(h, keys[i])); CUT_ASSERT_EQUAL(values[i], hashtbl_lookup(h, keys[i])); CUT_ASSERT_TRUE(STREQ(keys[i], (char *)hashtbl_lookup(h, keys[i]))); CUT_ASSERT_TRUE(STREQ(values[i], (char *)hashtbl_lookup(h, keys[i]))); } hashtbl_delete(h); return 0; }
static int test21(void) { unsigned int i; long long int keys[] = { 1LL << 32, 1LL << 33, 1LL << 34 }; long long int values[] = { 1LL << 35, 1LL << 36, 1LL << 37 }; struct hashtbl *h; h = hashtbl_create(ht_size, HASHTBL_MAX_LOAD_FACTOR, 1, hashtbl_int64_hash, hashtbl_int64_equals, NULL, NULL, NULL, NULL); CUT_ASSERT_NOT_NULL(h); CUT_ASSERT_EQUAL(0, hashtbl_count(h)); for (i = 0; i < NELEMENTS(keys); i++) { long long int x = keys[i]; CUT_ASSERT_EQUAL(0, hashtbl_insert(h, &keys[i], &values[i])); CUT_ASSERT_NOT_NULL(hashtbl_lookup(h, &x)); CUT_ASSERT_EQUAL(values[i], *(long long *)hashtbl_lookup(h, &x)); } CUT_ASSERT_EQUAL(NELEMENTS(keys), hashtbl_count(h)); for (i = 0; i < NELEMENTS(keys); i++) { long long x = keys[i]; CUT_ASSERT_EQUAL(0, hashtbl_remove(h, &x)); } CUT_ASSERT_EQUAL(0, hashtbl_count(h)); hashtbl_clear(h); hashtbl_delete(h); return 0; }
static int test7(void) { struct test_key k; struct test_val v; struct hashtbl *h; h = hashtbl_create(ht_size, HASHTBL_MAX_LOAD_FACTOR, 1, key_hash, key_equals, NULL, NULL, NULL, NULL); CUT_ASSERT_NOT_NULL(h); memset(&k, 0, sizeof(k)); memset(&v, 0, sizeof(v)); k.k = 3; v.v = 300; CUT_ASSERT_EQUAL(0, hashtbl_count(h)); CUT_ASSERT_EQUAL(0, hashtbl_insert(h, &k, &v)); CUT_ASSERT_EQUAL(1, hashtbl_count(h)); CUT_ASSERT_EQUAL(&v, hashtbl_lookup(h, &k)); CUT_ASSERT_EQUAL(300, ((struct test_val *)hashtbl_lookup(h, &k))->v); CUT_ASSERT_EQUAL(0, hashtbl_remove(h, &k)); CUT_ASSERT_EQUAL(0, hashtbl_count(h)); CUT_ASSERT_EQUAL(1, hashtbl_remove(h, &k)); CUT_ASSERT_EQUAL(0, hashtbl_count(h)); hashtbl_clear(h); CUT_ASSERT_EQUAL(0, hashtbl_count(h)); hashtbl_delete(h); return 0; }
static int test11(void) { unsigned int i; int keys[] = { 100, 200, 300 }; int values[] = { 1000, 2000, 3000 }; struct hashtbl *h; h = hashtbl_create(ht_size, HASHTBL_MAX_LOAD_FACTOR, 1, hashtbl_int_hash, hashtbl_int_equals, NULL, NULL, NULL, NULL); CUT_ASSERT_NOT_NULL(h); CUT_ASSERT_EQUAL(0, hashtbl_count(h)); for (i = 0; i < NELEMENTS(keys); i++) { int x = keys[i]; CUT_ASSERT_EQUAL(0, hashtbl_insert(h, &keys[i], &values[i])); CUT_ASSERT_NOT_NULL(hashtbl_lookup(h, &x)); CUT_ASSERT_EQUAL(values[i], *(int *)hashtbl_lookup(h, &x)); } CUT_ASSERT_EQUAL(NELEMENTS(keys), hashtbl_count(h)); for (i = 0; i < NELEMENTS(keys); i++) { int x = keys[i]; CUT_ASSERT_EQUAL(0, hashtbl_remove(h, &x)); } CUT_ASSERT_EQUAL(0, hashtbl_count(h)); hashtbl_clear(h); hashtbl_delete(h); return 0; }
static int test9(void) { int test9_max = 100; struct hashtbl *h; int i; h = hashtbl_create(ht_size, HASHTBL_MAX_LOAD_FACTOR, 1, key_hash, key_equals, free, free, NULL, NULL); CUT_ASSERT_NOT_NULL(h); CUT_ASSERT_EQUAL(0, hashtbl_count(h)); for (i = 0; i < test9_max; i++) { struct test_key *k = malloc(sizeof(struct test_key)); struct test_val *v = malloc(sizeof(struct test_val)); CUT_ASSERT_NOT_NULL(k); CUT_ASSERT_NOT_NULL(v); memset(k, 0, sizeof(*k)); memset(v, 0, sizeof(*v)); k->k = i; v->v = i + test9_max; CUT_ASSERT_EQUAL(0, hashtbl_insert(h, k, v)); CUT_ASSERT_EQUAL(i + 1, (int) hashtbl_count(h)); CUT_ASSERT_EQUAL(i + test9_max, ((struct test_val *)hashtbl_lookup(h, k))->v); } hashtbl_apply(h, test9_apply_fn1, &test9_max); for (i = 0; i < test9_max; i++) { struct test_key k; struct test_val *v; memset(&k, 0, sizeof(k)); k.k = i; v = hashtbl_lookup(h, &k); CUT_ASSERT_NOT_NULL(v); CUT_ASSERT_EQUAL(i + test9_max, v->v); } for (i = 99; i >= 0; i--) { struct test_key k; struct test_val *v; memset(&k, 0, sizeof(k)); k.k = i; v = hashtbl_lookup(h, &k); CUT_ASSERT_NOT_NULL(v); CUT_ASSERT_EQUAL(v->v - test9_max, i); } hashtbl_clear(h); CUT_ASSERT_EQUAL(0, hashtbl_count(h)); hashtbl_delete(h); return 0; }
static int test6(void) { int accumulator = 0; struct test_key k1, k2; struct test_val v1, v2; struct hashtbl *h; h = hashtbl_create(ht_size, HASHTBL_MAX_LOAD_FACTOR, 1, key_hash, key_equals, NULL, NULL, NULL, NULL); CUT_ASSERT_NOT_NULL(h); memset(&k1, 0, sizeof(k1)); memset(&k2, 0, sizeof(k2)); memset(&v1, 0, sizeof(v1)); memset(&v2, 0, sizeof(v2)); k1.k = 3; v1.v = 300; CUT_ASSERT_EQUAL(0, hashtbl_count(h)); CUT_ASSERT_EQUAL(0, hashtbl_insert(h, &k1, &v1)); CUT_ASSERT_EQUAL(1, hashtbl_count(h)); CUT_ASSERT_EQUAL(&v1, hashtbl_lookup(h, &k1)); CUT_ASSERT_EQUAL(300, ((struct test_val *)hashtbl_lookup(h, &k1))->v); CUT_ASSERT_EQUAL(1, hashtbl_count(h)); k2.k = 4; v2.v = 400; CUT_ASSERT_NOT_NULL(h); CUT_ASSERT_EQUAL(0, hashtbl_insert(h, &k2, &v2)); CUT_ASSERT_EQUAL(2, hashtbl_count(h)); CUT_ASSERT_EQUAL(&v2, hashtbl_lookup(h, &k2)); CUT_ASSERT_EQUAL(400, ((struct test_val *)hashtbl_lookup(h, &k2))->v); CUT_ASSERT_EQUAL(2, hashtbl_apply(h, test6_apply_fn1, &accumulator)); CUT_ASSERT_EQUAL(700, accumulator); CUT_ASSERT_EQUAL(1, hashtbl_apply(h, test6_apply_fn2, &accumulator)); CUT_ASSERT_EQUAL(1400, accumulator); hashtbl_clear(h); CUT_ASSERT_EQUAL(0, hashtbl_count(h)); hashtbl_delete(h); return 0; }
static int test15(void) { int i; struct hashtbl *h; h = hashtbl_create(ht_size, HASHTBL_MAX_LOAD_FACTOR, 1, hashtbl_direct_hash, hashtbl_direct_equals, NULL, NULL, NULL, NULL); CUT_ASSERT_NOT_NULL(h); CUT_ASSERT_EQUAL(0, hashtbl_count(h)); for (i = 0; i < (1 << TEST15_N); i++) { int *k = &test15_bigtable[i]; test15_bigtable[i] = i; CUT_ASSERT_EQUAL(0, hashtbl_insert(h, k, k)); CUT_ASSERT_NOT_NULL(hashtbl_lookup(h, k)); } /* Iteration order should reflect insertion order. */ for (i = 0; i < (1 << TEST15_N); i++) { int *k = &test15_bigtable[i]; CUT_ASSERT_EQUAL(0, hashtbl_remove(h, k)); } hashtbl_delete(h); return 0; }
static int test8(void) { struct hashtbl *h; struct test_key *k = malloc(sizeof(struct test_key)); struct test_val *v1 = malloc(sizeof(struct test_val)); struct test_val *v2 = malloc(sizeof(struct test_val)); h = hashtbl_create(ht_size, HASHTBL_MAX_LOAD_FACTOR, 1, key_hash, key_equals, free, free, NULL, NULL); CUT_ASSERT_NOT_NULL(h); CUT_ASSERT_NOT_NULL(k); CUT_ASSERT_NOT_NULL(v1); CUT_ASSERT_NOT_NULL(v2); memset(k, 0, sizeof(*k)); memset(v1, 0, sizeof(*v1)); memset(v2, 0, sizeof(*v2)); k->k = 3; v1->v = 300; v2->v = 600; CUT_ASSERT_EQUAL(0, hashtbl_count(h)); CUT_ASSERT_EQUAL(0, hashtbl_insert(h, k, v1)); CUT_ASSERT_EQUAL(1, hashtbl_count(h)); CUT_ASSERT_EQUAL(v1, hashtbl_lookup(h, k)); CUT_ASSERT_EQUAL(300, ((struct test_val *)hashtbl_lookup(h, k))->v); /* Replace value for same key. */ CUT_ASSERT_EQUAL(0, hashtbl_insert(h, k, v2)); CUT_ASSERT_EQUAL(1, hashtbl_count(h)); CUT_ASSERT_EQUAL(v2, hashtbl_lookup(h, k)); CUT_ASSERT_EQUAL(600, ((struct test_val *)hashtbl_lookup(h, k))->v); CUT_ASSERT_EQUAL(0, hashtbl_remove(h, k)); CUT_ASSERT_EQUAL(0, hashtbl_count(h)); hashtbl_clear(h); CUT_ASSERT_EQUAL(0, hashtbl_count(h)); hashtbl_delete(h); return 0; }
static int test14(void) { unsigned int i; char *keys[] = { "100", "200", "300" }; char *vals[] = { "1000", "2000", "3000" }; struct hashtbl *h; struct hashtbl_iter iter; int key_sum = 0; int val_sum = 0; h = hashtbl_create(ht_size, HASHTBL_MAX_LOAD_FACTOR, 1, hashtbl_string_hash, hashtbl_string_equals, NULL, NULL, NULL, NULL); CUT_ASSERT_NOT_NULL(h); CUT_ASSERT_EQUAL(0, hashtbl_count(h)); for (i = 0; i < NELEMENTS(keys); i++) { CUT_ASSERT_EQUAL(0, hashtbl_insert(h, keys[i], vals[i])); CUT_ASSERT_EQUAL(vals[i], hashtbl_lookup(h, keys[i])); CUT_ASSERT_TRUE(STREQ(vals[i], (char *)hashtbl_lookup(h, keys[i]))); } hashtbl_iter_init(h, &iter); while (hashtbl_iter_next(h, &iter)) { key_sum += atoi(iter.key); val_sum += atoi(iter.val); } CUT_ASSERT_EQUAL(600, key_sum); CUT_ASSERT_EQUAL(6000, val_sum); hashtbl_clear(h); hashtbl_delete(h); return 0; }
bool activation_get_binding(activation* act, scheme_symbol binding, struct _sexp** result) { assert(act != NULL); activation* cursor = act; while (cursor) { if (hashtbl_lookup(cursor->env, (void*)binding, (void**)result)) { return true; } cursor = cursor->parent; } return false; }
static void options_add_checkset(integrit_t *it, char *namebuf, size_t namebuf_chars, checkset *cset) { hashtbl_t *h = it->ruleset; checkset *old; void *replaced; if ( (old = hashtbl_lookup(h, namebuf, namebuf_chars)) ) { /* checkset merge frees the old checkset */ cset = checkset_merge(cset, old, namebuf); } if (hashtbl_store(h, namebuf, namebuf_chars, cset, &replaced) == -1) DIE("storing checkset in table"); }
int hashtbl_insert(HashTable *htbl, const void *data) { void *temp; int bucket, retval; temp = (void*)data; if (hashtbl_lookup(htbl, &temp) == 0) { return 1; } bucket = htbl->h(data) % htbl->buckets; retval = list_insert_next(&htbl->table[bucket], NULL, data); if (retval == 0) { htbl->size++; } return retval; }
static int test2(void) { struct test_key k; struct hashtbl *h; h = hashtbl_create(ht_size, HASHTBL_MAX_LOAD_FACTOR, 1, key_hash, key_equals, NULL, NULL, NULL, NULL); CUT_ASSERT_NOT_NULL(h); memset(&k, 0, sizeof(k)); k.k = 2; CUT_ASSERT_EQUAL(0, hashtbl_count(h)); CUT_ASSERT_NULL(hashtbl_lookup(h, &k)); hashtbl_clear(h); CUT_ASSERT_EQUAL(0, hashtbl_count(h)); hashtbl_delete(h); return 0; }
static int test23(void) { int i; HASHTBL_INT(h); CUT_ASSERT_NOT_NULL(h); CUT_ASSERT_EQUAL(0, hashtbl_count(h)); for (i = 0; i < 100; i++) { int *k = malloc(sizeof(int)); int *v = malloc(sizeof(int)); *k = i; *v = i; CUT_ASSERT_EQUAL(0, hashtbl_insert(h, k, v)); } CUT_ASSERT_EQUAL(100, hashtbl_count(h)); for (i = 0; i < 100; i++) { int k = i; CUT_ASSERT_EQUAL(i, *(int *)hashtbl_lookup(h, &k)); } hashtbl_delete(h); return 0; }
static int test22(void) { int i; HASHTBL_STRING(h); CUT_ASSERT_NOT_NULL(h); CUT_ASSERT_EQUAL(0, hashtbl_count(h)); for (i = 0; i < 100; i++) { char buf[64], *k, *v; sprintf(buf, "%d", i); CUT_ASSERT_NOT_NULL((k = strdup(buf))); CUT_ASSERT_NOT_NULL((v = strdup(buf))); CUT_ASSERT_EQUAL(0, hashtbl_insert(h, k, v)); } CUT_ASSERT_EQUAL(100, hashtbl_count(h)); for (i = 0; i < 100; i++) { char buf[64]; sprintf(buf, "%d", i); CUT_ASSERT_TRUE(STREQ(buf, (char *)hashtbl_lookup(h, buf))); } hashtbl_delete(h); return 0; }