xref: /titanic_50/usr/src/lib/libshell/common/tests/expand.sh (revision bdb9230ac765cb7af3fc1f4119caf2c5720dceb3)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2008 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
8#                                                                      #
9#                A copy of the License is available at                 #
10#            http://www.opensource.org/licenses/cpl1.0.txt             #
11#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12#                                                                      #
13#              Information and Software Systems Research               #
14#                            AT&T Research                             #
15#                           Florham Park NJ                            #
16#                                                                      #
17#                  David Korn <dgk@research.att.com>                   #
18#                                                                      #
19########################################################################
20function err_exit
21{
22	print -u2 -n "\t"
23	print -u2 -r ${Command}[$Line]: "$@"
24	((Errors++))
25}
26
27integer Errors=0
28Command=${0##*/}
29
30# {...} expansion tests -- ignore if not supported
31
32[[ $(print a{0,1}z) == "a0z a1z" ]] || exit 0
33
34integer Line=$LINENO+1
35set -- \
36	'ff{c,b,a}'				'ffc ffb ffa' \
37	'f{d,e,f}g'				'fdg feg ffg' \
38	'{l,n,m}xyz'				'lxyz nxyz mxyz' \
39	'{abc\,def}'				'{abc,def}' \
40	'{"abc,def"}'				'{abc,def}' \
41	"{'abc,def'}"				'{abc,def}' \
42	'{abc}'					'{abc}' \
43	'\{a,b,c,d,e}'				'{a,b,c,d,e}' \
44	'{x,y,\{a,b,c}}'			'x} y} {a} b} c}' \
45	'{x\,y,\{abc\},trie}'			'x,y {abc} trie' \
46	'/usr/{ucb/{ex,edit},lib/{ex,how_ex}}'	'/usr/ucb/ex /usr/ucb/edit /usr/lib/ex /usr/lib/how_ex' \
47	'XXXX\{a,b,c\}'				'XXXX{a,b,c}' \
48	'{}'					'{}' \
49	'{ }'					'{ }' \
50	'}'					'}' \
51	'{'					'{' \
52	'abcd{efgh'				'abcd{efgh' \
53	'foo {1,2} bar'				'foo 1 2 bar' \
54	'`print -r -- foo {1,2} bar`'		'foo 1 2 bar' \
55	'$(print -r -- foo {1,2} bar)'		'foo 1 2 bar' \
56	'{1..10}'				'1 2 3 4 5 6 7 8 9 10' \
57	'{0..10,braces}'			'0..10 braces' \
58	'{{0..10},braces}'			'0 1 2 3 4 5 6 7 8 9 10 braces' \
59	'x{{0..10},braces}y'			'x0y x1y x2y x3y x4y x5y x6y x7y x8y x9y x10y xbracesy' \
60	'{3..3}'				'3' \
61	'x{3..3}y'				'x3y' \
62	'{10..1}'				'10 9 8 7 6 5 4 3 2 1' \
63	'{10..1}y'				'10y 9y 8y 7y 6y 5y 4y 3y 2y 1y' \
64	'x{10..1}y'				'x10y x9y x8y x7y x6y x5y x4y x3y x2y x1y' \
65	'{a..f}'				'a b c d e f' \
66	'{f..a}'				'f e d c b a' \
67	'{a..A}'				'{a..A}' \
68	'{A..a}'				'{A..a}' \
69	'{f..f}'				'f' \
70	'{1..f}'				'{1..f}' \
71	'{f..1}'				'{f..1}' \
72	'0{1..9} {10..20}'			'01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20' \
73	'{-1..-10}'				'-1 -2 -3 -4 -5 -6 -7 -8 -9 -10' \
74	'{-19..0}'				'-19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0' \
75	'{0..10}'				'0 1 2 3 4 5 6 7 8 9 10' \
76	'{0..10..1}'				'0 1 2 3 4 5 6 7 8 9 10' \
77	'{0..10..2}'				'0 2 4 6 8 10' \
78	'{0..10..3}'				'0 3 6 9' \
79	'{0..10..0}'				'{0..10..0}' \
80	'{0..10..-1}'				'0' \
81	'{10..0}'				'10 9 8 7 6 5 4 3 2 1 0' \
82	'{10..0..-1}'				'10 9 8 7 6 5 4 3 2 1 0' \
83	'{10..0..-2}'				'10 8 6 4 2 0' \
84	'{10..0..-3}'				'10 7 4 1' \
85	'{10..0..0}'				'{10..0..0}' \
86	'{10..0..1}'				'10' \
87	'{a..z..2}'				'a c e g i k m o q s u w y' \
88	'{y..b..-3}'				'y v s p m j g d' \
89	'{0..0x1000..0x200}'			'0 512 1024 1536 2048 2560 3072 3584 4096' \
90	'{a,b}{0..2}{z,y}'			'a0z a0y a1z a1y a2z a2y b0z b0y b1z b1y b2z b2y' \
91	'{0..0100..8%03o}'			'000 010 020 030 040 050 060 070 100' \
92	'{0..0100..040%020o}'			'00000000000000000000 00000000000000000040 00000000000000000100' \
93	'{0..7%03..2u}'				'000 001 010 011 100 101 110 111' \
94	'{0..10%llu}'				'{0..10%llu}' \
95	'{0..10%s}'				'{0..10%s}' \
96	'{0..10%dl}'				'{0..10%dl}' \
97	'{a,b}{0..3%02..2u}{y,z}'		'a00y a00z a01y a01z a10y a10z a11y a11z b00y b00z b01y b01z b10y b10z b11y b11z' \
98
99while (($#>1))
100do	((Line++))
101	pattern=$1
102	shift
103	expected=$1
104	shift
105	got=$(eval print -r -- "$pattern")
106	[[ $got == $expected ]] || err_exit "'$pattern' failed -- expected '$expected' got '$got'"
107	#print -r -- "	'$pattern'			'$got' \\"
108done
109
110# ~(N) no expand glob pattern option
111set -- ~(N)/dev/null
112[[ $# == 1 && $1 == /dev/null ]] || err_exit "~(N)/dev/null not matching /dev/null"
113set -- ~(N)/dev/non_existant_file
114[[ $# == 0  ]] || err_exit "~(N)/dev/nonexistant not empty"
115set -- ""~(N)/dev/non_existant_file
116[[ $# == 1  && ! $1 ]] || err_exit '""~(N)/dev/nonexistant not null argument'
117set -- ~(N)/dev/non_existant_file""
118[[ $# == 1  && ! $1 ]] || err_exit '~(N)/dev/nonexistent"" not null argument'
119for i in ~(N)/dev/non_existent_file
120do	err_exit "~(N)/dev/non_existent_file in for loop is $i"
121done
122for i in ""~(N)/dev/non_existent_file
123do	[[ ! $i ]] || err_exit '""~(N)/dev/non_existent_file not null'
124done
125exit $((Errors))
126