]> git.ipfire.org Git - thirdparty/bash.git/blobdiff - tests/array.tests
Bash-4.3 patch 7
[thirdparty/bash.git] / tests / array.tests
index d8b6564fe8c533400c156d25b8dc30419a725487..4c176514a0b8f9ab928654601a858b224a7065f7 100644 (file)
@@ -6,6 +6,11 @@ set +a
 # The calls to egrep -v are to filter out builtin array variables that are
 # automatically set and possibly contain values that vary.
 
+# first make sure we handle the basics
+x=()
+echo ${x[@]}
+unset x
+
 # this should be an error
 test=(first & second)
 echo $?
@@ -21,6 +26,7 @@ unset a
 a=abcde
 a[2]=bdef
 
+unset b
 declare -a b[256]
 
 unset c[2]
@@ -63,6 +69,8 @@ echo ${#a[5]}
 echo ${#a[@]}
 
 a[4+5/2]="test expression"
+declare a["7 + 8"]="test 2"
+a[7 + 8]="test 2"
 echo ${a[@]}
 
 readonly a[5]
@@ -78,7 +86,7 @@ set +o posix
 declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")'
 d[9]="ninth element"
 
-declare -a e[10]=test
+declare -a e[10]=test  # this works in post-bash-2.05 versions
 declare -a e[10]='(test)'
 
 pass=/etc/passwd
@@ -170,6 +178,10 @@ echo ${xpath[@]##*/}
 echo ${xpath[0]##*/}
 echo ${xpath[@]%%[!/]*}
 echo ${xpath[0]%%[!/]*}
+recho ${xpath##*/}
+recho ${xpath%%[!/]*}
+recho ${xpath[5]##*/}
+recho ${xpath[5]%%[!/]*}
 
 # let's try to make it a DOS-style path
 
@@ -226,7 +238,15 @@ echo "value = ${barray[*]}"
 set -u
 ( echo ${#narray[4]} )
 
+${THIS_SH} ./array1.sub
+${THIS_SH} ./array2.sub
+
 # some old bugs and ksh93 compatibility tests
+${THIS_SH} ./array3.sub
+
+# some compound assingment parsing problems that showed up in bash-3.1-release
+${THIS_SH} ./array4.sub
+
 set +u
 cd /tmp
 
@@ -235,6 +255,8 @@ foo=([10]="bar")
 echo ${foo[0]}
 rm 1=bar
 
+cd $OLDPWD
+
 foo=(a b c d e f g)
 echo ${foo[@]}
 
@@ -258,11 +280,12 @@ declare -a ddd=(aaa
 bbb)
 echo ${ddd[@]}
 
-# errors
+# errors until post-bash-2.05a; now reserved words are OK
 foo=(a b c for case if then else)
 
 foo=(for case if then else)
 
+# errors
 metas=( <> < > ! )
 metas=( [1]=<> [2]=< [3]=> [4]=! )
 
@@ -278,3 +301,102 @@ echo ${foo[0]} ${#foo[0]}
 echo ${foo[1]} ${#foo[1]}
 echo ${foo[@]} ${#foo[@]}
 echo ${foo[*]} ${#foo[*]}
+
+# new expansions added after bash-2.05b
+x[0]=zero
+x[1]=one
+x[4]=four
+x[10]=ten
+
+recho ${!x[@]}
+recho "${!x[@]}"
+recho ${!x[*]}
+recho "${!x[*]}"
+
+# sparse array tests for code fixed in bash-3.0
+unset av
+av[1]='one'
+av[2]=''
+
+av[3]=three
+av[5]=five
+av[7]=seven
+
+echo include null element -- expect one
+echo ${av[@]:1:2}      # what happens when we include a null element?
+echo include unset element -- expect three five
+echo ${av[@]:3:2}      # what happens when we include an unset element?
+echo start at unset element -- expect five seven
+echo ${av[@]:4:2}      # what happens when we start at an unset element?
+
+echo too many elements -- expect three five seven
+echo ${av[@]:3:5}      # how about too many elements?
+
+echo positive offset - expect five seven
+echo ${av[@]:5:2}
+echo negative offset to unset element - expect seven
+echo ${av[@]: -2:2}
+
+echo positive offset 2 - expect seven
+echo ${av[@]: 6:2}
+echo negative offset 2 - expect seven
+echo ${av[@]: -1:2}
+
+echo out-of-range offset
+echo ${av[@]:12}
+
+# parsing problems and other inconsistencies not fixed until post bash-3.0
+unset x
+declare -a x=(')' $$)
+[ ${x[1]} -eq $$ ] || echo bad
+
+unset x
+declare -a x=(a b c d e)
+echo ${x[4]}
+
+z=([1]=one [4]=four [7]=seven [10]=ten)
+
+echo ${#z[@]}
+
+echo ${!z[@]}
+
+unset x
+declare -a x=(a \'b  c\')
+
+echo "${x[1]}"
+
+unset x
+declare -a x=(a 'b  c')
+
+echo "${x[1]}"
+
+unset x
+declare -a x=($0)
+[ "${x[@]}" = $0 ] || echo double expansion of \$0
+declare -a x=(\$0)
+echo "${x[@]}"
+
+# tests for bash-3.1 problems
+${THIS_SH} ./array5.sub
+
+# tests for post-bash-3.2 problems, most fixed in bash-3.2 patches
+${THIS_SH} ./array6.sub
+${THIS_SH} ./array7.sub
+
+${THIS_SH} ./array8.sub
+
+${THIS_SH} ./array9.sub
+
+${THIS_SH} ./array10.sub
+
+${THIS_SH} ./array11.sub
+
+${THIS_SH} ./array12.sub
+
+${THIS_SH} ./array13.sub
+
+${THIS_SH} ./array14.sub
+
+${THIS_SH} ./array15.sub
+
+${THIS_SH} ./array16.sub