Tizen 2.1 base
[platform/upstream/glib2.0.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
130 static void
131 test_dataset_destroy (void)
132 {
133   gpointer location = (gpointer)test_dataset_destroy;
134
135   destroy_count = 0;
136   g_dataset_set_data_full (location, "test1", "test1", notify);
137   g_dataset_set_data_full (location, "test2", "test2", notify);
138   g_dataset_set_data_full (location, "test3", "test3", notify);
139   g_dataset_destroy (location);
140   g_assert (destroy_count == 3);
141 }
142
143 static void
144 test_dataset_id (void)
145 {
146   gpointer location = (gpointer)test_dataset_id;
147   gpointer other = (gpointer)test_quark_basic;
148   gpointer data = "test1";
149   gpointer ret;
150   GQuark quark;
151
152   quark = g_quark_from_string ("test1");
153
154   g_dataset_id_set_data (location, quark, data);
155
156   ret = g_dataset_id_get_data (location, quark);
157   g_assert (ret == data);
158
159   ret = g_dataset_id_get_data (location, g_quark_from_string ("test2"));
160   g_assert (ret == NULL);
161
162   ret = g_dataset_id_get_data (other, quark);
163   g_assert (ret == NULL);
164
165   g_dataset_id_set_data (location, quark, "new-value");
166   ret = g_dataset_id_get_data (location, quark);
167   g_assert (ret != data);
168
169   g_dataset_id_remove_data (location, quark);
170   ret = g_dataset_id_get_data (location, quark);
171   g_assert (ret == NULL);
172 }
173
174 GData *list;
175
176 static void
177 free_one (gpointer data)
178 {
179   /* recurse */
180   g_datalist_clear (&list);
181 }
182
183 static void
184 test_datalist_clear (void)
185 {
186   if (g_test_trap_fork (500000, 0))
187     {
188       g_datalist_init (&list);
189       g_datalist_set_data_full (&list, "one", GINT_TO_POINTER (1), free_one);
190       g_datalist_set_data_full (&list, "two", GINT_TO_POINTER (2), NULL);
191       g_datalist_clear (&list);
192       g_assert (list == NULL);
193       exit (0);
194     }
195   g_test_trap_assert_passed ();
196 }
197
198 int
199 main (int argc, char *argv[])
200 {
201   g_test_init (&argc, &argv, NULL);
202
203   g_test_add_func ("/quark/basic", test_quark_basic);
204   g_test_add_func ("/quark/string", test_quark_string);
205   g_test_add_func ("/dataset/basic", test_dataset_basic);
206   g_test_add_func ("/dataset/id", test_dataset_id);
207   g_test_add_func ("/dataset/full", test_dataset_full);
208   g_test_add_func ("/dataset/foreach", test_dataset_foreach);
209   g_test_add_func ("/dataset/destroy", test_dataset_destroy);
210   g_test_add_func ("/datalist/recursive-clear", test_datalist_clear);
211
212   return g_test_run ();
213 }