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

614 lines, 335 significant
1## oils_failures_allowed: 0
2
3#### Minus operator is left associative
4
5var a = 1 - 0 - 1
6var b = (1 - 0) - 1
7echo a=$a b=$b
8
9var a = 3 - 1 - 2
10var b = (3 - 1) - 2
11echo a=$a b=$b
12
13## STDOUT:
14a=0 b=0
15a=0 b=0
16## END
17
18#### Division operators are left associative
19
20var a = 10 / 4 / 2
21var b = 10 / 4 / 2
22echo a=$a b=$b
23
24var a = 9 // 3 // 3
25var b = (9 // 3) // 3
26echo a=$a b=$b
27
28var a = 11 % 6 % 3
29var b = (11 % 6) % 3
30echo a=$a b=$b
31
32## STDOUT:
33a=1.25 b=1.25
34a=1 b=1
35a=2 b=2
36## END
37
38#### Exponentiation is right associative
39
40var a = 3 ** 2 ** 2
41var b = 3 ** (2 ** 2)
42echo a=$a b=$b
43
44## STDOUT:
45a=81 b=81
46## END
47
48#### Binary operators, with conversions from string
49
50echo ' i i' $[1 + 2]
51echo 'si i' $['1' + 2]
52echo ' i si' $[1 + '2']
53echo ---
54
55echo ' f f' $[2.5 - 1.5]
56echo 'sf f' $['2.5' - 1.5]
57echo ' f sf' $[2.5 - '1.5']
58echo ---
59
60echo ' i f' $[4 * 1.5]
61echo 'si f' $['4' * 1.5]
62echo ' i sf' $[4 * '1.5']
63echo ---
64
65echo ' f i' $[5.0 / 2]
66echo 'sf i' $['5.0' / 2]
67echo ' f si' $[5.0 / '2']
68
69## STDOUT:
70 i i 3
71si i 3
72 i si 3
73---
74 f f 1.0
75sf f 1.0
76 f sf 1.0
77---
78 i f 6.0
79si f 6.0
80 i sf 6.0
81---
82 f i 2.5
83sf i 2.5
84 f si 2.5
85## END
86
87#### Floating Point Division with /
88
89var i = '1.0' / '0.05'
90
91echo $i
92
93## STDOUT:
9420.0
95## END
96
97
98#### Operations That Convert to Integer: // % **
99shopt -s parse_brace
100
101var m = ' 5 ' // 2
102
103var n = ' 5 ' % 2
104
105var p = ' 5 ' ** 2
106
107write -- $m $n $p
108
109try {
110 var z = 'a' // 3
111}
112echo _status $_status
113
114try {
115 var z = 'z' % 3
116}
117echo _status $_status
118
119## STDOUT:
1202
1211
12225
123_status 3
124_status 3
125## END
126
127#### Division by zero
128shopt -s parse_brace
129
130try {
131 = 42 / 0
132}
133echo "status / is $_status"
134
135try {
136 = 42 // 0
137}
138echo "status // is $_status"
139
140try {
141 = 42 % 0
142}
143echo "status % is $_status"
144
145## STDOUT:
146status / is 3
147status // is 3
148status % is 3
149## END
150
151#### Unary Operations
152
153var a = ~1
154
155var b = -1
156var c = -2.3
157
158var d = not true
159
160
161write -- $a $b $c $d
162
163## STDOUT:
164-2
165-1
166-2.3
167false
168## END
169
170
171#### unary minus on strings
172json write (-3)
173json write (-'4')
174json write (-'5.5')
175
176# Not accepted
177json write (-'abc')
178
179## status: 3
180## STDOUT:
181-3
182-4
183-5.5
184## END
185
186#### unary ~ complement on strings
187json write (~0)
188json write (~'1')
189json write (~' 2 ')
190# Not accepted
191json 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 '
221var x = 0.12345
222pp test_ (x)
223'
224echo float=$?
225
226$SH -c '
227# Becomes infinity
228var x = 0.123456789e1234567
229pp test_ (x)
230
231var x = -0.123456789e1234567
232pp test_ (x)
233'
234echo float=$?
235
236$SH -c '
237# Becomes infinity
238var x = 0.123456789e-1234567
239pp test_ (x)
240
241var x = -0.123456789e-1234567
242pp test_ (x)
243'
244echo float=$?
245
246## STDOUT:
247(Float) 0.12345
248float=0
249(Float) INFINITY
250(Float) -INFINITY
251float=0
252(Float) 0.0
253(Float) -0.0
254float=0
255## END
256
257#### Int constants bigger than 64 bits
258
259# Decimal
260$SH -c '
261var x = 1111
262pp test_ (x)
263'
264echo dec=$?
265
266$SH -c '
267var x = 1111_2222_3333_4444_5555_6666
268pp test_ (x)
269'
270echo dec=$?
271
272# Binary
273$SH -c '
274var x = 0b11
275pp test_ (x)
276'
277echo bin=$?
278
279$SH -c '
280var x = 0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111
281pp test_ (x)
282'
283echo bin=$?
284
285# Octal
286$SH -c '
287var x = 0o77
288pp test_ (x)
289'
290echo oct=$?
291
292$SH -c '
293var x = 0o1111_2222_3333_4444_5555_6666
294pp test_ (x)
295'
296echo oct=$?
297
298# Hex
299$SH -c '
300var x = 0xff
301pp test_ (x)
302'
303echo hex=$?
304
305$SH -c '
306var x = 0xaaaa_bbbb_cccc_dddd_eeee_ffff
307pp test_ (x)
308'
309echo hex=$?
310
311## STDOUT:
312(Int) 1111
313dec=0
314dec=2
315(Int) 3
316bin=0
317bin=2
318(Int) 63
319oct=0
320oct=2
321(Int) 255
322hex=0
323hex=2
324## END
325
326#### Bit shift by negative number is not allowed
327
328shopt -s ysh:upgrade
329
330pp test_ (1 << 1)
331pp test_ (1 << 0)
332try {
333 pp test_ (1 << -1)
334}
335echo failed $[_error.code]
336echo
337
338pp test_ (16 >> 2)
339pp test_ (16 >> 1)
340pp test_ (16 >> 0)
341try {
342 pp test_ (16 >> -1)
343}
344echo failed $[_error.code]
345
346## STDOUT:
347(Int) 2
348(Int) 1
349failed 3
350
351(Int) 4
352(Int) 8
353(Int) 16
354failed 3
355## END
356
357#### 64-bit operations
358
359shopt -s ysh:upgrade
360
361var i = 1 << 32
362var s = str(i)
363
364echo "i = $i, s = $s"
365
366if (s ~== i) {
367 echo equal
368}
369
370## STDOUT:
371i = 4294967296, s = 4294967296
372equal
373## END
374
375#### 64-bit integer doesn't overflow
376
377# same as spec/arith.test.sh case 38
378
379var a= 1 << 31
380echo $a
381
382var b = a + a
383echo $b
384
385var c = b + a
386echo $c
387
388var x = 1 << 62
389var y = x - 1
390echo "max positive = $[ x + y ]"
391
392#echo "overflow $[ x + x ]"
393
394## STDOUT:
3952147483648
3964294967296
3976442450944
398max positive = 9223372036854775807
399## END
400
401#### Integer literals
402var d = 123
403var b = 0b11
404var o = 0o123
405var h = 0xff
406echo $d $b $o $h
407## STDOUT:
408123 3 83 255
409## END
410
411#### Integer literals with underscores
412const dec = 65_536
413const bin = 0b0001_0101
414const oct = 0o001_755
415const hex = 0x0001_000f
416
417echo SHELL
418echo $dec
419echo $bin
420echo $oct
421echo $hex
422const x = 1_1 + 0b1_1 + 0o1_1 + 0x1_1
423echo sum $x
424
425# This works under Python 3.6, but the continuous build has earlier versions
426if false; then
427 echo ---
428 echo PYTHON
429
430 python3 -c '
431 print(65_536)
432 print(0b0001_0101)
433 print(0o001_755)
434 print(0x0001_000f)
435
436 # Weird syntax
437 print("sum", 1_1 + 0b1_1 + 0o1_1 + 0x1_1)
438 '
439fi
440
441## STDOUT:
442SHELL
44365536
44421
4451005
44665551
447sum 40
448## END
449
450#### Exponentiation with **
451var x = 2**3
452echo $x
453
454var y = 2.0 ** 3.0 # NOT SUPPORTED
455echo 'should not get here'
456
457## status: 3
458## STDOUT:
4598
460## END
461
462#### Float Division
463pp test_ (5/2)
464pp test_ (-5/2)
465pp test_ (5/-2)
466pp test_ (-5/-2)
467
468echo ---
469
470var x = 9
471setvar x /= 2
472pp test_ (x)
473
474var x = -9
475setvar x /= 2
476pp test_ (x)
477
478var x = 9
479setvar x /= -2
480pp test_ (x)
481
482var x = -9
483setvar x /= -2
484pp test_ (x)
485
486
487## STDOUT:
488(Float) 2.5
489(Float) -2.5
490(Float) -2.5
491(Float) 2.5
492---
493(Float) 4.5
494(Float) -4.5
495(Float) -4.5
496(Float) 4.5
497## END
498
499#### Integer Division (rounds toward zero)
500pp test_ (5//2)
501pp test_ (-5//2)
502pp test_ (5//-2)
503pp test_ (-5//-2)
504
505echo ---
506
507var x = 9
508setvar x //= 2
509pp test_ (x)
510
511var x = -9
512setvar x //= 2
513pp test_ (x)
514
515var x = 9
516setvar x //= -2
517pp test_ (x)
518
519var x = -9
520setvar x //= -2
521pp test_ (x)
522
523## STDOUT:
524(Int) 2
525(Int) -2
526(Int) -2
527(Int) 2
528---
529(Int) 4
530(Int) -4
531(Int) -4
532(Int) 4
533## END
534
535#### % operator is remainder
536pp test_ ( 5 % 3)
537pp test_ (-5 % 3)
538
539# negative divisor illegal (tested in test/ysh-runtime-errors.sh)
540#pp test_ ( 5 % -3)
541#pp test_ (-5 % -3)
542
543var z = 10
544setvar z %= 3
545pp test_ (z)
546
547var z = -10
548setvar z %= 3
549pp test_ (z)
550
551## STDOUT:
552(Int) 2
553(Int) -2
554(Int) 1
555(Int) -1
556## END
557
558#### Bitwise logical
559var a = 0b0101 & 0b0011
560echo $a
561var b = 0b0101 | 0b0011
562echo $b
563var c = 0b0101 ^ 0b0011
564echo $c
565var d = ~b
566echo $d
567## STDOUT:
5681
5697
5706
571-8
572## END
573
574#### Shift operators
575var a = 1 << 4
576echo $a
577var b = 16 >> 4
578echo $b
579## STDOUT:
58016
5811
582## END
583
584#### multiline strings, list, tuple syntax for list, etc.
585var dq = "
586dq
5872
588"
589echo dq=$[len(dq)]
590
591var sq = '
592sq
5932
594'
595echo sq=$[len(sq)]
596
597var mylist = [
598 1,
599 2,
600 3,
601]
602echo mylist=$[len(mylist)]
603
604var mytuple = (1,
605 2, 3)
606echo mytuple=$[len(mytuple)]
607
608## STDOUT:
609dq=6
610sq=6
611mylist=3
612mytuple=3
613## END
614