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 3 of the License, or
7 (at your option) any later version.
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
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 const char *program_name = "test-dirname";
29 const char *name; /* Name under test. */
30 const char *dir; /* dir_name (name). */
31 const char *last; /* last_component (name). */
32 const char *base; /* base_name (name). */
33 const char *stripped; /* name after strip_trailing_slashes (name). */
34 bool modified; /* result of strip_trailing_slashes (name). */
35 bool absolute; /* IS_ABSOLUTE_FILE_NAME (name). */
38 static struct test tests[] = {
39 {"d/f", "d", "f", "f", "d/f", false, false},
40 {"/d/f", "/d", "f", "f", "/d/f", false, true},
41 {"d/f/", "d", "f/", "f/", "d/f", true, false},
42 {"d/f//", "d", "f//", "f/", "d/f", true, false},
43 {"f", ".", "f", "f", "f", false, false},
44 {"/", "/", "", "/", "/", false, true},
45 #if DOUBLE_SLASH_IS_DISTINCT_ROOT
46 {"//", "//", "", "//", "//", false, true},
47 {"//d", "//", "d", "d", "//d", false, true},
49 {"//", "/", "", "/", "/", true, true},
50 {"//d", "/", "d", "d", "//d", false, true},
52 {"///", "/", "", "/", "/", true, true},
53 {"///a///", "/", "a///", "a/", "///a", true, true},
54 /* POSIX requires dirname("") and basename("") to both return ".",
55 but dir_name and base_name are defined differently. */
56 {"", ".", "", "", "", false, false},
57 {".", ".", ".", ".", ".", false, false},
58 {"..", ".", "..", "..", "..", false, false},
59 #if FILE_SYSTEM_BACKSLASH_IS_FILE_NAME_SEPARATOR
60 {"a\\", ".", "a\\", "a\\", "a", true, false},
61 {"a\\b", "a", "b", "b", "a\\b", false, false},
62 {"\\", "\\", "", "\\", "\\", false, true},
63 {"\\/\\", "\\", "", "\\", "\\", true, true},
64 {"\\\\/", "\\", "", "\\", "\\", true, true},
65 {"\\//", "\\", "", "\\", "\\", true, true},
66 {"//\\", "/", "", "/", "/", true, true},
68 {"a\\", ".", "a\\", "a\\", "a\\", false, false},
69 {"a\\b", ".", "a\\b", "a\\b", "a\\b", false, false},
70 {"\\", ".", "\\", "\\", "\\", false, false},
71 {"\\/\\", "\\", "\\", "\\", "\\/\\",false, false},
72 {"\\\\/", ".", "\\\\/","\\\\/","\\\\", true, false},
73 {"\\//", ".", "\\//", "\\/", "\\", true, false},
74 # if DOUBLE_SLASH_IS_DISTINCT_ROOT
75 {"//\\", "//", "\\", "\\", "//\\", false, true},
77 {"//\\", "/", "\\", "\\", "//\\", false, true},
80 #if FILE_SYSTEM_ACCEPTS_DRIVE_LETTER_PREFIX
81 # if FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
82 {"c:", "c:", "", "c:", "c:", false, false},
83 {"c:/", "c:/", "", "c:/", "c:/", false, true},
84 {"c://", "c:/", "", "c:/", "c:/", true, true},
85 {"c:/d", "c:/", "d", "d", "c:/d", false, true},
86 {"c://d", "c:/", "d", "d", "c://d",false, true},
87 {"c:/d/", "c:/", "d/", "d/", "c:/d", true, true},
88 {"c:/d/f", "c:/d", "f", "f", "c:/d/f",false, true},
89 {"c:d", "c:.", "d", "d", "c:d", false, false},
90 {"c:d/", "c:.", "d/", "d/", "c:d", true, false},
91 {"c:d/f", "c:d", "f", "f", "c:d/f",false, false},
92 {"a:b:c", "a:.", "b:c", "./b:c","a:b:c",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",true, false},
95 # else /* ! FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE */
96 {"c:", "c:", "", "c:", "c:", false, true},
97 {"c:/", "c:", "", "c:", "c:", true, true},
98 {"c://", "c:", "", "c:", "c:", true, true},
99 {"c:/d", "c:", "d", "d", "c:/d", false, true},
100 {"c://d", "c:", "d", "d", "c://d",false, true},
101 {"c:/d/", "c:", "d/", "d/", "c:/d", true, true},
102 {"c:/d/f", "c:/d", "f", "f", "c:/d/f",false, true},
103 {"c:d", "c:", "d", "d", "c:d", false, true},
104 {"c:d/", "c:", "d/", "d/", "c:d", true, true},
105 {"c:d/f", "c:d", "f", "f", "c:d/f",false, true},
106 {"a:b:c", "a:", "b:c", "./b:c","a:b:c",false, true},
107 {"a/b:c", "a", "b:c", "./b:c","a/b:c",false, false},
108 {"a/b:c/", "a", "b:c/", "./b:c/","a/b:c",true, false},
110 #else /* ! FILE_SYSTEM_ACCEPTS_DRIVE_LETTER_PREFIX */
111 {"c:", ".", "c:", "c:", "c:", false, false},
112 {"c:/", ".", "c:/", "c:/", "c:", true, false},
113 {"c://", ".", "c://", "c:/", "c:", true, false},
114 {"c:/d", "c:", "d", "d", "c:/d", false, false},
115 {"c://d", "c:", "d", "d", "c://d",false, false},
116 {"c:/d/", "c:", "d/", "d/", "c:/d", true, false},
117 {"c:/d/f", "c:/d", "f", "f", "c:/d/f",false, false},
118 {"c:d", ".", "c:d", "c:d", "c:d", false, false},
119 {"c:d/", ".", "c:d/", "c:d/", "c:d", true, false},
120 {"c:d/f", "c:d", "f", "f", "c:d/f",false, false},
121 {"a:b:c", ".", "a:b:c","a:b:c","a:b:c",false, false},
122 {"a/b:c", "a", "b:c", "b:c", "a/b:c",false, false},
123 {"a/b:c/", "a", "b:c/", "b:c/", "a/b:c",true, false},
125 {"1:", ".", "1:", "1:", "1:", false, false},
126 {"1:/", ".", "1:/", "1:/", "1:", true, false},
127 {"/:", "/", ":", ":", "/:", false, true},
128 {"/:/", "/", ":/", ":/", "/:", true, true},
130 {NULL, NULL, NULL, NULL, NULL, false, false}
139 for (t = tests; t->name; t++)
141 char *dir = dir_name (t->name);
142 int dirlen = dir_len (t->name);
143 char *last = last_component (t->name);
144 char *base = base_name (t->name);
145 int baselen = base_len (base);
146 char *stripped = strdup (t->name);
147 bool modified = strip_trailing_slashes (stripped);
148 bool absolute = IS_ABSOLUTE_FILE_NAME (t->name);
149 if (! (strcmp (dir, t->dir) == 0
150 && (dirlen == strlen (dir)
151 || (dirlen + 1 == strlen (dir) && dir[dirlen] == '.'))))
154 printf ("dir_name `%s': got `%s' len %d, expected `%s' len %ld\n",
155 t->name, dir, dirlen,
156 t->dir, (unsigned long) strlen (t->dir));
158 if (strcmp (last, t->last))
161 printf ("last_component `%s': got `%s', expected `%s'\n",
162 t->name, last, t->last);
164 if (! (strcmp (base, t->base) == 0
165 && (baselen == strlen (base)
166 || (baselen + 1 == strlen (base)
167 && ISSLASH (base[baselen])))))
170 printf ("base_name `%s': got `%s' len %d, expected `%s' len %ld\n",
171 t->name, base, baselen,
172 t->base, (unsigned long) strlen (t->base));
174 if (strcmp (stripped, t->stripped) || modified != t->modified)
177 printf ("strip_trailing_slashes `%s': got %s %s, expected %s %s\n",
178 t->name, stripped, modified ? "changed" : "unchanged",
179 t->stripped, t->modified ? "changed" : "unchanged");
181 if (t->absolute != absolute)
184 printf ("`%s': got %s, expected %s\n", t->name,
185 absolute ? "absolute" : "relative",
186 t->absolute ? "absolute" : "relative");