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

81 lines, 26 significant
1#
2# Test arithmetic expressions in all their different contexts.
3
4# $(( 1 + 2 ))
5# (( a=1+2 ))
6# ${a[ 1 + 2 ]}
7# ${a : 1+2 : 1+2}
8# a[1 + 2]=foo
9
10#### Multiple right brackets inside expression
11a=(1 2 3)
12echo ${a[a[0]]} ${a[a[a[0]]]}
13## stdout: 2 3
14## N-I zsh status: 0
15## N-I zsh stdout-json: "\n"
16
17#### Slicing of string with constants
18s='abcd'
19echo ${s:0} ${s:0:4} ${s:1:1}
20## stdout: abcd abcd b
21
22#### Slicing of string with variables
23s='abcd'
24zero=0
25one=1
26echo ${s:$zero} ${s:$zero:4} ${s:$one:$one}
27## stdout: abcd abcd b
28
29#### Array index on LHS of assignment
30a=(1 2 3)
31zero=0
32a[zero+5-4]=X
33echo ${a[@]}
34## stdout: 1 X 3
35## OK zsh stdout: X 2 3
36
37#### Array index on LHS with indices
38a=(1 2 3)
39a[a[1]]=X
40echo ${a[@]}
41## stdout: 1 2 X
42## OK zsh stdout: X 2 3
43
44#### Slicing of string with expressions
45# mksh accepts ${s:0} and ${s:$zero} but not ${s:zero}
46# zsh says unrecognized modifier 'z'
47s='abcd'
48zero=0
49echo ${s:zero} ${s:zero+0} ${s:zero+1:zero+1}
50## stdout: abcd abcd b
51## BUG mksh stdout-json: ""
52## BUG mksh status: 1
53## BUG zsh stdout-json: ""
54## BUG zsh status: 1
55
56#### Ambiguous colon in slice
57s='abcd'
58echo $(( 0 < 1 ? 2 : 0 )) # evaluates to 2
59echo ${s: 0 < 1 ? 2 : 0 : 1} # 2:1 -- TRICKY THREE COLONS
60## stdout-json: "2\nc\n"
61## BUG mksh stdout-json: "2\n"
62## BUG mksh status: 1
63## BUG zsh stdout-json: "2\n"
64## BUG zsh status: 1
65
66#### Triple parens should be disambiguated
67# The first paren is part of the math, parens 2 and 3 are a single token ending
68# arith sub.
69((a=1 + (2*3)))
70echo $a $((1 + (2*3)))
71## stdout: 7 7
72
73#### Quadruple parens should be disambiguated
74((a=1 + (2 * (3+4))))
75echo $a $((1 + (2 * (3+4))))
76## stdout: 15 15
77
78#### ExprSub $[] happens to behave the same on simple cases
79echo $[1 + 2] "$[3 * 4]"
80## stdout: 3 12
81## N-I mksh stdout: $[1 + 2] $[3 * 4]