aboutsummaryrefslogtreecommitdiffstats
path: root/tests/assoc.tests
blob: 8b6fd5535b83d6c3a29532585c1ebf34330d23da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# TEST - basic declaration and assignment
typeset -A fluff
declare -A

fluff[foo]=one
fluff[bar]=two

declare -A
declare -p fluff

unset fluff[foo]
declare -p fluff

fluff[bar]=newval
declare fluff[qux]=assigned

declare -p fluff

unset fluff

# TEST - compount assignment and variable attributes
declare -A wheat chaff
wheat=( [zero]=0 [one]=a [two]=b [three]=c )

declare -i chaff
chaff=( [zero]=1+4 [one]=3+7 four )

declare -A waste=( [pid]=42134 [version]=4.0-devel [source]=$0 [lineno]=$LINENO )
declare -r waste

declare -A

declare +i chaff
chaff[hello world]=flip
declare -p chaff

# TEST - errors
unset waste
chaff[*]=12
chaff=( [one]=a [*]=12 )

# TEST - key expansion -- no word splitting
chaff[hello world]=flip
declare -p chaff
echo ${chaff[hello world]}

chaff[box]="multiple words"

recho ${chaff[@]}
recho "${chaff[@]}"

recho ${chaff[*]}
recho "${chaff[*]}"

unset chaff
declare -A chaff[200]
declare +A chaff

chaff[*]=12
chaff=( [one]=a [*]=12 )

# TEST - keys and values containing spaces
unset wheat
declare -A wheat
wheat=([six]=6 [foo bar]="qux qix" )

declare -p wheat

unset wheat
declare -A wheat=([six]=6 [foo bar]="qux qix" )

recho ${wheat[foo bar]}
recho "${wheat[foo bar]}"

declare -p wheat

# TEST - basic expansions: number of elements and value length
unset wheat
typeset -A wheat
wheat=([six]=6 [foo bar]="qux qix" )

recho ${#wheat[@]}

recho ${#wheat[foo bar]}

# TEST - appending assignment operator
unset wheat
typeset -A wheat
wheat=([six]=6 [foo bar]="qux qix" )

wheat[foo bar]+=' blat'

recho ${wheat[foo bar]}
recho "${wheat[foo bar]}"
unset wheat

flix=9
typeset -Ai wheat
wheat=([six]=6 [foo bar]=flix )

wheat[foo bar]+=7

recho ${wheat[foo bar]}
recho "${wheat[foo bar]}"
unset flix wheat

# TEST - index expansion: no word splitting or globbing
typeset -A wheat
cd /tmp
touch '[sfiri]'
wheat=([s*]=6 [foo bar]=flix )

recho ${wheat[@]}
rm '[sfiri]'
cd $OLDPWD

# TEST -- associative array keys expansion
unset wheat
typeset -A wheat

wheat=([six]=6 [foo bar]=flix )

recho ${!wheat[@]}
recho "${!wheat[@]}"

# TEST -- associative array pattern removal
unset xpath
typeset -A xpath

xpath=( [0]=/bin [one]=/bin [two]=/usr/bin [three]=/usr/ucb [four]=/usr/local/bin)
xpath+=( [five]=/sbin [six]=/usr/sbin [seven]=. )

echo ${#xpath[@]}

echo ${xpath[@]}
echo ${xpath[@]##*/}
echo ${xpath[0]##*/}
echo ${xpath[@]%%[!/]*}
echo ${xpath[0]%%[!/]*}
recho ${xpath##*/}
recho ${xpath%%[!/]*}
recho ${xpath[five]##*/}
recho ${xpath[five]%%[!/]*}

echo ${#xpath[*]}

echo ${xpath[*]}
echo ${xpath[*]##*/}
echo ${xpath[*]%%[!/]*}

# TEST -- associative array pattern substitution
unset xpath
typeset -A xpath

xpath=( [0]=/bin [one]=/bin [two]=/usr/bin [three]=/usr/ucb [four]=/usr/local/bin)
xpath+=( [five]=/sbin [six]=/usr/sbin [seven]=. )

echo ${#xpath[@]}
# default element is "0" (as a string)
echo ${#xpath} -- ${xpath["0"]}

echo ${xpath[@]//\//^}
echo "${xpath[@]//\//^}" | cat -v

zecho "${xpath[@]/\//\\}"
zecho "${xpath[@]//\//\\}"
zecho "${xpath[@]//[\/]/\\}"

# test assignment to key "0"
unset T
declare -A T
T='([a]=1)'
echo "${T[@]}"
unset T

${THIS_SH} ./assoc1.sub

${THIS_SH} ./assoc2.sub

${THIS_SH} ./assoc3.sub

${THIS_SH} ./assoc4.sub

${THIS_SH} ./assoc5.sub

${THIS_SH} ./assoc6.sub