1# Tests of parameter assignments
2
3%test
4
5 typeset -A assoc
6 assoc=(one 1 two 2 odd)
71:assign to association with odd no. of values
8?(eval):2: bad set of key/value pairs for associative array
9
10# tests of array element assignment
11
12 array=(1 2 3 4 5)
13 array[1]=42
14 print $array
150:Replacement of array element
16>42 2 3 4 5
17
18 array=(1 2 3 4 5)
19 array[1]=(42 43)
20 print $array
210:Replacement of array element with array
22>42 43 2 3 4 5
23
24 array=(1 2 3 4 5)
25 array[1,2]=(42 43)
26 print $array
270:Replacement of start of array
28>42 43 3 4 5
29
30 array=(1 2 3 4 5)
31 array[1,4]=(42 43)
32 print $array
330:Replacement of start of array with shorter slice
34>42 43 5
35
36 array=(1 2 3 4 5)
37 array[1,6]=(42 43)
38 print $array
390:Replacement of array by extending slice
40>42 43
41
42 array=(1 2 3 4 5)
43 array[3]=(42 43)
44 print $array
450:Replacement of middle element with array
46>1 2 42 43 4 5
47
48 array=(1 2 3 4 5)
49 array[3,4]=(42 43 44)
50 print $array
510:Replacement of slice in middle
52>1 2 42 43 44 5
53
54 array=(1 2 3 4 5)
55 array[7,8]=(42 43)
56 print $array
57 # check that [6] was left empty...
58 array[6]=41
59 print $array
600:Appending by replacing elements off the end
61>1 2 3 4 5 42 43
62>1 2 3 4 5 41 42 43
63
64 array=(1 2 3 4 5)
65 array[-1]=42
66 print $array
670:Replacement of last element of array, negative indices
68>1 2 3 4 42
69
70 array=(1 2 3 4 5)
71 array[-1]=(42 43)
72 print $array
730:Replacement of last element of array with array, negative indices
74>1 2 3 4 42 43
75
76 array=(1 2 3 4 5)
77 array[-3,-2]=(42 43 44)
78 print $array
790:Replacement of middle of array, negative indices
80>1 2 42 43 44 5
81
82 array=(1 2 3 4 5)
83 array[-5,-1]=(42 43)
84 print $array
850:Replacement of entire array, negative indices
86>42 43
87
88 array=(1 2 3 4 5)
89 array[-7,-1]=(42 43)
90 print $array
910:Replacement of more than entire array, negative indices
92>42 43
93
94 array=(1 2 3 4 5)
95 array[-7]=42
96 print $array
970:Replacement of element off start of array.
98>42 1 2 3 4 5
99
100 array=(1 2 3 4 5)
101 array[-7]=42
102 array[-6]=43
103 print $array
1040:Replacement off start doesn't leave gaps.  Hope this is right.
105>43 1 2 3 4 5
106
107 array=(1 2 3 4 5)
108 array[1,-1]=(42 43)
109 print $array
110 array[-3,3]=(1 2 3 4 5)
111 print $array
1120:Replacement of entire array, mixed indices
113>42 43
114>1 2 3 4 5
115
116 array=(1 2 3 4 5)
117 array[-7,7]=(42 43)
118 print $array
1190:Replacement of more than entire array, mixed indices
120>42 43
121
122 array=(1 2 3 4 5)
123 array[3,-2]=(42 43 44)
124 print $array
125 array[-3,5]=(100 99)
126 print $array
1270:Replacement of slice in middle, mixed indices
128>1 2 42 43 44 5
129>1 2 42 100 99 5
130
131# tests of var+=scalar
132
133 s+=foo
134 echo $s
1350:append scalar to unset scalar
136>foo
137
138 s=foo
139 s+=bar
140 echo $s
1410:append to scalar
142>foobar
143
144 set -- a b c
145 2+=end
146 echo $2
1470:append to positional parameter
148>bend
149
150 a=(first second)
151 a+=last
152 print -l $a
1530:add scalar to array
154>first
155>second
156>last
157
158 setopt ksharrays
159 a=(first second)
160 a+=last
161 print -l $a
162 unsetopt ksharrays
1630:add scalar to array with ksharrays set
164>firstlast
165
166 a=(1 2)
167 a[@]+=3
168 print -l $a
1690:add scalar to array with alternate syntax
170>1
171>2
172>3
173
174 integer i=10
175 i+=20
176 (( i == 30 ))
1770:add to integer
178
179 float f=3.4
180 f+=2.3
181 printf "%g\n" f
1820:add to float
183>5.7
184
185 typeset -A hash
186 hash=(one 1)
187 hash+=string
188 [[ $hash[@] == string ]]
1890:add scalar to association
190
191# tests of var+=(array)
192
193 unset a
194 a+=(1 2 3)
195 print -l $a
1960:add array to unset parameter
197>1
198>2
199>3
200
201 a=(a)
202 a+=(b)
203 print -l $a
2040:add array to existing array
205>a
206>b
207
208 s=foo
209 s+=(bar)
210 print -l $s
2110:add array to scalar
212>foo
213>bar
214
215 integer i=1
216 i+=(2 3)
217 print -l $i
2180:add array to integer
219>1
220>2
221>3
222
223 float f=2.5
224 f+=(3.5 4.5)
225 printf '%g\n' $f
2260:add array to float
227>2.5
228>3.5
229>4.5
230
231 typeset -A h
232 h+=(a 1 b 2)
233 print -l $h
2340:add to empty association
235>1
236>2
237
238 typeset -A h
239 h=(a 1)
240 h+=(b 2 c 3)
241 print -l $h
2420:add to association
243>1
244>2
245>3
246
247# tests of var[range]+=scalar
248
249 s=sting
250 s[2]+=art
251 echo $s
2520:insert scalar inside another
253>starting
254
255 s=inert
256 s[-4]+=s
257 echo $s
2580:insert scalar inside another with negative index
259>insert
260
261 s=append
262 s[2,6]+=age
263 echo $s
2640:append scalar to scalar using a range
265>appendage
266
267 s=123456789
268 s[3,-5]+=X
269 echo $s
2700:insert scalar inside another, specifying a slice
271>12345X6789
272
273 a=(a b c)
274 a[2]+=oo
275 echo $a
2760:append to array element
277>a boo c
278
279 a=(a b c d)
280 a[-2]+=ool
281 echo $a
2820:append to array element with negative index
283>a b cool d
284
285 a=(a b c d)
286 a[2,-1]+=oom
287 echo $a
2880:append to array element, specifying a slice
289>a b c doom
290
291 setopt ksharrays
292 a=(a b c d)
293 a[0]+=0
294 echo $a
295 unsetopt ksharrays
2960:append to array element with ksharrays set
297>a0
298
299 typeset -A h
300 h=(one foo)
301 h[one]+=bar
302 echo $h
3030:append to association element
304>foobar
305
306 typeset -A h
307 h[foo]+=bar
308 echo ${(kv)h}
3090:append to non-existent association element
310>foo bar
311
312 typeset -A h
313 h=(one a two b three c four d)
314 h[(I)*o*]+=append
3151:attempt to append to slice of association
316?(eval):3: h: attempt to set slice of associative array
317
318 integer i=123
319 i[2]+=6
3201:attempt to add to indexed integer variable
321?(eval):2: attempt to add to slice of a numeric variable
322
323 float f=1234.5
324 f[2,4]+=3
3251:attempt to add to slice of float variable
326?(eval):2: attempt to add to slice of a numeric variable
327
328 unset u
329 u[3]+=third
330 echo $u[1]:$u[3]
3310:append to unset variable with index
332>:third
333 
334# tests of var[range]+=(array)
335
336 a=(1 2 3)
337 a[2]+=(a b)
338 echo $a
3390:insert array inside another
340>1 2 a b 3
341
342 a=(a b c)
343 a[-1]+=(d)
344 echo $a
3450:append to array using negative index
346>a b c d
347
348 a=(1 2 3 4)
349 a[-1,-3]+=(x)
350 echo $a
3510:insert array using negative range
352>1 2 x 3 4
353
354 s=string
355 s[2]+=(a b)
3561:attempt to insert array into string
357?(eval):2: s: attempt to assign array value to non-array
358
359 integer i=365
360 i[2]+=(1 2)
3611:attempt to insert array into string
362?(eval):2: i: attempt to assign array value to non-array
363
364 typeset -A h
365 h=(a 1)
366 h[a]+=(b 2)
3671:attempt to append array to hash element
368?(eval):3: h: attempt to set slice of associative array
369
370 unset u
371 u[-34,-2]+=(a z)
372 echo $u
3730:add array to indexed unset variable
374>a z
375
376 repeat 10 PATH=. echo hello
3770:saving and restoring of exported special parameters
378>hello
379>hello
380>hello
381>hello
382>hello
383>hello
384>hello
385>hello
386>hello
387>hello
388
389 repeat 10 FOO=BAR BAR=FOO echo $FOO $BAR
3900:save and restore multiple variables around builtin
391>
392>
393>
394>
395>
396>
397>
398>
399>
400>
401
402  call() { print $HELLO; }
403  export HELLO=world
404  call
405  HELLO=universe call
406  call
407  HELLO=${HELLO}liness call
408  call
409  unset HELLO
4100:save and restore when using original value in temporary
411>world
412>universe
413>world
414>worldliness
415>world
416