summaryrefslogtreecommitdiffstats
path: root/tests/hash_table.c
blob: 4c3591a507b8777fe78825fcb3fe36c7a1ff22e2 (plain) (blame)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "test.h"
#include <lisiblestd/hash.h>

static void hash_table_create(void) {
  HashTable hash_table;
  T_ASSERT(HashTable_init(&system_allocator, &hash_table, 16, hash_str_hash,
                          hash_str_eq));
  T_ASSERT_EQ(hash_table.capacity, 16);
  T_ASSERT_EQ(hash_table.length, 0);
  HashTable_deinit(&hash_table);
}

static void hash_table_insert(void) {
  HashTable hash_table;
  T_ASSERT(HashTable_init(&system_allocator, &hash_table, 16, hash_str_hash,
                          hash_str_eq));
  T_ASSERT_EQ(hash_table.length, 0);

  char *first_key = "first_key";
  int first_value = 5;
  T_ASSERT(HashTable_insert(&hash_table, first_key, &first_value));
  T_ASSERT_EQ(hash_table.length, 1);

  char *second_key = "second_key";
  int second_value = 7;
  T_ASSERT(HashTable_insert(&hash_table, second_key, &second_value));
  char *third_key = "third_key";
  int third_value = 23;
  T_ASSERT(HashTable_insert(&hash_table, third_key, &third_value));
  T_ASSERT_EQ(hash_table.length, 3);
  HashTable_deinit(&hash_table);
}

static void hash_table_get(void) {
  HashTable hash_table;
  T_ASSERT(HashTable_init(&system_allocator, &hash_table, 16, hash_str_hash,
                          hash_str_eq));
  char *first_key = "first_key";
  int first_value = 5;
  T_ASSERT(HashTable_insert(&hash_table, first_key, &first_value));
  char *second_key = "second_key";
  int second_value = 7;
  T_ASSERT(HashTable_insert(&hash_table, second_key, &second_value));
  char *third_key = "third_key";
  int third_value = 23;
  T_ASSERT(HashTable_insert(&hash_table, third_key, &third_value));

  int *returned_value = HashTable_get(&hash_table, "second_key");
  T_ASSERT(returned_value != NULL);
  T_ASSERT_EQ(*returned_value, 7);
  HashTable_deinit(&hash_table);
}

static void hash_table_has(void) {
  HashTable hash_table;
  T_ASSERT(HashTable_init(&system_allocator, &hash_table, 16, hash_str_hash,
                          hash_str_eq));
  char *first_key = "first_key";
  int first_value = 5;
  T_ASSERT(HashTable_insert(&hash_table, first_key, &first_value));
  char *second_key = "second_key";
  int second_value = 7;
  T_ASSERT(HashTable_insert(&hash_table, second_key, &second_value));
  char *third_key = "third_key";
  int third_value = 23;
  T_ASSERT(HashTable_insert(&hash_table, third_key, &third_value));

  T_ASSERT(HashTable_has(&hash_table, "first_key"));
  T_ASSERT(HashTable_has(&hash_table, "second_key"));
  T_ASSERT(!HashTable_has(&hash_table, "thrid_key"));
  T_ASSERT(HashTable_has(&hash_table, "third_key"));
  HashTable_deinit(&hash_table);
}

static void hash_table_expand(void) {
  HashTable hash_table;
  T_ASSERT(HashTable_init(&system_allocator, &hash_table, 4, hash_str_hash,
                          hash_str_eq));
  char *first_key = "first_key";
  int first_value = 5;
  T_ASSERT(HashTable_insert(&hash_table, first_key, &first_value));
  char *second_key = "second_key";
  int second_value = 7;
  T_ASSERT(HashTable_insert(&hash_table, second_key, &second_value));
  char *third_key = "third_key";
  int third_value = 23;
  T_ASSERT(HashTable_insert(&hash_table, third_key, &third_value));
  char *fourth_key = "fourth_key";
  int fourth_value = 24;
  T_ASSERT(HashTable_insert(&hash_table, fourth_key, &fourth_value));
  char *fifth_key = "fifth_key";
  int fifth_value = 25;
  T_ASSERT(HashTable_insert(&hash_table, fifth_key, &fifth_value));

  T_ASSERT(HashTable_has(&hash_table, "first_key"));
  T_ASSERT(HashTable_has(&hash_table, "second_key"));
  T_ASSERT(!HashTable_has(&hash_table, "thrid_key"));
  T_ASSERT(HashTable_has(&hash_table, "third_key"));
  T_ASSERT(HashTable_has(&hash_table, "fourth_key"));
  T_ASSERT(HashTable_has(&hash_table, "fifth_key"));
  HashTable_deinit(&hash_table);
}

static void str_dctor_fn(Allocator *allocator, void *v) {
  Allocator_free(allocator, v);
}

static void hash_table_heap_allocated_str(void) {
  HashTable hash_table;
  T_ASSERT(HashTable_init_with_dctors(&system_allocator, &hash_table, 16,
                                      hash_str_hash, hash_str_eq,
                                      HashTable_noop_dctor_fn, str_dctor_fn));

  char *first_value = calloc(8, sizeof(char));
  strcpy(first_value, "bonjour");

  T_ASSERT(HashTable_insert(&hash_table, "first_key", first_value));

  char *returned_value = HashTable_get(&hash_table, "first_key");
  T_ASSERT(strcmp(returned_value, "bonjour") == 0);
  HashTable_deinit(&hash_table);
}

static uint64_t int_hash_fn(const void *v) { return *(uint64_t *)v; }
static bool int_eq_fn(const void *a, const void *b) {
  return *(uint64_t *)a == *(uint64_t *)b;
}

static void hash_set_init(void) {
  HashSet set;
  HashSet_init(&system_allocator, &set, 16, int_hash_fn, int_eq_fn);
  T_ASSERT_EQ(HashSet_length(&set), 0u);
  HashSet_deinit(&set);
}

static void hash_set_insert(void) {
  HashSet set;
  HashSet_init(&system_allocator, &set, 16, int_hash_fn, int_eq_fn);
  T_ASSERT_EQ(HashSet_length(&set), 0u);

  uint64_t a = 15;
  HashSet_insert(&set, &a);
  T_ASSERT_EQ(HashSet_length(&set), 1u);
  uint64_t b = 26;
  HashSet_insert(&set, &b);
  uint64_t c = 28;
  HashSet_insert(&set, &c);
  T_ASSERT_EQ(HashSet_length(&set), 3u);
  HashSet_deinit(&set);
}

static void hash_set_has(void) {
  HashSet set;
  HashSet_init(&system_allocator, &set, 16, int_hash_fn, int_eq_fn);
  T_ASSERT_EQ(HashSet_length(&set), 0u);

  uint64_t a = 15;
  HashSet_insert(&set, &a);
  uint64_t b = 26;
  HashSet_insert(&set, &b);
  uint64_t c = 28;
  HashSet_insert(&set, &c);

  uint64_t d = 0;
  T_ASSERT(HashSet_has(&set, &a));
  T_ASSERT(HashSet_has(&set, &b));
  T_ASSERT(HashSet_has(&set, &c));
  T_ASSERT(!HashSet_has(&set, &d));
  T_ASSERT_EQ(HashSet_length(&set), 3u);
  HashSet_deinit(&set);
}

TEST_SUITE(TEST(hash_table_create), TEST(hash_table_insert),
           TEST(hash_table_get), TEST(hash_table_heap_allocated_str),
           TEST(hash_table_has), TEST(hash_table_expand), TEST(hash_set_init),
           TEST(hash_set_insert), TEST(hash_set_has))
Go back to lisible.xyz