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

203 lines, 68 significant
1## compare_shells: bash mksh zsh
2## oils_failures_allowed: 1
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[@]:slice}
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# bash and zsh differ with respect to spaces
147#echo slice ${a[@]:: }
148
149## STDOUT:
150slice a b c d e f
151status=0
152
153slice
154status=0
155
156## END
157
158## BUG mksh status: 1
159## BUG mksh STDOUT:
160## END
161
162
163#### Empty expression a[]
164
165a=(1 2 3)
166
167a[]=42
168echo status=$?
169echo ${a[@]}
170
171echo ${a[]}
172echo status=$?
173
174## status: 2
175## STDOUT:
176## END
177
178## OK zsh status: 1
179
180# runtime failures
181
182## OK bash status: 0
183## OK bash STDOUT:
184status=1
1851 2 3
186status=1
187## END
188
189## BUG mksh status: 0
190## BUG mksh STDOUT:
191status=0
19242 2 3
19342
194status=0
195## END
196
197
198# Others
199# [ 1+2 -eq 3 ]
200# [[ 1+2 -eq 3 ]]
201# unset a[]
202# printf -v a[]
203