Branch data Line data Source code
1 : : #include <glib.h>
2 : : #include <string.h>
3 : :
4 : : typedef struct _HNVC HasNonVoidCleanup;
5 : : HasNonVoidCleanup * non_void_cleanup (HasNonVoidCleanup *);
6 : :
7 : : /* Should not cause any warnings with -Wextra */
8 : : G_DEFINE_AUTOPTR_CLEANUP_FUNC(HasNonVoidCleanup, non_void_cleanup)
9 : :
10 : : static void
11 : 1 : test_autofree (void)
12 : : {
13 : : #ifdef __clang_analyzer__
14 : : g_test_skip ("autofree tests aren’t understood by the clang analyser");
15 : : #else
16 : 1 : g_autofree gchar *p = NULL;
17 : 1 : g_autofree gchar *p2 = NULL;
18 : 1 : g_autofree gchar *alwaysnull = NULL;
19 : :
20 : 1 : p = g_malloc (10);
21 : 1 : p2 = g_malloc (42);
22 : :
23 : 1 : p[0] = 1;
24 : 1 : p2[0] = 1;
25 : :
26 : : if (TRUE)
27 : : {
28 : 2 : g_autofree guint8 *buf = g_malloc (128);
29 : 1 : g_autofree gchar *alwaysnull_again = NULL;
30 : :
31 : 1 : buf[0] = 1;
32 : :
33 : 1 : g_assert_null (alwaysnull_again);
34 : : }
35 : :
36 : : if (TRUE)
37 : : {
38 : 1 : g_autofree guint8 *buf2 = g_malloc (256);
39 : :
40 : 1 : buf2[255] = 42;
41 : : }
42 : :
43 : 1 : g_assert_null (alwaysnull);
44 : : #endif /* __clang_analyzer__ */
45 : 1 : }
46 : :
47 : : static void
48 : 1 : test_g_async_queue (void)
49 : : {
50 : 2 : g_autoptr(GAsyncQueue) val = g_async_queue_new ();
51 : 1 : g_assert_nonnull (val);
52 : 1 : }
53 : :
54 : : static void
55 : 1 : test_g_bookmark_file (void)
56 : : {
57 : 2 : g_autoptr(GBookmarkFile) val = g_bookmark_file_new ();
58 : 1 : g_assert_nonnull (val);
59 : 1 : }
60 : :
61 : : static void
62 : 1 : test_g_bytes (void)
63 : : {
64 : 2 : g_autoptr(GBytes) val = g_bytes_new ("foo", 3);
65 : 1 : g_assert_nonnull (val);
66 : 1 : }
67 : :
68 : : static void
69 : 1 : test_g_checksum (void)
70 : : {
71 : 2 : g_autoptr(GChecksum) val = g_checksum_new (G_CHECKSUM_SHA256);
72 : 1 : g_assert_nonnull (val);
73 : 1 : }
74 : :
75 : : static void
76 : 1 : test_g_date (void)
77 : : {
78 : 2 : g_autoptr(GDate) val = g_date_new ();
79 : 1 : g_assert_nonnull (val);
80 : 1 : }
81 : :
82 : : static void
83 : 1 : test_g_date_time (void)
84 : : {
85 : 2 : g_autoptr(GDateTime) val = g_date_time_new_now_utc ();
86 : 1 : g_assert_nonnull (val);
87 : 1 : }
88 : :
89 : : static void
90 : 1 : test_g_dir (void)
91 : : {
92 : 2 : g_autoptr(GDir) val = g_dir_open (".", 0, NULL);
93 : 1 : g_assert_nonnull (val);
94 : 1 : }
95 : :
96 : : static void
97 : 1 : test_g_error (void)
98 : : {
99 : 2 : g_autoptr(GError) val = g_error_new_literal (G_FILE_ERROR, G_FILE_ERROR_FAILED, "oops");
100 : 1 : g_assert_nonnull (val);
101 : 1 : }
102 : :
103 : : static void
104 : 1 : test_g_hash_table (void)
105 : : {
106 : 2 : g_autoptr(GHashTable) val = g_hash_table_new (NULL, NULL);
107 : 1 : g_assert_nonnull (val);
108 : 1 : }
109 : :
110 : : static void
111 : 1 : test_g_hmac (void)
112 : : {
113 : 2 : g_autoptr(GHmac) val = g_hmac_new (G_CHECKSUM_SHA256, (guint8*)"hello", 5);
114 : 1 : g_assert_nonnull (val);
115 : 1 : }
116 : :
117 : : static void
118 : 1 : test_g_io_channel (void)
119 : : {
120 : : #ifdef G_OS_WIN32
121 : : const gchar *devnull = "nul";
122 : : #else
123 : 1 : const gchar *devnull = "/dev/null";
124 : : #endif
125 : :
126 : 2 : g_autoptr(GIOChannel) val = g_io_channel_new_file (devnull, "r", NULL);
127 : 1 : g_assert_nonnull (val);
128 : 1 : }
129 : :
130 : : static void
131 : 1 : test_g_key_file (void)
132 : : {
133 : 2 : g_autoptr(GKeyFile) val = g_key_file_new ();
134 : 1 : g_assert_nonnull (val);
135 : 1 : }
136 : :
137 : : static void
138 : 1 : test_g_list (void)
139 : : {
140 : 1 : g_autoptr(GList) val = NULL;
141 : 2 : g_autoptr(GList) val2 = g_list_prepend (NULL, "foo");
142 : 1 : g_assert_null (val);
143 : 1 : g_assert_nonnull (val2);
144 : 1 : }
145 : :
146 : : static void
147 : 1 : test_g_array (void)
148 : : {
149 : 2 : g_autoptr(GArray) val = g_array_new (0, 0, sizeof (gpointer));
150 : 1 : g_assert_nonnull (val);
151 : 1 : }
152 : :
153 : : static void
154 : 1 : test_g_ptr_array (void)
155 : : {
156 : 2 : g_autoptr(GPtrArray) val = g_ptr_array_new ();
157 : 1 : g_assert_nonnull (val);
158 : 1 : }
159 : :
160 : : static void
161 : 1 : test_g_byte_array (void)
162 : : {
163 : 2 : g_autoptr(GByteArray) val = g_byte_array_new ();
164 : 1 : g_assert_nonnull (val);
165 : 1 : }
166 : :
167 : : static void
168 : 1 : test_g_main_context (void)
169 : : {
170 : 2 : g_autoptr(GMainContext) val = g_main_context_new ();
171 : 1 : g_assert_nonnull (val);
172 : 1 : }
173 : :
174 : : static void
175 : 1 : test_g_main_context_pusher (void)
176 : : {
177 : : GMainContext *context, *old_thread_default;
178 : :
179 : 1 : context = g_main_context_new ();
180 : 1 : old_thread_default = g_main_context_get_thread_default ();
181 : 1 : g_assert_false (old_thread_default == context);
182 : :
183 : : if (TRUE)
184 : : {
185 : 2 : g_autoptr(GMainContextPusher) val = g_main_context_pusher_new (context);
186 : 1 : g_assert_nonnull (val);
187 : :
188 : : /* Check it’s now the thread-default main context */
189 : 1 : g_assert_true (g_main_context_get_thread_default () == context);
190 : : }
191 : :
192 : : /* Check it’s now the old thread-default main context */
193 : 1 : g_assert_false (g_main_context_get_thread_default () == context);
194 : 1 : g_assert_true (g_main_context_get_thread_default () == old_thread_default);
195 : :
196 : 1 : g_main_context_unref (context);
197 : 1 : }
198 : :
199 : : static void
200 : 1 : test_g_main_loop (void)
201 : : {
202 : 2 : g_autoptr(GMainLoop) val = g_main_loop_new (NULL, TRUE);
203 : 1 : g_assert_nonnull (val);
204 : 1 : }
205 : :
206 : : static void
207 : 1 : test_g_source (void)
208 : : {
209 : 2 : g_autoptr(GSource) val = g_timeout_source_new_seconds (2);
210 : 1 : g_assert_nonnull (val);
211 : 1 : }
212 : :
213 : : static void
214 : 1 : test_g_mapped_file (void)
215 : : {
216 : 2 : g_autoptr(GMappedFile) val = g_mapped_file_new (g_test_get_filename (G_TEST_DIST, "keyfiletest.ini", NULL), FALSE, NULL);
217 : 1 : g_assert_nonnull (val);
218 : 1 : }
219 : :
220 : : static void
221 : 0 : parser_start (GMarkupParseContext *context,
222 : : const gchar *element_name,
223 : : const gchar **attribute_names,
224 : : const gchar **attribute_values,
225 : : gpointer user_data,
226 : : GError **error)
227 : : {
228 : 0 : }
229 : :
230 : : static void
231 : 0 : parser_end (GMarkupParseContext *context,
232 : : const gchar *element_name,
233 : : gpointer user_data,
234 : : GError **error)
235 : : {
236 : 0 : }
237 : :
238 : : static GMarkupParser parser = {
239 : : .start_element = parser_start,
240 : : .end_element = parser_end
241 : : };
242 : :
243 : : static void
244 : 1 : test_g_markup_parse_context (void)
245 : : {
246 : 2 : g_autoptr(GMarkupParseContext) val = g_markup_parse_context_new (&parser,
247 : : G_MARKUP_DEFAULT_FLAGS,
248 : : NULL, NULL);
249 : 1 : g_assert_nonnull (val);
250 : 1 : }
251 : :
252 : : static void
253 : 1 : test_g_node (void)
254 : : {
255 : 2 : g_autoptr(GNode) val = g_node_new ("hello");
256 : 1 : g_assert_nonnull (val);
257 : 1 : }
258 : :
259 : : static void
260 : 1 : test_g_option_context (void)
261 : : {
262 : 2 : g_autoptr(GOptionContext) val = g_option_context_new ("hello");
263 : 1 : g_assert_nonnull (val);
264 : 1 : }
265 : :
266 : : static void
267 : 1 : test_g_option_group (void)
268 : : {
269 : 2 : g_autoptr(GOptionGroup) val = g_option_group_new ("hello", "world", "helpme", NULL, NULL);
270 : 1 : g_assert_nonnull (val);
271 : 1 : }
272 : :
273 : : static void
274 : 1 : test_g_pattern_spec (void)
275 : : {
276 : 2 : g_autoptr(GPatternSpec) val = g_pattern_spec_new ("plaid");
277 : 1 : g_assert_nonnull (val);
278 : 1 : }
279 : :
280 : : static void
281 : 1 : test_g_queue (void)
282 : : {
283 : 2 : g_autoptr(GQueue) val = g_queue_new ();
284 : 1 : g_auto(GQueue) stackval = G_QUEUE_INIT;
285 : 1 : g_assert_nonnull (val);
286 : 1 : g_assert_null (stackval.head);
287 : 1 : }
288 : :
289 : : static void
290 : 1 : test_g_rand (void)
291 : : {
292 : 2 : g_autoptr(GRand) val = g_rand_new ();
293 : 1 : g_assert_nonnull (val);
294 : 1 : }
295 : :
296 : : static void
297 : 1 : test_g_regex (void)
298 : : {
299 : 2 : g_autoptr(GRegex) val = g_regex_new (".*", G_REGEX_DEFAULT,
300 : : G_REGEX_MATCH_DEFAULT, NULL);
301 : 1 : g_assert_nonnull (val);
302 : 1 : }
303 : :
304 : : static void
305 : 1 : test_g_match_info (void)
306 : : {
307 : 2 : g_autoptr(GRegex) regex = g_regex_new (".*", G_REGEX_DEFAULT,
308 : : G_REGEX_MATCH_DEFAULT, NULL);
309 : 1 : g_autoptr(GMatchInfo) match = NULL;
310 : :
311 [ - + ]: 1 : if (!g_regex_match (regex, "hello", 0, &match))
312 : : g_assert_not_reached ();
313 : 1 : }
314 : :
315 : : static void
316 : 1 : test_g_scanner (void)
317 : : {
318 : 1 : GScannerConfig config = { 0, };
319 : 2 : g_autoptr(GScanner) val = g_scanner_new (&config);
320 : 1 : g_assert_nonnull (val);
321 : 1 : }
322 : :
323 : : static void
324 : 1 : test_g_sequence (void)
325 : : {
326 : 2 : g_autoptr(GSequence) val = g_sequence_new (NULL);
327 : 1 : g_assert_nonnull (val);
328 : 1 : }
329 : :
330 : : static void
331 : 1 : test_g_slist (void)
332 : : {
333 : 1 : g_autoptr(GSList) val = NULL;
334 : 2 : g_autoptr(GSList) nonempty_val = g_slist_prepend (NULL, "hello");
335 : 1 : g_assert_null (val);
336 : 1 : g_assert_nonnull (nonempty_val);
337 : 1 : }
338 : :
339 : : static void
340 : 1 : test_g_string (void)
341 : : {
342 : 2 : g_autoptr(GString) val = g_string_new ("");
343 : 1 : g_assert_nonnull (val);
344 : 1 : }
345 : :
346 : : static void
347 : 1 : test_g_string_chunk (void)
348 : : {
349 : 2 : g_autoptr(GStringChunk) val = g_string_chunk_new (42);
350 : 1 : g_assert_nonnull (val);
351 : 1 : }
352 : :
353 : : static gpointer
354 : 1 : mythread (gpointer data)
355 : : {
356 : 1 : g_usleep (G_USEC_PER_SEC);
357 : 0 : return NULL;
358 : : }
359 : :
360 : : static void
361 : 1 : test_g_thread (void)
362 : : {
363 : 2 : g_autoptr(GThread) val = g_thread_new ("bob", mythread, NULL);
364 : 1 : g_assert_nonnull (val);
365 : 1 : }
366 : :
367 : : static void
368 : 1 : test_g_mutex (void)
369 : : {
370 : 1 : g_auto(GMutex) val;
371 : :
372 : 1 : g_mutex_init (&val);
373 : 1 : }
374 : :
375 : : /* Thread function to check that a mutex given in @data is locked */
376 : : static gpointer
377 : 2 : mutex_locked_thread (gpointer data)
378 : : {
379 : 2 : GMutex *mutex = (GMutex *) data;
380 : 2 : g_assert_false (g_mutex_trylock (mutex));
381 : 2 : return NULL;
382 : : }
383 : :
384 : : /* Thread function to check that a mutex given in @data is unlocked */
385 : : static gpointer
386 : 2 : mutex_unlocked_thread (gpointer data)
387 : : {
388 : 2 : GMutex *mutex = (GMutex *) data;
389 : 2 : g_assert_true (g_mutex_trylock (mutex));
390 : 2 : g_mutex_unlock (mutex);
391 : 2 : return NULL;
392 : : }
393 : :
394 : : static void
395 : 1 : test_g_mutex_locker (void)
396 : : {
397 : : GMutex mutex;
398 : : GThread *thread;
399 : :
400 : 1 : g_mutex_init (&mutex);
401 : :
402 : : if (TRUE)
403 : : {
404 : : /* val is unused in this scope but compiler should not warn. */
405 : 1 : G_MUTEX_AUTO_LOCK (&mutex, val);
406 : : }
407 : :
408 : : if (TRUE)
409 : : {
410 : 2 : g_autoptr(GMutexLocker) val = g_mutex_locker_new (&mutex);
411 : :
412 : 1 : g_assert_nonnull (val);
413 : :
414 : : /* Verify that the mutex is actually locked */
415 : 1 : thread = g_thread_new ("mutex locked", mutex_locked_thread, &mutex);
416 : 1 : g_thread_join (thread);
417 : : }
418 : :
419 : : /* Verify that the mutex is unlocked again */
420 : 1 : thread = g_thread_new ("mutex unlocked", mutex_unlocked_thread, &mutex);
421 : 1 : g_thread_join (thread);
422 : 1 : }
423 : :
424 : : /* Thread function to check that a recursive mutex given in @data is locked */
425 : : static gpointer
426 : 2 : rec_mutex_locked_thread (gpointer data)
427 : : {
428 : 2 : GRecMutex *rec_mutex = (GRecMutex *) data;
429 : 2 : g_assert_false (g_rec_mutex_trylock (rec_mutex));
430 : 2 : return NULL;
431 : : }
432 : :
433 : : /* Thread function to check that a recursive mutex given in @data is unlocked */
434 : : static gpointer
435 : 1 : rec_mutex_unlocked_thread (gpointer data)
436 : : {
437 : 1 : GRecMutex *rec_mutex = (GRecMutex *) data;
438 : 1 : g_assert_true (g_rec_mutex_trylock (rec_mutex));
439 : 1 : g_rec_mutex_unlock (rec_mutex);
440 : 1 : return NULL;
441 : : }
442 : :
443 : : static void
444 : 1 : test_g_rec_mutex_locker (void)
445 : : {
446 : : GRecMutex rec_mutex;
447 : : GThread *thread;
448 : :
449 : 1 : g_rec_mutex_init (&rec_mutex);
450 : :
451 : : if (TRUE)
452 : : {
453 : : /* val is unused in this scope but compiler should not warn. */
454 : 1 : G_REC_MUTEX_AUTO_LOCK (&rec_mutex, val);
455 : : }
456 : :
457 : : if (TRUE)
458 : : {
459 : 2 : g_autoptr(GRecMutexLocker) val = g_rec_mutex_locker_new (&rec_mutex);
460 : 1 : g_autoptr(GRecMutexLocker) other = NULL;
461 : :
462 : 1 : g_assert_nonnull (val);
463 : :
464 : : /* Verify that the mutex is actually locked */
465 : 1 : thread = g_thread_new ("rec mutex locked", rec_mutex_locked_thread, &rec_mutex);
466 : 1 : g_thread_join (g_steal_pointer (&thread));
467 : :
468 : 1 : other = g_rec_mutex_locker_new (&rec_mutex);
469 : 1 : thread = g_thread_new ("rec mutex locked", rec_mutex_locked_thread, &rec_mutex);
470 : 1 : g_thread_join (g_steal_pointer (&thread));
471 : : }
472 : :
473 : : /* Verify that the mutex is unlocked again */
474 : 1 : thread = g_thread_new ("rec mutex unlocked", rec_mutex_unlocked_thread, &rec_mutex);
475 : 1 : g_thread_join (thread);
476 : :
477 : 1 : g_rec_mutex_clear (&rec_mutex);
478 : 1 : }
479 : :
480 : : /* Thread function to check that an rw lock given in @data cannot be writer locked */
481 : : static gpointer
482 : 2 : rw_lock_cannot_take_writer_lock_thread (gpointer data)
483 : : {
484 : 2 : GRWLock *lock = (GRWLock *) data;
485 : 2 : g_assert_false (g_rw_lock_writer_trylock (lock));
486 : 2 : return NULL;
487 : : }
488 : :
489 : : /* Thread function to check that an rw lock given in @data can be reader locked */
490 : : static gpointer
491 : 1 : rw_lock_can_take_reader_lock_thread (gpointer data)
492 : : {
493 : 1 : GRWLock *lock = (GRWLock *) data;
494 : 1 : g_assert_true (g_rw_lock_reader_trylock (lock));
495 : 1 : g_rw_lock_reader_unlock (lock);
496 : 1 : return NULL;
497 : : }
498 : :
499 : : static void
500 : 1 : test_g_rw_lock_lockers (void)
501 : : {
502 : : GRWLock lock;
503 : : GThread *thread;
504 : :
505 : 1 : g_rw_lock_init (&lock);
506 : :
507 : : if (TRUE)
508 : : {
509 : : /* val is unused in this scope but compiler should not warn. */
510 : 1 : G_RW_LOCK_WRITER_AUTO_LOCK (&lock, val);
511 : : }
512 : :
513 : : if (TRUE)
514 : : {
515 : : /* val is unused in this scope but compiler should not warn. */
516 : 1 : G_RW_LOCK_READER_AUTO_LOCK (&lock, val);
517 : : }
518 : :
519 : : if (TRUE)
520 : : {
521 : 2 : g_autoptr(GRWLockWriterLocker) val = g_rw_lock_writer_locker_new (&lock);
522 : :
523 : 1 : g_assert_nonnull (val);
524 : :
525 : : /* Verify that we cannot take another writer lock as a writer lock is currently held */
526 : 1 : thread = g_thread_new ("rw lock cannot take writer lock", rw_lock_cannot_take_writer_lock_thread, &lock);
527 : 1 : g_thread_join (thread);
528 : :
529 : : /* Verify that we cannot take a reader lock as a writer lock is currently held */
530 : 1 : g_assert_false (g_rw_lock_reader_trylock (&lock));
531 : : }
532 : :
533 : : if (TRUE)
534 : : {
535 : 2 : g_autoptr(GRWLockReaderLocker) val = g_rw_lock_reader_locker_new (&lock);
536 : :
537 : 1 : g_assert_nonnull (val);
538 : :
539 : : /* Verify that we can take another reader lock from another thread */
540 : 1 : thread = g_thread_new ("rw lock can take reader lock", rw_lock_can_take_reader_lock_thread, &lock);
541 : 1 : g_thread_join (thread);
542 : :
543 : : /* ... and also that recursive reader locking from the same thread works */
544 : 1 : g_assert_true (g_rw_lock_reader_trylock (&lock));
545 : 1 : g_rw_lock_reader_unlock (&lock);
546 : :
547 : : /* Verify that we cannot take a writer lock as a reader lock is currently held */
548 : 1 : thread = g_thread_new ("rw lock cannot take writer lock", rw_lock_cannot_take_writer_lock_thread, &lock);
549 : 1 : g_thread_join (thread);
550 : : }
551 : :
552 : : /* Verify that we can take a writer lock again: this can only work if all of
553 : : * the locks taken above have been correctly released. */
554 : 1 : g_assert_true (g_rw_lock_writer_trylock (&lock));
555 : 1 : g_rw_lock_writer_unlock (&lock);
556 : :
557 : 1 : g_rw_lock_clear (&lock);
558 : 1 : }
559 : :
560 : : G_LOCK_DEFINE (test_g_auto_lock);
561 : :
562 : : static void
563 : 1 : test_g_auto_lock (void)
564 : : {
565 : : GThread *thread;
566 : :
567 : : if (TRUE)
568 : : {
569 : 2 : G_AUTO_LOCK (test_g_auto_lock);
570 : :
571 : : /* Verify that the mutex is actually locked */
572 : 1 : thread = g_thread_new ("mutex locked", mutex_locked_thread, &G_LOCK_NAME (test_g_auto_lock));
573 : 1 : g_thread_join (thread);
574 : : }
575 : :
576 : : /* Verify that the mutex is unlocked again */
577 : 1 : thread = g_thread_new ("mutex unlocked", mutex_unlocked_thread, &G_LOCK_NAME (test_g_auto_lock));
578 : 1 : g_thread_join (thread);
579 : 1 : }
580 : :
581 : : static void
582 : 1 : test_g_cond (void)
583 : : {
584 : 1 : g_auto(GCond) val;
585 : 1 : g_cond_init (&val);
586 : 1 : }
587 : :
588 : : static void
589 : 1 : test_g_timer (void)
590 : : {
591 : 2 : g_autoptr(GTimer) val = g_timer_new ();
592 : 1 : g_assert_nonnull (val);
593 : 1 : }
594 : :
595 : : static void
596 : 1 : test_g_time_zone (void)
597 : : {
598 : 2 : g_autoptr(GTimeZone) val = g_time_zone_new_utc ();
599 : 1 : g_assert_nonnull (val);
600 : 1 : }
601 : :
602 : : static void
603 : 1 : test_g_tree (void)
604 : : {
605 : 2 : g_autoptr(GTree) val = g_tree_new ((GCompareFunc)strcmp);
606 : 1 : g_assert_nonnull (val);
607 : 1 : }
608 : :
609 : : static void
610 : 1 : test_g_variant (void)
611 : : {
612 : 2 : g_autoptr(GVariant) val = g_variant_new_string ("hello");
613 : 1 : g_assert_nonnull (val);
614 : 1 : }
615 : :
616 : : static void
617 : 1 : test_g_variant_builder (void)
618 : : {
619 : 2 : g_autoptr(GVariantBuilder) val = g_variant_builder_new (G_VARIANT_TYPE ("as"));
620 : 1 : g_auto(GVariantBuilder) stackval;
621 : :
622 : 1 : g_assert_nonnull (val);
623 : 1 : g_variant_builder_init (&stackval, G_VARIANT_TYPE ("as"));
624 : 1 : }
625 : :
626 : : static void
627 : 1 : test_g_variant_iter (void)
628 : : {
629 : 2 : g_autoptr(GVariant) var = g_variant_new_fixed_array (G_VARIANT_TYPE_UINT32, "", 0, sizeof(guint32));
630 : 2 : g_autoptr(GVariantIter) val = g_variant_iter_new (var);
631 : 1 : g_assert_nonnull (val);
632 : 1 : }
633 : :
634 : : static void
635 : 1 : test_g_variant_dict (void)
636 : : {
637 : 2 : g_autoptr(GVariant) data = g_variant_new_from_data (G_VARIANT_TYPE ("a{sv}"), "", 0, FALSE, NULL, NULL);
638 : 1 : g_auto(GVariantDict) stackval;
639 : 2 : g_autoptr(GVariantDict) val = g_variant_dict_new (data);
640 : :
641 : 1 : g_variant_dict_init (&stackval, data);
642 : 1 : g_assert_nonnull (val);
643 : 1 : }
644 : :
645 : : static void
646 : 1 : test_g_variant_type (void)
647 : : {
648 : 2 : g_autoptr(GVariantType) val = g_variant_type_new ("s");
649 : 1 : g_assert_nonnull (val);
650 : 1 : }
651 : :
652 : : static void
653 : 1 : test_strv (void)
654 : : {
655 : 2 : g_auto(GStrv) val = g_strsplit("a:b:c", ":", -1);
656 : 1 : g_assert_nonnull (val);
657 : 1 : }
658 : :
659 : : static void
660 : 1 : test_refstring (void)
661 : : {
662 : 2 : g_autoptr(GRefString) str = g_ref_string_new ("hello, world");
663 : 1 : g_assert_nonnull (str);
664 : 1 : }
665 : :
666 : : static void
667 : 1 : test_pathbuf (void)
668 : : {
669 : : #if defined(G_OS_UNIX)
670 : 2 : g_autoptr(GPathBuf) buf1 = g_path_buf_new_from_path ("/bin/sh");
671 : 1 : g_auto(GPathBuf) buf2 = G_PATH_BUF_INIT;
672 : :
673 : 1 : g_path_buf_push (&buf2, "/bin/sh");
674 : : #elif defined(G_OS_WIN32)
675 : : g_autoptr(GPathBuf) buf1 = g_path_buf_new_from_path ("C:\\windows\\system32.dll");
676 : : g_auto(GPathBuf) buf2 = G_PATH_BUF_INIT;
677 : :
678 : : g_path_buf_push (&buf2, "C:\\windows\\system32.dll");
679 : : #else
680 : : g_test_skip ("Unsupported platform");
681 : : return;
682 : : #endif
683 : :
684 : 2 : g_autofree char *path1 = g_path_buf_to_path (buf1);
685 : 2 : g_autofree char *path2 = g_path_buf_to_path (&buf2);
686 : :
687 : 1 : g_assert_cmpstr (path1, ==, path2);
688 : 1 : }
689 : :
690 : : static void
691 : 9 : mark_freed (gpointer ptr)
692 : : {
693 : 9 : gboolean *freed = ptr;
694 : 9 : *freed = TRUE;
695 : 9 : }
696 : :
697 : : static void
698 : 1 : test_autolist (void)
699 : : {
700 : 1 : char data[1] = {0};
701 : 1 : gboolean freed1 = FALSE;
702 : 1 : gboolean freed2 = FALSE;
703 : 1 : gboolean freed3 = FALSE;
704 : 1 : GBytes *b1 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed1);
705 : 1 : GBytes *b2 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed2);
706 : 1 : GBytes *b3 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed3);
707 : :
708 : : {
709 : 2 : g_autolist(GBytes) l = NULL;
710 : :
711 : 1 : l = g_list_prepend (l, b1);
712 : 1 : l = g_list_prepend (l, b3);
713 : :
714 : : /* Squash warnings about dead stores */
715 : : (void) l;
716 : : }
717 : :
718 : : /* Only assert if autoptr works */
719 : : #ifdef __GNUC__
720 : 1 : g_assert_true (freed1);
721 : 1 : g_assert_true (freed3);
722 : : #endif
723 : 1 : g_assert_false (freed2);
724 : :
725 : 1 : g_bytes_unref (b2);
726 : 1 : g_assert_true (freed2);
727 : 1 : }
728 : :
729 : : static void
730 : 1 : test_autoslist (void)
731 : : {
732 : 1 : char data[1] = {0};
733 : 1 : gboolean freed1 = FALSE;
734 : 1 : gboolean freed2 = FALSE;
735 : 1 : gboolean freed3 = FALSE;
736 : 1 : GBytes *b1 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed1);
737 : 1 : GBytes *b2 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed2);
738 : 1 : GBytes *b3 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed3);
739 : :
740 : : {
741 : 2 : g_autoslist(GBytes) l = NULL;
742 : :
743 : 1 : l = g_slist_prepend (l, b1);
744 : 1 : l = g_slist_prepend (l, b3);
745 : : }
746 : :
747 : : /* Only assert if autoptr works */
748 : : #ifdef __GNUC__
749 : 1 : g_assert_true (freed1);
750 : 1 : g_assert_true (freed3);
751 : : #endif
752 : 1 : g_assert_false (freed2);
753 : :
754 : 1 : g_bytes_unref (b2);
755 : 1 : g_assert_true (freed2);
756 : 1 : }
757 : :
758 : : static void
759 : 1 : test_autoqueue (void)
760 : : {
761 : 1 : char data[1] = {0};
762 : 1 : gboolean freed1 = FALSE;
763 : 1 : gboolean freed2 = FALSE;
764 : 1 : gboolean freed3 = FALSE;
765 : 1 : GBytes *b1 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed1);
766 : 1 : GBytes *b2 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed2);
767 : 1 : GBytes *b3 = g_bytes_new_with_free_func (data, sizeof(data), mark_freed, &freed3);
768 : :
769 : : {
770 : 2 : g_autoqueue(GBytes) q = g_queue_new ();
771 : :
772 : 1 : g_queue_push_head (q, b1);
773 : 1 : g_queue_push_tail (q, b3);
774 : : }
775 : :
776 : : /* Only assert if autoptr works */
777 : : #ifdef __GNUC__
778 : 1 : g_assert_true (freed1);
779 : 1 : g_assert_true (freed3);
780 : : #endif
781 : 1 : g_assert_false (freed2);
782 : :
783 : 1 : g_bytes_unref (b2);
784 : 1 : g_assert_true (freed2);
785 : 1 : }
786 : :
787 : : int
788 : 1 : main (int argc, gchar *argv[])
789 : : {
790 : 1 : g_test_init (&argc, &argv, NULL);
791 : :
792 : 1 : g_test_add_func ("/autoptr/autofree", test_autofree);
793 : 1 : g_test_add_func ("/autoptr/g_async_queue", test_g_async_queue);
794 : 1 : g_test_add_func ("/autoptr/g_bookmark_file", test_g_bookmark_file);
795 : 1 : g_test_add_func ("/autoptr/g_bytes", test_g_bytes);
796 : 1 : g_test_add_func ("/autoptr/g_checksum", test_g_checksum);
797 : 1 : g_test_add_func ("/autoptr/g_date", test_g_date);
798 : 1 : g_test_add_func ("/autoptr/g_date_time", test_g_date_time);
799 : 1 : g_test_add_func ("/autoptr/g_dir", test_g_dir);
800 : 1 : g_test_add_func ("/autoptr/g_error", test_g_error);
801 : 1 : g_test_add_func ("/autoptr/g_hash_table", test_g_hash_table);
802 : 1 : g_test_add_func ("/autoptr/g_hmac", test_g_hmac);
803 : 1 : g_test_add_func ("/autoptr/g_io_channel", test_g_io_channel);
804 : 1 : g_test_add_func ("/autoptr/g_key_file", test_g_key_file);
805 : 1 : g_test_add_func ("/autoptr/g_list", test_g_list);
806 : 1 : g_test_add_func ("/autoptr/g_array", test_g_array);
807 : 1 : g_test_add_func ("/autoptr/g_ptr_array", test_g_ptr_array);
808 : 1 : g_test_add_func ("/autoptr/g_byte_array", test_g_byte_array);
809 : 1 : g_test_add_func ("/autoptr/g_main_context", test_g_main_context);
810 : 1 : g_test_add_func ("/autoptr/g_main_context_pusher", test_g_main_context_pusher);
811 : 1 : g_test_add_func ("/autoptr/g_main_loop", test_g_main_loop);
812 : 1 : g_test_add_func ("/autoptr/g_source", test_g_source);
813 : 1 : g_test_add_func ("/autoptr/g_mapped_file", test_g_mapped_file);
814 : 1 : g_test_add_func ("/autoptr/g_markup_parse_context", test_g_markup_parse_context);
815 : 1 : g_test_add_func ("/autoptr/g_node", test_g_node);
816 : 1 : g_test_add_func ("/autoptr/g_option_context", test_g_option_context);
817 : 1 : g_test_add_func ("/autoptr/g_option_group", test_g_option_group);
818 : 1 : g_test_add_func ("/autoptr/g_pattern_spec", test_g_pattern_spec);
819 : 1 : g_test_add_func ("/autoptr/g_queue", test_g_queue);
820 : 1 : g_test_add_func ("/autoptr/g_rand", test_g_rand);
821 : 1 : g_test_add_func ("/autoptr/g_regex", test_g_regex);
822 : 1 : g_test_add_func ("/autoptr/g_match_info", test_g_match_info);
823 : 1 : g_test_add_func ("/autoptr/g_scanner", test_g_scanner);
824 : 1 : g_test_add_func ("/autoptr/g_sequence", test_g_sequence);
825 : 1 : g_test_add_func ("/autoptr/g_slist", test_g_slist);
826 : 1 : g_test_add_func ("/autoptr/g_string", test_g_string);
827 : 1 : g_test_add_func ("/autoptr/g_string_chunk", test_g_string_chunk);
828 : 1 : g_test_add_func ("/autoptr/g_thread", test_g_thread);
829 : 1 : g_test_add_func ("/autoptr/g_mutex", test_g_mutex);
830 : 1 : g_test_add_func ("/autoptr/g_mutex_locker", test_g_mutex_locker);
831 : 1 : g_test_add_func ("/autoptr/g_rec_mutex_locker", test_g_rec_mutex_locker);
832 : 1 : g_test_add_func ("/autoptr/g_rw_lock_lockers", test_g_rw_lock_lockers);
833 : 1 : g_test_add_func ("/autoptr/g_auto_lock", test_g_auto_lock);
834 : 1 : g_test_add_func ("/autoptr/g_cond", test_g_cond);
835 : 1 : g_test_add_func ("/autoptr/g_timer", test_g_timer);
836 : 1 : g_test_add_func ("/autoptr/g_time_zone", test_g_time_zone);
837 : 1 : g_test_add_func ("/autoptr/g_tree", test_g_tree);
838 : 1 : g_test_add_func ("/autoptr/g_variant", test_g_variant);
839 : 1 : g_test_add_func ("/autoptr/g_variant_builder", test_g_variant_builder);
840 : 1 : g_test_add_func ("/autoptr/g_variant_iter", test_g_variant_iter);
841 : 1 : g_test_add_func ("/autoptr/g_variant_dict", test_g_variant_dict);
842 : 1 : g_test_add_func ("/autoptr/g_variant_type", test_g_variant_type);
843 : 1 : g_test_add_func ("/autoptr/strv", test_strv);
844 : 1 : g_test_add_func ("/autoptr/refstring", test_refstring);
845 : 1 : g_test_add_func ("/autoptr/pathbuf", test_pathbuf);
846 : 1 : g_test_add_func ("/autoptr/autolist", test_autolist);
847 : 1 : g_test_add_func ("/autoptr/autoslist", test_autoslist);
848 : 1 : g_test_add_func ("/autoptr/autoqueue", test_autoqueue);
849 : :
850 : 1 : return g_test_run ();
851 : : }
|