Fix %z in g_date_time_format()
[glib.git] / glib / tests / dataset.c
blob4b0196591a007ccf75ace1a7f278f2115b72b8f4
1 #include <glib.h>
3 static void
4 test_quark_basic (void)
6 GQuark quark;
7 const gchar *orig = "blargh";
8 gchar *copy;
9 const gchar *str;
11 quark = g_quark_try_string ("no-such-quark");
12 g_assert (quark == 0);
14 copy = g_strdup (orig);
15 quark = g_quark_from_static_string (orig);
16 g_assert (quark != 0);
17 g_assert (g_quark_from_string (orig) == quark);
18 g_assert (g_quark_from_string (copy) == quark);
19 g_assert (g_quark_try_string (orig) == quark);
21 str = g_quark_to_string (quark);
22 g_assert_cmpstr (str, ==, orig);
24 g_free (copy);
27 static void
28 test_quark_string (void)
30 const gchar *orig = "string1";
31 gchar *copy;
32 const gchar *str1;
33 const gchar *str2;
35 copy = g_strdup (orig);
37 str1 = g_intern_static_string (orig);
38 str2 = g_intern_string (copy);
39 g_assert (str1 == str2);
40 g_assert (str1 == orig);
42 g_free (copy);
45 static void
46 test_dataset_basic (void)
48 gpointer location = (gpointer)test_dataset_basic;
49 gpointer other = (gpointer)test_quark_basic;
50 gpointer data = "test1";
51 gpointer ret;
53 g_dataset_set_data (location, "test1", data);
55 ret = g_dataset_get_data (location, "test1");
56 g_assert (ret == data);
58 ret = g_dataset_get_data (location, "test2");
59 g_assert (ret == NULL);
61 ret = g_dataset_get_data (other, "test1");
62 g_assert (ret == NULL);
64 g_dataset_set_data (location, "test1", "new-value");
65 ret = g_dataset_get_data (location, "test1");
66 g_assert (ret != data);
68 g_dataset_remove_data (location, "test1");
69 ret = g_dataset_get_data (location, "test1");
70 g_assert (ret == NULL);
73 static gint destroy_count;
75 static void
76 notify (gpointer data)
78 destroy_count++;
81 static void
82 test_dataset_full (void)
84 gpointer location = (gpointer)test_dataset_full;
86 g_dataset_set_data_full (location, "test1", "test1", notify);
88 destroy_count = 0;
89 g_dataset_set_data (location, "test1", NULL);
90 g_assert (destroy_count == 1);
92 g_dataset_set_data_full (location, "test1", "test1", notify);
94 destroy_count = 0;
95 g_dataset_remove_data (location, "test1");
96 g_assert (destroy_count == 1);
98 g_dataset_set_data_full (location, "test1", "test1", notify);
100 destroy_count = 0;
101 g_dataset_remove_no_notify (location, "test1");
102 g_assert (destroy_count == 0);
105 static void
106 foreach (GQuark id,
107 gpointer data,
108 gpointer user_data)
110 gint *counter = user_data;
112 *counter += 1;
115 static void
116 test_dataset_foreach (void)
118 gpointer location = (gpointer)test_dataset_foreach;
119 gint my_count;
121 my_count = 0;
122 g_dataset_set_data_full (location, "test1", "test1", notify);
123 g_dataset_set_data_full (location, "test2", "test2", notify);
124 g_dataset_set_data_full (location, "test3", "test3", notify);
125 g_dataset_foreach (location, foreach, &my_count);
126 g_assert (my_count == 3);
129 static void
130 test_dataset_destroy (void)
132 gpointer location = (gpointer)test_dataset_destroy;
134 destroy_count = 0;
135 g_dataset_set_data_full (location, "test1", "test1", notify);
136 g_dataset_set_data_full (location, "test2", "test2", notify);
137 g_dataset_set_data_full (location, "test3", "test3", notify);
138 g_dataset_destroy (location);
139 g_assert (destroy_count == 3);
142 static void
143 test_dataset_id (void)
145 gpointer location = (gpointer)test_dataset_id;
146 gpointer other = (gpointer)test_quark_basic;
147 gpointer data = "test1";
148 gpointer ret;
149 GQuark quark;
151 quark = g_quark_from_string ("test1");
153 g_dataset_id_set_data (location, quark, data);
155 ret = g_dataset_id_get_data (location, quark);
156 g_assert (ret == data);
158 ret = g_dataset_id_get_data (location, g_quark_from_string ("test2"));
159 g_assert (ret == NULL);
161 ret = g_dataset_id_get_data (other, quark);
162 g_assert (ret == NULL);
164 g_dataset_id_set_data (location, quark, "new-value");
165 ret = g_dataset_id_get_data (location, quark);
166 g_assert (ret != data);
168 g_dataset_id_remove_data (location, quark);
169 ret = g_dataset_id_get_data (location, quark);
170 g_assert (ret == NULL);
174 main (int argc, char *argv[])
176 g_test_init (&argc, &argv, NULL);
178 g_test_add_func ("/quark/basic", test_quark_basic);
179 g_test_add_func ("/quark/string", test_quark_string);
180 g_test_add_func ("/dataset/basic", test_dataset_basic);
181 g_test_add_func ("/dataset/id", test_dataset_id);
182 g_test_add_func ("/dataset/full", test_dataset_full);
183 g_test_add_func ("/dataset/foreach", test_dataset_foreach);
184 g_test_add_func ("/dataset/destroy", test_dataset_destroy);
186 return g_test_run ();