OILS / spec / for-expr.test.sh View on Github | oilshell.org

173 lines, 108 significant
1## compare_shells: bash-4.4 zsh
2
3# Constructs borrowed from ksh. Hm I didn't realize zsh also implements these!
4# mksh implements most too.
5
6#### C-style for loop
7n=10
8for ((a=1; a <= n ; a++)) # Double parentheses, and naked 'n'
9do
10 if test $a = 3; then
11 continue
12 fi
13 if test $a = 6; then
14 break
15 fi
16 echo $a
17done
18## status: 0
19## STDOUT:
201
212
224
235
24## END
25
26#### For loop with and without semicolon
27for ((a=1; a <= 3; a++)); do
28 echo $a
29done
30for ((a=1; a <= 3; a++)) do
31 echo $a
32done
33## status: 0
34## STDOUT:
351
362
373
381
392
403
41## END
42
43#### Accepts { } syntax too
44for ((a=1; a <= 3; a++)) {
45 echo $a
46}
47## STDOUT:
481
492
503
51## END
52
53#### Empty init
54i=1
55for (( ;i < 4; i++ )); do
56 echo $i
57done
58## status: 0
59## STDOUT:
601
612
623
63## END
64
65#### Empty init and cond
66i=1
67for (( ; ; i++ )); do
68 if test $i = 4; then
69 break
70 fi
71 echo $i
72done
73## status: 0
74## STDOUT:
751
762
773
78## END
79
80#### Infinite loop with ((;;))
81a=1
82for (( ; ; )); do
83 if test $a = 4; then
84 break
85 fi
86 echo $((a++))
87done
88## status: 0
89## STDOUT:
901
912
923
93## END
94
95
96#### Arith lexer mode
97
98# bash is lenient; zsh disagrees
99
100for ((i = '3'; i < '5'; ++i)); do echo $i; done
101for ((i = "3"; i < "5"; ++i)); do echo $i; done
102for ((i = $'3'; i < $'5'; ++i)); do echo $i; done
103for ((i = $"3"; i < $"5"; ++i)); do echo $i; done
104
105## STDOUT:
1063
1074
1083
1094
1103
1114
1123
1134
114## END
115## OK zsh status: 1
116## OK zsh STDOUT:
117## END
118
119
120#### Integers near 31, 32, 62 bits
121
122# Hm this was never a bug, but it's worth testing.
123# The bug was EvalToInt() in the condition.
124
125for base in 31 32 62; do
126
127 start=$(( (1 << $base) - 2))
128 end=$(( (1 << $base) + 2))
129
130 for ((i = start; i < end; ++i)); do
131 echo $i
132 done
133 echo ---
134done
135
136## STDOUT:
1372147483646
1382147483647
1392147483648
1402147483649
141---
1424294967294
1434294967295
1444294967296
1454294967297
146---
1474611686018427387902
1484611686018427387903
1494611686018427387904
1504611686018427387905
151---
152## END
153
154
155#### Condition that's greater than 32 bits
156
157iters=0
158
159for ((i = 1 << 32; i; ++i)); do
160 echo $i
161 iters=$(( iters + 1 ))
162 if test $iters -eq 5; then
163 break
164 fi
165done
166
167## STDOUT:
1684294967296
1694294967297
1704294967298
1714294967299
1724294967300
173## END