Avoid gcc warnings.
[gnulib.git] / tests / test-dirname.c
1 /* Test the gnulib dirname module.
2    Copyright (C) 2005, 2006, 2007 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 2, or (at your option)
7    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 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.  */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include "dirname.h"
23
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 const char *program_name = "test-dirname";
30
31 struct test {
32   const char *name;     /* Name under test.  */
33   const char *dir;      /* dir_name (name).  */
34   const char *last;     /* last_component (name).  */
35   const char *base;     /* base_name (name).  */
36   const char *stripped; /* name after strip_trailing_slashes (name).  */
37   bool modified;        /* result of strip_trailing_slashes (name).  */
38   bool absolute;        /* IS_ABSOLUTE_FILE_NAME (name).  */
39 };
40
41 static struct test tests[] = {
42   {"d/f",       "d",    "f",    "f",    "d/f",  false,  false},
43   {"/d/f",      "/d",   "f",    "f",    "/d/f", false,  true},
44   {"d/f/",      "d",    "f/",   "f/",   "d/f",  true,   false},
45   {"d/f//",     "d",    "f//",  "f/",   "d/f",  true,   false},
46   {"f",         ".",    "f",    "f",    "f",    false,  false},
47   {"/",         "/",    "",     "/",    "/",    false,  true},
48 #if DOUBLE_SLASH_IS_DISTINCT_ROOT
49   {"//",        "//",   "",     "//",   "//",   false,  true},
50   {"//d",       "//",   "d",    "d",    "//d",  false,  true},
51 #else
52   {"//",        "/",    "",     "/",    "/",    true,   true},
53   {"//d",       "/",    "d",    "d",    "//d",  false,  true},
54 #endif
55   {"///",       "/",    "",     "/",    "/",    true,   true},
56   {"///a///",   "/",    "a///", "a/",   "///a", true,   true},
57   /* POSIX requires dirname("") and basename("") to both return ".",
58      but dir_name and base_name are defined differently.  */
59   {"",          ".",    "",     "",     "",     false,  false},
60   {".",         ".",    ".",    ".",    ".",    false,  false},
61   {"..",        ".",    "..",   "..",   "..",   false,  false},
62 #if FILE_SYSTEM_BACKSLASH_IS_FILE_NAME_SEPARATOR
63   {"a\\",       ".",    "a\\",  "a\\",  "a",    true,   false},
64   {"a\\b",      "a",    "b",    "b",    "a\\b", false,  false},
65   {"\\",        "\\",   "",     "\\",   "\\",   false,  true},
66   {"\\/\\",     "\\",   "",     "\\",   "\\",   true,   true},
67   {"\\\\/",     "\\",   "",     "\\",   "\\",   true,   true},
68   {"\\//",      "\\",   "",     "\\",   "\\",   true,   true},
69   {"//\\",      "/",    "",     "/",    "/",    true,   true},
70 #else
71   {"a\\",       ".",    "a\\",  "a\\",  "a\\",  false,  false},
72   {"a\\b",      ".",    "a\\b", "a\\b", "a\\b", false,  false},
73   {"\\",        ".",    "\\",   "\\",   "\\",   false,  false},
74   {"\\/\\",     "\\",   "\\",   "\\",   "\\/\\",false,  false},
75   {"\\\\/",     ".",    "\\\\/","\\\\/","\\\\", true,   false},
76   {"\\//",      ".",    "\\//", "\\/",  "\\",   true,   false},
77 # if DOUBLE_SLASH_IS_DISTINCT_ROOT
78   {"//\\",      "//",   "\\",   "\\",   "//\\", false,  true},
79 # else
80   {"//\\",      "/",    "\\",   "\\",   "//\\", false,  true},
81 # endif
82 #endif
83 #if FILE_SYSTEM_ACCEPTS_DRIVE_LETTER_PREFIX
84 # if FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
85   {"c:",        "c:",   "",     "c:",   "c:",   false,  false},
86   {"c:/",       "c:/",  "",     "c:/",  "c:/",  false,  true},
87   {"c://",      "c:/",  "",     "c:/",  "c:/",  true,   true},
88   {"c:/d",      "c:/",  "d",    "d",    "c:/d", false,  true},
89   {"c://d",     "c:/",  "d",    "d",    "c://d",false,  true},
90   {"c:/d/",     "c:/",  "d/",   "d/",   "c:/d", true,   true},
91   {"c:/d/f",    "c:/d", "f",    "f",    "c:/d/f",false, true},
92   {"c:d",       "c:.",  "d",    "d",    "c:d",  false,  false},
93   {"c:d/",      "c:.",  "d/",   "d/",   "c:d",  true,   false},
94   {"c:d/f",     "c:d",  "f",    "f",    "c:d/f",false,  false},
95   {"a:b:c",     "a:.",  "b:c",  "./b:c","a:b:c",false,  false},
96   {"a/b:c",     "a",    "b:c",  "./b:c","a/b:c",false,  false},
97   {"a/b:c/",    "a",    "b:c/", "./b:c/","a/b:c",true,  false},
98 # else /* ! FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE */
99   {"c:",        "c:",   "",     "c:",   "c:",   false,  true},
100   {"c:/",       "c:",   "",     "c:",   "c:",   true,   true},
101   {"c://",      "c:",   "",     "c:",   "c:",   true,   true},
102   {"c:/d",      "c:",   "d",    "d",    "c:/d", 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   {"c:d",       "c:",   "d",    "d",    "c:d",  false,  true},
107   {"c:d/",      "c:",   "d/",   "d/",   "c:d",  true,   true},
108   {"c:d/f",     "c:d",  "f",    "f",    "c:d/f",false,  true},
109   {"a:b:c",     "a:",   "b:c",  "./b:c","a:b:c",false,  true},
110   {"a/b:c",     "a",    "b:c",  "./b:c","a/b:c",false,  false},
111   {"a/b:c/",    "a",    "b:c/", "./b:c/","a/b:c",true,  false},
112 # endif
113 #else /* ! FILE_SYSTEM_ACCEPTS_DRIVE_LETTER_PREFIX */
114   {"c:",        ".",    "c:",   "c:",   "c:",   false,  false},
115   {"c:/",       ".",    "c:/",  "c:/",  "c:",   true,   false},
116   {"c://",      ".",    "c://", "c:/",  "c:",   true,   false},
117   {"c:/d",      "c:",   "d",    "d",    "c:/d", false,  false},
118   {"c://d",     "c:",   "d",    "d",    "c://d",false,  false},
119   {"c:/d/",     "c:",   "d/",   "d/",   "c:/d", true,   false},
120   {"c:/d/f",    "c:/d", "f",    "f",    "c:/d/f",false, false},
121   {"c:d",       ".",    "c:d",  "c:d",  "c:d",  false,  false},
122   {"c:d/",      ".",    "c:d/", "c:d/", "c:d",  true,   false},
123   {"c:d/f",     "c:d",  "f",    "f",    "c:d/f",false,  false},
124   {"a:b:c",     ".",    "a:b:c","a:b:c","a:b:c",false,  false},
125   {"a/b:c",     "a",    "b:c",  "b:c",  "a/b:c",false,  false},
126   {"a/b:c/",    "a",    "b:c/", "b:c/", "a/b:c",true,   false},
127 #endif
128   {"1:",        ".",    "1:",   "1:",   "1:",   false,  false},
129   {"1:/",       ".",    "1:/",  "1:/",  "1:",   true,   false},
130   {"/:",        "/",    ":",    ":",    "/:",   false,  true},
131   {"/:/",       "/",    ":/",   ":/",   "/:",   true,   true},
132   /* End sentinel.  */
133   {NULL,        NULL,   NULL,   NULL,   NULL,   false,  false}
134 };
135
136 int
137 main ()
138 {
139   struct test *t;
140   bool ok = true;
141
142   for (t = tests; t->name; t++)
143     {
144       char *dir = dir_name (t->name);
145       int dirlen = dir_len (t->name);
146       char *last = last_component (t->name);
147       char *base = base_name (t->name);
148       int baselen = base_len (base);
149       char *stripped = strdup (t->name);
150       bool modified = strip_trailing_slashes (stripped);
151       bool absolute = IS_ABSOLUTE_FILE_NAME (t->name);
152       if (! (strcmp (dir, t->dir) == 0
153              && (dirlen == strlen (dir)
154                  || (dirlen + 1 == strlen (dir) && dir[dirlen] == '.'))))
155         {
156           ok = false;
157           printf ("dir_name `%s': got `%s' len %d, expected `%s' len %ld\n",
158                   t->name, dir, dirlen,
159                   t->dir, (unsigned long) strlen (t->dir));
160         }
161       if (strcmp (last, t->last))
162         {
163           ok = false;
164           printf ("last_component `%s': got `%s', expected `%s'\n",
165                   t->name, last, t->last);
166         }
167       if (! (strcmp (base, t->base) == 0
168              && (baselen == strlen (base)
169                  || (baselen + 1 == strlen (base)
170                      && ISSLASH (base[baselen])))))
171         {
172           ok = false;
173           printf ("base_name `%s': got `%s' len %d, expected `%s' len %ld\n",
174                   t->name, base, baselen,
175                   t->base, (unsigned long) strlen (t->base));
176         }
177       if (strcmp (stripped, t->stripped) || modified != t->modified)
178         {
179           ok = false;
180           printf ("strip_trailing_slashes `%s': got %s %s, expected %s %s\n",
181                   t->name, stripped, modified ? "changed" : "unchanged",
182                   t->stripped, t->modified ? "changed" : "unchanged");
183         }
184       if (t->absolute != absolute)
185         {
186           ok = false;
187           printf ("`%s': got %s, expected %s\n", t->name,
188                   absolute ? "absolute" : "relative",
189                   t->absolute ? "absolute" : "relative");
190         }
191       free (dir);
192       free (base);
193       free (stripped);
194     }
195   return ok ? 0 : 1;
196 }