* lib/sys_time_.h: Use a recursion-safe inclusion guard rather than
[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 #include <config.h>
19
20 #include "dirname.h"
21
22 #include <stdbool.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 const char *program_name = "test-dirname";
28
29 struct test {
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).  */
37 };
38
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},
49 #else
50   {"//",        "/",    "",     "/",    "/",    true,   true},
51   {"//d",       "/",    "d",    "d",    "//d",  false,  true},
52 #endif
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},
68 #else
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},
77 # else
78   {"//\\",      "/",    "\\",   "\\",   "//\\", false,  true},
79 # endif
80 #endif
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},
110 # endif
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},
125 #endif
126   {"1:",        ".",    "1:",   "1:",   "1:",   false,  false},
127   {"1:/",       ".",    "1:/",  "1:/",  "1:",   true,   false},
128   {"/:",        "/",    ":",    ":",    "/:",   false,  true},
129   {"/:/",       "/",    ":/",   ":/",   "/:",   true,   true},
130   /* End sentinel.  */
131   {NULL,        NULL,   NULL,   NULL,   NULL,   false,  false}
132 };
133
134 int
135 main ()
136 {
137   struct test *t;
138   bool ok = true;
139
140   for (t = tests; t->name; t++)
141     {
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] == '.'))))
153         {
154           ok = false;
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));
158         }
159       if (strcmp (last, t->last))
160         {
161           ok = false;
162           printf ("last_component `%s': got `%s', expected `%s'\n",
163                   t->name, last, t->last);
164         }
165       if (! (strcmp (base, t->base) == 0
166              && (baselen == strlen (base)
167                  || (baselen + 1 == strlen (base)
168                      && ISSLASH (base[baselen])))))
169         {
170           ok = false;
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));
174         }
175       if (strcmp (stripped, t->stripped) || modified != t->modified)
176         {
177           ok = false;
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");
181         }
182       if (t->absolute != absolute)
183         {
184           ok = false;
185           printf ("`%s': got %s, expected %s\n", t->name,
186                   absolute ? "absolute" : "relative",
187                   t->absolute ? "absolute" : "relative");
188         }
189       free (dir);
190       free (base);
191       free (stripped);
192     }
193   return ok ? 0 : 1;
194 }