Merge branch '976-disable-assert-checks' into 'master'
[glib.git] / glib / tests / autoptr.c
blob5b3bce71cbe17cd8889cfb85caa0c9d3a57237e3
1 #include <glib.h>
2 #include <string.h>
4 typedef struct _HNVC HasNonVoidCleanup;
5 HasNonVoidCleanup * non_void_cleanup (HasNonVoidCleanup *);
7 /* Should not cause any warnings with -Wextra */
8 G_DEFINE_AUTOPTR_CLEANUP_FUNC(HasNonVoidCleanup, non_void_cleanup)
10 static void
11 test_autofree (void)
13 g_autofree gchar *p = NULL;
14 g_autofree gchar *p2 = NULL;
15 g_autofree gchar *alwaysnull = NULL;
17 p = g_malloc (10);
18 p2 = g_malloc (42);
20 if (TRUE)
22 g_autofree guint8 *buf = g_malloc (128);
23 g_autofree gchar *alwaysnull_again = NULL;
25 buf[0] = 1;
27 g_assert_null (alwaysnull_again);
30 if (TRUE)
32 g_autofree guint8 *buf2 = g_malloc (256);
34 buf2[255] = 42;
37 g_assert_null (alwaysnull);
40 static void
41 test_g_async_queue (void)
43 g_autoptr(GAsyncQueue) val = g_async_queue_new ();
44 g_assert (val != NULL);
47 static void
48 test_g_bookmark_file (void)
50 g_autoptr(GBookmarkFile) val = g_bookmark_file_new ();
51 g_assert (val != NULL);
54 static void
55 test_g_bytes (void)
57 g_autoptr(GBytes) val = g_bytes_new ("foo", 3);
58 g_assert (val != NULL);
61 static void
62 test_g_checksum (void)
64 g_autoptr(GChecksum) val = g_checksum_new (G_CHECKSUM_SHA256);
65 g_assert (val != NULL);
68 static void
69 test_g_date_time (void)
71 g_autoptr(GDateTime) val = g_date_time_new_now_utc ();
72 g_assert (val != NULL);
75 static void
76 test_g_dir (void)
78 g_autoptr(GDir) val = g_dir_open (".", 0, NULL);
79 g_assert (val != NULL);
82 static void
83 test_g_error (void)
85 g_autoptr(GError) val = g_error_new_literal (G_FILE_ERROR, G_FILE_ERROR_FAILED, "oops");
86 g_assert (val != NULL);
89 static void
90 test_g_hash_table (void)
92 g_autoptr(GHashTable) val = g_hash_table_new (NULL, NULL);
93 g_assert (val != NULL);
96 static void
97 test_g_hmac (void)
99 g_autoptr(GHmac) val = g_hmac_new (G_CHECKSUM_SHA256, (guint8*)"hello", 5);
100 g_assert (val != NULL);
103 static void
104 test_g_io_channel (void)
106 #ifdef G_OS_WIN32
107 const gchar *devnull = "nul";
108 #else
109 const gchar *devnull = "/dev/null";
110 #endif
112 g_autoptr(GIOChannel) val = g_io_channel_new_file (devnull, "r", NULL);
113 g_assert (val != NULL);
116 static void
117 test_g_key_file (void)
119 g_autoptr(GKeyFile) val = g_key_file_new ();
120 g_assert (val != NULL);
123 static void
124 test_g_list (void)
126 g_autoptr(GList) val = NULL;
127 g_autoptr(GList) val2 = g_list_prepend (NULL, "foo");
128 g_assert (val == NULL);
129 g_assert (val2 != NULL);
132 static void
133 test_g_array (void)
135 g_autoptr(GArray) val = g_array_new (0, 0, sizeof (gpointer));
136 g_assert (val != NULL);
139 static void
140 test_g_ptr_array (void)
142 g_autoptr(GPtrArray) val = g_ptr_array_new ();
143 g_assert (val != NULL);
146 static void
147 test_g_byte_array (void)
149 g_autoptr(GByteArray) val = g_byte_array_new ();
150 g_assert (val != NULL);
153 static void
154 test_g_main_context (void)
156 g_autoptr(GMainContext) val = g_main_context_new ();
157 g_assert (val != NULL);
160 static void
161 test_g_main_loop (void)
163 g_autoptr(GMainLoop) val = g_main_loop_new (NULL, TRUE);
164 g_assert (val != NULL);
167 static void
168 test_g_source (void)
170 g_autoptr(GSource) val = g_timeout_source_new_seconds (2);
171 g_assert (val != NULL);
174 static void
175 test_g_mapped_file (void)
177 g_autoptr(GMappedFile) val = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "keyfiletest.ini", NULL), FALSE, NULL);
178 g_assert (val != NULL);
181 static void
182 parser_start (GMarkupParseContext *context,
183 const gchar *element_name,
184 const gchar **attribute_names,
185 const gchar **attribute_values,
186 gpointer user_data,
187 GError **error)
191 static void
192 parser_end (GMarkupParseContext *context,
193 const gchar *element_name,
194 gpointer user_data,
195 GError **error)
199 static GMarkupParser parser = {
200 .start_element = parser_start,
201 .end_element = parser_end
204 static void
205 test_g_markup_parse_context (void)
207 g_autoptr(GMarkupParseContext) val = g_markup_parse_context_new (&parser, 0, NULL, NULL);
208 g_assert (val != NULL);
211 static void
212 test_g_node (void)
214 g_autoptr(GNode) val = g_node_new ("hello");
215 g_assert (val != NULL);
218 static void
219 test_g_option_context (void)
221 g_autoptr(GOptionContext) val = g_option_context_new ("hello");
222 g_assert (val != NULL);
225 static void
226 test_g_option_group (void)
228 g_autoptr(GOptionGroup) val = g_option_group_new ("hello", "world", "helpme", NULL, NULL);
229 g_assert (val != NULL);
232 static void
233 test_g_pattern_spec (void)
235 g_autoptr(GPatternSpec) val = g_pattern_spec_new ("plaid");
236 g_assert (val != NULL);
239 static void
240 test_g_queue (void)
242 g_autoptr(GQueue) val = g_queue_new ();
243 g_auto(GQueue) stackval = G_QUEUE_INIT;
244 g_assert (val != NULL);
245 g_assert_null (stackval.head);
248 static void
249 test_g_rand (void)
251 g_autoptr(GRand) val = g_rand_new ();
252 g_assert (val != NULL);
255 static void
256 test_g_regex (void)
258 g_autoptr(GRegex) val = g_regex_new (".*", 0, 0, NULL);
259 g_assert (val != NULL);
262 static void
263 test_g_match_info (void)
265 g_autoptr(GRegex) regex = g_regex_new (".*", 0, 0, NULL);
266 g_autoptr(GMatchInfo) match = NULL;
268 if (!g_regex_match (regex, "hello", 0, &match))
269 g_assert_not_reached ();
272 static void
273 test_g_scanner (void)
275 GScannerConfig config = { 0, };
276 g_autoptr(GScanner) val = g_scanner_new (&config);
277 g_assert (val != NULL);
280 static void
281 test_g_sequence (void)
283 g_autoptr(GSequence) val = g_sequence_new (NULL);
284 g_assert (val != NULL);
287 static void
288 test_g_slist (void)
290 g_autoptr(GSList) val = NULL;
291 g_autoptr(GSList) nonempty_val = g_slist_prepend (NULL, "hello");
292 g_assert (val == NULL);
293 g_assert (nonempty_val != NULL);
296 static void
297 test_g_string (void)
299 g_autoptr(GString) val = g_string_new ("");
300 g_assert (val != NULL);
303 static void
304 test_g_string_chunk (void)
306 g_autoptr(GStringChunk) val = g_string_chunk_new (42);
307 g_assert (val != NULL);
310 static gpointer
311 mythread (gpointer data)
313 g_usleep (G_USEC_PER_SEC);
314 return NULL;
317 static void
318 test_g_thread (void)
320 g_autoptr(GThread) val = g_thread_new ("bob", mythread, NULL);
321 g_assert (val != NULL);
324 static void
325 test_g_mutex (void)
327 g_auto(GMutex) val;
329 g_mutex_init (&val);
332 static void
333 test_g_mutex_locker (void)
335 GMutex mutex;
337 g_mutex_init (&mutex);
339 if (TRUE)
341 g_autoptr(GMutexLocker) val = g_mutex_locker_new (&mutex);
343 g_assert (val != NULL);
347 static void
348 test_g_cond (void)
350 g_auto(GCond) val;
351 g_cond_init (&val);
354 static void
355 test_g_timer (void)
357 g_autoptr(GTimer) val = g_timer_new ();
358 g_assert (val != NULL);
361 static void
362 test_g_time_zone (void)
364 g_autoptr(GTimeZone) val = g_time_zone_new ("UTC");
365 g_assert (val != NULL);
368 static void
369 test_g_tree (void)
371 g_autoptr(GTree) val = g_tree_new ((GCompareFunc)strcmp);
372 g_assert (val != NULL);
375 static void
376 test_g_variant (void)
378 g_autoptr(GVariant) val = g_variant_new_string ("hello");
379 g_assert (val != NULL);
382 static void
383 test_g_variant_builder (void)
385 g_autoptr(GVariantBuilder) val = g_variant_builder_new (G_VARIANT_TYPE ("as"));
386 g_auto(GVariantBuilder) stackval;
388 g_assert (val != NULL);
389 g_variant_builder_init (&stackval, G_VARIANT_TYPE ("as"));
392 static void
393 test_g_variant_iter (void)
395 g_autoptr(GVariant) var = g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32, "", 0, sizeof(guint32));
396 g_autoptr(GVariantIter) val = g_variant_iter_new (var);
397 g_assert (val != NULL);
400 static void
401 test_g_variant_dict (void)
403 g_autoptr(GVariant) data = g_variant_new_from_data (G_VARIANT_TYPE ("a{sv}"), "", 0, FALSE, NULL, NULL);
404 g_auto(GVariantDict) stackval;
405 g_autoptr(GVariantDict) val = g_variant_dict_new (data);
407 g_variant_dict_init (&stackval, data);
408 g_assert (val != NULL);
411 static void
412 test_g_variant_type (void)
414 g_autoptr(GVariantType) val = g_variant_type_new ("s");
415 g_assert (val != NULL);
418 static void
419 test_strv (void)
421 g_auto(GStrv) val = g_strsplit("a:b:c", ":", -1);
422 g_assert (val != NULL);
425 static void
426 test_refstring (void)
428 g_autoptr(GRefString) str = g_ref_string_new ("hello, world");
429 g_assert_nonnull (str);
432 static void
433 mark_freed (gpointer ptr)
435 gboolean *freed = ptr;
436 *freed = TRUE;
439 static void
440 test_autolist (void)
442 char data[1] = {0};
443 gboolean freed1 = FALSE;
444 gboolean freed2 = FALSE;
445 gboolean freed3 = FALSE;
446 GBytes *b1 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed1);
447 GBytes *b2 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed2);
448 GBytes *b3 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed3);
451 g_autolist(GBytes) l = NULL;
453 l = g_list_prepend (l, b1);
454 l = g_list_prepend (l, b3);
457 /* Only assert if autoptr works */
458 #ifdef __GNUC__
459 g_assert (freed1);
460 g_assert (freed3);
461 #endif
462 g_assert (!freed2);
464 g_bytes_unref (b2);
465 g_assert (freed2);
468 static void
469 test_autoslist (void)
471 char data[1] = {0};
472 gboolean freed1 = FALSE;
473 gboolean freed2 = FALSE;
474 gboolean freed3 = FALSE;
475 GBytes *b1 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed1);
476 GBytes *b2 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed2);
477 GBytes *b3 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed3);
480 g_autoslist(GBytes) l = NULL;
482 l = g_slist_prepend (l, b1);
483 l = g_slist_prepend (l, b3);
486 /* Only assert if autoptr works */
487 #ifdef __GNUC__
488 g_assert (freed1);
489 g_assert (freed3);
490 #endif
491 g_assert (!freed2);
493 g_bytes_unref (b2);
494 g_assert (freed2);
498 main (int argc, gchar *argv[])
500 g_test_init (&argc, &argv, NULL);
502 g_test_add_func ("/autoptr/autofree", test_autofree);
503 g_test_add_func ("/autoptr/g_async_queue", test_g_async_queue);
504 g_test_add_func ("/autoptr/g_bookmark_file", test_g_bookmark_file);
505 g_test_add_func ("/autoptr/g_bytes", test_g_bytes);
506 g_test_add_func ("/autoptr/g_checksum", test_g_checksum);
507 g_test_add_func ("/autoptr/g_date_time", test_g_date_time);
508 g_test_add_func ("/autoptr/g_dir", test_g_dir);
509 g_test_add_func ("/autoptr/g_error", test_g_error);
510 g_test_add_func ("/autoptr/g_hash_table", test_g_hash_table);
511 g_test_add_func ("/autoptr/g_hmac", test_g_hmac);
512 g_test_add_func ("/autoptr/g_io_channel", test_g_io_channel);
513 g_test_add_func ("/autoptr/g_key_file", test_g_key_file);
514 g_test_add_func ("/autoptr/g_list", test_g_list);
515 g_test_add_func ("/autoptr/g_array", test_g_array);
516 g_test_add_func ("/autoptr/g_ptr_array", test_g_ptr_array);
517 g_test_add_func ("/autoptr/g_byte_array", test_g_byte_array);
518 g_test_add_func ("/autoptr/g_main_context", test_g_main_context);
519 g_test_add_func ("/autoptr/g_main_loop", test_g_main_loop);
520 g_test_add_func ("/autoptr/g_source", test_g_source);
521 g_test_add_func ("/autoptr/g_mapped_file", test_g_mapped_file);
522 g_test_add_func ("/autoptr/g_markup_parse_context", test_g_markup_parse_context);
523 g_test_add_func ("/autoptr/g_node", test_g_node);
524 g_test_add_func ("/autoptr/g_option_context", test_g_option_context);
525 g_test_add_func ("/autoptr/g_option_group", test_g_option_group);
526 g_test_add_func ("/autoptr/g_pattern_spec", test_g_pattern_spec);
527 g_test_add_func ("/autoptr/g_queue", test_g_queue);
528 g_test_add_func ("/autoptr/g_rand", test_g_rand);
529 g_test_add_func ("/autoptr/g_regex", test_g_regex);
530 g_test_add_func ("/autoptr/g_match_info", test_g_match_info);
531 g_test_add_func ("/autoptr/g_scanner", test_g_scanner);
532 g_test_add_func ("/autoptr/g_sequence", test_g_sequence);
533 g_test_add_func ("/autoptr/g_slist", test_g_slist);
534 g_test_add_func ("/autoptr/g_string", test_g_string);
535 g_test_add_func ("/autoptr/g_string_chunk", test_g_string_chunk);
536 g_test_add_func ("/autoptr/g_thread", test_g_thread);
537 g_test_add_func ("/autoptr/g_mutex", test_g_mutex);
538 g_test_add_func ("/autoptr/g_mutex_locker", test_g_mutex_locker);
539 g_test_add_func ("/autoptr/g_cond", test_g_cond);
540 g_test_add_func ("/autoptr/g_timer", test_g_timer);
541 g_test_add_func ("/autoptr/g_time_zone", test_g_time_zone);
542 g_test_add_func ("/autoptr/g_tree", test_g_tree);
543 g_test_add_func ("/autoptr/g_variant", test_g_variant);
544 g_test_add_func ("/autoptr/g_variant_builder", test_g_variant_builder);
545 g_test_add_func ("/autoptr/g_variant_iter", test_g_variant_iter);
546 g_test_add_func ("/autoptr/g_variant_dict", test_g_variant_dict);
547 g_test_add_func ("/autoptr/g_variant_type", test_g_variant_type);
548 g_test_add_func ("/autoptr/strv", test_strv);
549 g_test_add_func ("/autoptr/refstring", test_refstring);
550 g_test_add_func ("/autoptr/autolist", test_autolist);
551 g_test_add_func ("/autoptr/autoslist", test_autoslist);
553 return g_test_run ();