Make sure x86 ATOMIC_CAS doesn't overwrite its own operands.
[mono-debugger.git] / eglib / test / hashtable.c
blob0b2f20ca5dfd4c28271334d4a0a7132fafa5e9c2
1 #include <stdio.h>
2 #include <string.h>
3 #include <glib.h>
4 #include "test.h"
6 int foreach_count = 0;
7 int foreach_fail = 0;
9 void foreach (gpointer key, gpointer value, gpointer user_data)
11 foreach_count++;
12 if (GPOINTER_TO_INT (user_data) != 'a')
13 foreach_fail = 1;
16 RESULT hash_t1 (void)
18 GHashTable *t = g_hash_table_new (g_str_hash, g_str_equal);
20 foreach_count = 0;
21 foreach_fail = 0;
22 g_hash_table_insert (t, "hello", "world");
23 g_hash_table_insert (t, "my", "god");
25 g_hash_table_foreach (t, foreach, GINT_TO_POINTER('a'));
26 if (foreach_count != 2)
27 return FAILED ("did not find all keys, got %d expected 2", foreach_count);
28 if (foreach_fail)
29 return FAILED("failed to pass the user-data to foreach");
31 if (!g_hash_table_remove (t, "my"))
32 return FAILED ("did not find known key");
33 if (g_hash_table_size (t) != 1)
34 return FAILED ("unexpected size");
35 g_hash_table_insert(t, "hello", "moon");
36 if (strcmp (g_hash_table_lookup (t, "hello"), "moon") != 0)
37 return FAILED ("did not replace world with moon");
39 if (!g_hash_table_remove (t, "hello"))
40 return FAILED ("did not find known key");
41 if (g_hash_table_size (t) != 0)
42 return FAILED ("unexpected size");
43 g_hash_table_destroy (t);
45 return OK;
48 RESULT hash_t2 (void)
50 return OK;
53 RESULT hash_default (void)
55 GHashTable *hash = g_hash_table_new (NULL, NULL);
57 if (hash == NULL)
58 return FAILED ("g_hash_table_new should return a valid hash");
60 g_hash_table_destroy (hash);
61 return NULL;
64 RESULT
65 hash_null_lookup (void)
67 GHashTable *hash = g_hash_table_new (NULL, NULL);
68 gpointer ok, ov;
70 g_hash_table_insert (hash, NULL, GINT_TO_POINTER (1));
71 g_hash_table_insert (hash, GINT_TO_POINTER(1), GINT_TO_POINTER(2));
73 if (!g_hash_table_lookup_extended (hash, NULL, &ok, &ov))
74 return FAILED ("Did not find the NULL");
75 if (ok != NULL)
76 return FAILED ("Incorrect key found");
77 if (ov != GINT_TO_POINTER (1))
78 return FAILED ("Got wrong value %p\n", ov);
80 if (!g_hash_table_lookup_extended (hash, GINT_TO_POINTER(1), &ok, &ov))
81 return FAILED ("Did not find the 1");
82 if (ok != GINT_TO_POINTER(1))
83 return FAILED ("Incorrect key found");
84 if (ov != GINT_TO_POINTER (2))
85 return FAILED ("Got wrong value %p\n", ov);
87 g_hash_table_destroy (hash);
89 return NULL;
92 static void
93 counter (gpointer key, gpointer value, gpointer user_data)
95 int *counter = (int *) user_data;
97 (*counter)++;
100 RESULT hash_grow (void)
102 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
103 int i, count = 0;
105 for (i = 0; i < 1000; i++)
106 g_hash_table_insert (hash, g_strdup_printf ("%d", i), g_strdup_printf ("x-%d", i));
108 for (i = 0; i < 1000; i++){
109 char buffer [30];
110 gpointer value;
112 sprintf (buffer, "%d", i);
114 value = g_hash_table_lookup (hash, buffer);
115 sprintf (buffer, "x-%d", i);
116 if (strcmp (value, buffer) != 0){
117 return FAILED ("Failed to lookup the key %d, the value was %s\n", i, value);
121 if (g_hash_table_size (hash) != 1000)
122 return FAILED ("Did not find 1000 elements on the hash, found %d\n", g_hash_table_size (hash));
124 /* Now do the manual count, lets not trust the internals */
125 g_hash_table_foreach (hash, counter, &count);
126 if (count != 1000){
127 return FAILED ("Foreach count is not 1000");
130 g_hash_table_destroy (hash);
131 return NULL;
134 static Test hashtable_tests [] = {
135 {"t1", hash_t1},
136 {"t2", hash_t2},
137 {"grow", hash_grow},
138 {"default", hash_default},
139 {"null_lookup", hash_null_lookup},
140 {NULL, NULL}
143 DEFINE_TEST_GROUP_INIT(hashtable_tests_init, hashtable_tests)