]>
sourceware.org Git - glibc.git/blob - posix/wordexp-test.c
75cfe5f3602eef2745d727de76080dd485cb8cb6
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 main (int argc
, char * argv
[])
99 setenv ("IFS", IFS
, 1);
100 for (test
= 0; test_case
[test
].retval
!= -1; test
++)
101 if (testit (&test_case
[test
]))
104 pw
= getpwnam ("root");
107 struct test_case_struct ts
;
114 ts
.wordv
[0] = pw
->pw_dir
;
125 testit (struct test_case_struct
*tc
)
134 setenv ("var", tc
->env
, 1);
138 printf ("Test %d: ", ++test
);
139 retval
= wordexp (tc
->words
, &we
, tc
->flags
);
141 if (retval
!= tc
->retval
|| we
.we_wordc
!= tc
->wordc
)
144 for (i
= 0; i
< we
.we_wordc
; ++i
)
145 if (strcmp (tc
->wordv
[i
], we
.we_wordv
[i
]) != 0)
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
]);
This page took 0.04835 seconds and 4 git commands to generate.