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