1 ## oils_failures_allowed: 1
2
3 #### Minus operator is left associative
4
5 var a = 1 - 0 - 1
6 var b = (1 - 0) - 1
7 echo a=$a b=$b
8
9 var a = 3 - 1 - 2
10 var b = (3 - 1) - 2
11 echo a=$a b=$b
12
13 ## STDOUT:
14 a=0 b=0
15 a=0 b=0
16 ## END
17
18 #### Division operators are left associative
19
20 var a = 10 / 4 / 2
21 var b = 10 / 4 / 2
22 echo a=$a b=$b
23
24 var a = 9 // 3 // 3
25 var b = (9 // 3) // 3
26 echo a=$a b=$b
27
28 var a = 11 % 6 % 3
29 var b = (11 % 6) % 3
30 echo a=$a b=$b
31
32 ## STDOUT:
33 a=1.25 b=1.25
34 a=1 b=1
35 a=2 b=2
36 ## END
37
38 #### Exponentiation is right associative
39
40 var a = 3 ** 2 ** 2
41 var b = 3 ** (2 ** 2)
42 echo a=$a b=$b
43
44 ## STDOUT:
45 a=81 b=81
46 ## END
47
48 #### Binary operators, with conversions from string
49
50 echo ' i i' $[1 + 2]
51 echo 'si i' $['1' + 2]
52 echo ' i si' $[1 + '2']
53 echo ---
54
55 echo ' f f' $[2.5 - 1.5]
56 echo 'sf f' $['2.5' - 1.5]
57 echo ' f sf' $[2.5 - '1.5']
58 echo ---
59
60 echo ' i f' $[4 * 1.5]
61 echo 'si f' $['4' * 1.5]
62 echo ' i sf' $[4 * '1.5']
63 echo ---
64
65 echo ' f i' $[5.0 / 2]
66 echo 'sf i' $['5.0' / 2]
67 echo ' f si' $[5.0 / '2']
68
69 ## STDOUT:
70 i i 3
71 si i 3
72 i si 3
73 ---
74 f f 1.0
75 sf f 1.0
76 f sf 1.0
77 ---
78 i f 6.0
79 si f 6.0
80 i sf 6.0
81 ---
82 f i 2.5
83 sf i 2.5
84 f si 2.5
85 ## END
86
87 #### Floating Point Division with /
88
89 var i = '1.0' / '0.05'
90
91 echo $i
92
93 ## STDOUT:
94 20.0
95 ## END
96
97
98 #### Operations That Convert to Integer: // % **
99 shopt -s parse_brace
100
101 var m = ' 5 ' // 2
102
103 var n = ' 5 ' % 2
104
105 var p = ' 5 ' ** 2
106
107 write -- $m $n $p
108
109 try {
110 var z = 'a' // 3
111 }
112 echo _status $_status
113
114 try {
115 var z = 'z' % 3
116 }
117 echo _status $_status
118
119 ## STDOUT:
120 2
121 1
122 25
123 _status 3
124 _status 3
125 ## END
126
127 #### Division by zero
128 shopt -s parse_brace
129
130 try {
131 = 42 / 0
132 }
133 echo "status / is $_status"
134
135 try {
136 = 42 // 0
137 }
138 echo "status // is $_status"
139
140 try {
141 = 42 % 0
142 }
143 echo "status % is $_status"
144
145 ## STDOUT:
146 status / is 3
147 status // is 3
148 status % is 3
149 ## END
150
151 #### Unary Operations
152
153 var a = ~1
154
155 var b = -1
156 var c = -2.3
157
158 var d = not true
159
160
161 write -- $a $b $c $d
162
163 ## STDOUT:
164 -2
165 -1
166 -2.3
167 false
168 ## END
169
170
171 #### unary minus on strings
172 json write (-3)
173 json write (-'4')
174 json write (-'5.5')
175
176 # Not accepted
177 json write (-'abc')
178
179 ## status: 3
180 ## STDOUT:
181 -3
182 -4
183 -5.5
184 ## END
185
186 #### unary ~ complement on strings
187 json write (~0)
188 json write (~'1')
189 json write (~' 2 ')
190 # Not accepted
191 json write (~'3.5')
192
193 ## status: 3
194 ## STDOUT:
195 -1
196 -2
197 -3
198 ## END
199
200 #### unary ~ doesn't work on bool
201 = ~false
202 ## status: 3
203 ## STDOUT:
204 ## END
205
206 #### unary ~ doesn't work on float
207 = ~1.0
208 ## status: 3
209 ## STDOUT:
210 ## END
211
212 #### unary - applied to bool is not allowed
213 = ~false
214 ## status: 3
215 ## STDOUT:
216 ## END
217
218 #### Big float constants becomes inf and -inf, tiny become 0.0 and -0.0
219
220 $SH -c '
221 var x = 0.12345
222 pp test_ (x)
223 '
224 echo float=$?
225
226 $SH -c '
227 # Becomes infinity
228 var x = 0.123456789e1234567
229 pp test_ (x)
230
231 var x = -0.123456789e1234567
232 pp test_ (x)
233 '
234 echo float=$?
235
236 $SH -c '
237 # Becomes infinity
238 var x = 0.123456789e-1234567
239 pp test_ (x)
240
241 var x = -0.123456789e-1234567
242 pp test_ (x)
243 '
244 echo float=$?
245
246 ## STDOUT:
247 (Float) 0.12345
248 float=0
249 (Float) INFINITY
250 (Float) -INFINITY
251 float=0
252 (Float) 0.0
253 (Float) -0.0
254 float=0
255 ## END
256
257 #### Int constants bigger than 64 bits
258
259 # Decimal
260 $SH -c '
261 var x = 1111
262 pp test_ (x)
263 '
264 echo dec=$?
265
266 $SH -c '
267 var x = 1111_2222_3333_4444_5555_6666
268 pp test_ (x)
269 '
270 echo dec=$?
271
272 # Binary
273 $SH -c '
274 var x = 0b11
275 pp test_ (x)
276 '
277 echo bin=$?
278
279 $SH -c '
280 var x = 0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111
281 pp test_ (x)
282 '
283 echo bin=$?
284
285 # Octal
286 $SH -c '
287 var x = 0o77
288 pp test_ (x)
289 '
290 echo oct=$?
291
292 $SH -c '
293 var x = 0o1111_2222_3333_4444_5555_6666
294 pp test_ (x)
295 '
296 echo oct=$?
297
298 # Hex
299 $SH -c '
300 var x = 0xff
301 pp test_ (x)
302 '
303 echo hex=$?
304
305 $SH -c '
306 var x = 0xaaaa_bbbb_cccc_dddd_eeee_ffff
307 pp test_ (x)
308 '
309 echo hex=$?
310
311 ## STDOUT:
312 (Int) 1111
313 dec=0
314 dec=2
315 (Int) 3
316 bin=0
317 bin=2
318 (Int) 63
319 oct=0
320 oct=2
321 (Int) 255
322 hex=0
323 hex=2
324 ## END
325
326 #### Bit shift by negative number is not allowed
327
328 shopt -s ysh:upgrade
329
330 pp test_ (1 << 1)
331 pp test_ (1 << 0)
332 try {
333 pp test_ (1 << -1)
334 }
335 echo failed $[_error.code]
336 echo
337
338 pp test_ (16 >> 2)
339 pp test_ (16 >> 1)
340 pp test_ (16 >> 0)
341 try {
342 pp test_ (16 >> -1)
343 }
344 echo failed $[_error.code]
345
346 ## STDOUT:
347 (Int) 2
348 (Int) 1
349 failed 3
350
351 (Int) 4
352 (Int) 8
353 (Int) 16
354 failed 3
355 ## END
356
357 #### 64-bit operations
358
359 shopt -s ysh:upgrade
360
361 var i = 1 << 32
362 var s = str(i)
363
364 echo "i = $i, s = $s"
365
366 if (s ~== i) {
367 echo equal
368 }
369
370 ## STDOUT:
371 i = 4294967296, s = 4294967296
372 equal
373 ## END
374
375 #### 64-bit integer doesn't overflow
376
377 # same as spec/arith.test.sh case 38
378
379 var a= 1 << 31
380 echo $a
381
382 var b = a + a
383 echo $b
384
385 var c = b + a
386 echo $c
387
388 var x = 1 << 62
389 var y = x - 1
390 echo "max positive = $[ x + y ]"
391
392 #echo "overflow $[ x + x ]"
393
394 ## STDOUT:
395 2147483648
396 4294967296
397 6442450944
398 max positive = 9223372036854775807
399 ## END
400