OILS / spec / arith-context.test.sh View on Github | oilshell.org

216 lines, 76 significant
1## compare_shells: bash mksh zsh
2## oils_failures_allowed: 0
3
4# Test arithmetic expressions in all their different contexts.
5
6# $(( 1 + 2 ))
7# (( a=1+2 ))
8# ${a[ 1 + 2 ]}
9# ${a : 1+2 : 1+2}
10# a[1 + 2]=foo
11
12#### Multiple right brackets inside expression
13
14a=(1 2 3)
15echo ${a[a[0]]} ${a[a[a[0]]]}
16
17## STDOUT:
182 3
19## END
20
21## N-I zsh status: 0
22## N-I zsh STDOUT:
23
24## END
25
26#### Slicing of string with constants
27s='abcd'
28echo ${s:0} ${s:0:4} ${s:1:1}
29## stdout: abcd abcd b
30
31#### Slicing of string with variables
32s='abcd'
33zero=0
34one=1
35echo ${s:$zero} ${s:$zero:4} ${s:$one:$one}
36## stdout: abcd abcd b
37
38#### Array index on LHS of assignment
39a=(1 2 3)
40zero=0
41a[zero+5-4]=X
42echo ${a[@]}
43## stdout: 1 X 3
44## OK zsh stdout: X 2 3
45
46#### Array index on LHS with indices
47a=(1 2 3)
48a[a[1]]=X
49echo ${a[@]}
50## stdout: 1 2 X
51## OK zsh stdout: X 2 3
52
53#### Slicing of string with expressions
54# mksh accepts ${s:0} and ${s:$zero} but not ${s:zero}
55# zsh says unrecognized modifier 'z'
56s='abcd'
57zero=0
58echo ${s:zero} ${s:zero+0} ${s:zero+1:zero+1}
59## stdout: abcd abcd b
60## BUG mksh stdout-json: ""
61## BUG mksh status: 1
62## BUG zsh stdout-json: ""
63## BUG zsh status: 1
64
65#### Ambiguous colon in slice
66s='abcd'
67echo $(( 0 < 1 ? 2 : 0 )) # evaluates to 2
68echo ${s: 0 < 1 ? 2 : 0 : 1} # 2:1 -- TRICKY THREE COLONS
69## STDOUT:
702
71c
72## END
73## BUG mksh/zsh STDOUT:
742
75## END
76## BUG mksh/zsh status: 1
77
78#### Triple parens should be disambiguated
79# The first paren is part of the math, parens 2 and 3 are a single token ending
80# arith sub.
81((a=1 + (2*3)))
82echo $a $((1 + (2*3)))
83## STDOUT:
847 7
85## END
86
87#### Quadruple parens should be disambiguated
88((a=1 + (2 * (3+4))))
89echo $a $((1 + (2 * (3+4))))
90## STDOUT:
9115 15
92## END
93
94#### ExprSub $[] happens to behave the same on simple cases
95echo $[1 + 2] "$[3 * 4]"
96## STDOUT:
973 12
98## END
99## N-I mksh STDOUT:
100$[1 + 2] $[3 * 4]
101## END
102
103
104#### Empty expression (( )) $(( ))
105
106(( ))
107echo status=$?
108
109echo $(( ))
110
111#echo $[]
112
113## STDOUT:
114status=1
1150
116## END
117
118#### Empty expression for (( ))
119
120for (( ; ; )); do
121 echo one
122 break
123done
124
125## STDOUT:
126one
127## END
128
129## N-I mksh status: 1
130## N-I mksh STDOUT:
131## END
132
133#### Empty expression in ${a[@]: : }
134
135a=(a b c d e f)
136
137# space required here -- see spec/var-op-slice
138echo slice ${a[@]: }
139echo status=$?
140echo
141
142echo slice ${a[@]: : }
143echo status=$?
144echo
145
146# zsh doesn't accept this
147echo slice ${a[@]:: }
148echo status=$?
149
150## STDOUT:
151slice a b c d e f
152status=0
153
154slice
155status=0
156
157slice
158status=0
159## END
160
161## BUG zsh status: 1
162## BUG zsh STDOUT:
163slice a b c d e f
164status=0
165
166slice
167status=0
168
169## END
170
171## BUG mksh status: 1
172## BUG mksh STDOUT:
173## END
174
175
176#### Empty expression a[]
177
178a=(1 2 3)
179
180a[]=42
181echo status=$?
182echo ${a[@]}
183
184echo ${a[]}
185echo status=$?
186
187## status: 2
188## STDOUT:
189## END
190
191## OK zsh status: 1
192
193# runtime failures
194
195## OK bash status: 0
196## OK bash STDOUT:
197status=1
1981 2 3
199status=1
200## END
201
202## BUG mksh status: 0
203## BUG mksh STDOUT:
204status=0
20542 2 3
20642
207status=0
208## END
209
210
211# Others
212# [ 1+2 -eq 3 ]
213# [[ 1+2 -eq 3 ]]
214# unset a[]
215# printf -v a[]
216