/
hash-table-tests.c
69 lines (49 loc) · 1.55 KB
/
hash-table-tests.c
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
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include "hash-table.h"
static void test_hashitem_set_and_get() {
hash_table table = ht_new();
char* key = "hello";
int value = 100;
ht_set_value(&table, key, (void *) &value);
int* value_get = (int *)ht_get_value(&table, key);
assert(value == *value_get
&& "The value set is not the same as the get");
}
static void test_hashitem_collisions() {
hash_table table = ht_new();
/*The keys k1 and k2 create the same index.*/
char *k1 = "2WwtnwoDx";
int v1 = 10;
ht_set_value(&table, k1, &v1);
char *k2 = "2Uiihmi2/";
int v2 = 20;
ht_set_value(&table, k2, &v2);
int* v1_get = (int *)ht_get_value(&table, k1);
assert(v1 == *v1_get
&& "The value set is not the same as get");
int* v2_get = (int *)ht_get_value(&table, k2);
assert(v2 == *v2_get
&& "The value set is not the same as get");
}
static void test_hashitem_delete() {
hash_table table = ht_new();
char* key = "hello";
int value = 100;
ht_set_value(&table, key, (void *) &value);
int* value_get = (int *)ht_get_value(&table, key);
assert(value == *value_get
&& "The value set is not the same as the get");
ht_delete_value(&table, key);
value_get = (int *)ht_get_value(&table, key);
if (value_get != NULL) {
assert(0 && "The value is not deleted.");
}
}
int main() {
test_hashitem_set_and_get();
test_hashitem_collisions();
test_hashitem_delete();
return EXIT_SUCCESS;
}