######################################################################## # # # This software is part of the ast package # # Copyright (c) 1982-2010 AT&T Intellectual Property # # and is licensed under the # # Common Public License, Version 1.0 # # by AT&T Intellectual Property # # # # A copy of the License is available at # # http://www.opensource.org/licenses/cpl1.0.txt # # (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) # # # # Information and Software Systems Research # # AT&T Research # # Florham Park NJ # # # # David Korn # # # ######################################################################## proc err_exit { print -u2 -n "\t" print -u2 -r ${Command}[$1]: ${@:2} let Errors+=1 } alias err_exit='err_exit $LINENO' setvar Command = ${0##*/} integer Errors=0 setvar tmp = $(mktemp -dt) || do { err_exit mktemp -dt failed; exit 1; } trap "cd /; rm -rf $tmp" EXIT setvar r = 'readonly', u = 'Uppercase', l = 'Lowercase', i = '22', i8 = '10', L = 'abc', L5 = 'def', uL5 = 'abcdef', xi = '20' setvar x = 'export', t = 'tagged', H = 'hostname', LZ5 = '026', RZ5 = '026', Z5 = '123', lR5 = 'ABcdef', R5 = 'def', n = 'l' for option in [u l i i8 L L5 LZ5 RZ5 Z5 r x H t R5 uL5 lR5 xi n] { typeset -$option $option } shell {setvar r = 'newval'} 2> /dev/null && err_exit readonly attribute fails setvar i = "i+5" if ((i != 27)) { err_exit integer attributes fails } if [[ $i8 != 8#12 ]] { err_exit integer base 8 fails } if [[ $u != UPPERCASE ]] { err_exit uppercase fails } if [[ $l != lowercase ]] { err_exit lowercase fails } if [[ $n != lowercase ]] { err_exit reference variables fail } if [[ t=tagged != $(typeset -t) ]] { err_exit tagged fails } if [[ t != $(typeset +t) ]] { err_exit tagged fails } if [[ $Z5 != 00123 ]] { err_exit zerofill fails } if [[ $RZ5 != 00026 ]] { err_exit right zerofill fails } setvar L = '12345' if [[ $L != 123 ]] { err_exit leftjust fails } if [[ $L5 != "def " ]] { err_exit leftjust fails } if [[ $uL5 != ABCDE ]] { err_exit leftjust uppercase fails } if [[ $lR5 != bcdef ]] { err_exit rightjust fails } if [[ $R5 != " def" ]] { err_exit rightjust fails } if [[ $($SHELL -c 'echo $x') != export ]] { err_exit export fails } if [[ $($SHELL -c 'xi=xi+4;echo $xi') != 24 ]] { err_exit export attributes fails } setvar x = $(foo=abc $SHELL < /dev/null 2>&1 || err_exit 'typeset + not working' shell {typeset -L-5 buf="A" 2>/dev/null} if [[ $? == 0 ]] { err_exit 'typeset allows negative field for left/right adjust' } setvar a = 'b' readonly $a=foo if [[ $b != foo ]] { err_exit 'readonly $a=b not working' } if [[ $(export | grep '^PATH=') != PATH=* ]] { err_exit 'export not working' } setvar picture = ''( bitmap=/fruit size=(typeset -E x=2.5) ) setvar string = "$(print $picture)" if [[ "${string}" != *'size=( typeset -E'* ]] { err_exit 'print of compound exponential variable not working' } setvar sz = ''(typeset -E y=2.2) setvar string = "$(print $sz)" if [[ "${sz}" == *'typeset -E -F'* ]] { err_exit 'print of exponential shows both -E and -F attributes' } print 'typeset -i m=48/4+1;print -- $m' > $tmp/script chmod +x $tmp/script typeset -Z2 m if [[ $($tmp/script) != 13 ]] { err_exit 'attributes not cleared for script execution' } print 'print VAR=$VAR' > $tmp/script typeset -L70 VAR=var $tmp/script > $tmp/script.1 [[ $(< $tmp/script.1) == VAR= ]] || err_exit 'typeset -L should not be inherited' typeset -Z LAST=00 unset -f foo proc foo { if [[ $1 ]] { setvar LAST = "$1" } else { ((LAST++)) } } foo 1 if (( ${#LAST} != 2 )) { err_exit 'LAST!=2' } foo if (( ${#LAST} != 2 )) { err_exit 'LAST!=2' } [[ $(set | grep LAST) == LAST=02 ]] || err_exit "LAST not correct in set list" set -a unset foo setvar foo = 'bar' if [[ $(export | grep ^foo=) != 'foo=bar' ]] { err_exit 'all export not working' } unset foo read foo <<< """ bar """ if [[ $(export | grep ^foo=) != 'foo=bar' ]] { err_exit 'all export not working with read' } if [[ $(typeset | grep PS2) == PS2 ]] { err_exit 'typeset without arguments outputs names without attributes' } unset a z q x setvar w1 = 'hello' setvar w2 = 'world' setvar t1 = ""$w1 $w2"" if (( 'a' == 97 )) { setvar b1 = 'aGVsbG8gd29ybGQ=' setvar b2 = "aGVsbG8gd29ybGRoZWxsbyB3b3JsZA==" } else { setvar b1 = 'iIWTk5ZAppaZk4Q=' setvar b2 = "iIWTk5ZAppaZk4SIhZOTlkCmlpmThA==" } setvar z = "$b1" typeset -b x=$b1 [[ $x == "$z" ]] || print -u2 'binary variable not expanding correctly' [[ $(printf "%B" x) == $t1 ]] || err_exit 'typeset -b not working' typeset -b -Z5 a=$b1 [[ $(printf "%B" a) == $w1 ]] || err_exit 'typeset -b -Z5 not working' typeset -b q=$x$x [[ $q == $b2 ]] || err_exit 'typeset -b not working with concatination' [[ $(printf "%B" q) == $t1$t1 ]] || err_exit 'typeset -b concatination not working' setvar x = "$b1" [[ $x == $b2 ]] || err_exit 'typeset -b not working with append' [[ $(printf "%B" x) == $t1$t1 ]] || err_exit 'typeset -b append not working' typeset -b -Z20 z=$b1 (( $(printf "%B" z | wc -c) == 20 )) || err_exit 'typeset -b -Z20 not storing 20 bytes' do { typeset -b v1 v2 read -N11 v1 read -N22 v2 } << ! hello worldhello worldhello world ! [[ $v1 == "$b1" ]] || err_exit "v1=$v1 should be $b1" [[ $v2 == "$x" ]] || err_exit "v1=$v2 should be $x" if env '!=1' >/dev/null 2>&1 { [[ $(env '!=1' $SHELL -c 'echo ok' 2>/dev/null) == ok ]] || err_exit 'malformed environment terminates shell' } unset var typeset -b var printf '12%Z34' | read -r -N 5 var [[ $var == MTIAMzQ= ]] || err_exit 'binary files with zeros not working' unset var if command typeset -usi var=0xfffff 2> /dev/null { (( $var == 0xffff )) || err_exit 'unsigned short integers not working' } else { err_exit 'typeset -usi cannot be used for unsigned short' } [[ $($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' [[ $($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' unset foo bar unset -f fun proc fun { export foo=hello typeset -x bar=world [[ $foo == hello ]] || err_exit 'export scoping problem in function' } fun [[ $(export | grep foo) == 'foo=hello' ]] || err_exit 'export not working in functions' [[ $(export | grep bar) ]] && err_exit 'typeset -x not local' [[ $($SHELL -c 'typeset -r IFS=;print -r $(pwd)' 2> /dev/null) == "$(pwd)" ]] || err_exit 'readonly IFS causes command substitution to fail' setvar fred[66]=88 [[ $(typeset -pa) == *fred* ]] || err_exit 'typeset -pa not working' unset x y z typeset -LZ3 x=abcd y z=00abcd setvar y = '03' [[ $y == "3 " ]] || err_exit '-LZ3 not working for value 03' [[ $x == "abc" ]] || err_exit '-LZ3 not working for value abcd' [[ $x == "abc" ]] || err_exit '-LZ3 not working for value 00abcd' unset x z set +a [[ $(typeset -p z) ]] && err_exit "typeset -p for z undefined failed" unset z setvar x = ''typeset -i z=45'' eval $x [[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed" [[ $(typeset +p z) == "${x%=*}" ]] || err_exit "typeset +p for '$x' failed" unset z setvar x = ''typeset -a z=(a b c)'' eval $x [[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed" [[ $(typeset +p z) == "${x%=*}" ]] || err_exit "typeset +p for '$x' failed" unset z setvar x = ''typeset -C z=( foo=bar xxx=bam )'' eval $x setvar x = ${x//$'\t'} setvar x = ${x//$'(\n'/'('} setvar x = ${x//$'\n'/';'} [[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed" [[ $(typeset +p z) == "${x%%=*}" ]] || err_exit "typeset +p for '$x' failed" unset z setvar x = ''typeset -A z=([bar]=bam [xyz]=bar)'' eval $x [[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed" [[ $(typeset +p z) == "${x%%=*}" ]] || err_exit "typeset +p for '$x' failed" unset z setvar foo = 'abc' setvar x = ''typeset -n z=foo'' eval $x [[ $(typeset -p z) == "$x" ]] || err_exit "typeset -p for '$x' failed" [[ $(typeset +p z) == "${x%%=*}" ]] || err_exit "typeset +p for '$x' failed" typeset +n z unset foo z typeset -T Pt_t=( float x=1 y=2 ) Pt_t z setvar x = ${z//$'\t'} setvar x = ${x//$'(\n'/'('} setvar x = ${x//$'\n'/';'} [[ $(typeset -p z) == "Pt_t z=$x" ]] || err_exit "typeset -p for type failed" [[ $(typeset +p z) == "Pt_t z" ]] || err_exit "typeset +p for type failed" unset z proc foo { typeset -p bar } setvar bar = 'xxx' [[ $(foo) == bar=xxx ]] || err_exit 'typeset -p not working inside a function' unset foo typeset -L5 foo [[ $(typeset -p foo) == 'typeset -L 5 foo' ]] || err_exit 'typeset -p not working for variables with attributes but without a value' do { $SHELL <<< """ typeset -L3 foo=aaa typeset -L6 foo=bbbbbb [[ '$'foo == bbbbbb ]] """ } || err_exit 'typeset -L should not preserve old attributes' do { $SHELL <<< """ typeset -R3 foo=aaa typeset -R6 foo=bbbbbb [[ '$'foo == bbbbbb ]] """ } 2> /dev/null || err_exit 'typeset -R should not preserve old attributes' setvar expected = ''YWJjZGVmZ2hpag=='' unset foo typeset -b -Z10 foo read foo <<< 'abcdefghijklmnop' [[ $foo == "$expected" ]] || err_exit 'read foo, where foo is "typeset -b -Z10" not working' unset foo typeset -b -Z10 foo read -N10 foo <<< 'abcdefghijklmnop' [[ $foo == "$expected" ]] || err_exit 'read -N10 foo, where foo is "typeset -b -Z10" not working' unset foo typeset -b -A foo read -N10 foo[4] <<< 'abcdefghijklmnop' [[ ${foo[4]} == "$expected" ]] || err_exit 'read -N10 foo, where foo is "typeset -b -A" foo not working' unset foo typeset -b -a foo read -N10 foo[4] <<< 'abcdefghijklmnop' [[ ${foo[4]} == "$expected" ]] || err_exit 'read -N10 foo, where foo is "typeset -b -a" foo not working' [[ $(printf %B foo[4]) == abcdefghij ]] || err_exit 'printf %B for binary associative array element not working' [[ $(printf %B foo[4]) == abcdefghij ]] || err_exit 'printf %B for binary indexed array element not working' unset foo $SHELL 2> /dev/null -c 'export foo=(bar=3)2> /dev/null -c 'export foo=(bar=3)' && err_exit 'compound variables cannot be exported' $SHELL -c 'builtin date' >/dev/null 2>&1 && do { # check env var changes against a builtin that uses the env var setvar SEC = '1234252800' setvar ETZ = 'EST5EDT' setvar EDT = '03' setvar PTZ = 'PST8PDT' setvar PDT = '00' setvar CMD = ""date -f%H \\#$SEC"" export TZ=$ETZ set -- \ "$EDT $PDT $EDT" "" "TZ=$PTZ" "" \ "$EDT $PDT $EDT" "" "TZ=$PTZ" "TZ=$ETZ" \ "$EDT $PDT $EDT" "TZ=$ETZ" "TZ=$PTZ" "TZ=$ETZ" \ "$PDT $EDT $PDT" "TZ=$PTZ" "" "TZ=$PTZ" \ "$PDT $EDT $PDT" "TZ=$PTZ" "TZ=$ETZ" "TZ=$PTZ" \ "$EDT $PDT $EDT" "foo=bar" "TZ=$PTZ" "TZ=$ETZ" \ while (( $# >= 4 )) { setvar exp = "$1" setvar got = $(print $($SHELL -c "builtin date; $2 $CMD; $3 $CMD; $4 $CMD")) [[ $got == $exp ]] || err_exit "[ '$2' '$3' '$4' ] env sequence failed -- expected '$exp', got '$got'" shift 4 } } unset v typeset -H v=/dev/null [[ $v == *nul* ]] || err_exit 'typeset -H for /dev/null not working' exit $((Errors))