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
)
13 g_autofree gchar
*p
= NULL
;
14 g_autofree gchar
*p2
= NULL
;
15 g_autofree gchar
*alwaysnull
= NULL
;
22 g_autofree guint8
*buf
= g_malloc (128);
23 g_autofree gchar
*alwaysnull_again
= NULL
;
27 g_assert_null (alwaysnull_again
);
32 g_autofree guint8
*buf2
= g_malloc (256);
37 g_assert_null (alwaysnull
);
41 test_g_async_queue (void)
43 g_autoptr(GAsyncQueue
) val
= g_async_queue_new ();
44 g_assert (val
!= NULL
);
48 test_g_bookmark_file (void)
50 g_autoptr(GBookmarkFile
) val
= g_bookmark_file_new ();
51 g_assert (val
!= NULL
);
57 g_autoptr(GBytes
) val
= g_bytes_new ("foo", 3);
58 g_assert (val
!= NULL
);
62 test_g_checksum (void)
64 g_autoptr(GChecksum
) val
= g_checksum_new (G_CHECKSUM_SHA256
);
65 g_assert (val
!= NULL
);
69 test_g_date_time (void)
71 g_autoptr(GDateTime
) val
= g_date_time_new_now_utc ();
72 g_assert (val
!= NULL
);
78 g_autoptr(GDir
) val
= g_dir_open (".", 0, NULL
);
79 g_assert (val
!= NULL
);
85 g_autoptr(GError
) val
= g_error_new_literal (G_FILE_ERROR
, G_FILE_ERROR_FAILED
, "oops");
86 g_assert (val
!= NULL
);
90 test_g_hash_table (void)
92 g_autoptr(GHashTable
) val
= g_hash_table_new (NULL
, NULL
);
93 g_assert (val
!= NULL
);
99 g_autoptr(GHmac
) val
= g_hmac_new (G_CHECKSUM_SHA256
, (guint8
*)"hello", 5);
100 g_assert (val
!= NULL
);
104 test_g_io_channel (void)
107 const gchar
*devnull
= "nul";
109 const gchar
*devnull
= "/dev/null";
112 g_autoptr(GIOChannel
) val
= g_io_channel_new_file (devnull
, "r", NULL
);
113 g_assert (val
!= NULL
);
117 test_g_key_file (void)
119 g_autoptr(GKeyFile
) val
= g_key_file_new ();
120 g_assert (val
!= NULL
);
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
);
135 g_autoptr(GArray
) val
= g_array_new (0, 0, sizeof (gpointer
));
136 g_assert (val
!= NULL
);
140 test_g_ptr_array (void)
142 g_autoptr(GPtrArray
) val
= g_ptr_array_new ();
143 g_assert (val
!= NULL
);
147 test_g_byte_array (void)
149 g_autoptr(GByteArray
) val
= g_byte_array_new ();
150 g_assert (val
!= NULL
);
154 test_g_main_context (void)
156 g_autoptr(GMainContext
) val
= g_main_context_new ();
157 g_assert (val
!= NULL
);
161 test_g_main_loop (void)
163 g_autoptr(GMainLoop
) val
= g_main_loop_new (NULL
, TRUE
);
164 g_assert (val
!= NULL
);
170 g_autoptr(GSource
) val
= g_timeout_source_new_seconds (2);
171 g_assert (val
!= NULL
);
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
);
182 parser_start (GMarkupParseContext
*context
,
183 const gchar
*element_name
,
184 const gchar
**attribute_names
,
185 const gchar
**attribute_values
,
192 parser_end (GMarkupParseContext
*context
,
193 const gchar
*element_name
,
199 static GMarkupParser parser
= {
200 .start_element
= parser_start
,
201 .end_element
= parser_end
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
);
214 g_autoptr(GNode
) val
= g_node_new ("hello");
215 g_assert (val
!= NULL
);
219 test_g_option_context (void)
221 g_autoptr(GOptionContext
) val
= g_option_context_new ("hello");
222 g_assert (val
!= NULL
);
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
);
233 test_g_pattern_spec (void)
235 g_autoptr(GPatternSpec
) val
= g_pattern_spec_new ("plaid");
236 g_assert (val
!= NULL
);
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
);
251 g_autoptr(GRand
) val
= g_rand_new ();
252 g_assert (val
!= NULL
);
258 g_autoptr(GRegex
) val
= g_regex_new (".*", 0, 0, NULL
);
259 g_assert (val
!= NULL
);
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 ();
273 test_g_scanner (void)
275 GScannerConfig config
= { 0, };
276 g_autoptr(GScanner
) val
= g_scanner_new (&config
);
277 g_assert (val
!= NULL
);
281 test_g_sequence (void)
283 g_autoptr(GSequence
) val
= g_sequence_new (NULL
);
284 g_assert (val
!= NULL
);
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
);
299 g_autoptr(GString
) val
= g_string_new ("");
300 g_assert (val
!= NULL
);
304 test_g_string_chunk (void)
306 g_autoptr(GStringChunk
) val
= g_string_chunk_new (42);
307 g_assert (val
!= NULL
);
311 mythread (gpointer data
)
313 g_usleep (G_USEC_PER_SEC
);
320 g_autoptr(GThread
) val
= g_thread_new ("bob", mythread
, NULL
);
321 g_assert (val
!= NULL
);
333 test_g_mutex_locker (void)
337 g_mutex_init (&mutex
);
341 g_autoptr(GMutexLocker
) val
= g_mutex_locker_new (&mutex
);
343 g_assert (val
!= NULL
);
357 g_autoptr(GTimer
) val
= g_timer_new ();
358 g_assert (val
!= NULL
);
362 test_g_time_zone (void)
364 g_autoptr(GTimeZone
) val
= g_time_zone_new ("UTC");
365 g_assert (val
!= NULL
);
371 g_autoptr(GTree
) val
= g_tree_new ((GCompareFunc
)strcmp
);
372 g_assert (val
!= NULL
);
376 test_g_variant (void)
378 g_autoptr(GVariant
) val
= g_variant_new_string ("hello");
379 g_assert (val
!= NULL
);
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"));
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
);
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
);
412 test_g_variant_type (void)
414 g_autoptr(GVariantType
) val
= g_variant_type_new ("s");
415 g_assert (val
!= NULL
);
421 g_auto(GStrv
) val
= g_strsplit("a:b:c", ":", -1);
422 g_assert (val
!= NULL
);
426 test_refstring (void)
428 g_autoptr(GRefString
) str
= g_ref_string_new ("hello, world");
429 g_assert_nonnull (str
);
433 mark_freed (gpointer ptr
)
435 gboolean
*freed
= ptr
;
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 */
469 test_autoslist (void)
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 */
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 ();