LCOV - code coverage report
Current view: top level - glib/gobject/tests - closure.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 92 95 96.8 %
Date: 2024-04-30 05:17:35 Functions: 13 13 100.0 %
Branches: 4 8 50.0 %

           Branch data     Line data    Source code
       1                 :            : #include <glib-object.h>
       2                 :            : 
       3                 :            : #ifdef G_OS_UNIX
       4                 :            : #include <glib-unix.h>
       5                 :            : 
       6                 :            : #include <fcntl.h>
       7                 :            : #include <signal.h>
       8                 :            : #include <unistd.h>
       9                 :            : #endif
      10                 :            : 
      11                 :            : static void
      12                 :          6 : test_source (GSource *one, GCallback quit_callback)
      13                 :            : {
      14                 :            :   GClosure *closure;
      15                 :            :   GMainLoop *loop;
      16                 :            : 
      17                 :            :   /* Callback with GMainLoop user_data */
      18                 :          6 :   loop = g_main_loop_new (NULL, FALSE);
      19                 :            : 
      20                 :          6 :   closure = g_cclosure_new (quit_callback, loop, NULL);
      21                 :          6 :   g_source_set_closure (one, closure);
      22                 :            : 
      23                 :          6 :   g_source_attach (one, NULL);
      24                 :          6 :   g_main_loop_run (loop);
      25                 :            : 
      26                 :          6 :   g_source_destroy (one);
      27                 :          6 :   g_main_loop_unref (loop);
      28                 :          6 : }
      29                 :            : 
      30                 :            : static gboolean
      31                 :          2 : simple_quit_callback (gpointer user_data)
      32                 :            : {
      33                 :          2 :   GMainLoop *loop = user_data;
      34                 :            : 
      35                 :          2 :   g_main_loop_quit (loop);
      36                 :            : 
      37                 :          2 :   return TRUE;
      38                 :            : }
      39                 :            : 
      40                 :            : static void
      41                 :          1 : test_closure_idle (void)
      42                 :            : {
      43                 :            :   GSource *source;
      44                 :            : 
      45                 :          1 :   source = g_idle_source_new ();
      46                 :          1 :   test_source (source, G_CALLBACK (simple_quit_callback));
      47                 :          1 :   g_source_unref (source);
      48                 :          1 : }
      49                 :            : 
      50                 :            : static void
      51                 :          1 : test_closure_timeout (void)
      52                 :            : {
      53                 :            :   GSource *source;
      54                 :            : 
      55                 :          1 :   source = g_timeout_source_new (10);
      56                 :          1 :   test_source (source, G_CALLBACK (simple_quit_callback));
      57                 :          1 :   g_source_unref (source);
      58                 :          1 : }
      59                 :            : 
      60                 :            : static gboolean
      61                 :          2 : iochannel_quit_callback (GIOChannel   *channel,
      62                 :            :                          GIOCondition  cond,
      63                 :            :                          gpointer      user_data)
      64                 :            : {
      65                 :          2 :   GMainLoop *loop = user_data;
      66                 :            : 
      67                 :          2 :   g_main_loop_quit (loop);
      68                 :            : 
      69                 :          2 :   return TRUE;
      70                 :            : }
      71                 :            : 
      72                 :            : static void
      73                 :          1 : test_closure_iochannel (void)
      74                 :            : {
      75                 :            :   GIOChannel *chan;
      76                 :            :   GSource *source;
      77                 :            :   char *path;
      78                 :          1 :   GError *error = NULL;
      79                 :            : 
      80         [ +  - ]:          1 :   if (g_path_is_absolute (g_get_prgname ()))
      81                 :          2 :     path = g_strdup (g_get_prgname ());
      82                 :            :   else
      83                 :            :     {
      84                 :          0 :       path = g_test_build_filename (G_TEST_BUILT,
      85                 :            :                                     g_get_prgname (),
      86                 :            :                                     NULL);
      87                 :            :     }
      88                 :          1 :   chan = g_io_channel_new_file (path, "r", &error);
      89                 :          1 :   g_assert_no_error (error);
      90                 :          1 :   g_free (path);
      91                 :            : 
      92                 :          1 :   source = g_io_create_watch (chan, G_IO_IN);
      93                 :          1 :   test_source (source, G_CALLBACK (iochannel_quit_callback));
      94                 :          1 :   g_source_unref (source);
      95                 :            : 
      96                 :          1 :   g_io_channel_unref (chan);
      97                 :          1 : }
      98                 :            : 
      99                 :            : static void
     100                 :          1 : test_closure_child (void)
     101                 :            : {
     102                 :            :   GSource *source;
     103                 :            :   GPid pid;
     104                 :          1 :   GError *error = NULL;
     105                 :            :   gchar *argv[3];
     106                 :            : 
     107                 :          1 :   g_assert (g_getenv ("DO_NOT_ACCIDENTALLY_RECURSE") == NULL);
     108                 :          1 :   g_setenv ("DO_NOT_ACCIDENTALLY_RECURSE", "1", TRUE);
     109                 :            : 
     110         [ +  - ]:          1 :   if (g_path_is_absolute (g_get_prgname ()))
     111                 :          2 :     argv[0] = g_strdup (g_get_prgname ());
     112                 :            :   else
     113                 :            :     {
     114                 :          0 :       argv[0] = g_test_build_filename (G_TEST_BUILT,
     115                 :            :                                        g_get_prgname (),
     116                 :            :                                        NULL);
     117                 :            :     }
     118                 :          1 :   argv[1] = "-l";
     119                 :          1 :   argv[2] = NULL;
     120                 :            : 
     121                 :          1 :   g_spawn_async (NULL, argv, NULL,
     122                 :            :                  G_SPAWN_STDOUT_TO_DEV_NULL |
     123                 :            :                  G_SPAWN_STDERR_TO_DEV_NULL |
     124                 :            :                  G_SPAWN_DO_NOT_REAP_CHILD,
     125                 :            :                  NULL, NULL,
     126                 :            :                  &pid, &error);
     127                 :          1 :   g_assert_no_error (error);
     128                 :            : 
     129                 :          1 :   g_free (argv[0]);
     130                 :            : 
     131                 :          1 :   source = g_child_watch_source_new (pid);
     132                 :          1 :   test_source (source, G_CALLBACK (iochannel_quit_callback));
     133                 :          1 :   g_source_unref (source);
     134                 :          1 : }
     135                 :            : 
     136                 :            : #ifdef G_OS_UNIX
     137                 :            : static gboolean
     138                 :          1 : fd_quit_callback (gint         fd,
     139                 :            :                   GIOCondition condition,
     140                 :            :                   gpointer     user_data)
     141                 :            : {
     142                 :          1 :   GMainLoop *loop = user_data;
     143                 :            : 
     144                 :          1 :   g_main_loop_quit (loop);
     145                 :            : 
     146                 :          1 :   return TRUE;
     147                 :            : }
     148                 :            : 
     149                 :            : static void
     150                 :          1 : test_closure_fd (void)
     151                 :            : {
     152                 :            :   gint fd;
     153                 :            :   GSource *source;
     154                 :            : 
     155                 :          1 :   fd = open ("/dev/null", O_RDONLY);
     156                 :          1 :   g_assert (fd != -1);
     157                 :            : 
     158                 :          1 :   source = g_unix_fd_source_new (fd, G_IO_IN);
     159                 :          1 :   test_source (source, G_CALLBACK (fd_quit_callback));
     160                 :          1 :   g_source_unref (source);
     161                 :            : 
     162                 :          1 :   close (fd);
     163                 :          1 : }
     164                 :            : 
     165                 :            : static gboolean
     166                 :          1 : send_usr1 (gpointer user_data)
     167                 :            : {
     168                 :          1 :   kill (getpid (), SIGUSR1);
     169                 :          1 :   return FALSE;
     170                 :            : }
     171                 :            : 
     172                 :            : static gboolean
     173                 :          1 : closure_quit_callback (gpointer     user_data)
     174                 :            : {
     175                 :          1 :   GMainLoop *loop = user_data;
     176                 :            : 
     177                 :          1 :   g_main_loop_quit (loop);
     178                 :            : 
     179                 :          1 :   return TRUE;
     180                 :            : }
     181                 :            : 
     182                 :            : static void
     183                 :          1 : test_closure_signal (void)
     184                 :            : {
     185                 :            :   GSource *source;
     186                 :            : 
     187                 :          1 :   g_idle_add_full (G_PRIORITY_LOW, send_usr1, NULL, NULL);
     188                 :            : 
     189                 :          1 :   source = g_unix_signal_source_new (SIGUSR1);
     190                 :          1 :   test_source (source, G_CALLBACK (closure_quit_callback));
     191                 :          1 :   g_source_unref (source);
     192                 :          1 : }
     193                 :            : #endif
     194                 :            : 
     195                 :            : int
     196                 :          2 : main (int argc,
     197                 :            :       char *argv[])
     198                 :            : {
     199                 :            : #ifndef G_OS_WIN32
     200                 :            :   sigset_t sig_mask, old_mask;
     201                 :            : 
     202                 :          2 :   sigemptyset (&sig_mask);
     203                 :          2 :   sigaddset (&sig_mask, SIGUSR1);
     204         [ +  - ]:          2 :   if (sigprocmask (SIG_UNBLOCK, &sig_mask, &old_mask) == 0)
     205                 :            :     {
     206         [ -  + ]:          2 :       if (sigismember (&old_mask, SIGUSR1))
     207                 :          0 :         g_message ("SIGUSR1 was blocked, unblocking it");
     208                 :            :     }
     209                 :            : #endif
     210                 :            : 
     211                 :          2 :   g_test_init (&argc, &argv, NULL);
     212                 :            : 
     213                 :          2 :   g_test_add_func ("/closure/idle", test_closure_idle);
     214                 :          2 :   g_test_add_func ("/closure/timeout", test_closure_timeout);
     215                 :          2 :   g_test_add_func ("/closure/iochannel", test_closure_iochannel);
     216                 :          2 :   g_test_add_func ("/closure/child", test_closure_child);
     217                 :            : #ifdef G_OS_UNIX
     218                 :          2 :   g_test_add_func ("/closure/fd", test_closure_fd);
     219                 :          2 :   g_test_add_func ("/closure/signal", test_closure_signal);
     220                 :            : #endif
     221                 :            : 
     222                 :          2 :   return g_test_run ();
     223                 :            : }

Generated by: LCOV version 1.14