]> git.ipfire.org Git - thirdparty/glibc.git/blob - posix/wordexp-test.c
Update.
[thirdparty/glibc.git] / posix / wordexp-test.c
1 /* Copyright (C) 1997, 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 The GNU C Library 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 GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
18
19 #include <pwd.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <wordexp.h>
23
24 #define IFS ", \n\t"
25
26 struct test_case_struct
27 {
28 int retval;
29 const char *env;
30 const char *words;
31 int flags;
32 int wordc;
33 const char *wordv[10];
34 } test_case[] =
35 {
36 /* Simple word-splitting */
37 { 0, NULL, "one", 0, 1, { "one", } },
38 { 0, NULL, "one two", 0, 2, { "one", "two", } },
39 { 0, NULL, "one two three", 0, 3, { "one", "two", "three", } },
40
41 /* Simple parameter expansion */
42 { 0, "foo", "${var}", 0, 1, { "foo", } },
43 { 0, "foo", "$var", 0, 1, { "foo", } },
44
45 /* Simple quote removal */
46 { 0, NULL, "\"quoted\"", 0, 1, { "quoted", } },
47 { 0, "foo", "\"$var\"\"$var\"", 0, 1, { "foofoo", } },
48 { 0, NULL, "'singly-quoted'", 0, 1, { "singly-quoted", } },
49
50 /* Simple command substitution */
51 { 0, NULL, "$(echo hello)", 0, 1, { "hello", } },
52 { 0, NULL, "$( (echo hello) )", 0, 1, { "hello", } },
53
54 /* Simple arithmetic expansion */
55 { 0, NULL, "$((1 + 1))", 0, 1, { "2", } },
56 { 0, NULL, "$((2-3))", 0, 1, { "-1", } },
57 { 0, NULL, "$((-1))", 0, 1, { "-1", } },
58
59 /* Field splitting */
60 { 0, NULL, " \tfoo\t\tbar ", 0, 2, { "foo", "bar", } },
61 { 0, NULL, " red , white blue", 0, 3, { "red", "white", "blue", } },
62
63 /* Advanced parameter expansion */
64 { 0, NULL, "${var:-bar}", 0, 1, { "bar", } },
65 { 0, NULL, "${var-bar}", 0, 1, { "bar", } },
66 { 0, "", "${var:-bar}", 0, 1, { "bar", } },
67 { 0, "foo", "${var:-bar}", 0, 1, { "foo", } },
68 { 0, "", "${var-bar}", 0, 0, { NULL, } },
69 { 0, NULL, "${var:=bar}", 0, 1, { "bar", } },
70 { 0, NULL, "${var=bar}", 0, 1, { "bar", } },
71 { 0, "", "${var:=bar}", 0, 1, { "bar", } },
72 { 0, "foo", "${var:=bar}", 0, 1, { "foo", } },
73 { 0, "", "${var=bar}", 0, 0, { NULL, } },
74 { 0, "foo", "${var:?bar}", 0, 1, { "foo", } },
75 { 0, NULL, "${var:+bar}", 0, 0, { NULL, } },
76 { 0, NULL, "${var+bar}", 0, 0, { NULL, } },
77 { 0, "", "${var:+bar}", 0, 0, { NULL, } },
78 { 0, "foo", "${var:+bar}", 0, 1, { "bar", } },
79 { 0, "", "${var+bar}", 0, 1, { "bar", } },
80 { 0, "12345", "${#var}", 0, 1, { "5", } },
81
82 { 0, "banana", "${var%na*}", 0, 1, { "bana", } },
83 { 0, "banana", "${var%%na*}", 0, 1, { "ba", } },
84 { 0, "borabora-island", "${var#*bora}", 0, 1, { "bora-island", } },
85 { 0, "borabora-island", "${var##*bora}", 0, 1, {"-island", } },
86
87 { -1, NULL, NULL, 0, 0, { NULL, } },
88 };
89
90 static int testit (struct test_case_struct *tc);
91
92 int
93 main (int argc, char * argv[])
94 {
95 struct passwd *pw;
96 int test;
97 int fail = 0;
98
99 setenv ("IFS", IFS, 1);
100 for (test = 0; test_case[test].retval != -1; test++)
101 if (testit (&test_case[test]))
102 ++fail;
103
104 pw = getpwnam ("root");
105 if (pw != NULL)
106 {
107 struct test_case_struct ts;
108
109 ts.retval = 0;
110 ts.env = NULL;
111 ts.words = "~root";
112 ts.flags = 0;
113 ts.wordc = 1;
114 ts.wordv[0] = pw->pw_dir;
115
116 if (testit (&ts))
117 ++fail;
118 }
119
120 return fail != 0;
121 }
122
123
124 static int
125 testit (struct test_case_struct *tc)
126 {
127 static int test;
128 int retval;
129 wordexp_t we;
130 int bzzzt = 0;
131 int i;
132
133 if (tc->env)
134 setenv ("var", tc->env, 1);
135 else
136 unsetenv ("var");
137
138 printf ("Test %d: ", ++test);
139 retval = wordexp (tc->words, &we, tc->flags);
140
141 if (retval != tc->retval || we.we_wordc != tc->wordc)
142 bzzzt = 1;
143 else
144 for (i = 0; i < we.we_wordc; ++i)
145 if (strcmp (tc->wordv[i], we.we_wordv[i]) != 0)
146 {
147 bzzzt = 1;
148 break;
149 }
150
151 if (bzzzt)
152 {
153 printf ("FAILED\n");
154 printf ("Test words: <%s>, need retval %d, wordc %d\n",
155 tc->words, tc->retval, tc->wordc);
156 printf ("Got retval %d, wordc %d: ", retval, we.we_wordc);
157 for (i = 0; i < we.we_wordc; ++i)
158 printf ("<%s> ", we.we_wordv[i]);
159 printf ("\n");
160 }
161 else
162 printf ("OK\n");
163
164 wordfree (&we);
165
166 return bzzzt;
167 }