root/maint/gnulib/tests/test-dirname.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /* Test the gnulib dirname module.
   2    Copyright (C) 2005-2007, 2009-2021 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 <https://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 struct test {
  27   const char *name;     /* Name under test.  */
  28   const char *dir;      /* dir_name (name).  */
  29   const char *last;     /* last_component (name).  */
  30   const char *base;     /* base_name (name).  */
  31   const char *stripped; /* name after strip_trailing_slashes (name).  */
  32   bool modified;        /* result of strip_trailing_slashes (name).  */
  33   bool absolute;        /* IS_ABSOLUTE_FILE_NAME (name).  */
  34 };
  35 
  36 static struct test tests[] = {
  37   {"d/f",       "d",    "f",    "f",    "d/f",  false,  false},
  38   {"/d/f",      "/d",   "f",    "f",    "/d/f", false,  true},
  39   {"d/f/",      "d",    "f/",   "f/",   "d/f",  true,   false},
  40   {"d/f//",     "d",    "f//",  "f/",   "d/f",  true,   false},
  41   {"f",         ".",    "f",    "f",    "f",    false,  false},
  42   {"/",         "/",    "",     "/",    "/",    false,  true},
  43 #if DOUBLE_SLASH_IS_DISTINCT_ROOT
  44   {"//",        "//",   "",     "//",   "//",   false,  true},
  45   {"//d",       "//",   "d",    "d",    "//d",  false,  true},
  46 #else
  47   {"//",        "/",    "",     "/",    "/",    true,   true},
  48   {"//d",       "/",    "d",    "d",    "//d",  false,  true},
  49 #endif
  50   {"///",       "/",    "",     "/",    "/",    true,   true},
  51   {"///a///",   "/",    "a///", "a/",   "///a", true,   true},
  52   /* POSIX requires dirname("") and basename("") to both return ".",
  53      but dir_name and base_name are defined differently.  */
  54   {"",          ".",    "",     "",     "",     false,  false},
  55   {".",         ".",    ".",    ".",    ".",    false,  false},
  56   {"..",        ".",    "..",   "..",   "..",   false,  false},
  57 #if ISSLASH ('\\')
  58   {"a\\",       ".",    "a\\",  "a\\",  "a",    true,   false},
  59   {"a\\b",      "a",    "b",    "b",    "a\\b", false,  false},
  60   {"\\",        "\\",   "",     "\\",   "\\",   false,  true},
  61   {"\\/\\",     "\\",   "",     "\\",   "\\",   true,   true},
  62   {"\\\\/",     "\\",   "",     "\\",   "\\",   true,   true},
  63   {"\\//",      "\\",   "",     "\\",   "\\",   true,   true},
  64   {"//\\",      "/",    "",     "/",    "/",    true,   true},
  65 #else
  66   {"a\\",       ".",    "a\\",  "a\\",  "a\\",  false,  false},
  67   {"a\\b",      ".",    "a\\b", "a\\b", "a\\b", false,  false},
  68   {"\\",        ".",    "\\",   "\\",   "\\",   false,  false},
  69   {"\\/\\",     "\\",   "\\",   "\\",   "\\/\\",false,  false},
  70   {"\\\\/",     ".",    "\\\\/","\\\\/","\\\\", true,   false},
  71   {"\\//",      ".",    "\\//", "\\/",  "\\",   true,   false},
  72 # if DOUBLE_SLASH_IS_DISTINCT_ROOT
  73   {"//\\",      "//",   "\\",   "\\",   "//\\", false,  true},
  74 # else
  75   {"//\\",      "/",    "\\",   "\\",   "//\\", false,  true},
  76 # endif
  77 #endif
  78 #if ISSLASH ('\\')
  79 # if FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE
  80   {"c:",        "c:",   "",     "c:",   "c:",   false,  false},
  81   {"c:/",       "c:/",  "",     "c:/",  "c:/",  false,  true},
  82   {"c://",      "c:/",  "",     "c:/",  "c:/",  true,   true},
  83   {"c:/d",      "c:/",  "d",    "d",    "c:/d", false,  true},
  84   {"c://d",     "c:/",  "d",    "d",    "c://d",false,  true},
  85   {"c:/d/",     "c:/",  "d/",   "d/",   "c:/d", true,   true},
  86   {"c:/d/f",    "c:/d", "f",    "f",    "c:/d/f",false, true},
  87   {"c:d",       "c:.",  "d",    "d",    "c:d",  false,  false},
  88   {"c:d/",      "c:.",  "d/",   "d/",   "c:d",  true,   false},
  89   {"c:d/f",     "c:d",  "f",    "f",    "c:d/f",false,  false},
  90   {"a:b:c",     "a:.",  "b:c",  "./b:c","a:b:c",false,  false},
  91   {"a/b:c",     "a",    "b:c",  "./b:c","a/b:c",false,  false},
  92   {"a/b:c/",    "a",    "b:c/", "./b:c/","a/b:c",true,  false},
  93 # else /* ! FILE_SYSTEM_DRIVE_PREFIX_CAN_BE_RELATIVE */
  94   {"c:",        "c:",   "",     "c:",   "c:",   false,  true},
  95   {"c:/",       "c:",   "",     "c:",   "c:",   true,   true},
  96   {"c://",      "c:",   "",     "c:",   "c:",   true,   true},
  97   {"c:/d",      "c:",   "d",    "d",    "c:/d", false,  true},
  98   {"c://d",     "c:",   "d",    "d",    "c://d",false,  true},
  99   {"c:/d/",     "c:",   "d/",   "d/",   "c:/d", true,   true},
 100   {"c:/d/f",    "c:/d", "f",    "f",    "c:/d/f",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   {"a:b:c",     "a:",   "b:c",  "./b:c","a:b:c",false,  true},
 105   {"a/b:c",     "a",    "b:c",  "./b:c","a/b:c",false,  false},
 106   {"a/b:c/",    "a",    "b:c/", "./b:c/","a/b:c",true,  false},
 107 # endif
 108 #else /* ! ISSLASH ('\\') */
 109   {"c:",        ".",    "c:",   "c:",   "c:",   false,  false},
 110   {"c:/",       ".",    "c:/",  "c:/",  "c:",   true,   false},
 111   {"c://",      ".",    "c://", "c:/",  "c:",   true,   false},
 112   {"c:/d",      "c:",   "d",    "d",    "c:/d", false,  false},
 113   {"c://d",     "c:",   "d",    "d",    "c://d",false,  false},
 114   {"c:/d/",     "c:",   "d/",   "d/",   "c:/d", true,   false},
 115   {"c:/d/f",    "c:/d", "f",    "f",    "c:/d/f",false, false},
 116   {"c:d",       ".",    "c:d",  "c:d",  "c:d",  false,  false},
 117   {"c:d/",      ".",    "c:d/", "c:d/", "c:d",  true,   false},
 118   {"c:d/f",     "c:d",  "f",    "f",    "c:d/f",false,  false},
 119   {"a:b:c",     ".",    "a:b:c","a:b:c","a:b:c",false,  false},
 120   {"a/b:c",     "a",    "b:c",  "b:c",  "a/b:c",false,  false},
 121   {"a/b:c/",    "a",    "b:c/", "b:c/", "a/b:c",true,   false},
 122 #endif
 123   {"1:",        ".",    "1:",   "1:",   "1:",   false,  false},
 124   {"1:/",       ".",    "1:/",  "1:/",  "1:",   true,   false},
 125   {"/:",        "/",    ":",    ":",    "/:",   false,  true},
 126   {"/:/",       "/",    ":/",   ":/",   "/:",   true,   true},
 127   /* End sentinel.  */
 128   {NULL,        NULL,   NULL,   NULL,   NULL,   false,  false}
 129 };
 130 
 131 int
 132 main (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 133 {
 134   struct test *t;
 135   bool ok = true;
 136 
 137   for (t = tests; t->name; t++)
 138     {
 139       char *dir = dir_name (t->name);
 140       int dirlen = dir_len (t->name);
 141       char *last = last_component (t->name);
 142       char *base = base_name (t->name);
 143       int baselen = base_len (base);
 144       char *stripped = strdup (t->name);
 145       bool modified = strip_trailing_slashes (stripped);
 146       bool absolute = IS_ABSOLUTE_FILE_NAME (t->name);
 147       if (! (strcmp (dir, t->dir) == 0
 148              && (dirlen == strlen (dir)
 149                  || (dirlen + 1 == strlen (dir) && dir[dirlen] == '.'))))
 150         {
 151           ok = false;
 152           printf ("dir_name '%s': got '%s' len %d,"
 153                   " expected '%s' len %lu\n",
 154                   t->name, dir, dirlen,
 155                   t->dir, (unsigned long) strlen (t->dir));
 156         }
 157       if (strcmp (last, t->last))
 158         {
 159           ok = false;
 160           printf ("last_component '%s': got '%s', expected '%s'\n",
 161                   t->name, last, t->last);
 162         }
 163       if (! (strcmp (base, t->base) == 0
 164              && (baselen == strlen (base)
 165                  || (baselen + 1 == strlen (base)
 166                      && ISSLASH (base[baselen])))))
 167         {
 168           ok = false;
 169           printf ("base_name '%s': got '%s' len %d,"
 170                   " expected '%s' len %lu\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 }

/* [previous][next][first][last][top][bottom][index][help] */