@@ -39,6 +39,130 @@ static void normalize_argv_string(const char **var, const char *input)
3939 die ("Bad value: %s\n" , input );
4040}
4141
42+ struct test_data {
43+ const char * from ; /* input: transform from this ... */
44+ const char * to ; /* output: ... to this. */
45+ const char * alternative ; /* output: ... or this. */
46+ };
47+
48+ static int test_function (struct test_data * data , char * (* func )(char * input ),
49+ const char * funcname )
50+ {
51+ int failed = 0 , i ;
52+ char buffer [1024 ];
53+ char * to ;
54+
55+ for (i = 0 ; data [i ].to ; i ++ ) {
56+ if (!data [i ].from )
57+ to = func (NULL );
58+ else {
59+ strcpy (buffer , data [i ].from );
60+ to = func (buffer );
61+ }
62+ if (!strcmp (to , data [i ].to ))
63+ continue ;
64+ if (!data [i ].alternative )
65+ error ("FAIL: %s(%s) => '%s' != '%s'\n" ,
66+ funcname , data [i ].from , to , data [i ].to );
67+ else if (!strcmp (to , data [i ].alternative ))
68+ continue ;
69+ else
70+ error ("FAIL: %s(%s) => '%s' != '%s', '%s'\n" ,
71+ funcname , data [i ].from , to , data [i ].to ,
72+ data [i ].alternative );
73+ failed = 1 ;
74+ }
75+ return failed ;
76+ }
77+
78+ static struct test_data basename_data [] = {
79+ /* --- POSIX type paths --- */
80+ { NULL , "." },
81+ { "" , "." },
82+ { "." , "." },
83+ { ".." , ".." },
84+ { "/" , "/" },
85+ { "//" , "/" , "//" },
86+ { "///" , "/" , "//" },
87+ { "////" , "/" , "//" },
88+ { "usr" , "usr" },
89+ { "/usr" , "usr" },
90+ { "/usr/" , "usr" },
91+ { "/usr//" , "usr" },
92+ { "/usr/lib" , "lib" },
93+ { "usr/lib" , "lib" },
94+ { "usr/lib///" , "lib" },
95+
96+ #if defined(__MINGW32__ ) || defined (_MSC_VER )
97+ /* --- win32 type paths --- */
98+ { "\\usr" , "usr" },
99+ { "\\usr\\" , "usr" },
100+ { "\\usr\\\\" , "usr" },
101+ { "\\usr\\lib" , "lib" },
102+ { "usr\\lib" , "lib" },
103+ { "usr\\lib\\\\\\" , "lib" },
104+ { "C:/usr" , "usr" },
105+ { "C:/usr" , "usr" },
106+ { "C:/usr/" , "usr" },
107+ { "C:/usr//" , "usr" },
108+ { "C:/usr/lib" , "lib" },
109+ { "C:usr/lib" , "lib" },
110+ { "C:usr/lib///" , "lib" },
111+ { "C:" , "." },
112+ { "C:a" , "a" },
113+ { "C:/" , "/" },
114+ { "C:///" , "/" },
115+ { "\\" , "\\" , "/" },
116+ { "\\\\" , "\\" , "/" },
117+ { "\\\\\\" , "\\" , "/" },
118+ #endif
119+ { NULL , NULL }
120+ };
121+
122+ static struct test_data dirname_data [] = {
123+ /* --- POSIX type paths --- */
124+ { NULL , "." },
125+ { "" , "." },
126+ { "." , "." },
127+ { ".." , "." },
128+ { "/" , "/" },
129+ { "//" , "/" , "//" },
130+ { "///" , "/" , "//" },
131+ { "////" , "/" , "//" },
132+ { "usr" , "." },
133+ { "/usr" , "/" },
134+ { "/usr/" , "/" },
135+ { "/usr//" , "/" },
136+ { "/usr/lib" , "/usr" },
137+ { "usr/lib" , "usr" },
138+ { "usr/lib///" , "usr" },
139+
140+ #if defined(__MINGW32__ ) || defined (_MSC_VER )
141+ /* --- win32 type paths --- */
142+ { "\\" , "\\" },
143+ { "\\\\" , "\\\\" },
144+ { "\\usr" , "\\" },
145+ { "\\usr\\" , "\\" },
146+ { "\\usr\\\\" , "\\" },
147+ { "\\usr\\lib" , "\\usr" },
148+ { "usr\\lib" , "usr" },
149+ { "usr\\lib\\\\\\" , "usr" },
150+ { "C:a" , "C:." },
151+ { "C:/" , "C:/" },
152+ { "C:///" , "C:/" },
153+ { "C:/usr" , "C:/" },
154+ { "C:/usr/" , "C:/" },
155+ { "C:/usr//" , "C:/" },
156+ { "C:/usr/lib" , "C:/usr" },
157+ { "C:usr/lib" , "C:usr" },
158+ { "C:usr/lib///" , "C:usr" },
159+ { "\\\\\\" , "\\" },
160+ { "\\\\\\\\" , "\\" },
161+ { "C:" , "C:." , "." },
162+ #endif
163+ { NULL , NULL }
164+ };
165+
42166int main (int argc , char * * argv )
43167{
44168 if (argc == 3 && !strcmp (argv [1 ], "normalize_path_copy" )) {
@@ -133,6 +257,12 @@ int main(int argc, char **argv)
133257 return 0 ;
134258 }
135259
260+ if (argc == 2 && !strcmp (argv [1 ], "basename" ))
261+ return test_function (basename_data , basename , argv [1 ]);
262+
263+ if (argc == 2 && !strcmp (argv [1 ], "dirname" ))
264+ return test_function (dirname_data , dirname , argv [1 ]);
265+
136266 fprintf (stderr , "%s: unknown function name: %s\n" , argv [0 ],
137267 argv [1 ] ? argv [1 ] : "(there was none)" );
138268 return 1 ;
0 commit comments