aboutsummaryrefslogtreecommitdiff
path: root/tests/test_htable.c
blob: 3dee2d31c9e7fbeb8f15a1a678fe38480568f96e (plain)
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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "hash_table.h"

bool found[10000];

int main() {
    printf("[TEST] htable\n");

    Int2IntHashTable ht;
    Int2IntHashTable_init(&ht);
    for (int i = 0; i < 10000; i++) {
        Int2IntHashTable_insert(&ht, i, i*2);
        assert(ht.ht.size == i + 1);
        assert(ht.ht.taken == i + 1);
        assert(ht.ht.cap >= i + 1);
    }

    for (int i = 0; i < 10000; i++) {
        assert(Int2IntHashTable_get(&ht, i) != NULL);
        assert(*Int2IntHashTable_get(&ht, i) == i * 2);
        int t = 10000 + i;
        assert(Int2IntHashTable_get(&ht, t) == NULL);
    }

    memset(found, 0, sizeof(bool) * 10000);
    Int2IntHashTableIter iter = Int2IntHashTable_begin(&ht);
    while (iter != NULL) {
        found[iter->key] = true;
        iter = Int2IntHashTable_next(&ht, iter);
    }
    for (int i = 0; i < 10000; i++) {
        assert(found[i]);
    }

    for (int i = 0; i < 5000; i++) {
        Int2IntHashTableIter iter = Int2IntHashTable_find(&ht, i);
        Int2IntHashTable_remove(&ht, iter);
    }
    for (int i = 0; i < 5000; i++) {
        assert(Int2IntHashTable_find(&ht, i) == NULL);
        int t = 5000 + i;
        assert(Int2IntHashTable_find(&ht, t) != NULL);
    }

    for (int i = 0; i < 5000; i++) {
        Int2IntHashTable_insert(&ht, i, i);
    }

    memset(found, 0, sizeof(bool) * 10000);
    iter = Int2IntHashTable_begin(&ht);
    while (iter != NULL) {
        found[iter->key] = true;
        iter = Int2IntHashTable_next(&ht, iter);
    }
    for (int i = 0; i < 10000; i++) {
        assert(found[i]);
    }
    Int2IntHashTable_free(&ht);

    printf("[PASS] htable\n");
}