LCOV - code coverage report
Current view: top level - glib/glib/tests - dir.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 40 40 100.0 %
Date: 2024-04-23 05:16:05 Functions: 4 4 100.0 %
Branches: 4 4 100.0 %

           Branch data     Line data    Source code
       1                 :            : #include <glib.h>
       2                 :            : 
       3                 :            : static void
       4                 :          1 : test_dir_read (void)
       5                 :            : {
       6                 :            :   GDir *dir;
       7                 :            :   GError *error;
       8                 :            :   gchar *first;
       9                 :            :   const gchar *name;
      10                 :            : 
      11                 :          1 :   error = NULL;
      12                 :          1 :   dir = g_dir_open (".", 0, &error);
      13                 :          1 :   g_assert_no_error (error);
      14                 :            : 
      15                 :          1 :   first = NULL;
      16         [ +  + ]:         24 :   while ((name = g_dir_read_name (dir)) != NULL)
      17                 :            :     {
      18         [ +  + ]:         23 :       if (first == NULL)
      19                 :          1 :         first = g_strdup (name);
      20                 :         23 :       g_assert_cmpstr (name, !=, ".");
      21                 :         23 :       g_assert_cmpstr (name, !=, "..");
      22                 :            :     }
      23                 :            : 
      24                 :          1 :   g_dir_rewind (dir);
      25                 :          1 :   g_assert_cmpstr (g_dir_read_name (dir), ==, first);
      26                 :            : 
      27                 :          1 :   g_free (first);
      28                 :          1 :   g_dir_close (dir);
      29                 :          1 : }
      30                 :            : 
      31                 :            : static void
      32                 :          1 : test_dir_nonexisting (void)
      33                 :            : {
      34                 :            :   GDir *dir;
      35                 :            :   GError *error;
      36                 :            : 
      37                 :          1 :   error = NULL;
      38                 :          1 :   dir = g_dir_open ("/pfrkstrf", 0, &error);
      39                 :          1 :   g_assert (dir == NULL);
      40                 :          1 :   g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
      41                 :          1 :   g_error_free (error);
      42                 :          1 : }
      43                 :            : 
      44                 :            : static void
      45                 :          1 : test_dir_refcounting (void)
      46                 :            : {
      47                 :            :   GDir *dir;
      48                 :          1 :   GError *local_error = NULL;
      49                 :            : 
      50                 :          1 :   g_test_summary ("Test refcounting interactions with g_dir_close()");
      51                 :            : 
      52                 :            :   /* Try keeping the `GDir` struct alive after closing it. */
      53                 :          1 :   dir = g_dir_open (".", 0, &local_error);
      54                 :          1 :   g_assert_no_error (local_error);
      55                 :            : 
      56                 :          1 :   g_dir_ref (dir);
      57                 :          1 :   g_dir_close (dir);
      58                 :          1 :   g_dir_unref (dir);
      59                 :            : 
      60                 :            :   /* Test that dropping the last ref closes it. Any leak here should be caught
      61                 :            :    * when the test is run under valgrind. */
      62                 :          1 :   dir = g_dir_open (".", 0, &local_error);
      63                 :          1 :   g_assert_no_error (local_error);
      64                 :          1 :   g_dir_unref (dir);
      65                 :          1 : }
      66                 :            : 
      67                 :            : int
      68                 :          1 : main (int argc, char *argv[])
      69                 :            : {
      70                 :          1 :   g_test_init (&argc, &argv, NULL);
      71                 :            : 
      72                 :          1 :   g_test_add_func ("/dir/read", test_dir_read);
      73                 :          1 :   g_test_add_func ("/dir/nonexisting", test_dir_nonexisting);
      74                 :          1 :   g_test_add_func ("/dir/refcounting", test_dir_refcounting);
      75                 :            : 
      76                 :          1 :   return g_test_run ();
      77                 :            : }

Generated by: LCOV version 1.14