tests: avoid several compiler warnings
[gnulib.git] / tests / test-dirname.c
1 /* Test the gnulib dirname module.
2    Copyright (C) 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 #include <config.h>
18
19 #include "dirname.h"
20
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 const char *program_name = "test-dirname";
27
28 struct test {
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).  */
36 };
37
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},
48 #else
49   {"//",        "/",    "",     "/",    "/",    true,   true},
50   {"//d",       "/",    "d",    "d",    "//d",  false,  true},
51 #endif
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},
67 #else
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},
76 # else
77   {"//\\",      "/",    "\\",   "\\",   "//\\", false,  true},
78 # endif
79 #endif
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},
109 # endif
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},
124 #endif
125   {"1:",        ".",    "1:",   "1:",   "1:",   false,  false},
126   {"1:/",       ".",    "1:/",  "1:/",  "1:",   true,   false},
127   {"/:",        "/",    ":",    ":",    "/:",   false,  true},
128   {"/:/",       "/",    ":/",   ":/",   "/:",   true,   true},
129   /* End sentinel.  */
130   {NULL,        NULL,   NULL,   NULL,   NULL,   false,  false}
131 };
132
133 int
134 main (void)
135 {
136   struct test *t;
137   bool ok = true;
138
139   for (t = tests; t->name; t++)
140     {
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] == '.'))))
152         {
153           ok = false;
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));
157         }
158       if (strcmp (last, t->last))
159         {
160           ok = false;
161           printf ("last_component `%s': got `%s', expected `%s'\n",
162                   t->name, last, t->last);
163         }
164       if (! (strcmp (base, t->base) == 0
165              && (baselen == strlen (base)
166                  || (baselen + 1 == strlen (base)
167                      && ISSLASH (base[baselen])))))
168         {
169           ok = false;
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));
173         }
174       if (strcmp (stripped, t->stripped) || modified != t->modified)
175         {
176           ok = false;
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");
180         }
181       if (t->absolute != absolute)
182         {
183           ok = false;
184           printf ("`%s': got %s, expected %s\n", t->name,
185                   absolute ? "absolute" : "relative",
186                   t->absolute ? "absolute" : "relative");
187         }
188       free (dir);
189       free (base);
190       free (stripped);
191     }
192   return ok ? 0 : 1;
193 }