5 test_quark_basic (void)
8 const gchar *orig = "blargh";
12 quark = g_quark_try_string ("no-such-quark");
13 g_assert (quark == 0);
15 copy = g_strdup (orig);
16 quark = g_quark_from_static_string (orig);
17 g_assert (quark != 0);
18 g_assert (g_quark_from_string (orig) == quark);
19 g_assert (g_quark_from_string (copy) == quark);
20 g_assert (g_quark_try_string (orig) == quark);
22 str = g_quark_to_string (quark);
23 g_assert_cmpstr (str, ==, orig);
29 test_quark_string (void)
31 const gchar *orig = "string1";
36 copy = g_strdup (orig);
38 str1 = g_intern_static_string (orig);
39 str2 = g_intern_string (copy);
40 g_assert (str1 == str2);
41 g_assert (str1 == orig);
47 test_dataset_basic (void)
49 gpointer location = (gpointer)test_dataset_basic;
50 gpointer other = (gpointer)test_quark_basic;
51 gpointer data = "test1";
54 g_dataset_set_data (location, "test1", data);
56 ret = g_dataset_get_data (location, "test1");
57 g_assert (ret == data);
59 ret = g_dataset_get_data (location, "test2");
60 g_assert (ret == NULL);
62 ret = g_dataset_get_data (other, "test1");
63 g_assert (ret == NULL);
65 g_dataset_set_data (location, "test1", "new-value");
66 ret = g_dataset_get_data (location, "test1");
67 g_assert (ret != data);
69 g_dataset_remove_data (location, "test1");
70 ret = g_dataset_get_data (location, "test1");
71 g_assert (ret == NULL);
74 static gint destroy_count;
77 notify (gpointer data)
83 test_dataset_full (void)
85 gpointer location = (gpointer)test_dataset_full;
87 g_dataset_set_data_full (location, "test1", "test1", notify);
90 g_dataset_set_data (location, "test1", NULL);
91 g_assert (destroy_count == 1);
93 g_dataset_set_data_full (location, "test1", "test1", notify);
96 g_dataset_remove_data (location, "test1");
97 g_assert (destroy_count == 1);
99 g_dataset_set_data_full (location, "test1", "test1", notify);
102 g_dataset_remove_no_notify (location, "test1");
103 g_assert (destroy_count == 0);
111 gint *counter = user_data;
117 test_dataset_foreach (void)
119 gpointer location = (gpointer)test_dataset_foreach;
123 g_dataset_set_data_full (location, "test1", "test1", notify);
124 g_dataset_set_data_full (location, "test2", "test2", notify);
125 g_dataset_set_data_full (location, "test3", "test3", notify);
126 g_dataset_foreach (location, foreach, &my_count);
127 g_assert (my_count == 3);
129 g_dataset_destroy (location);
133 test_dataset_destroy (void)
135 gpointer location = (gpointer)test_dataset_destroy;
138 g_dataset_set_data_full (location, "test1", "test1", notify);
139 g_dataset_set_data_full (location, "test2", "test2", notify);
140 g_dataset_set_data_full (location, "test3", "test3", notify);
141 g_dataset_destroy (location);
142 g_assert (destroy_count == 3);
146 test_dataset_id (void)
148 gpointer location = (gpointer)test_dataset_id;
149 gpointer other = (gpointer)test_quark_basic;
150 gpointer data = "test1";
154 quark = g_quark_from_string ("test1");
156 g_dataset_id_set_data (location, quark, data);
158 ret = g_dataset_id_get_data (location, quark);
159 g_assert (ret == data);
161 ret = g_dataset_id_get_data (location, g_quark_from_string ("test2"));
162 g_assert (ret == NULL);
164 ret = g_dataset_id_get_data (other, quark);
165 g_assert (ret == NULL);
167 g_dataset_id_set_data (location, quark, "new-value");
168 ret = g_dataset_id_get_data (location, quark);
169 g_assert (ret != data);
171 g_dataset_id_remove_data (location, quark);
172 ret = g_dataset_id_get_data (location, quark);
173 g_assert (ret == NULL);
179 free_one (gpointer data)
182 g_datalist_clear (&list);
186 test_datalist_clear (void)
188 /* Need to use a subprocess because it will deadlock if it fails */
189 if (g_test_subprocess ())
191 g_datalist_init (&list);
192 g_datalist_set_data_full (&list, "one", GINT_TO_POINTER (1), free_one);
193 g_datalist_set_data_full (&list, "two", GINT_TO_POINTER (2), NULL);
194 g_datalist_clear (&list);
195 g_assert (list == NULL);
199 g_test_trap_subprocess (NULL, 500000, 0);
200 g_test_trap_assert_passed ();
204 main (int argc, char *argv[])
206 g_test_init (&argc, &argv, NULL);
208 g_test_add_func ("/quark/basic", test_quark_basic);
209 g_test_add_func ("/quark/string", test_quark_string);
210 g_test_add_func ("/dataset/basic", test_dataset_basic);
211 g_test_add_func ("/dataset/id", test_dataset_id);
212 g_test_add_func ("/dataset/full", test_dataset_full);
213 g_test_add_func ("/dataset/foreach", test_dataset_foreach);
214 g_test_add_func ("/dataset/destroy", test_dataset_destroy);
215 g_test_add_func ("/datalist/recursive-clear", test_datalist_clear);
217 return g_test_run ();