File: dir.c

package info (click to toggle)
glib2.0 2.84.3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie, trixie-proposed-updates, trixie-updates
  • size: 66,100 kB
  • sloc: ansic: 538,905; python: 9,624; sh: 1,572; xml: 1,482; perl: 1,222; cpp: 535; makefile: 316; javascript: 11
file content (79 lines) | stat: -rw-r--r-- 1,734 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <glib.h>

static void
test_dir_read (void)
{
  GDir *dir;
  GError *error;
  gchar *first;
  const gchar *name;

  error = NULL;
  dir = g_dir_open (".", 0, &error);
  g_assert_no_error (error);

  first = NULL;
  while ((name = g_dir_read_name (dir)) != NULL)
    {
      if (first == NULL)
        first = g_strdup (name);
      g_assert_cmpstr (name, !=, ".");
      g_assert_cmpstr (name, !=, "..");
    }

  g_dir_rewind (dir);
  g_assert_cmpstr (g_dir_read_name (dir), ==, first);

  g_free (first);
  g_dir_close (dir);
}

static void
test_dir_nonexisting (void)
{
  GDir *dir;
  GError *error = NULL;
  char *path = NULL;

  path = g_build_filename (g_get_tmp_dir (), "does-not-exist", NULL);
  dir = g_dir_open (path, 0, &error);
  g_assert_null (dir);
  g_assert_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
  g_error_free (error);
  g_free (path);
}

static void
test_dir_refcounting (void)
{
  GDir *dir;
  GError *local_error = NULL;

  g_test_summary ("Test refcounting interactions with g_dir_close()");

  /* Try keeping the `GDir` struct alive after closing it. */
  dir = g_dir_open (".", 0, &local_error);
  g_assert_no_error (local_error);

  g_dir_ref (dir);
  g_dir_close (dir);
  g_dir_unref (dir);

  /* Test that dropping the last ref closes it. Any leak here should be caught
   * when the test is run under valgrind. */
  dir = g_dir_open (".", 0, &local_error);
  g_assert_no_error (local_error);
  g_dir_unref (dir);
}

int
main (int argc, char *argv[])
{
  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/dir/read", test_dir_read);
  g_test_add_func ("/dir/nonexisting", test_dir_nonexisting);
  g_test_add_func ("/dir/refcounting", test_dir_refcounting);

  return g_test_run ();
}