sd: remove 'ssd' driver support
[unleashed/tickless.git] / usr / src / lib / libshell / common / tests / attributes.sh
blobda6c2292d05fa5cae9a9e4c366691d420008ec3e
1 ########################################################################
2 # #
3 # This software is part of the ast package #
4 # Copyright (c) 1982-2010 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 ########################################################################
20 function err_exit
22 print -u2 -n "\t"
23 print -u2 -r ${Command}[$1]: "${@:2}"
24 let Errors+=1
26 alias err_exit='err_exit $LINENO'
28 Command=${0##*/}
29 integer Errors=0
31 tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
32 trap "cd /; rm -rf $tmp" EXIT
34 r=readonly u=Uppercase l=Lowercase i=22 i8=10 L=abc L5=def uL5=abcdef xi=20
35 x=export t=tagged H=hostname LZ5=026 RZ5=026 Z5=123 lR5=ABcdef R5=def n=l
36 for option in u l i i8 L L5 LZ5 RZ5 Z5 r x H t R5 uL5 lR5 xi n
37 do typeset -$option $option
38 done
39 (r=newval) 2> /dev/null && err_exit readonly attribute fails
40 i=i+5
41 if ((i != 27))
42 then err_exit integer attributes fails
44 if [[ $i8 != 8#12 ]]
45 then err_exit integer base 8 fails
47 if [[ $u != UPPERCASE ]]
48 then err_exit uppercase fails
50 if [[ $l != lowercase ]]
51 then err_exit lowercase fails
53 if [[ $n != lowercase ]]
54 then err_exit reference variables fail
56 if [[ t=tagged != $(typeset -t) ]]
57 then err_exit tagged fails
59 if [[ t != $(typeset +t) ]]
60 then err_exit tagged fails
62 if [[ $Z5 != 00123 ]]
63 then err_exit zerofill fails
65 if [[ $RZ5 != 00026 ]]
66 then err_exit right zerofill fails
68 L=12345
69 if [[ $L != 123 ]]
70 then err_exit leftjust fails
72 if [[ $L5 != "def " ]]
73 then err_exit leftjust fails
75 if [[ $uL5 != ABCDE ]]
76 then err_exit leftjust uppercase fails
78 if [[ $lR5 != bcdef ]]
79 then err_exit rightjust fails
81 if [[ $R5 != " def" ]]
82 then err_exit rightjust fails
84 if [[ $($SHELL -c 'echo $x') != export ]]
85 then err_exit export fails
87 if [[ $($SHELL -c 'xi=xi+4;echo $xi') != 24 ]]
88 then err_exit export attributes fails
90 x=$(foo=abc $SHELL <<!
91 foo=bar
92 $SHELL -c 'print \$foo'
95 if [[ $x != bar ]]
96 then err_exit 'environment variables require re-export'
98 (typeset + ) > /dev/null 2>&1 || err_exit 'typeset + not working'
99 (typeset -L-5 buf="A" 2>/dev/null)
100 if [[ $? == 0 ]]
101 then err_exit 'typeset allows negative field for left/right adjust'
104 readonly $a=foo
105 if [[ $b != foo ]]
106 then err_exit 'readonly $a=b not working'
108 if [[ $(export | grep '^PATH=') != PATH=* ]]
109 then err_exit 'export not working'
111 picture=(
112 bitmap=/fruit
113 size=(typeset -E x=2.5)
115 string="$(print $picture)"
116 if [[ "${string}" != *'size=( typeset -E'* ]]
117 then err_exit 'print of compound exponential variable not working'
119 sz=(typeset -E y=2.2)
120 string="$(print $sz)"
121 if [[ "${sz}" == *'typeset -E -F'* ]]
122 then err_exit 'print of exponential shows both -E and -F attributes'
124 print 'typeset -i m=48/4+1;print -- $m' > $tmp/script
125 chmod +x $tmp/script
126 typeset -Z2 m
127 if [[ $($tmp/script) != 13 ]]
128 then err_exit 'attributes not cleared for script execution'
130 print 'print VAR=$VAR' > $tmp/script
131 typeset -L70 VAR=var
132 $tmp/script > $tmp/script.1
133 [[ $(< $tmp/script.1) == VAR= ]] || err_exit 'typeset -L should not be inherited'
134 typeset -Z LAST=00
135 unset -f foo
136 function foo
138 if [[ $1 ]]
139 then LAST=$1
140 else ((LAST++))
143 foo 1
144 if (( ${#LAST} != 2 ))
145 then err_exit 'LAST!=2'
148 if (( ${#LAST} != 2 ))
149 then err_exit 'LAST!=2'
151 [[ $(set | grep LAST) == LAST=02 ]] || err_exit "LAST not correct in set list"
152 set -a
153 unset foo
154 foo=bar
155 if [[ $(export | grep ^foo=) != 'foo=bar' ]]
156 then err_exit 'all export not working'
158 unset foo
159 read foo <<!
162 if [[ $(export | grep ^foo=) != 'foo=bar' ]]
163 then err_exit 'all export not working with read'
165 if [[ $(typeset | grep PS2) == PS2 ]]
166 then err_exit 'typeset without arguments outputs names without attributes'
168 unset a z q x
169 w1=hello
170 w2=world
171 t1="$w1 $w2"
172 if (( 'a' == 97 ))
173 then b1=aGVsbG8gd29ybGQ=
174 b2=aGVsbG8gd29ybGRoZWxsbyB3b3JsZA==
175 else b1=iIWTk5ZAppaZk4Q=
176 b2=iIWTk5ZAppaZk4SIhZOTlkCmlpmThA==
178 z=$b1
179 typeset -b x=$b1
180 [[ $x == "$z" ]] || print -u2 'binary variable not expanding correctly'
181 [[ $(printf "%B" x) == $t1 ]] || err_exit 'typeset -b not working'
182 typeset -b -Z5 a=$b1
183 [[ $(printf "%B" a) == $w1 ]] || err_exit 'typeset -b -Z5 not working'
184 typeset -b q=$x$x
185 [[ $q == $b2 ]] || err_exit 'typeset -b not working with concatination'
186 [[ $(printf "%B" q) == $t1$t1 ]] || err_exit 'typeset -b concatination not working'
187 x+=$b1
188 [[ $x == $b2 ]] || err_exit 'typeset -b not working with append'
189 [[ $(printf "%B" x) == $t1$t1 ]] || err_exit 'typeset -b append not working'
190 typeset -b -Z20 z=$b1
191 (( $(printf "%B" z | wc -c) == 20 )) || err_exit 'typeset -b -Z20 not storing 20 bytes'
193 typeset -b v1 v2
194 read -N11 v1
195 read -N22 v2
196 } << !
197 hello worldhello worldhello world
199 [[ $v1 == "$b1" ]] || err_exit "v1=$v1 should be $b1"
200 [[ $v2 == "$x" ]] || err_exit "v1=$v2 should be $x"
201 if env '!=1' >/dev/null 2>&1
202 then [[ $(env '!=1' $SHELL -c 'echo ok' 2>/dev/null) == ok ]] || err_exit 'malformed environment terminates shell'
204 unset var
205 typeset -b var
206 printf '12%Z34' | read -r -N 5 var
207 [[ $var == MTIAMzQ= ]] || err_exit 'binary files with zeros not working'
208 unset var
209 if command typeset -usi var=0xfffff 2> /dev/null
210 then (( $var == 0xffff )) || err_exit 'unsigned short integers not working'
211 else err_exit 'typeset -usi cannot be used for unsigned short'
213 [[ $($SHELL -c 'unset foo;typeset -Z2 foo; print ${foo:-3}' 2> /dev/null) == 3 ]] || err_exit '${foo:-3} not 3 when typeset -Z2 field undefined'
214 [[ $($SHELL -c 'unset foo;typeset -Z2 foo; print ${foo:=3}' 2> /dev/null) == 03 ]] || err_exit '${foo:=-3} not 3 when typeset -Z2 foo undefined'
215 unset foo bar
216 unset -f fun
217 function fun
219 export foo=hello
220 typeset -x bar=world
221 [[ $foo == hello ]] || err_exit 'export scoping problem in function'
224 [[ $(export | grep foo) == 'foo=hello' ]] || err_exit 'export not working in functions'
225 [[ $(export | grep bar) ]] && err_exit 'typeset -x not local'
226 [[ $($SHELL -c 'typeset -r IFS=;print -r $(pwd)' 2> /dev/null) == "$(pwd)" ]] || err_exit 'readonly IFS causes command substitution to fail'
227 fred[66]=88
228 [[ $(typeset -pa) == *fred* ]] || err_exit 'typeset -pa not working'
229 unset x y z
230 typeset -LZ3 x=abcd y z=00abcd
231 y=03
232 [[ $y == "3 " ]] || err_exit '-LZ3 not working for value 03'
233 [[ $x == "abc" ]] || err_exit '-LZ3 not working for value abcd'
234 [[ $x == "abc" ]] || err_exit '-LZ3 not working for value 00abcd'
235 unset x z
236 set +a
237 [[ $(typeset -p z) ]] && err_exit "typeset -p for z undefined failed"
238 unset z
239 x='typeset -i z=45'
240 eval "$x"
241 [[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed"
242 [[ $(typeset +p z) == "${x%=*}" ]] || err_exit "typeset +p for '$x' failed"
243 unset z
244 x='typeset -a z=(a b c)'
245 eval "$x"
246 [[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed"
247 [[ $(typeset +p z) == "${x%=*}" ]] || err_exit "typeset +p for '$x' failed"
248 unset z
249 x='typeset -C z=(
250 foo=bar
251 xxx=bam
253 eval "$x"
254 x=${x//$'\t'}
255 x=${x//$'(\n'/'('}
256 x=${x//$'\n'/';'}
257 [[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed"
258 [[ $(typeset +p z) == "${x%%=*}" ]] || err_exit "typeset +p for '$x' failed"
259 unset z
260 x='typeset -A z=([bar]=bam [xyz]=bar)'
261 eval "$x"
262 [[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed"
263 [[ $(typeset +p z) == "${x%%=*}" ]] || err_exit "typeset +p for '$x' failed"
264 unset z
265 foo=abc
266 x='typeset -n z=foo'
267 eval "$x"
268 [[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed"
269 [[ $(typeset +p z) == "${x%%=*}" ]] || err_exit "typeset +p for '$x' failed"
270 typeset +n z
271 unset foo z
272 typeset -T Pt_t=(
273 float x=1 y=2
275 Pt_t z
276 x=${z//$'\t'}
277 x=${x//$'(\n'/'('}
278 x=${x//$'\n'/';'}
279 [[ $(typeset -p z) == "Pt_t z=$x" ]] || err_exit "typeset -p for type failed"
280 [[ $(typeset +p z) == "Pt_t z" ]] || err_exit "typeset +p for type failed"
281 unset z
282 function foo
284 typeset -p bar
286 bar=xxx
287 [[ $(foo) == bar=xxx ]] || err_exit 'typeset -p not working inside a function'
288 unset foo
289 typeset -L5 foo
290 [[ $(typeset -p foo) == 'typeset -L 5 foo' ]] || err_exit 'typeset -p not working for variables with attributes but without a value'
291 { $SHELL <<- EOF
292 typeset -L3 foo=aaa
293 typeset -L6 foo=bbbbbb
294 [[ \$foo == bbbbbb ]]
296 } || err_exit 'typeset -L should not preserve old attributes'
297 { $SHELL <<- EOF
298 typeset -R3 foo=aaa
299 typeset -R6 foo=bbbbbb
300 [[ \$foo == bbbbbb ]]
302 } 2> /dev/null || err_exit 'typeset -R should not preserve old attributes'
304 expected='YWJjZGVmZ2hpag=='
305 unset foo
306 typeset -b -Z10 foo
307 read foo <<< 'abcdefghijklmnop'
308 [[ $foo == "$expected" ]] || err_exit 'read foo, where foo is "typeset -b -Z10" not working'
309 unset foo
310 typeset -b -Z10 foo
311 read -N10 foo <<< 'abcdefghijklmnop'
312 [[ $foo == "$expected" ]] || err_exit 'read -N10 foo, where foo is "typeset -b -Z10" not working'
313 unset foo
314 typeset -b -A foo
315 read -N10 foo[4] <<< 'abcdefghijklmnop'
316 [[ ${foo[4]} == "$expected" ]] || err_exit 'read -N10 foo, where foo is "typeset -b -A" foo not working'
317 unset foo
318 typeset -b -a foo
319 read -N10 foo[4] <<< 'abcdefghijklmnop'
320 [[ ${foo[4]} == "$expected" ]] || err_exit 'read -N10 foo, where foo is "typeset -b -a" foo not working'
321 [[ $(printf %B foo[4]) == abcdefghij ]] || err_exit 'printf %B for binary associative array element not working'
322 [[ $(printf %B foo[4]) == abcdefghij ]] || err_exit 'printf %B for binary indexed array element not working'
323 unset foo
325 $SHELL 2> /dev/null -c 'export foo=(bar=3)' && err_exit 'compound variables cannot be exported'
327 $SHELL -c 'builtin date' >/dev/null 2>&1 &&
330 # check env var changes against a builtin that uses the env var
332 SEC=1234252800
333 ETZ=EST5EDT
334 EDT=03
335 PTZ=PST8PDT
336 PDT=00
338 CMD="date -f%H \\#$SEC"
340 export TZ=$ETZ
342 set -- \
343 "$EDT $PDT $EDT" "" "TZ=$PTZ" "" \
344 "$EDT $PDT $EDT" "" "TZ=$PTZ" "TZ=$ETZ" \
345 "$EDT $PDT $EDT" "TZ=$ETZ" "TZ=$PTZ" "TZ=$ETZ" \
346 "$PDT $EDT $PDT" "TZ=$PTZ" "" "TZ=$PTZ" \
347 "$PDT $EDT $PDT" "TZ=$PTZ" "TZ=$ETZ" "TZ=$PTZ" \
348 "$EDT $PDT $EDT" "foo=bar" "TZ=$PTZ" "TZ=$ETZ" \
350 while (( $# >= 4 ))
351 do exp=$1
352 got=$(print $($SHELL -c "builtin date; $2 $CMD; $3 $CMD; $4 $CMD"))
353 [[ $got == $exp ]] || err_exit "[ '$2' '$3' '$4' ] env sequence failed -- expected '$exp', got '$got'"
354 shift 4
355 done
359 unset v
360 typeset -H v=/dev/null
361 [[ $v == *nul* ]] || err_exit 'typeset -H for /dev/null not working'
363 exit $((Errors))