e695f5706dc500d99560dddd5695dc50d50e6568
2 #include "cgds/HashTable.h"
6 void t_hashtable_clear()
8 HashTable
* h
= hashtable_new(int, 16);
9 lu_assert(hashtable_empty(h
));
11 hashtable_set(h
, "key1", 0);
12 hashtable_set(h
, "key2", 1);
13 hashtable_set(h
, "key3", 2);
16 h
= hashtable_new(int, 8);
18 hashtable_set(h
, "key1", 1);
19 hashtable_set(h
, "key2", 2);
20 hashtable_set(h
, "key3", 3);
23 lu_assert(hashtable_empty(h
));
28 void t_hashtable_size()
30 HashTable
* h
= hashtable_new(int, 16);
31 lu_assert(hashtable_empty(h
));
33 hashtable_set(h
, "key1", 0);
34 hashtable_set(h
, "key2", 1);
35 hashtable_set(h
, "key3", 2);
36 lu_assert_int_eq(hashtable_size(h
), 3);
38 hashtable_set(h
, "key4", 3);
39 hashtable_set(h
, "key5", 4);
40 lu_assert_int_eq(hashtable_size(h
), 5);
42 hashtable_set(h
, "key6", 5);
43 hashtable_set(h
, "key7", 6);
44 hashtable_set(h
, "key8", 7);
45 lu_assert_int_eq(hashtable_size(h
), 8);
50 void t_hashtable_set_remove_basic()
54 HashTable
* h
= hashtable_new(double, 4);
56 for (int i
= 0; i
< n
; i
++)
58 key
[3] = (char)(48 + i
);
59 hashtable_set(h
, key
, (double)i
);
61 lu_assert_int_eq(hashtable_size(h
), n
);
65 for (int i
= 0; i
< n
; i
++)
68 key
[3] = (char)(48 + i
);
69 hashtable_get(h
, key
, d
);
70 lu_assert_dbl_eq(*d
, ckValue
);
74 // Remove keys / values
75 for (int i
= 0; i
< n
; i
++)
77 key
[3] = (char)(48 + i
);
78 hashtable_delete(h
, key
);
80 lu_assert_int_eq(hashtable_size(h
), 0);
85 void t_hashtable_getnull_modify()
89 HashTable
* h
= hashtable_new(StructTest1
, 4);
90 StructTest1
* st1
= (StructTest1
*) malloc(n
* sizeof(StructTest1
));
92 for (int i
= 0; i
< n
; i
++)
94 key
[3] = (char)(48 + i
);
95 st1
[i
].a
= random() % 42;
96 st1
[i
].b
= (double) random() / RAND_MAX
;
97 hashtable_set(h
, key
, *(st1
+ i
));
99 for (int i
= 0; i
< n
; i
++)
101 // Another way to access elements
102 key
[3] = (char)(48 + i
);
103 StructTest1
* st1Cell
;
104 hashtable_get(h
, key
, st1Cell
);
105 lu_assert_int_eq(st1Cell
->a
, st1
[i
].a
);
106 lu_assert_dbl_eq(st1Cell
->b
, st1
[i
].b
);
112 hashtable_get(h
, "key12", stmp
);
113 lu_assert(stmp
== NULL
);
114 hashtable_get(h
, "key32", stmp
);
115 lu_assert(stmp
== NULL
);
120 hashtable_set(h
, "key1", stMod
);
121 hashtable_get(h
, "key1", stmp
);
122 lu_assert_int_eq(stmp
->a
, stMod
.a
);
123 lu_assert_dbl_eq(stmp
->b
, stMod
.b
);
126 hashtable_set(h
, "key5", stMod
);
127 hashtable_get(h
, "key5", stmp
);
128 lu_assert_int_eq(stmp
->a
, stMod
.a
);
129 lu_assert_dbl_eq(stmp
->b
, stMod
.b
);
131 hashtable_destroy(h
);
134 void t_hashtable_copy()
138 HashTable
* h
= hashtable_new(int, 8);
140 for (int i
= 0; i
< n
; i
++)
142 key
[3] = (char)(48 + i
);
143 hashtable_set(h
, key
, random() % 42);
145 HashTable
* hc
= hashtable_copy(h
);
147 lu_assert_int_eq(h
->size
, hc
->size
);
149 for (int i
= 0; i
< n
; i
++)
151 key
[3] = (char)(48 + i
);
152 hashtable_get(h
, key
, a
);
153 hashtable_get(hc
, key
, b
);
154 lu_assert_int_eq(*a
, *b
);
156 hashtable_destroy(h
);
157 hashtable_destroy(hc
);