tests: Fix some minor leaks in the unit tests
[platform/upstream/glib.git] / glib / tests / dataset.c
1 #include <glib.h>
2 #include <stdlib.h>
3
4 static void
5 test_quark_basic (void)
6 {
7   GQuark quark;
8   const gchar *orig = "blargh";
9   gchar *copy;
10   const gchar *str;
11
12   quark = g_quark_try_string ("no-such-quark");
13   g_assert (quark == 0);
14
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);
21
22   str = g_quark_to_string (quark);
23   g_assert_cmpstr (str, ==, orig);
24
25   g_free (copy);
26 }
27
28 static void
29 test_quark_string (void)
30 {
31   const gchar *orig = "string1";
32   gchar *copy;
33   const gchar *str1;
34   const gchar *str2;
35
36   copy = g_strdup (orig);
37
38   str1 = g_intern_static_string (orig);
39   str2 = g_intern_string (copy);
40   g_assert (str1 == str2);
41   g_assert (str1 == orig);
42
43   g_free (copy);
44 }
45
46 static void
47 test_dataset_basic (void)
48 {
49   gpointer location = (gpointer)test_dataset_basic;
50   gpointer other = (gpointer)test_quark_basic;
51   gpointer data = "test1";
52   gpointer ret;
53
54   g_dataset_set_data (location, "test1", data);
55
56   ret = g_dataset_get_data (location, "test1");
57   g_assert (ret == data);
58
59   ret = g_dataset_get_data (location, "test2");
60   g_assert (ret == NULL);
61
62   ret = g_dataset_get_data (other, "test1");
63   g_assert (ret == NULL);
64
65   g_dataset_set_data (location, "test1", "new-value");
66   ret = g_dataset_get_data (location, "test1");
67   g_assert (ret != data);
68
69   g_dataset_remove_data (location, "test1");
70   ret = g_dataset_get_data (location, "test1");
71   g_assert (ret == NULL);
72 }
73
74 static gint destroy_count;
75
76 static void
77 notify (gpointer data)
78 {
79   destroy_count++;
80 }
81
82 static void
83 test_dataset_full (void)
84 {
85   gpointer location = (gpointer)test_dataset_full;
86
87   g_dataset_set_data_full (location, "test1", "test1", notify);
88
89   destroy_count = 0;
90   g_dataset_set_data (location, "test1", NULL);
91   g_assert (destroy_count == 1);
92
93   g_dataset_set_data_full (location, "test1", "test1", notify);
94
95   destroy_count = 0;
96   g_dataset_remove_data (location, "test1");
97   g_assert (destroy_count == 1);
98
99   g_dataset_set_data_full (location, "test1", "test1", notify);
100
101   destroy_count = 0;
102   g_dataset_remove_no_notify (location, "test1");
103   g_assert (destroy_count == 0);
104 }
105
106 static void
107 foreach (GQuark   id,
108          gpointer data,
109          gpointer user_data)
110 {
111   gint *counter = user_data;
112
113   *counter += 1;
114 }
115
116 static void
117 test_dataset_foreach (void)
118 {
119   gpointer location = (gpointer)test_dataset_foreach;
120   gint my_count;
121
122   my_count = 0;
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);
128
129   g_dataset_destroy (location);
130 }
131
132 static void
133 test_dataset_destroy (void)
134 {
135   gpointer location = (gpointer)test_dataset_destroy;
136
137   destroy_count = 0;
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);
143 }
144
145 static void
146 test_dataset_id (void)
147 {
148   gpointer location = (gpointer)test_dataset_id;
149   gpointer other = (gpointer)test_quark_basic;
150   gpointer data = "test1";
151   gpointer ret;
152   GQuark quark;
153
154   quark = g_quark_from_string ("test1");
155
156   g_dataset_id_set_data (location, quark, data);
157
158   ret = g_dataset_id_get_data (location, quark);
159   g_assert (ret == data);
160
161   ret = g_dataset_id_get_data (location, g_quark_from_string ("test2"));
162   g_assert (ret == NULL);
163
164   ret = g_dataset_id_get_data (other, quark);
165   g_assert (ret == NULL);
166
167   g_dataset_id_set_data (location, quark, "new-value");
168   ret = g_dataset_id_get_data (location, quark);
169   g_assert (ret != data);
170
171   g_dataset_id_remove_data (location, quark);
172   ret = g_dataset_id_get_data (location, quark);
173   g_assert (ret == NULL);
174 }
175
176 GData *list;
177
178 static void
179 free_one (gpointer data)
180 {
181   /* recurse */
182   g_datalist_clear (&list);
183 }
184
185 static void
186 test_datalist_clear (void)
187 {
188   /* Need to use a subprocess because it will deadlock if it fails */
189   if (g_test_subprocess ())
190     {
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);
196       return;
197     }
198
199   g_test_trap_subprocess (NULL, 500000, 0);
200   g_test_trap_assert_passed ();
201 }
202
203 int
204 main (int argc, char *argv[])
205 {
206   g_test_init (&argc, &argv, NULL);
207
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);
216
217   return g_test_run ();
218 }