Skip to content

Commit

Permalink
hash-table: improve and add additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
recp committed Jul 4, 2017
1 parent 1066693 commit 8c5b22d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ I think `ds` namespace is very good choice, we can use it like this:
```

## Todo
- [x] rbtree (top-down insertion, top-down deletion)
- [x] rbtree
- [x] top-down insertion
- [x] top-down deletion
- [x] forward-list
- [x] hash-table
- [x] builtin hash functions e.g. djb2
- [ ] resizing hash table
- [x] resizing hash table
- [ ] octree
- [ ] quadtree

Expand Down
10 changes: 6 additions & 4 deletions src/hash_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,16 @@ hash_insert(HTable *htable,
return;
}

if (!found) {
if (!prev) {
item->next = htable->table[idx];
htable->table[idx] = item;
}

/* there is no need to access array (read + write),
just append it */
else {
found->next = item;
prev->next = item;
item->next = NULL;
}

htable->count++;
Expand All @@ -148,6 +149,7 @@ hash_set(HTable *htable,
}

if (hash_finditem(htable, key, &idx, &found, &prev)) {
found->key = key;
found->data = value;
return;
}
Expand All @@ -156,15 +158,15 @@ hash_set(HTable *htable,
item->key = key;
item->data = value;

if (!found) {
if (!prev) {
item->next = htable->table[idx];
htable->table[idx] = item;
}

/* there is no need to access array (read + write),
just append it */
else {
found->next = item;
prev->next = item;
}

htable->count++;
Expand Down
45 changes: 40 additions & 5 deletions test/src/test_htable.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,38 @@ test_htable(void **state) {
uint32_t count, i, k;
uint32_t capacity;

capacity = ds_prime_num(25);
capacity = ds_prime_num(3);
htable = hash_new_str(capacity);

/* 25 is not a prime number */
assert_true(htable->capacity > 25);
assert_true(htable->capacity > 3);

count = 1000;

srand((unsigned int)time(NULL));

/* make sure it is samll size: max 32 */
for (i = 0; i < 2; i++) {
/* random key length */
k = rand() % (sizeof(keybuf) / 8 - 1);

rand_str(keybuf, k);
key = strdup(keybuf);

hash_set(htable, key, key);

/* test find value */
found = hash_get(htable, key);
assert_non_null(found);

/* found values must be same */
assert_ptr_equal(key, found);
inserted_items[i] = key;
}

/* increase size */
hash_resize(htable, 9);
assert_true(htable->count == 2);

for (i = 0; i < count; i++) {
/* random key length */
k = rand() % (sizeof(keybuf) / 8 - 1);
Expand All @@ -47,8 +68,23 @@ test_htable(void **state) {
assert_ptr_equal(key, found);
inserted_items[i] = key;

if (i == 10 || i == 50) {
if (i == 0 || i == 10 || i == 50) {
hash_unset(htable, key);

/* try to unset again */
hash_unset(htable, key);

found = hash_get(htable, key);
assert_null(found);

/* allow only once */
hash_set(htable, key, key);
hash_set(htable, key, key);
hash_set(htable, key, key);

/* test remove by NULL */
hash_set(htable, key, NULL);

found = hash_get(htable, key);
assert_null(found);
}
Expand All @@ -66,7 +102,6 @@ test_htable(void **state) {
hash_resize(htable, 15);
assert_true(htable->count == count);

/* some extra test after resizing table */
for (i = 0; i < 10; i++) {
/* random key length */
k = rand() % (sizeof(keybuf) / 8 - 1);
Expand Down

0 comments on commit 8c5b22d

Please sign in to comment.