Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
0.braceexp.cpp
Go to the documentation of this file.
1
28#include <rw_braceexp.h>
29#include <rw_environ.h> // for rw_putenv()
30
31#include <stdio.h> // for fprintf(), size_t, stderr
32#include <stdlib.h> // for free()
33#include <string.h> // for strcmp()
34
35// the number of failed tests
36static int nerrors;
37
38static void
39test (int line, const char* brace_expr, size_t n, const char* expect,
40 const char* fname,
41 char* (fn)(const char*, size_t, char*, size_t, char))
42{
43 char buf [128];
44
45 char* result = fn (brace_expr, n, buf, sizeof (buf), ' ');
46
47 const bool equal = (expect && result)
48 ? !strcmp (expect, result)
49 : expect == result;
50
51 if (!equal) {
52
53 ++nerrors;
54
55 fprintf (stderr,
56 "%d. %s(\"%s\", ...) failed. "
57 "expected \"%s\" got \"%s\"\n",
58 line, fname, brace_expr,
59 expect ? expect : "(null)",
60 result ? result : "(null)");
61 }
62
63 if (result != buf)
64 free (result);
65}
66
67
68#define DO_TEST(s,e,f) test (__LINE__, s, strlen (s), e, #f, f)
69#define TEST_COMMON(fn) run_tests (#fn, fn)
70
71static void
72run_tests (const char* fname,
73 char* (fn)(const char*, size_t, char*, size_t, char))
74{
75#undef TEST
76#define TEST(s,e) test (__LINE__, s, strlen (s), e, fname, fn)
77
78 // run our tests
79 TEST ("", "");
80
81 // test plain and escaped whitespace
82 TEST ("\\ ", " ");
83 TEST ("\\ \\ ", " ");
84 TEST ("\\ \\\t\\ ", " \t ");
85 TEST ("a\\ b", "a b");
86 TEST ("a b", "a b");
87
88 TEST ("a", "a");
89 TEST ("a\\b", "ab");
90
91 TEST ("{", 0);
92 TEST ("}", "}");
93 TEST ("{}", "");
94
95 TEST ("}{", 0);
96 TEST ("}{}", "}");
97 TEST ("}{}{", 0);
98 TEST ("{{", 0);
99 TEST ("}}", "}}");
100
101 TEST ("{0}" , "0");
102 TEST ("\\{0}", "{0}");
103 TEST ("{\\0}", "0");
104 TEST ("{0\\}", 0);
105
106 TEST ("{\\{}", "{");
107 TEST ("{\\}}", "}");
108
109 TEST ("a{0}", "a0");
110 TEST ("a{0}b", "a0b");
111 TEST ("a\\{0\\}b", "a{0}b");
112 TEST ("a\\{0,123,4\\}b", "a{0,123,4}b");
113
114 // extension: bash sequence expansion
115 TEST ("{0..a}", "0..a");
116 TEST ("{a..0}", "a..0");
117
118 TEST ("{0..0}", "0");
119 TEST ("{0..5}", "0 1 2 3 4 5");
120 TEST ("{4..2}", "4 3 2");
121 TEST ("{+5..6}", "5 6");
122 TEST ("{6..+7}", "6 7");
123 TEST ("{+7..+8}", "7 8");
124 TEST ("{11..21}", "11 12 13 14 15 16 17 18 19 20 21");
125 TEST ("{0001..0002}", "1 2");
126 TEST ("{-0001..0002}", "-1 0 1 2");
127
128 TEST ("{-3..-1}", "-3 -2 -1");
129 TEST ("{+3..-2}", "3 2 1 0 -1 -2");
130 TEST ("{-3..+2}", "-3 -2 -1 0 1 2");
131
132 TEST ("{a..g}", "a b c d e f g");
133 TEST ("{g..c}", "g f e d c");
134 TEST ("{A..G}", "A B C D E F G");
135 TEST ("{G..C}", "G F E D C");
136 TEST ("{AB..CD}", "AB..CD");
137
138 TEST ("{0..1}", "0 1");
139 TEST ("\\{0..1}", "{0..1}");
140 TEST ("{\\0..1}", "0..1");
141 TEST ("{0\\..1}", "0..1");
142 TEST ("{0.\\.1}", "0..1");
143 TEST ("{0..\\1}", "0..1");
144 TEST ("{0..1\\}", 0);
145
146 // list expansion
147 TEST ("{,}", "");
148 TEST ("{,", 0);
149 TEST ("a{,}", "a a");
150 TEST ("b{,", 0);
151 TEST ("{c,}", "c");
152 TEST ("{d,", 0);
153 TEST ("{,e}", "e"); // for 100% compatibility this should be " e"
154 TEST ("{,f", 0);
155 TEST ("{,}g", "g g");
156 TEST ("a{,}b", "ab ab");
157
158 TEST ("{,,}", "");
159 TEST ("a{,,}", "a a a");
160 TEST ("{b,,}", "b");
161 TEST ("{,c,}", "c");
162 TEST ("{,,d}", "d");
163 TEST ("{,,}e", "e e e");
164 TEST ("a{,,}b", "ab ab ab");
165
166 TEST ("{abc,def}", "abc def");
167 TEST ("{ab\\c,d\\ef}", "abc def");
168 TEST ("abc{d,e,f}", "abcd abce abcf");
169
170 TEST ("z{c,a{d..f}a,c}z", "zcz zadaz zaeaz zafaz zcz");
171 TEST ("z{c,a{d,e,f}a,c}z", "zcz zadaz zaeaz zafaz zcz");
172
173 TEST ("{abc,{,d,e,f,}}", "abc d e f");
174 TEST ("{abc,{,d,e,f,}}{x,y}", "abcx abcy x y dx dy ex ey fx fy x y");
175 TEST ("{abc,{,d\\,e\\,f,}}", "abc d,e,f");
176
177 // list expansion with embedded whitespace
178 TEST ("a{b\\ ,c}", "ab ac");
179 TEST ("a{b\\ \\ ,c\\ }", "ab ac ");
180
181 // series of list and sequence expansions
182 TEST ("A{0..3}", "A0 A1 A2 A3");
183 TEST ("A{0..2}{6..7}", "A06 A07 A16 A17 A26 A27");
184 TEST ("A{A}{0..3}", "AA0 AA1 AA2 AA3");
185 TEST ("A{0..3}{A}", "A0A A1A A2A A3A");
186 TEST ("{A,a}{I,i}{X,x}", "AIX AIx AiX Aix aIX aIx aiX aix");
187
188 // list expansion with nested sequence expansions
189 TEST ("A{0,{4..7}{,}}", "A0 A4 A4 A5 A5 A6 A6 A7 A7");
190 TEST ("a{1,3,x{5..9}y}b", "a1b a3b ax5yb ax6yb ax7yb ax8yb ax9yb");
191
192 // make absolutely sure that we don't treat ${ as an open brace.
193 // we must expand its contents if appropriate.
194 TEST ("a${b}c", "a${b}c");
195 TEST ("a{${b},c,d}e", "a${b}e ace ade");
196 TEST ("a{b,${c},d}e", "abe a${c}e ade");
197 TEST ("a{b,${c{1,2}},d}e", "abe a${c1}e a${c2}e ade");
198
199 // csh specific behavior, lists with single items are valid and
200 // expanded
201 TEST ("{s}", "s");
202 TEST ("{{s}}", "s");
203 TEST ("{{{s}}", 0);
204 TEST ("{s,t}", "s t");
205 TEST ("{{s,t}}", "s t");
206 TEST ("{{{{s,t}}}}", "s t");
207
208 // extension: bash sequence expansion with csh behavior
209 TEST ("{{-10..10}}",
210 "-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10");
211 TEST ("{{{-10..10}}}",
212 "-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10");
213
214 // escaping nested braces
215 TEST ("\\{{{-3..3}}", "{-3 {-2 {-1 {0 {1 {2 {3");
216 TEST ("{\\{{-3..3}}", "{-3 {-2 {-1 {0 {1 {2 {3");
217 TEST ("{{\\{-3..3}}", "{-3..3");
218
219 TEST ("{{{-3..3\\}}}}", "-3..3}");
220 TEST ("{{{-3..3}\\}}}", "-3} -2} -1} 0} 1} 2} 3}");
221 TEST ("{{{-3..3}}\\}}", "-3} -2} -1} 0} 1} 2} 3}");
222 TEST ("{{{-3..3}}}\\}", "-3} -2} -1} 0} 1} 2} 3}");
223
224
225 // lifted from the bash-3.2 testsuite and modified to be consistent
226 // with expectations of the csh shell and our extensions.
227
228 TEST ("ff{c,b,a}", "ffc ffb ffa");
229 TEST ("f{d,e,f}g", "fdg feg ffg");
230 TEST ("{l,n,m}xyz", "lxyz nxyz mxyz");
231 TEST ("{abc\\,def}", "abc,def");
232 TEST ("{abc}", "abc");
233
234 TEST ("\\{a,b,c,d,e}", "{a,b,c,d,e}");
235 TEST ("{x,y,\\{a,b,c}}", "x} y} {a} b} c}");
236 TEST ("{x\\,y,\\{abc\\},trie}", "x,y {abc} trie");
237
238 TEST ("/usr/{ucb/{ex,edit},lib/{ex,how_ex}}",
239 "/usr/ucb/ex /usr/ucb/edit /usr/lib/ex /usr/lib/how_ex");
240
241 TEST ("{}", "");
242 TEST ("}", "}");
243 TEST ("{", 0);
244
245 TEST ("abcd{efgh", 0);
246
247 TEST ("{1..10}", "1 2 3 4 5 6 7 8 9 10");
248
249 TEST ("{{0..10},braces}", "0 1 2 3 4 5 6 7 8 9 10 braces");
250 TEST ("x{{0..10},braces}y",
251 "x0y x1y x2y x3y x4y x5y x6y x7y x8y x9y x10y xbracesy");
252
253 TEST ("{3..3}", "3");
254 TEST ("x{3..3}y", "x3y");
255 TEST ("{10..1}", "10 9 8 7 6 5 4 3 2 1");
256 TEST ("{10..1}y", "10y 9y 8y 7y 6y 5y 4y 3y 2y 1y");
257 TEST ("x{10..1}y", "x10y x9y x8y x7y x6y x5y x4y x3y x2y x1y");
258
259 TEST ("{a..f}", "a b c d e f");
260 TEST ("{f..a}", "f e d c b a");
261 TEST ("{f..f}", "f");
262
263 // mixes are incorrectly-formed brace expansions
264 TEST ("{1..f}", "1..f");
265 TEST ("{f..1}", "f..1");
266
267 // do negative numbers work?
268 TEST ("{-1..-10}", "-1 -2 -3 -4 -5 -6 -7 -8 -9 -10");
269 TEST ("{-20..0}",
270 "-20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 "
271 "-9 -8 -7 -6 -5 -4 -3 -2 -1 0");
272
273 TEST ("a-{b{d,e}}-c", "a-bd-c a-be-c");
274 TEST ("a-{bdef-{g,i}-c", 0);
275}
276
277static void
278run_brace_expand_tests ()
279{
280#undef TEST
281#define TEST(s,e) DO_TEST (s,e,rw_brace_expand)
282
284
285 // rw_brace_expand does not do whitespace collapse.
286 TEST ("a {1,2} b", "a 1 b a 2 b");
287 TEST ("a\t\t{1,2}\t\tb", "a\t\t1\t\tb a\t\t2\t\tb");
288
289 // test whitespace
290 TEST (" ", " ");
291 TEST (" ", " ");
292 TEST (" \t", " \t");
293 TEST ("a b", "a b");
294 TEST (" a b ", " a b ");
295 TEST (" a{b,c}", " ab ac");
296 TEST ("a {b,c}", "a b a c");
297 TEST ("a{ b,c}", "a b ac");
298 TEST ("a{b ,c}", "ab ac");
299 TEST ("a{b, c}", "ab a c");
300 TEST ("a{b,c }", "ab ac ");
301 TEST ("a{b,c} ", "ab ac ");
302
303 TEST ("{ }", " ");
304 TEST ("{{ }}", " ");
305 TEST ("{{ }", 0); // brace mismatch
306}
307
308static void
309run_shell_expand_tests ()
310{
311#undef TEST
312#define TEST(s,e) DO_TEST (s,e,rw_shell_expand)
313
315
316 // rw_shell_expand does whitespace collapse
317 TEST ("a {1,2} b", "a 1 2 b");
318 TEST ("a\t\t{1,2}\t\tb", "a 1 2 b");
319
320 // test whitespace
321 TEST (" ", "");
322 TEST (" ", "");
323 TEST (" \t", "");
324 TEST ("a b", "a b");
325 TEST (" a b ", "a b");
326 TEST (" a{b,c}", "ab ac");
327 TEST ("a {b,c}", "a b c");
328 TEST ("a{ b,c}", 0);
329 TEST ("a{b ,c}", 0);
330 TEST ("a{b, c}", 0);
331 TEST ("a{b,c }", 0);
332 TEST ("a{b,c} ", "ab ac");
333
334 TEST ("{ }", 0); // brace mismatch
335 TEST ("{{ }}", 0); // brace mismatch
336 TEST ("{{ }", 0); // brace mismatch
337
338#if 0 // not implemented yet
339
340 // set the three variables
341 rw_putenv ("var=baz:varx=vx:vary=vy");
342
343 TEST ("foo{bar,${var}.}", "foobar foobaz.");
344 TEST ("foo{bar,${var}}", "foobar foobaz");
345
346 TEST ("${var}\"{x,y}", "bazx bazy");
347 TEST ("$var{x,y}", "vx vy");
348 TEST ("${var}{x,y}", "bazx bazy");
349
350 // unset all three variables
351 rw_putenv ("var=:varx=:vary=");
352
353#endif // 0
354}
355
356int main ()
357{
358 run_brace_expand_tests ();
359
360 run_shell_expand_tests ();
361
362 // return 0 on success, 1 on failure
363 return !(0 == nerrors);
364}
#define TEST_COMMON(fn)
#define TEST(s, e)
int main()
#define line
Definition 17.names.cpp:490
#define buf
Definition 17.names.cpp:452
#define n
Definition 17.names.cpp:130
volatile int result
bool equal(const T &x, const U &y)
void run_tests(ForwardIterator dummy_iter, const T *, const char *predname)
#define stderr
_TEST_EXPORT char * rw_shell_expand(const char *shell_expr, _RWSTD_SIZE_T sz, char *s, _RWSTD_SIZE_T n, char sep)
_TEST_EXPORT char * rw_brace_expand(const char *brace_expr, _RWSTD_SIZE_T sz, char *s, _RWSTD_SIZE_T n, char sep)
_TEST_EXPORT int rw_putenv(const char *, int=-1)
Definition environ.cpp:69