5c23cb7f99985a8d6757b84d56483bed841df3d3
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
++)
78 key
[3] = (char)(48 + i
);
79 hashtable_delete(h
, key
);
81 lu_assert_int_eq(hashtable_size(h
), 0);
86 void t_hashtable_getnull_modify()
90 HashTable
* h
= hashtable_new(StructTest1
, 4);
91 StructTest1
* st1
= (StructTest1
*) malloc(n
* sizeof (StructTest1
));
93 for (int i
= 0; i
< n
; i
++)
95 key
[3] = (char)(48 + i
);
96 st1
[i
].a
= random() % 42;
97 st1
[i
].b
= (double) random() / RAND_MAX
;
98 hashtable_set(h
, key
, *(st1
+ i
));
100 for (int i
= 0; i
< n
; i
++)
102 //another way to access elements
103 key
[3] = (char)(48 + i
);
105 hashtable_get(h
, key
, st1Cell
);
106 lu_assert_int_eq(st1Cell
.a
, st1
[i
].a
);
107 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
);
132 hashtable_destroy(h
);
135 void t_hashtable_copy()
139 HashTable
* h
= hashtable_new(int, 8);
141 for (int i
= 0; i
< n
; i
++)
143 key
[3] = (char)(48 + i
);
144 hashtable_set(h
, key
, random() % 42);
146 HashTable
* hc
= hashtable_copy(h
);
148 lu_assert_int_eq(h
->size
, hc
->size
);
150 for (int i
= 0; i
< n
; i
++)
152 key
[3] = (char)(48 + i
);
153 hashtable_get(h
, key
, a
);
154 hashtable_get(hc
, key
, b
);
155 lu_assert_int_eq(a
, b
);
157 hashtable_destroy(h
);
158 hashtable_destroy(hc
);