]>
sourceware.org Git - glibc.git/blob - posix/wordexp-test.c
1 /* Copyright (C) 1997, 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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. */
26 struct test_case_struct
33 const char *wordv
[10];
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", } },
41 /* Simple parameter expansion */
42 { 0, "foo", "${var}", 0, 1, { "foo", } },
43 { 0, "foo", "$var", 0, 1, { "foo", } },
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", } },
50 /* Simple command substitution */
51 { 0, NULL
, "$(echo hello)", 0, 1, { "hello", } },
52 { 0, NULL
, "$( (echo hello) )", 0, 1, { "hello", } },
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", } },
60 { 0, NULL
, " \tfoo\t\tbar ", 0, 2, { "foo", "bar", } },
61 { 0, NULL
, " red , white blue", 0, 3, { "red", "white", "blue", } },
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", } },
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", } },
87 { -1, NULL
, NULL
, 0, 0, { NULL
, } },
90 static int testit (struct test_case_struct
*tc
);
93 command_line_test (const char *words
)
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
]);
104 main (int argc
, char *argv
[])
110 setenv ("IFS", IFS
, 1);
111 for (test
= 0; test_case
[test
].retval
!= -1; test
++)
112 if (testit (&test_case
[test
]))
117 command_line_test (argv
[1]);
121 pw
= getpwnam ("root");
124 struct test_case_struct ts
;
131 ts
.wordv
[0] = pw
->pw_dir
;
142 testit (struct test_case_struct
*tc
)
151 setenv ("var", tc
->env
, 1);
155 printf ("Test %d: ", ++test
);
156 retval
= wordexp (tc
->words
, &we
, tc
->flags
);
158 if (retval
!= tc
->retval
|| we
.we_wordc
!= tc
->wordc
)
161 for (i
= 0; i
< we
.we_wordc
; ++i
)
162 if (strcmp (tc
->wordv
[i
], we
.we_wordv
[i
]) != 0)
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
]);
This page took 0.048908 seconds and 5 git commands to generate.