Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/ksh93/tests/append.sh
1810 views
1
########################################################################
2
# #
3
# This software is part of the ast package #
4
# Copyright (c) 1982-2012 AT&T Intellectual Property #
5
# and is licensed under the #
6
# Eclipse Public License, Version 1.0 #
7
# by AT&T Intellectual Property #
8
# #
9
# A copy of the License is available at #
10
# http://www.eclipse.org/org/documents/epl-v10.html #
11
# (with md5 checksum b35adb5213ca9657e911e9befb180842) #
12
# #
13
# Information and Software Systems Research #
14
# AT&T Research #
15
# Florham Park NJ #
16
# #
17
# David Korn <[email protected]> #
18
# #
19
########################################################################
20
function err_exit
21
{
22
print -u2 -n "\t"
23
print -u2 -r ${Command}[$1]: "${@:2}"
24
let Errors+=1
25
}
26
alias err_exit='err_exit $LINENO'
27
28
Command=${0##*/}
29
integer Errors=0
30
{
31
x=abc
32
x+=def ;} 2> /dev/null
33
if [[ $x != abcdef ]]
34
then err_exit 'abc+def != abcdef'
35
fi
36
integer i=3
37
{ i+=4;} 2> /dev/null
38
if (( i != 7 ))
39
then err_exit '3+4!=7'
40
fi
41
iarray=( one two three )
42
{ iarray+= (four five six) ;} 2> /dev/null
43
if [[ ${iarray[@]} != 'one two three four five six' ]]
44
then err_exit 'index array append fails'
45
fi
46
unset iarray
47
iarray=one
48
{ iarray+= (four five six) ;} 2> /dev/null
49
if [[ ${iarray[@]} != 'one four five six' ]]
50
then err_exit 'index array append to scalar fails'
51
fi
52
typeset -A aarray
53
aarray=( [1]=1 [3]=4 [xyz]=xyz )
54
aarray+=( [2]=2 [3]=3 [foo]=bar )
55
if [[ ${aarray[3]} != 3 ]]
56
then err_exit 'associative array append fails'
57
fi
58
if [[ ${#aarray[@]} != 5 ]]
59
then err_exit 'number of elements of associative array append fails'
60
fi
61
point=(x=1 y=2)
62
point+=( y=3 z=4)
63
if [[ ${point.y} != 3 ]]
64
then err_exit 'compound append fails'
65
fi
66
if [[ ${point.x} != 1 ]]
67
then err_exit 'compound append to compound variable unsets existing variables'
68
fi
69
unset foo
70
foo=one
71
foo+=(two)
72
if [[ ${foo[@]} != 'one two' ]]
73
then err_exit 'array append to non array variable fails'
74
fi
75
unset foo
76
foo[0]=(x=3)
77
foo+=(x=4)
78
[[ ${foo[1].x} == 4 ]] || err_exit 'compound append to index array not working'
79
[[ ${foo[0].x} == 3 ]] || err_exit 'compound append to index array unsets existing variables'
80
81
unset foo
82
foo=a
83
foo+=''
84
[[ $foo == 'a' ]] || err_exit 'appending an empty string not working'
85
86
unset x z arr
87
typeset -a x=(a b)
88
x+=(c d)
89
exp='typeset -a x=(a b c d)'
90
[[ $(typeset -p x) == "$exp" ]] || err_exit 'append (c d) to index array not working'
91
92
typeset -a arr=(a=b b=c)
93
arr+=(c=d d=e)
94
exp='typeset -a arr=(a\=b b\=c c\=d d\=e)'
95
[[ $(typeset -p arr) == "$exp" ]] || err_exit 'append (c=d d=e) to index array not working'
96
97
exp='typeset -a z=(a\=b b\=c d\=3 e f\=l)'
98
typeset -a z=(a=b b=c)
99
{ z+=(d=3 e f=l); } 2> /dev/null
100
[[ $(typeset -p z) == "$exp" ]] || err_exit 'append (d=3 e f=l) to index array not working'
101
102
unset arr2
103
exp='typeset -a arr2=(b\=c :)'
104
typeset -a arr2
105
arr2+=(b=c :)
106
[[ $(typeset -p arr2) == "$exp" ]] || err_exit 'append (b=c :) to index array not working'
107
108
unset arr2
109
exp='typeset -a arr2=(b\=c xxxxx)'
110
typeset -a arr2
111
{
112
arr2+=(b=c xxxxx)
113
} 2> /dev/null
114
[[ $(typeset -p arr2) == "$exp" ]] || err_exit 'append (b=c xxxxx) to index array not working'
115
116
exit $((Errors<125?Errors:125))
117
118