utf8: add unit test for g_utf8_make_valid
[glib.git] / glib / tests / autoptr.c
blobb44cf138beb4a66227dc177664714e06ecc0daa3
1 #include <glib.h>
2 #include <string.h>
4 static void
5 test_autofree (void)
7 g_autofree gchar *p = NULL;
8 g_autofree gchar *p2 = NULL;
9 g_autofree gchar *alwaysnull = NULL;
11 p = g_malloc (10);
12 p2 = g_malloc (42);
14 if (TRUE)
16 g_autofree guint8 *buf = g_malloc (128);
17 g_autofree gchar *alwaysnull_again = NULL;
19 buf[0] = 1;
22 if (TRUE)
24 g_autofree guint8 *buf2 = g_malloc (256);
26 buf2[255] = 42;
30 static void
31 test_g_async_queue (void)
33 g_autoptr(GAsyncQueue) val = g_async_queue_new ();
34 g_assert (val != NULL);
37 static void
38 test_g_bookmark_file (void)
40 g_autoptr(GBookmarkFile) val = g_bookmark_file_new ();
41 g_assert (val != NULL);
44 static void
45 test_g_bytes (void)
47 g_autoptr(GBytes) val = g_bytes_new ("foo", 3);
48 g_assert (val != NULL);
51 static void
52 test_g_checksum (void)
54 g_autoptr(GChecksum) val = g_checksum_new (G_CHECKSUM_SHA256);
55 g_assert (val != NULL);
58 static void
59 test_g_date_time (void)
61 g_autoptr(GDateTime) val = g_date_time_new_now_utc ();
62 g_assert (val != NULL);
65 static void
66 test_g_dir (void)
68 g_autoptr(GDir) val = g_dir_open (".", 0, NULL);
69 g_assert (val != NULL);
72 static void
73 test_g_error (void)
75 g_autoptr(GError) val = g_error_new_literal (G_FILE_ERROR, G_FILE_ERROR_FAILED, "oops");
76 g_assert (val != NULL);
79 static void
80 test_g_hash_table (void)
82 g_autoptr(GHashTable) val = g_hash_table_new (NULL, NULL);
83 g_assert (val != NULL);
86 static void
87 test_g_hmac (void)
89 g_autoptr(GHmac) val = g_hmac_new (G_CHECKSUM_SHA256, (guint8*)"hello", 5);
90 g_assert (val != NULL);
93 static void
94 test_g_io_channel (void)
96 g_autoptr(GIOChannel) val = g_io_channel_new_file ("/dev/null", "r", NULL);
97 g_assert (val != NULL);
100 static void
101 test_g_key_file (void)
103 g_autoptr(GKeyFile) val = g_key_file_new ();
104 g_assert (val != NULL);
107 static void
108 test_g_list (void)
110 g_autoptr(GList) val = NULL;
111 g_autoptr(GList) val2 = g_list_prepend (NULL, "foo");
112 g_assert (val == NULL);
113 g_assert (val2 != NULL);
116 static void
117 test_g_array (void)
119 g_autoptr(GArray) val = g_array_new (0, 0, sizeof (gpointer));
120 g_assert (val != NULL);
123 static void
124 test_g_ptr_array (void)
126 g_autoptr(GPtrArray) val = g_ptr_array_new ();
127 g_assert (val != NULL);
130 static void
131 test_g_byte_array (void)
133 g_autoptr(GByteArray) val = g_byte_array_new ();
134 g_assert (val != NULL);
137 static void
138 test_g_main_context (void)
140 g_autoptr(GMainContext) val = g_main_context_new ();
141 g_assert (val != NULL);
144 static void
145 test_g_main_loop (void)
147 g_autoptr(GMainLoop) val = g_main_loop_new (NULL, TRUE);
148 g_assert (val != NULL);
151 static void
152 test_g_source (void)
154 g_autoptr(GSource) val = g_timeout_source_new_seconds (2);
155 g_assert (val != NULL);
158 static void
159 test_g_mapped_file (void)
161 g_autoptr(GMappedFile) val = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "keyfiletest.ini", NULL), FALSE, NULL);
162 g_assert (val != NULL);
165 static void
166 parser_start (GMarkupParseContext *context,
167 const gchar *element_name,
168 const gchar **attribute_names,
169 const gchar **attribute_values,
170 gpointer user_data,
171 GError **error)
175 static void
176 parser_end (GMarkupParseContext *context,
177 const gchar *element_name,
178 gpointer user_data,
179 GError **error)
183 static GMarkupParser parser = {
184 .start_element = parser_start,
185 .end_element = parser_end
188 static void
189 test_g_markup_parse_context (void)
191 g_autoptr(GMarkupParseContext) val = g_markup_parse_context_new (&parser, 0, NULL, NULL);
192 g_assert (val != NULL);
195 static void
196 test_g_node (void)
198 g_autoptr(GNode) val = g_node_new ("hello");
199 g_assert (val != NULL);
202 static void
203 test_g_option_context (void)
205 g_autoptr(GOptionContext) val = g_option_context_new ("hello");
206 g_assert (val != NULL);
209 static void
210 test_g_option_group (void)
212 g_autoptr(GOptionGroup) val = g_option_group_new ("hello", "world", "helpme", NULL, NULL);
213 g_assert (val != NULL);
216 static void
217 test_g_pattern_spec (void)
219 g_autoptr(GPatternSpec) val = g_pattern_spec_new ("plaid");
220 g_assert (val != NULL);
223 static void
224 test_g_queue (void)
226 g_autoptr(GQueue) val = g_queue_new ();
227 g_auto(GQueue) stackval = G_QUEUE_INIT;
228 g_assert (val != NULL);
231 static void
232 test_g_rand (void)
234 g_autoptr(GRand) val = g_rand_new ();
235 g_assert (val != NULL);
238 static void
239 test_g_regex (void)
241 g_autoptr(GRegex) val = g_regex_new (".*", 0, 0, NULL);
242 g_assert (val != NULL);
245 static void
246 test_g_match_info (void)
248 g_autoptr(GRegex) regex = g_regex_new (".*", 0, 0, NULL);
249 g_autoptr(GMatchInfo) match = NULL;
251 if (!g_regex_match (regex, "hello", 0, &match))
252 g_assert_not_reached ();
255 static void
256 test_g_scanner (void)
258 GScannerConfig config = { 0, };
259 g_autoptr(GScanner) val = g_scanner_new (&config);
260 g_assert (val != NULL);
263 static void
264 test_g_sequence (void)
266 g_autoptr(GSequence) val = g_sequence_new (NULL);
267 g_assert (val != NULL);
270 static void
271 test_g_slist (void)
273 g_autoptr(GSList) val = NULL;
274 g_autoptr(GSList) nonempty_val = g_slist_prepend (NULL, "hello");
275 g_assert (val == NULL);
276 g_assert (nonempty_val != NULL);
279 static void
280 test_g_string (void)
282 g_autoptr(GString) val = g_string_new ("");
283 g_assert (val != NULL);
286 static void
287 test_g_string_chunk (void)
289 g_autoptr(GStringChunk) val = g_string_chunk_new (42);
290 g_assert (val != NULL);
293 static gpointer
294 mythread (gpointer data)
296 g_usleep (G_USEC_PER_SEC);
297 return NULL;
300 static void
301 test_g_thread (void)
303 g_autoptr(GThread) val = g_thread_new ("bob", mythread, NULL);
304 g_assert (val != NULL);
307 static void
308 test_g_mutex (void)
310 g_auto(GMutex) val;
312 g_mutex_init (&val);
315 static void
316 test_g_mutex_locker (void)
318 GMutex mutex;
320 g_mutex_init (&mutex);
322 if (TRUE)
324 g_autoptr(GMutexLocker) val = g_mutex_locker_new (&mutex);
326 g_assert (val != NULL);
330 static void
331 test_g_cond (void)
333 g_auto(GCond) val;
334 g_cond_init (&val);
337 static void
338 test_g_timer (void)
340 g_autoptr(GTimer) val = g_timer_new ();
341 g_assert (val != NULL);
344 static void
345 test_g_time_zone (void)
347 g_autoptr(GTimeZone) val = g_time_zone_new ("UTC");
348 g_assert (val != NULL);
351 static void
352 test_g_tree (void)
354 g_autoptr(GTree) val = g_tree_new ((GCompareFunc)strcmp);
355 g_assert (val != NULL);
358 static void
359 test_g_variant (void)
361 g_autoptr(GVariant) val = g_variant_new_string ("hello");
362 g_assert (val != NULL);
365 static void
366 test_g_variant_builder (void)
368 g_autoptr(GVariantBuilder) val = g_variant_builder_new (G_VARIANT_TYPE ("as"));
369 g_auto(GVariantBuilder) stackval;
371 g_assert (val != NULL);
372 g_variant_builder_init (&stackval, G_VARIANT_TYPE ("as"));
375 static void
376 test_g_variant_iter (void)
378 g_autoptr(GVariant) var = g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32, "", 0, sizeof(guint32));
379 g_autoptr(GVariantIter) val = g_variant_iter_new (var);
380 g_assert (val != NULL);
383 static void
384 test_g_variant_dict (void)
386 g_autoptr(GVariant) data = g_variant_new_from_data (G_VARIANT_TYPE ("a{sv}"), "", 0, FALSE, NULL, NULL);
387 g_auto(GVariantDict) stackval;
388 g_autoptr(GVariantDict) val = g_variant_dict_new (data);
390 g_variant_dict_init (&stackval, data);
391 g_assert (val != NULL);
394 static void
395 test_g_variant_type (void)
397 g_autoptr(GVariantType) val = g_variant_type_new ("s");
398 g_assert (val != NULL);
401 static void
402 test_strv (void)
404 g_auto(GStrv) val = g_strsplit("a:b:c", ":", -1);
405 g_assert (val != NULL);
409 main (int argc, gchar *argv[])
411 g_test_init (&argc, &argv, NULL);
413 g_test_add_func ("/autoptr/autofree", test_autofree);
414 g_test_add_func ("/autoptr/g_async_queue", test_g_async_queue);
415 g_test_add_func ("/autoptr/g_bookmark_file", test_g_bookmark_file);
416 g_test_add_func ("/autoptr/g_bytes", test_g_bytes);
417 g_test_add_func ("/autoptr/g_checksum", test_g_checksum);
418 g_test_add_func ("/autoptr/g_date_time", test_g_date_time);
419 g_test_add_func ("/autoptr/g_dir", test_g_dir);
420 g_test_add_func ("/autoptr/g_error", test_g_error);
421 g_test_add_func ("/autoptr/g_hash_table", test_g_hash_table);
422 g_test_add_func ("/autoptr/g_hmac", test_g_hmac);
423 g_test_add_func ("/autoptr/g_io_channel", test_g_io_channel);
424 g_test_add_func ("/autoptr/g_key_file", test_g_key_file);
425 g_test_add_func ("/autoptr/g_list", test_g_list);
426 g_test_add_func ("/autoptr/g_array", test_g_array);
427 g_test_add_func ("/autoptr/g_ptr_array", test_g_ptr_array);
428 g_test_add_func ("/autoptr/g_byte_array", test_g_byte_array);
429 g_test_add_func ("/autoptr/g_main_context", test_g_main_context);
430 g_test_add_func ("/autoptr/g_main_loop", test_g_main_loop);
431 g_test_add_func ("/autoptr/g_source", test_g_source);
432 g_test_add_func ("/autoptr/g_mapped_file", test_g_mapped_file);
433 g_test_add_func ("/autoptr/g_markup_parse_context", test_g_markup_parse_context);
434 g_test_add_func ("/autoptr/g_node", test_g_node);
435 g_test_add_func ("/autoptr/g_option_context", test_g_option_context);
436 g_test_add_func ("/autoptr/g_option_group", test_g_option_group);
437 g_test_add_func ("/autoptr/g_pattern_spec", test_g_pattern_spec);
438 g_test_add_func ("/autoptr/g_queue", test_g_queue);
439 g_test_add_func ("/autoptr/g_rand", test_g_rand);
440 g_test_add_func ("/autoptr/g_regex", test_g_regex);
441 g_test_add_func ("/autoptr/g_match_info", test_g_match_info);
442 g_test_add_func ("/autoptr/g_scanner", test_g_scanner);
443 g_test_add_func ("/autoptr/g_sequence", test_g_sequence);
444 g_test_add_func ("/autoptr/g_slist", test_g_slist);
445 g_test_add_func ("/autoptr/g_string", test_g_string);
446 g_test_add_func ("/autoptr/g_string_chunk", test_g_string_chunk);
447 g_test_add_func ("/autoptr/g_thread", test_g_thread);
448 g_test_add_func ("/autoptr/g_mutex", test_g_mutex);
449 g_test_add_func ("/autoptr/g_mutex_locker", test_g_mutex_locker);
450 g_test_add_func ("/autoptr/g_cond", test_g_cond);
451 g_test_add_func ("/autoptr/g_timer", test_g_timer);
452 g_test_add_func ("/autoptr/g_time_zone", test_g_time_zone);
453 g_test_add_func ("/autoptr/g_tree", test_g_tree);
454 g_test_add_func ("/autoptr/g_variant", test_g_variant);
455 g_test_add_func ("/autoptr/g_variant_builder", test_g_variant_builder);
456 g_test_add_func ("/autoptr/g_variant_iter", test_g_variant_iter);
457 g_test_add_func ("/autoptr/g_variant_dict", test_g_variant_dict);
458 g_test_add_func ("/autoptr/g_variant_type", test_g_variant_type);
459 g_test_add_func ("/autoptr/strv", test_strv);
461 return g_test_run ();