LCOV - code coverage report
Current view: top level - glib/gio/tests - simple-async-result.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 116 116 100.0 %
Date: 2024-04-23 05:16:05 Functions: 8 8 100.0 %
Branches: 2 2 100.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright © 2009 Ryan Lortie
       3                 :            :  *
       4                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       5                 :            :  *
       6                 :            :  * This library is free software; you can redistribute it and/or
       7                 :            :  * modify it under the terms of the GNU Lesser General Public
       8                 :            :  * License as published by the Free Software Foundation; either
       9                 :            :  * version 2.1 of the License, or (at your option) any later version.
      10                 :            :  *
      11                 :            :  * See the included COPYING file for more information.
      12                 :            :  */
      13                 :            : 
      14                 :            : #include <glib/glib.h>
      15                 :            : #include <gio/gio.h>
      16                 :            : #include <stdlib.h>
      17                 :            : #include <string.h>
      18                 :            : 
      19                 :            : G_GNUC_BEGIN_IGNORE_DEPRECATIONS
      20                 :            : 
      21                 :            : static GObject      *got_source;
      22                 :            : static GAsyncResult *got_result;
      23                 :            : static gpointer      got_user_data;
      24                 :            : 
      25                 :            : static void
      26                 :          7 : ensure_destroyed (gpointer obj)
      27                 :            : {
      28                 :          7 :   g_object_add_weak_pointer (obj, &obj);
      29                 :          7 :   g_object_unref (obj);
      30                 :          7 :   g_assert (obj == NULL);
      31                 :          7 : }
      32                 :            : 
      33                 :            : static void
      34                 :          4 : reset (void)
      35                 :            : {
      36                 :          4 :   got_source = NULL;
      37                 :            : 
      38         [ +  + ]:          4 :   if (got_result)
      39                 :          2 :     ensure_destroyed (got_result);
      40                 :            : 
      41                 :          4 :   got_result = NULL;
      42                 :          4 :   got_user_data = NULL;
      43                 :          4 : }
      44                 :            : 
      45                 :            : static void
      46                 :          5 : check (gpointer a, gpointer b, gpointer c)
      47                 :            : {
      48                 :          5 :   g_assert (a == got_source);
      49                 :          5 :   g_assert (b == got_result);
      50                 :          5 :   g_assert (c == got_user_data);
      51                 :          5 : }
      52                 :            : 
      53                 :            : static void
      54                 :          2 : callback_func (GObject      *source,
      55                 :            :                GAsyncResult *result,
      56                 :            :                gpointer      user_data)
      57                 :            : {
      58                 :          2 :   got_source = source;
      59                 :          2 :   got_result = g_object_ref (result);
      60                 :          2 :   got_user_data = user_data;
      61                 :          2 : }
      62                 :            : 
      63                 :            : static gboolean
      64                 :          1 : test_simple_async_idle (gpointer user_data)
      65                 :            : {
      66                 :            :   GSimpleAsyncResult *result;
      67                 :            :   GObject *a, *b, *c;
      68                 :          1 :   gboolean *ran = user_data;
      69                 :            : 
      70                 :          1 :   a = g_object_new (G_TYPE_OBJECT, NULL);
      71                 :          1 :   b = g_object_new (G_TYPE_OBJECT, NULL);
      72                 :          1 :   c = g_object_new (G_TYPE_OBJECT, NULL);
      73                 :            : 
      74                 :          1 :   result = g_simple_async_result_new (a, callback_func, b, test_simple_async_idle);
      75                 :          1 :   g_assert (g_async_result_get_user_data (G_ASYNC_RESULT (result)) == b);
      76                 :          1 :   check (NULL, NULL, NULL);
      77                 :          1 :   g_simple_async_result_complete (result);
      78                 :          1 :   check (a, result, b);
      79                 :          1 :   g_object_unref (result);
      80                 :            : 
      81                 :          1 :   g_assert (g_simple_async_result_is_valid (got_result, a, test_simple_async_idle));
      82                 :          1 :   g_assert (!g_simple_async_result_is_valid (got_result, b, test_simple_async_idle));
      83                 :          1 :   g_assert (!g_simple_async_result_is_valid (got_result, c, test_simple_async_idle));
      84                 :          1 :   g_assert (!g_simple_async_result_is_valid (got_result, b, callback_func));
      85                 :          1 :   g_assert (!g_simple_async_result_is_valid ((gpointer) a, NULL, NULL));
      86                 :          1 :   reset ();
      87                 :          1 :   reset ();
      88                 :          1 :   reset ();
      89                 :            : 
      90                 :          1 :   ensure_destroyed (a);
      91                 :          1 :   ensure_destroyed (b);
      92                 :          1 :   ensure_destroyed (c);
      93                 :            : 
      94                 :          1 :   *ran = TRUE;
      95                 :          1 :   return G_SOURCE_REMOVE;
      96                 :            : }
      97                 :            : 
      98                 :            : static void
      99                 :          1 : test_simple_async (void)
     100                 :            : {
     101                 :            :   GSimpleAsyncResult *result;
     102                 :            :   GObject *a, *b;
     103                 :          1 :   gboolean ran_test_in_idle = FALSE;
     104                 :            : 
     105                 :          1 :   g_idle_add (test_simple_async_idle, &ran_test_in_idle);
     106                 :          1 :   g_main_context_iteration (NULL, FALSE);
     107                 :            : 
     108                 :          1 :   g_assert (ran_test_in_idle);
     109                 :            : 
     110                 :          1 :   a = g_object_new (G_TYPE_OBJECT, NULL);
     111                 :          1 :   b = g_object_new (G_TYPE_OBJECT, NULL);
     112                 :            : 
     113                 :          1 :   result = g_simple_async_result_new (a, callback_func, b, test_simple_async);
     114                 :          1 :   check (NULL, NULL, NULL);
     115                 :          1 :   g_simple_async_result_complete_in_idle (result);
     116                 :          1 :   g_object_unref (result);
     117                 :          1 :   check (NULL, NULL, NULL);
     118                 :          1 :   g_main_context_iteration (NULL, FALSE);
     119                 :          1 :   check (a, result, b);
     120                 :          1 :   reset ();
     121                 :            : 
     122                 :          1 :   ensure_destroyed (a);
     123                 :          1 :   ensure_destroyed (b);
     124                 :          1 : }
     125                 :            : 
     126                 :            : static void
     127                 :          1 : test_valid (void)
     128                 :            : {
     129                 :            :   GAsyncResult *result;
     130                 :            :   GObject *a, *b;
     131                 :            : 
     132                 :          1 :   a = g_object_new (G_TYPE_OBJECT, NULL);
     133                 :          1 :   b = g_object_new (G_TYPE_OBJECT, NULL);
     134                 :            : 
     135                 :            :   /* Without source or tag */
     136                 :          1 :   result = (GAsyncResult *) g_simple_async_result_new (NULL, NULL, NULL, NULL);
     137                 :          1 :   g_assert_true (g_simple_async_result_is_valid (result, NULL, NULL));
     138                 :          1 :   g_assert_true (g_simple_async_result_is_valid (result, NULL, test_valid));
     139                 :          1 :   g_assert_true (g_simple_async_result_is_valid (result, NULL, test_simple_async));
     140                 :          1 :   g_assert_false (g_simple_async_result_is_valid (result, a, NULL));
     141                 :          1 :   g_assert_false (g_simple_async_result_is_valid (result, a, test_valid));
     142                 :          1 :   g_assert_false (g_simple_async_result_is_valid (result, a, test_simple_async));
     143                 :          1 :   g_object_unref (result);
     144                 :            : 
     145                 :            :   /* Without source, with tag */
     146                 :          1 :   result = (GAsyncResult *) g_simple_async_result_new (NULL, NULL, NULL, test_valid);
     147                 :          1 :   g_assert_true (g_simple_async_result_is_valid (result, NULL, NULL));
     148                 :          1 :   g_assert_true (g_simple_async_result_is_valid (result, NULL, test_valid));
     149                 :          1 :   g_assert_false (g_simple_async_result_is_valid (result, NULL, test_simple_async));
     150                 :          1 :   g_assert_false (g_simple_async_result_is_valid (result, a, NULL));
     151                 :          1 :   g_assert_false (g_simple_async_result_is_valid (result, a, test_valid));
     152                 :          1 :   g_assert_false (g_simple_async_result_is_valid (result, a, test_simple_async));
     153                 :          1 :   g_object_unref (result);
     154                 :            : 
     155                 :            :   /* With source, without tag */
     156                 :          1 :   result = (GAsyncResult *) g_simple_async_result_new (a, NULL, NULL, NULL);
     157                 :          1 :   g_assert_true (g_simple_async_result_is_valid (result, a, NULL));
     158                 :          1 :   g_assert_true (g_simple_async_result_is_valid (result, a, test_valid));
     159                 :          1 :   g_assert_true (g_simple_async_result_is_valid (result, a, test_simple_async));
     160                 :          1 :   g_assert_false (g_simple_async_result_is_valid (result, NULL, NULL));
     161                 :          1 :   g_assert_false (g_simple_async_result_is_valid (result, NULL, test_valid));
     162                 :          1 :   g_assert_false (g_simple_async_result_is_valid (result, NULL, test_simple_async));
     163                 :          1 :   g_assert_false (g_simple_async_result_is_valid (result, b, NULL));
     164                 :          1 :   g_assert_false (g_simple_async_result_is_valid (result, b, test_valid));
     165                 :          1 :   g_assert_false (g_simple_async_result_is_valid (result, b, test_simple_async));
     166                 :          1 :   g_object_unref (result);
     167                 :            : 
     168                 :            :   /* With source and tag */
     169                 :          1 :   result = (GAsyncResult *) g_simple_async_result_new (a, NULL, NULL, test_valid);
     170                 :          1 :   g_assert_true (g_simple_async_result_is_valid (result, a, test_valid));
     171                 :          1 :   g_assert_true (g_simple_async_result_is_valid (result, a, NULL));
     172                 :          1 :   g_assert_false (g_simple_async_result_is_valid (result, a, test_simple_async));
     173                 :          1 :   g_assert_false (g_simple_async_result_is_valid (result, NULL, NULL));
     174                 :          1 :   g_assert_false (g_simple_async_result_is_valid (result, NULL, test_valid));
     175                 :          1 :   g_assert_false (g_simple_async_result_is_valid (result, NULL, test_simple_async));
     176                 :          1 :   g_assert_false (g_simple_async_result_is_valid (result, b, NULL));
     177                 :          1 :   g_assert_false (g_simple_async_result_is_valid (result, b, test_valid));
     178                 :          1 :   g_assert_false (g_simple_async_result_is_valid (result, b, test_simple_async));
     179                 :          1 :   g_object_unref (result);
     180                 :            : 
     181                 :            :   /* Non-GSimpleAsyncResult */
     182                 :          1 :   result = (GAsyncResult *) g_task_new (NULL, NULL, NULL, NULL);
     183                 :          1 :   g_assert_false (g_simple_async_result_is_valid (result, NULL, NULL));
     184                 :          1 :   g_object_unref (result);
     185                 :            : 
     186                 :          1 :   g_object_unref (a);
     187                 :          1 :   g_object_unref (b);
     188                 :          1 : }
     189                 :            : 
     190                 :            : int
     191                 :          1 : main (int argc, char **argv)
     192                 :            : {
     193                 :          1 :   g_test_init (&argc, &argv, NULL);
     194                 :            : 
     195                 :          1 :   g_test_add_func ("/gio/simple-async-result/test", test_simple_async);
     196                 :          1 :   g_test_add_func ("/gio/simple-async-result/valid", test_valid);
     197                 :            : 
     198                 :          1 :   return g_test_run();
     199                 :            : }
     200                 :            : 
     201                 :            : G_GNUC_END_IGNORE_DEPRECATIONS

Generated by: LCOV version 1.14