1 /* Test the gnulib dirname module.
2 Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
27 const char *program_name = "test-dirname";
30 const char *name; /* Name under test. */
31 const char *dir; /* dir_name (name). */
32 const char *last; /* last_component (name). */
33 const char *base; /* base_name (name). */
34 const char *stripped; /* name after strip_trailing_slashes (name). */
35 bool modified; /* result of strip_trailing_slashes (name). */
36 bool absolute; /* IS_ABSOLUTE_FILE_NAME (name). */
39 static struct test tests[] = {
40 {"d/f", "d", "f", "f", "d/f", false, false},
41 {"/d/f", "/d", "f", "f", "/d/f", false, true},
42 {"d/f/", "d", "f/", "f/", "d/f", true, false},
43 {"d/f//", "d", "f//", "f/", "d/f", true, false},
44 {"f", ".", "f", "f", "f", false, false},
45 {"/", "/", "", "/", "/", false, true},
46 #if DOUBLE_SLASH_IS_DISTINCT_ROOT
47 {"//", "//", "", "//", "//", false, true},
48 {"//d", "//", "d", "d", "//d", false, true},
50 {"//", "/", "", "/", "/", true, true},
51 {"//d", "/", "d", "d", "//d", false, true},
53 {"///", "/", "", "/", "/", true, true},
54 {"///a///", "/", "a///", "a/", "///a", true, true},
55 /* POSIX requires dirname("") and basename("") to both return ".",
56 but dir_name and base_name are defined differently. */
57 {"", ".", "", "", "", false, false},
58 {".", ".", ".", ".", ".", false, false},
59 {"..", ".", "..", "..", "..", false, false},
60 #if FILE_SYSTEM_BACKSLASH_IS_FILE_NAME_SEPARATOR
61 {"a\\", ".", "a\\", "a\\", "a", true, false},
62 {"a\\b", "a", "b", "b", "a\\b", false, false},
63 {"\\", "\\", "", "\\", "\\", false, true},
64 {"\\/\\", "\\", "", "\\", "\\", true, true},
65 {"\\\\/", "\\", "", "\\", "\\", true, true},
66 {"\\//", "\\", "", "\\", "\\", true, true},
67 {"//\\", "/", "", "/", "/", true, true},
69 {"a\\", ".", "a\\", "a\\", "a\\", false, false},
70 {"a\\b", ".", "a\\b", "a\\b", "a\\b", false, false},
71 {"\\", ".", "\\", "\\", "\\", false, false},
72 {"\\/\\", "\\", "\\", "\\", "\\/\\",false, false},
73 {"\\\\/", ".", "\\\\/","\\\\/","\\\\", true, false},
74 {"\\//", ".", "\\//", "\\/", "\\", true, false},
75 # if DOUBLE_SLASH_IS_DISTINCT_ROOT
76 {"//\\", "//", "\\", "\\", "//\\", false, true},
78 {"//\\", "/", "\\", "\\", "//\\", false, true},
81 #if FILE_SYSTEM_ACCEPTS_DRIVE_LETTER_PREFIX
82 # if FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
83 {"c:", "c:", "", "c:", "c:", false, false},
84 {"c:/", "c:/", "", "c:/", "c:/", false, true},
85 {"c://", "c:/", "", "c:/", "c:/", true, true},
86 {"c:/d", "c:/", "d", "d", "c:/d", false, true},
87 {"c://d", "c:/", "d", "d", "c://d",false, true},
88 {"c:/d/", "c:/", "d/", "d/", "c:/d", true, true},
89 {"c:/d/f", "c:/d", "f", "f", "c:/d/f",false, true},
90 {"c:d", "c:.", "d", "d", "c:d", false, false},
91 {"c:d/", "c:.", "d/", "d/", "c:d", true, false},
92 {"c:d/f", "c:d", "f", "f", "c:d/f",false, false},
93 {"a:b:c", "a:.", "b:c", "./b:c","a:b:c",false, false},
94 {"a/b:c", "a", "b:c", "./b:c","a/b:c",false, false},
95 {"a/b:c/", "a", "b:c/", "./b:c/","a/b:c",true, false},
96 # else /* ! FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE */
97 {"c:", "c:", "", "c:", "c:", false, true},
98 {"c:/", "c:", "", "c:", "c:", true, true},
99 {"c://", "c:", "", "c:", "c:", true, true},
100 {"c:/d", "c:", "d", "d", "c:/d", false, true},
101 {"c://d", "c:", "d", "d", "c://d",false, true},
102 {"c:/d/", "c:", "d/", "d/", "c:/d", true, true},
103 {"c:/d/f", "c:/d", "f", "f", "c:/d/f",false, true},
104 {"c:d", "c:", "d", "d", "c:d", false, true},
105 {"c:d/", "c:", "d/", "d/", "c:d", true, true},
106 {"c:d/f", "c:d", "f", "f", "c:d/f",false, true},
107 {"a:b:c", "a:", "b:c", "./b:c","a:b:c",false, true},
108 {"a/b:c", "a", "b:c", "./b:c","a/b:c",false, false},
109 {"a/b:c/", "a", "b:c/", "./b:c/","a/b:c",true, false},
111 #else /* ! FILE_SYSTEM_ACCEPTS_DRIVE_LETTER_PREFIX */
112 {"c:", ".", "c:", "c:", "c:", false, false},
113 {"c:/", ".", "c:/", "c:/", "c:", true, false},
114 {"c://", ".", "c://", "c:/", "c:", true, false},
115 {"c:/d", "c:", "d", "d", "c:/d", false, false},
116 {"c://d", "c:", "d", "d", "c://d",false, false},
117 {"c:/d/", "c:", "d/", "d/", "c:/d", true, false},
118 {"c:/d/f", "c:/d", "f", "f", "c:/d/f",false, false},
119 {"c:d", ".", "c:d", "c:d", "c:d", false, false},
120 {"c:d/", ".", "c:d/", "c:d/", "c:d", true, false},
121 {"c:d/f", "c:d", "f", "f", "c:d/f",false, false},
122 {"a:b:c", ".", "a:b:c","a:b:c","a:b:c",false, false},
123 {"a/b:c", "a", "b:c", "b:c", "a/b:c",false, false},
124 {"a/b:c/", "a", "b:c/", "b:c/", "a/b:c",true, false},
126 {"1:", ".", "1:", "1:", "1:", false, false},
127 {"1:/", ".", "1:/", "1:/", "1:", true, false},
128 {"/:", "/", ":", ":", "/:", false, true},
129 {"/:/", "/", ":/", ":/", "/:", true, true},
131 {NULL, NULL, NULL, NULL, NULL, false, false}
140 for (t = tests; t->name; t++)
142 char *dir = dir_name (t->name);
143 int dirlen = dir_len (t->name);
144 char *last = last_component (t->name);
145 char *base = base_name (t->name);
146 int baselen = base_len (base);
147 char *stripped = strdup (t->name);
148 bool modified = strip_trailing_slashes (stripped);
149 bool absolute = IS_ABSOLUTE_FILE_NAME (t->name);
150 if (! (strcmp (dir, t->dir) == 0
151 && (dirlen == strlen (dir)
152 || (dirlen + 1 == strlen (dir) && dir[dirlen] == '.'))))
155 printf ("dir_name `%s': got `%s' len %d, expected `%s' len %ld\n",
156 t->name, dir, dirlen,
157 t->dir, (unsigned long) strlen (t->dir));
159 if (strcmp (last, t->last))
162 printf ("last_component `%s': got `%s', expected `%s'\n",
163 t->name, last, t->last);
165 if (! (strcmp (base, t->base) == 0
166 && (baselen == strlen (base)
167 || (baselen + 1 == strlen (base)
168 && ISSLASH (base[baselen])))))
171 printf ("base_name `%s': got `%s' len %d, expected `%s' len %ld\n",
172 t->name, base, baselen,
173 t->base, (unsigned long) strlen (t->base));
175 if (strcmp (stripped, t->stripped) || modified != t->modified)
178 printf ("strip_trailing_slashes `%s': got %s %s, expected %s %s\n",
179 t->name, stripped, modified ? "changed" : "unchanged",
180 t->stripped, t->modified ? "changed" : "unchanged");
182 if (t->absolute != absolute)
185 printf ("`%s': got %s, expected %s\n", t->name,
186 absolute ? "absolute" : "relative",
187 t->absolute ? "absolute" : "relative");