1 /* t-stringhelp.c - Regression tests for stringhelp.c
2 * Copyright (C) 2007 Free Software Foundation, Inc.
4 * This file is part of JNLIB.
6 * JNLIB is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 3 of
9 * the License, or (at your option) any later version.
11 * JNLIB is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
29 #include <sys/types.h>
31 #include "stringhelp.h"
33 #include "t-support.h"
36 static char *home_buffer
;
44 char *home
= getenv("HOME");
46 #if defined(HAVE_GETPWUID) && defined(HAVE_PWD_H)
48 home_buffer
= xstrdup (home
);
53 pwd
= getpwuid (getuid());
55 home_buffer
= xstrdup (pwd
->pw_dir
);
64 test_percent_escape (void)
75 { NULL
, "%%", "%25%25" },
76 { NULL
, " %", " %25" },
78 { NULL
, " :", " %3a" },
79 { NULL
, ": ", "%3a " },
80 { NULL
, " : ", " %3a " },
81 { NULL
, "::", "%3a%3a" },
82 { NULL
, ": :", "%3a %3a" },
83 { NULL
, "%:", "%25%3a" },
84 { NULL
, ":%", "%3a%25" },
85 { "\\\n:", ":%", "%3a%25" },
86 { "\\\n:", "\\:%", "%5c%3a%25" },
87 { "\\\n:", "\n:%", "%0a%3a%25" },
88 { "\\\n:", "\xff:%", "\xff%3a%25" },
89 { "\\\n:", "\xfe:%", "\xfe%3a%25" },
90 { "\\\n:", "\x01:%", "\x01%3a%25" },
91 { "\x01", "\x01:%", "%01%3a%25" },
92 { "\xfe", "\xfe:%", "%fe%3a%25" },
93 { "\xfe", "\xff:%", "\xff%3a%25" },
99 result
= percent_escape (NULL
, NULL
);
102 for (testno
=0; tests
[testno
].value
; testno
++)
104 result
= percent_escape (tests
[testno
].value
, tests
[testno
].extra
);
107 if (strcmp (result
, tests
[testno
].expected
))
116 test_compare_filenames (void)
131 #ifdef HAVE_W32_SYSTEM
134 { "foo/bar", "foo\\bar", 0 },
135 { "foo\\bar", "foo/bar", 0 },
136 { "foo\\", "foo/", 0 },
137 { "foo/", "foo\\", 0 },
138 #endif /*HAVE_W32_SYSTEM*/
143 for (testno
=0; tests
[testno
].a
; testno
++)
145 result
= compare_filenames (tests
[testno
].a
, tests
[testno
].b
);
146 result
= result
< 0? -1 : result
> 0? 1 : 0;
147 if (result
!= tests
[testno
].result
)
154 test_strconcat (void)
158 out
= strconcat ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
159 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
160 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
161 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
162 "1", "2", "3", "4", "5", "6", "7", NULL
);
167 out
= strconcat ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
168 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
169 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
170 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
171 "1", "2", "3", "4", "5", "6", "7", "8", NULL
);
174 else if (errno
!= EINVAL
)
177 out
= strconcat ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
178 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
179 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
180 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
181 "1", "2", "3", "4", "5", "6", "7", "8", "9", NULL
);
184 else if (errno
!= EINVAL
)
187 #if __GNUC__ < 4 /* gcc 4.0 has a sentinel attribute. */
188 out
= strconcat (NULL
);
192 out
= strconcat (NULL
, NULL
);
195 out
= strconcat ("", NULL
);
200 out
= strconcat ("", "", NULL
);
205 out
= strconcat ("a", "b", NULL
);
206 if (!out
|| strcmp (out
, "ab"))
209 out
= strconcat ("a", "b", "c", NULL
);
210 if (!out
|| strcmp (out
, "abc"))
214 out
= strconcat ("a", "b", "cc", NULL
);
215 if (!out
|| strcmp (out
, "abcc"))
218 out
= strconcat ("a1", "b1", "c1", NULL
);
219 if (!out
|| strcmp (out
, "a1b1c1"))
223 out
= strconcat ("", " long b ", "", "--even-longer--", NULL
);
224 if (!out
|| strcmp (out
, " long b --even-longer--"))
228 out
= strconcat ("", " long b ", "", "--even-longer--", NULL
);
229 if (!out
|| strcmp (out
, " long b --even-longer--"))
235 test_xstrconcat (void)
239 out
= xstrconcat ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
240 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
241 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
242 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
243 "1", "2", "3", "4", "5", "6", "7", NULL
);
247 #if __GNUC__ < 4 /* gcc 4.0 has a sentinel attribute. */
248 out
= xstrconcat (NULL
);
252 out
= xstrconcat (NULL
, NULL
);
255 out
= xstrconcat ("", NULL
);
260 out
= xstrconcat ("", "", NULL
);
265 out
= xstrconcat ("a", "b", NULL
);
266 if (!out
|| strcmp (out
, "ab"))
269 out
= xstrconcat ("a", "b", "c", NULL
);
270 if (!out
|| strcmp (out
, "abc"))
274 out
= xstrconcat ("a", "b", "cc", NULL
);
275 if (!out
|| strcmp (out
, "abcc"))
278 out
= xstrconcat ("a1", "b1", "c1", NULL
);
279 if (!out
|| strcmp (out
, "a1b1c1"))
283 out
= xstrconcat ("", " long b ", "", "--even-longer--", NULL
);
284 if (!out
|| strcmp (out
, " long b --even-longer--"))
288 out
= xstrconcat ("", " long b ", "", "--even-longer--", NULL
);
289 if (!out
|| strcmp (out
, " long b --even-longer--"))
296 test_make_filename_try (void)
299 const char *home
= gethome ();
300 size_t homelen
= home
? strlen (home
):0;
302 out
= make_filename_try ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
303 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
304 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
305 "1", "2", "3", NULL
);
308 else if (errno
!= EINVAL
)
311 out
= make_filename_try ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
312 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
313 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
314 "1", "2", "3", "4", NULL
);
317 else if (errno
!= EINVAL
)
321 out
= make_filename_try ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
322 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
323 "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
325 if (!out
|| strcmp (out
,
326 "1/2/3/4/5/6/7/8/9/10/"
327 "1/2/3/4/5/6/7/8/9/10/"
328 "1/2/3/4/5/6/7/8/9/10/"
333 out
= make_filename_try ("foo", "~/bar", "baz/cde", NULL
);
334 if (!out
|| strcmp (out
, "foo/~/bar/baz/cde"))
338 out
= make_filename_try ("foo", "~/bar", "baz/cde/", NULL
);
339 if (!out
|| strcmp (out
, "foo/~/bar/baz/cde/"))
343 out
= make_filename_try ("/foo", "~/bar", "baz/cde/", NULL
);
344 if (!out
|| strcmp (out
, "/foo/~/bar/baz/cde/"))
348 out
= make_filename_try ("//foo", "~/bar", "baz/cde/", NULL
);
349 if (!out
|| strcmp (out
, "//foo/~/bar/baz/cde/"))
353 out
= make_filename_try ("", "~/bar", "baz/cde", NULL
);
354 if (!out
|| strcmp (out
, "/~/bar/baz/cde"))
359 out
= make_filename_try ("~/foo", "bar", NULL
);
364 if (strlen (out
) < homelen
+ 7)
366 if (strncmp (out
, home
, homelen
))
368 if (strcmp (out
+homelen
, "/foo/bar"))
373 if (strcmp (out
, "~/foo/bar"))
378 out
= make_filename_try ("~", "bar", NULL
);
383 if (strlen (out
) < homelen
+ 3)
385 if (strncmp (out
, home
, homelen
))
387 if (strcmp (out
+homelen
, "/bar"))
392 if (strcmp (out
, "~/bar"))
400 main (int argc
, char **argv
)
405 test_percent_escape ();
406 test_compare_filenames ();
409 test_make_filename_try ();