Increase the timeout for some GLib tests
[glib.git] / glib / tests / autoptr.c
blobca6ae3464fde9cccefaca8a7d9ee75a611b03320
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;
21 g_assert_null (alwaysnull_again);
24 if (TRUE)
26 g_autofree guint8 *buf2 = g_malloc (256);
28 buf2[255] = 42;
31 g_assert_null (alwaysnull);
34 static void
35 test_g_async_queue (void)
37 g_autoptr(GAsyncQueue) val = g_async_queue_new ();
38 g_assert (val != NULL);
41 static void
42 test_g_bookmark_file (void)
44 g_autoptr(GBookmarkFile) val = g_bookmark_file_new ();
45 g_assert (val != NULL);
48 static void
49 test_g_bytes (void)
51 g_autoptr(GBytes) val = g_bytes_new ("foo", 3);
52 g_assert (val != NULL);
55 static void
56 test_g_checksum (void)
58 g_autoptr(GChecksum) val = g_checksum_new (G_CHECKSUM_SHA256);
59 g_assert (val != NULL);
62 static void
63 test_g_date_time (void)
65 g_autoptr(GDateTime) val = g_date_time_new_now_utc ();
66 g_assert (val != NULL);
69 static void
70 test_g_dir (void)
72 g_autoptr(GDir) val = g_dir_open (".", 0, NULL);
73 g_assert (val != NULL);
76 static void
77 test_g_error (void)
79 g_autoptr(GError) val = g_error_new_literal (G_FILE_ERROR, G_FILE_ERROR_FAILED, "oops");
80 g_assert (val != NULL);
83 static void
84 test_g_hash_table (void)
86 g_autoptr(GHashTable) val = g_hash_table_new (NULL, NULL);
87 g_assert (val != NULL);
90 static void
91 test_g_hmac (void)
93 g_autoptr(GHmac) val = g_hmac_new (G_CHECKSUM_SHA256, (guint8*)"hello", 5);
94 g_assert (val != NULL);
97 static void
98 test_g_io_channel (void)
100 g_autoptr(GIOChannel) val = g_io_channel_new_file ("/dev/null", "r", NULL);
101 g_assert (val != NULL);
104 static void
105 test_g_key_file (void)
107 g_autoptr(GKeyFile) val = g_key_file_new ();
108 g_assert (val != NULL);
111 static void
112 test_g_list (void)
114 g_autoptr(GList) val = NULL;
115 g_autoptr(GList) val2 = g_list_prepend (NULL, "foo");
116 g_assert (val == NULL);
117 g_assert (val2 != NULL);
120 static void
121 test_g_array (void)
123 g_autoptr(GArray) val = g_array_new (0, 0, sizeof (gpointer));
124 g_assert (val != NULL);
127 static void
128 test_g_ptr_array (void)
130 g_autoptr(GPtrArray) val = g_ptr_array_new ();
131 g_assert (val != NULL);
134 static void
135 test_g_byte_array (void)
137 g_autoptr(GByteArray) val = g_byte_array_new ();
138 g_assert (val != NULL);
141 static void
142 test_g_main_context (void)
144 g_autoptr(GMainContext) val = g_main_context_new ();
145 g_assert (val != NULL);
148 static void
149 test_g_main_loop (void)
151 g_autoptr(GMainLoop) val = g_main_loop_new (NULL, TRUE);
152 g_assert (val != NULL);
155 static void
156 test_g_source (void)
158 g_autoptr(GSource) val = g_timeout_source_new_seconds (2);
159 g_assert (val != NULL);
162 static void
163 test_g_mapped_file (void)
165 g_autoptr(GMappedFile) val = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "keyfiletest.ini", NULL), FALSE, NULL);
166 g_assert (val != NULL);
169 static void
170 parser_start (GMarkupParseContext *context,
171 const gchar *element_name,
172 const gchar **attribute_names,
173 const gchar **attribute_values,
174 gpointer user_data,
175 GError **error)
179 static void
180 parser_end (GMarkupParseContext *context,
181 const gchar *element_name,
182 gpointer user_data,
183 GError **error)
187 static GMarkupParser parser = {
188 .start_element = parser_start,
189 .end_element = parser_end
192 static void
193 test_g_markup_parse_context (void)
195 g_autoptr(GMarkupParseContext) val = g_markup_parse_context_new (&parser, 0, NULL, NULL);
196 g_assert (val != NULL);
199 static void
200 test_g_node (void)
202 g_autoptr(GNode) val = g_node_new ("hello");
203 g_assert (val != NULL);
206 static void
207 test_g_option_context (void)
209 g_autoptr(GOptionContext) val = g_option_context_new ("hello");
210 g_assert (val != NULL);
213 static void
214 test_g_option_group (void)
216 g_autoptr(GOptionGroup) val = g_option_group_new ("hello", "world", "helpme", NULL, NULL);
217 g_assert (val != NULL);
220 static void
221 test_g_pattern_spec (void)
223 g_autoptr(GPatternSpec) val = g_pattern_spec_new ("plaid");
224 g_assert (val != NULL);
227 static void
228 test_g_queue (void)
230 g_autoptr(GQueue) val = g_queue_new ();
231 g_auto(GQueue) stackval = G_QUEUE_INIT;
232 g_assert (val != NULL);
233 g_assert_null (stackval.head);
236 static void
237 test_g_rand (void)
239 g_autoptr(GRand) val = g_rand_new ();
240 g_assert (val != NULL);
243 static void
244 test_g_regex (void)
246 g_autoptr(GRegex) val = g_regex_new (".*", 0, 0, NULL);
247 g_assert (val != NULL);
250 static void
251 test_g_match_info (void)
253 g_autoptr(GRegex) regex = g_regex_new (".*", 0, 0, NULL);
254 g_autoptr(GMatchInfo) match = NULL;
256 if (!g_regex_match (regex, "hello", 0, &match))
257 g_assert_not_reached ();
260 static void
261 test_g_scanner (void)
263 GScannerConfig config = { 0, };
264 g_autoptr(GScanner) val = g_scanner_new (&config);
265 g_assert (val != NULL);
268 static void
269 test_g_sequence (void)
271 g_autoptr(GSequence) val = g_sequence_new (NULL);
272 g_assert (val != NULL);
275 static void
276 test_g_slist (void)
278 g_autoptr(GSList) val = NULL;
279 g_autoptr(GSList) nonempty_val = g_slist_prepend (NULL, "hello");
280 g_assert (val == NULL);
281 g_assert (nonempty_val != NULL);
284 static void
285 test_g_string (void)
287 g_autoptr(GString) val = g_string_new ("");
288 g_assert (val != NULL);
291 static void
292 test_g_string_chunk (void)
294 g_autoptr(GStringChunk) val = g_string_chunk_new (42);
295 g_assert (val != NULL);
298 static gpointer
299 mythread (gpointer data)
301 g_usleep (G_USEC_PER_SEC);
302 return NULL;
305 static void
306 test_g_thread (void)
308 g_autoptr(GThread) val = g_thread_new ("bob", mythread, NULL);
309 g_assert (val != NULL);
312 static void
313 test_g_mutex (void)
315 g_auto(GMutex) val;
317 g_mutex_init (&val);
320 static void
321 test_g_mutex_locker (void)
323 GMutex mutex;
325 g_mutex_init (&mutex);
327 if (TRUE)
329 g_autoptr(GMutexLocker) val = g_mutex_locker_new (&mutex);
331 g_assert (val != NULL);
335 static void
336 test_g_cond (void)
338 g_auto(GCond) val;
339 g_cond_init (&val);
342 static void
343 test_g_timer (void)
345 g_autoptr(GTimer) val = g_timer_new ();
346 g_assert (val != NULL);
349 static void
350 test_g_time_zone (void)
352 g_autoptr(GTimeZone) val = g_time_zone_new ("UTC");
353 g_assert (val != NULL);
356 static void
357 test_g_tree (void)
359 g_autoptr(GTree) val = g_tree_new ((GCompareFunc)strcmp);
360 g_assert (val != NULL);
363 static void
364 test_g_variant (void)
366 g_autoptr(GVariant) val = g_variant_new_string ("hello");
367 g_assert (val != NULL);
370 static void
371 test_g_variant_builder (void)
373 g_autoptr(GVariantBuilder) val = g_variant_builder_new (G_VARIANT_TYPE ("as"));
374 g_auto(GVariantBuilder) stackval;
376 g_assert (val != NULL);
377 g_variant_builder_init (&stackval, G_VARIANT_TYPE ("as"));
380 static void
381 test_g_variant_iter (void)
383 g_autoptr(GVariant) var = g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32, "", 0, sizeof(guint32));
384 g_autoptr(GVariantIter) val = g_variant_iter_new (var);
385 g_assert (val != NULL);
388 static void
389 test_g_variant_dict (void)
391 g_autoptr(GVariant) data = g_variant_new_from_data (G_VARIANT_TYPE ("a{sv}"), "", 0, FALSE, NULL, NULL);
392 g_auto(GVariantDict) stackval;
393 g_autoptr(GVariantDict) val = g_variant_dict_new (data);
395 g_variant_dict_init (&stackval, data);
396 g_assert (val != NULL);
399 static void
400 test_g_variant_type (void)
402 g_autoptr(GVariantType) val = g_variant_type_new ("s");
403 g_assert (val != NULL);
406 static void
407 test_strv (void)
409 g_auto(GStrv) val = g_strsplit("a:b:c", ":", -1);
410 g_assert (val != NULL);
413 static void
414 mark_freed (gpointer ptr)
416 gboolean *freed = ptr;
417 *freed = TRUE;
420 static void
421 test_autolist (void)
423 char data[1] = {0};
424 gboolean freed1 = FALSE;
425 gboolean freed2 = FALSE;
426 gboolean freed3 = FALSE;
427 GBytes *b1 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed1);
428 GBytes *b2 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed2);
429 GBytes *b3 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed3);
432 g_autolist(GBytes) l = NULL;
434 l = g_list_prepend (l, b1);
435 l = g_list_prepend (l, b3);
438 /* Only assert if autoptr works */
439 #ifdef __GNUC__
440 g_assert (freed1);
441 g_assert (freed3);
442 #endif
443 g_assert (!freed2);
445 g_bytes_unref (b2);
446 g_assert (freed2);
449 static void
450 test_autoslist (void)
452 char data[1] = {0};
453 gboolean freed1 = FALSE;
454 gboolean freed2 = FALSE;
455 gboolean freed3 = FALSE;
456 GBytes *b1 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed1);
457 GBytes *b2 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed2);
458 GBytes *b3 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed3);
461 g_autoslist(GBytes) l = NULL;
463 l = g_slist_prepend (l, b1);
464 l = g_slist_prepend (l, b3);
467 /* Only assert if autoptr works */
468 #ifdef __GNUC__
469 g_assert (freed1);
470 g_assert (freed3);
471 #endif
472 g_assert (!freed2);
474 g_bytes_unref (b2);
475 g_assert (freed2);
479 main (int argc, gchar *argv[])
481 g_test_init (&argc, &argv, NULL);
483 g_test_add_func ("/autoptr/autofree", test_autofree);
484 g_test_add_func ("/autoptr/g_async_queue", test_g_async_queue);
485 g_test_add_func ("/autoptr/g_bookmark_file", test_g_bookmark_file);
486 g_test_add_func ("/autoptr/g_bytes", test_g_bytes);
487 g_test_add_func ("/autoptr/g_checksum", test_g_checksum);
488 g_test_add_func ("/autoptr/g_date_time", test_g_date_time);
489 g_test_add_func ("/autoptr/g_dir", test_g_dir);
490 g_test_add_func ("/autoptr/g_error", test_g_error);
491 g_test_add_func ("/autoptr/g_hash_table", test_g_hash_table);
492 g_test_add_func ("/autoptr/g_hmac", test_g_hmac);
493 g_test_add_func ("/autoptr/g_io_channel", test_g_io_channel);
494 g_test_add_func ("/autoptr/g_key_file", test_g_key_file);
495 g_test_add_func ("/autoptr/g_list", test_g_list);
496 g_test_add_func ("/autoptr/g_array", test_g_array);
497 g_test_add_func ("/autoptr/g_ptr_array", test_g_ptr_array);
498 g_test_add_func ("/autoptr/g_byte_array", test_g_byte_array);
499 g_test_add_func ("/autoptr/g_main_context", test_g_main_context);
500 g_test_add_func ("/autoptr/g_main_loop", test_g_main_loop);
501 g_test_add_func ("/autoptr/g_source", test_g_source);
502 g_test_add_func ("/autoptr/g_mapped_file", test_g_mapped_file);
503 g_test_add_func ("/autoptr/g_markup_parse_context", test_g_markup_parse_context);
504 g_test_add_func ("/autoptr/g_node", test_g_node);
505 g_test_add_func ("/autoptr/g_option_context", test_g_option_context);
506 g_test_add_func ("/autoptr/g_option_group", test_g_option_group);
507 g_test_add_func ("/autoptr/g_pattern_spec", test_g_pattern_spec);
508 g_test_add_func ("/autoptr/g_queue", test_g_queue);
509 g_test_add_func ("/autoptr/g_rand", test_g_rand);
510 g_test_add_func ("/autoptr/g_regex", test_g_regex);
511 g_test_add_func ("/autoptr/g_match_info", test_g_match_info);
512 g_test_add_func ("/autoptr/g_scanner", test_g_scanner);
513 g_test_add_func ("/autoptr/g_sequence", test_g_sequence);
514 g_test_add_func ("/autoptr/g_slist", test_g_slist);
515 g_test_add_func ("/autoptr/g_string", test_g_string);
516 g_test_add_func ("/autoptr/g_string_chunk", test_g_string_chunk);
517 g_test_add_func ("/autoptr/g_thread", test_g_thread);
518 g_test_add_func ("/autoptr/g_mutex", test_g_mutex);
519 g_test_add_func ("/autoptr/g_mutex_locker", test_g_mutex_locker);
520 g_test_add_func ("/autoptr/g_cond", test_g_cond);
521 g_test_add_func ("/autoptr/g_timer", test_g_timer);
522 g_test_add_func ("/autoptr/g_time_zone", test_g_time_zone);
523 g_test_add_func ("/autoptr/g_tree", test_g_tree);
524 g_test_add_func ("/autoptr/g_variant", test_g_variant);
525 g_test_add_func ("/autoptr/g_variant_builder", test_g_variant_builder);
526 g_test_add_func ("/autoptr/g_variant_iter", test_g_variant_iter);
527 g_test_add_func ("/autoptr/g_variant_dict", test_g_variant_dict);
528 g_test_add_func ("/autoptr/g_variant_type", test_g_variant_type);
529 g_test_add_func ("/autoptr/strv", test_strv);
530 g_test_add_func ("/autoptr/autolist", test_autolist);
531 g_test_add_func ("/autoptr/autoslist", test_autoslist);
533 return g_test_run ();