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
177
|
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include "test.h"
int foreach_count = 0;
int foreach_fail = 0;
void foreach (gpointer key, gpointer value, gpointer user_data)
{
foreach_count++;
if (GPOINTER_TO_INT (user_data) != 'a')
foreach_fail = 1;
}
RESULT hash_t1 (void)
{
GHashTable *t = g_hash_table_new (g_str_hash, g_str_equal);
foreach_count = 0;
foreach_fail = 0;
g_hash_table_insert (t, "hello", "world");
g_hash_table_insert (t, "my", "god");
g_hash_table_foreach (t, foreach, GINT_TO_POINTER('a'));
if (foreach_count != 2)
return FAILED ("did not find all keys, got %d expected 2", foreach_count);
if (foreach_fail)
return FAILED("failed to pass the user-data to foreach");
if (!g_hash_table_remove (t, "my"))
return FAILED ("did not find known key");
if (g_hash_table_size (t) != 1)
return FAILED ("unexpected size");
g_hash_table_insert(t, "hello", "moon");
if (strcmp (g_hash_table_lookup (t, "hello"), "moon") != 0)
return FAILED ("did not replace world with moon");
if (!g_hash_table_remove (t, "hello"))
return FAILED ("did not find known key");
if (g_hash_table_size (t) != 0)
return FAILED ("unexpected size");
g_hash_table_destroy (t);
return OK;
}
RESULT hash_t2 (void)
{
return OK;
}
RESULT hash_default (void)
{
GHashTable *hash = g_hash_table_new (NULL, NULL);
if (hash == NULL)
return FAILED ("g_hash_table_new should return a valid hash");
g_hash_table_destroy (hash);
return NULL;
}
RESULT
hash_null_lookup (void)
{
GHashTable *hash = g_hash_table_new (NULL, NULL);
gpointer ok, ov;
g_hash_table_insert (hash, NULL, GINT_TO_POINTER (1));
g_hash_table_insert (hash, GINT_TO_POINTER(1), GINT_TO_POINTER(2));
if (!g_hash_table_lookup_extended (hash, NULL, &ok, &ov))
return FAILED ("Did not find the NULL");
if (ok != NULL)
return FAILED ("Incorrect key found");
if (ov != GINT_TO_POINTER (1))
return FAILED ("Got wrong value %p\n", ov);
if (!g_hash_table_lookup_extended (hash, GINT_TO_POINTER(1), &ok, &ov))
return FAILED ("Did not find the 1");
if (ok != GINT_TO_POINTER(1))
return FAILED ("Incorrect key found");
if (ov != GINT_TO_POINTER (2))
return FAILED ("Got wrong value %p\n", ov);
g_hash_table_destroy (hash);
return NULL;
}
static void
counter (gpointer key, gpointer value, gpointer user_data)
{
int *counter = (int *) user_data;
(*counter)++;
}
RESULT hash_grow (void)
{
GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
int i, count = 0;
for (i = 0; i < 1000; i++)
g_hash_table_insert (hash, g_strdup_printf ("%d", i), g_strdup_printf ("x-%d", i));
for (i = 0; i < 1000; i++){
char buffer [30];
gpointer value;
sprintf (buffer, "%d", i);
value = g_hash_table_lookup (hash, buffer);
sprintf (buffer, "x-%d", i);
if (strcmp (value, buffer) != 0){
return FAILED ("Failed to lookup the key %d, the value was %s\n", i, value);
}
}
if (g_hash_table_size (hash) != 1000)
return FAILED ("Did not find 1000 elements on the hash, found %d\n", g_hash_table_size (hash));
/* Now do the manual count, lets not trust the internals */
g_hash_table_foreach (hash, counter, &count);
if (count != 1000){
return FAILED ("Foreach count is not 1000");
}
g_hash_table_destroy (hash);
return NULL;
}
RESULT hash_iter (void)
{
#if !defined(GLIB_MAJOR_VERSION) || GLIB_CHECK_VERSION(2, 16, 0)
GHashTable *hash = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, NULL);
GHashTableIter iter;
int i, sum, keys_sum, values_sum;
gpointer key, value;
sum = 0;
for (i = 0; i < 1000; i++) {
sum += i;
g_hash_table_insert (hash, GUINT_TO_POINTER (i), GUINT_TO_POINTER (i));
}
keys_sum = values_sum = 0;
g_hash_table_iter_init (&iter, hash);
while (g_hash_table_iter_next (&iter, &key, &value)) {
if (key != value)
return FAILED ("key != value");
keys_sum += GPOINTER_TO_UINT (key);
values_sum += GPOINTER_TO_UINT (value);
}
if (keys_sum != sum || values_sum != sum)
return FAILED ("Did not find all key-value pairs");
g_hash_table_destroy (hash);
return NULL;
#else
/* GHashTableIter was added in glib 2.16 */
return NULL;
#endif
}
static Test hashtable_tests [] = {
{"t1", hash_t1},
{"t2", hash_t2},
{"grow", hash_grow},
{"default", hash_default},
{"null_lookup", hash_null_lookup},
{"iter", hash_iter},
{NULL, NULL}
};
DEFINE_TEST_GROUP_INIT(hashtable_tests_init, hashtable_tests)
|