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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
#include <glib.h>
#include <stdlib.h>
static void
test_quark_basic (void)
{
GQuark quark;
const gchar *orig = "blargh";
gchar *copy;
const gchar *str;
quark = g_quark_try_string ("no-such-quark");
g_assert (quark == 0);
copy = g_strdup (orig);
quark = g_quark_from_static_string (orig);
g_assert (quark != 0);
g_assert (g_quark_from_string (orig) == quark);
g_assert (g_quark_from_string (copy) == quark);
g_assert (g_quark_try_string (orig) == quark);
str = g_quark_to_string (quark);
g_assert_cmpstr (str, ==, orig);
g_free (copy);
}
static void
test_quark_string (void)
{
const gchar *orig = "string1";
gchar *copy;
const gchar *str1;
const gchar *str2;
copy = g_strdup (orig);
str1 = g_intern_static_string (orig);
str2 = g_intern_string (copy);
g_assert (str1 == str2);
g_assert (str1 == orig);
g_free (copy);
}
static void
test_dataset_basic (void)
{
gpointer location = (gpointer)test_dataset_basic;
gpointer other = (gpointer)test_quark_basic;
gpointer data = "test1";
gpointer ret;
g_dataset_set_data (location, "test1", data);
ret = g_dataset_get_data (location, "test1");
g_assert (ret == data);
ret = g_dataset_get_data (location, "test2");
g_assert (ret == NULL);
ret = g_dataset_get_data (other, "test1");
g_assert (ret == NULL);
g_dataset_set_data (location, "test1", "new-value");
ret = g_dataset_get_data (location, "test1");
g_assert (ret != data);
g_dataset_remove_data (location, "test1");
ret = g_dataset_get_data (location, "test1");
g_assert (ret == NULL);
ret = g_dataset_get_data (location, NULL);
g_assert (ret == NULL);
}
static gint destroy_count;
static void
notify (gpointer data)
{
destroy_count++;
}
static void
test_dataset_full (void)
{
gpointer location = (gpointer)test_dataset_full;
g_dataset_set_data_full (location, "test1", "test1", notify);
destroy_count = 0;
g_dataset_set_data (location, "test1", NULL);
g_assert (destroy_count == 1);
g_dataset_set_data_full (location, "test1", "test1", notify);
destroy_count = 0;
g_dataset_remove_data (location, "test1");
g_assert (destroy_count == 1);
g_dataset_set_data_full (location, "test1", "test1", notify);
destroy_count = 0;
g_dataset_remove_no_notify (location, "test1");
g_assert (destroy_count == 0);
}
static void
foreach (GQuark id,
gpointer data,
gpointer user_data)
{
gint *counter = user_data;
*counter += 1;
}
static void
test_dataset_foreach (void)
{
gpointer location = (gpointer)test_dataset_foreach;
gint my_count;
my_count = 0;
g_dataset_set_data_full (location, "test1", "test1", notify);
g_dataset_set_data_full (location, "test2", "test2", notify);
g_dataset_set_data_full (location, "test3", "test3", notify);
g_dataset_foreach (location, foreach, &my_count);
g_assert (my_count == 3);
g_dataset_destroy (location);
}
static void
test_dataset_destroy (void)
{
gpointer location = (gpointer)test_dataset_destroy;
destroy_count = 0;
g_dataset_set_data_full (location, "test1", "test1", notify);
g_dataset_set_data_full (location, "test2", "test2", notify);
g_dataset_set_data_full (location, "test3", "test3", notify);
g_dataset_destroy (location);
g_assert (destroy_count == 3);
}
static void
test_dataset_id (void)
{
gpointer location = (gpointer)test_dataset_id;
gpointer other = (gpointer)test_quark_basic;
gpointer data = "test1";
gpointer ret;
GQuark quark;
quark = g_quark_from_string ("test1");
g_dataset_id_set_data (location, quark, data);
ret = g_dataset_id_get_data (location, quark);
g_assert (ret == data);
ret = g_dataset_id_get_data (location, g_quark_from_string ("test2"));
g_assert (ret == NULL);
ret = g_dataset_id_get_data (other, quark);
g_assert (ret == NULL);
g_dataset_id_set_data (location, quark, "new-value");
ret = g_dataset_id_get_data (location, quark);
g_assert (ret != data);
g_dataset_id_remove_data (location, quark);
ret = g_dataset_id_get_data (location, quark);
g_assert (ret == NULL);
ret = g_dataset_id_get_data (location, 0);
g_assert (ret == NULL);
}
static GData *global_list;
static void
free_one (gpointer data)
{
/* recurse */
g_datalist_clear (&global_list);
}
static void
test_datalist_clear (void)
{
/* Need to use a subprocess because it will deadlock if it fails */
if (g_test_subprocess ())
{
g_datalist_init (&global_list);
g_datalist_set_data_full (&global_list, "one", GINT_TO_POINTER (1), free_one);
g_datalist_set_data_full (&global_list, "two", GINT_TO_POINTER (2), NULL);
g_datalist_clear (&global_list);
g_assert (global_list == NULL);
return;
}
g_test_trap_subprocess (NULL, 500000, G_TEST_SUBPROCESS_DEFAULT);
g_test_trap_assert_passed ();
}
static void
test_datalist_basic (void)
{
GData *list = NULL;
gpointer data;
gpointer ret;
g_datalist_init (&list);
data = "one";
g_datalist_set_data (&list, "one", data);
ret = g_datalist_get_data (&list, "one");
g_assert (ret == data);
ret = g_datalist_get_data (&list, "two");
g_assert (ret == NULL);
ret = g_datalist_get_data (&list, NULL);
g_assert (ret == NULL);
g_datalist_clear (&list);
}
static void
test_datalist_id (void)
{
GData *list = NULL;
gpointer data;
gpointer ret;
g_datalist_init (&list);
data = "one";
g_datalist_id_set_data (&list, g_quark_from_string ("one"), data);
ret = g_datalist_id_get_data (&list, g_quark_from_string ("one"));
g_assert (ret == data);
ret = g_datalist_id_get_data (&list, g_quark_from_string ("two"));
g_assert (ret == NULL);
ret = g_datalist_id_get_data (&list, 0);
g_assert (ret == NULL);
g_datalist_clear (&list);
}
static void
test_datalist_id_remove_multiple (void)
{
/* Test that g_datalist_id_remove_multiple() removes all the keys it
* is given. */
GData *list = NULL;
GQuark one = g_quark_from_static_string ("one");
GQuark two = g_quark_from_static_string ("two");
GQuark three = g_quark_from_static_string ("three");
GQuark keys[] = {
one,
two,
three,
};
g_test_bug ("https://gitlab.gnome.org/GNOME/glib/issues/2672");
g_datalist_init (&list);
g_datalist_id_set_data (&list, one, GINT_TO_POINTER (1));
g_datalist_id_set_data (&list, two, GINT_TO_POINTER (2));
g_datalist_id_set_data (&list, three, GINT_TO_POINTER (3));
destroy_count = 0;
g_datalist_foreach (&list, (GDataForeachFunc) notify, NULL);
g_assert_cmpint (destroy_count, ==, 3);
g_datalist_id_remove_multiple (&list, keys, G_N_ELEMENTS (keys));
destroy_count = 0;
g_datalist_foreach (&list, (GDataForeachFunc) notify, NULL);
g_assert_cmpint (destroy_count, ==, 0);
}
static void
destroy_func (gpointer data)
{
destroy_count++;
g_assert_cmpint (GPOINTER_TO_INT (data), ==, destroy_count);
}
static void
test_datalist_id_remove_multiple_destroy_order (void)
{
/* Test that destroy-funcs are called in the order that the keys are
* specified, not the order that they are found in the datalist. */
GData *list = NULL;
GQuark one = g_quark_from_static_string ("one");
GQuark two = g_quark_from_static_string ("two");
GQuark three = g_quark_from_static_string ("three");
GQuark keys[] = {
one,
two,
three,
};
g_test_bug ("https://gitlab.gnome.org/GNOME/glib/issues/2672");
g_datalist_init (&list);
g_datalist_id_set_data_full (&list, two, GINT_TO_POINTER (2), destroy_func);
g_datalist_id_set_data_full (&list, three, GINT_TO_POINTER (3), destroy_func);
g_datalist_id_set_data_full (&list, one, GINT_TO_POINTER (1), destroy_func);
destroy_count = 0;
g_datalist_id_remove_multiple (&list, keys, G_N_ELEMENTS (keys));
/* This verifies that destroy_func() was called three times: */
g_assert_cmpint (destroy_count, ==, 3);
}
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/quark/basic", test_quark_basic);
g_test_add_func ("/quark/string", test_quark_string);
g_test_add_func ("/dataset/basic", test_dataset_basic);
g_test_add_func ("/dataset/id", test_dataset_id);
g_test_add_func ("/dataset/full", test_dataset_full);
g_test_add_func ("/dataset/foreach", test_dataset_foreach);
g_test_add_func ("/dataset/destroy", test_dataset_destroy);
g_test_add_func ("/datalist/basic", test_datalist_basic);
g_test_add_func ("/datalist/id", test_datalist_id);
g_test_add_func ("/datalist/recursive-clear", test_datalist_clear);
g_test_add_func ("/datalist/id-remove-multiple", test_datalist_id_remove_multiple);
g_test_add_func ("/datalist/id-remove-multiple-destroy-order",
test_datalist_id_remove_multiple_destroy_order);
return g_test_run ();
}
|