]> sourceware.org Git - glibc.git/blob - posix/wordexp-test.c
Update.
[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 void
93 command_line_test (const char *words)
94 {
95 wordexp_t we;
96 int i;
97 int retval = wordexp (words, &we, 0);
98 printf ("wordexp returned %d\n", retval);
99 for (i = 0; i < we.we_wordc; i++)
100 printf ("we_wordv[%d] = \"%s\"\n", i, we.we_wordv[i]);
101 }
102
103 int
104 main (int argc, char *argv[])
105 {
106 struct passwd *pw;
107 int test;
108 int fail = 0;
109
110 setenv ("IFS", IFS, 1);
111 for (test = 0; test_case[test].retval != -1; test++)
112 if (testit (&test_case[test]))
113 ++fail;
114
115 if (argc > 1)
116 {
117 command_line_test (argv[1]);
118 return 0;
119 }
120
121 pw = getpwnam ("root");
122 if (pw != NULL)
123 {
124 struct test_case_struct ts;
125
126 ts.retval = 0;
127 ts.env = NULL;
128 ts.words = "~root";
129 ts.flags = 0;
130 ts.wordc = 1;
131 ts.wordv[0] = pw->pw_dir;
132
133 if (testit (&ts))
134 ++fail;
135 }
136
137 return fail != 0;
138 }
139
140
141 static int
142 testit (struct test_case_struct *tc)
143 {
144 static int test;
145 int retval;
146 wordexp_t we;
147 int bzzzt = 0;
148 int i;
149
150 if (tc->env)
151 setenv ("var", tc->env, 1);
152 else
153 unsetenv ("var");
154
155 printf ("Test %d: ", ++test);
156 retval = wordexp (tc->words, &we, tc->flags);
157
158 if (retval != tc->retval || we.we_wordc != tc->wordc)
159 bzzzt = 1;
160 else
161 for (i = 0; i < we.we_wordc; ++i)
162 if (strcmp (tc->wordv[i], we.we_wordv[i]) != 0)
163 {
164 bzzzt = 1;
165 break;
166 }
167
168 if (bzzzt)
169 {
170 printf ("FAILED\n");
171 printf ("Test words: <%s>, need retval %d, wordc %d\n",
172 tc->words, tc->retval, tc->wordc);
173 printf ("Got retval %d, wordc %d: ", retval, we.we_wordc);
174 for (i = 0; i < we.we_wordc; ++i)
175 printf ("<%s> ", we.we_wordv[i]);
176 printf ("\n");
177 }
178 else
179 printf ("OK\n");
180
181 wordfree (&we);
182
183 return bzzzt;
184 }
This page took 0.048908 seconds and 5 git commands to generate.