OILS / spec / ysh-expr-arith.test.sh View on Github | oils.pub

660 lines, 359 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#### Unary plus on integers and floats
171
172var a = +1
173var b = +42
174var c = +2.5
175
176var d = -10
177var e = +d
178
179write -- $a $b $c $e
180
181## STDOUT:
1821
18342
1842.5
185-10
186## END
187
188#### unary plus and minus combined
189
190var a = +-5
191var b = -+5
192var c = +(-3)
193
194write -- $a $b $c
195
196## STDOUT:
197-5
198-5
199-3
200## END
201
202#### unary minus on strings
203json write (-3)
204json write (-'4')
205json write (-'5.5')
206
207# Not accepted
208json write (-'abc')
209
210## status: 3
211## STDOUT:
212-3
213-4
214-5.5
215## END
216
217#### unary plus on strings
218json write (+3)
219json write (+'4')
220json write (+'5.5')
221
222# Not accepted
223json write (+'abc')
224
225## status: 3
226## STDOUT:
2273
2284
2295.5
230## END
231
232#### unary ~ complement on strings
233json write (~0)
234json write (~'1')
235json write (~' 2 ')
236# Not accepted
237json write (~'3.5')
238
239## status: 3
240## STDOUT:
241-1
242-2
243-3
244## END
245
246#### unary ~ doesn't work on bool
247= ~false
248## status: 3
249## STDOUT:
250## END
251
252#### unary ~ doesn't work on float
253= ~1.0
254## status: 3
255## STDOUT:
256## END
257
258#### unary - applied to bool is not allowed
259= ~false
260## status: 3
261## STDOUT:
262## END
263
264#### Big float constants becomes inf and -inf, tiny become 0.0 and -0.0
265
266$SH -c '
267var x = 0.12345
268pp test_ (x)
269'
270echo float=$?
271
272$SH -c '
273# Becomes infinity
274var x = 0.123456789e1234567
275pp test_ (x)
276
277var x = -0.123456789e1234567
278pp test_ (x)
279'
280echo float=$?
281
282$SH -c '
283# Becomes infinity
284var x = 0.123456789e-1234567
285pp test_ (x)
286
287var x = -0.123456789e-1234567
288pp test_ (x)
289'
290echo float=$?
291
292## STDOUT:
293(Float) 0.12345
294float=0
295(Float) INFINITY
296(Float) -INFINITY
297float=0
298(Float) 0.0
299(Float) -0.0
300float=0
301## END
302
303#### Int constants bigger than 64 bits
304
305# Decimal
306$SH -c '
307var x = 1111
308pp test_ (x)
309'
310echo dec=$?
311
312$SH -c '
313var x = 1111_2222_3333_4444_5555_6666
314pp test_ (x)
315'
316echo dec=$?
317
318# Binary
319$SH -c '
320var x = 0b11
321pp test_ (x)
322'
323echo bin=$?
324
325$SH -c '
326var x = 0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111
327pp test_ (x)
328'
329echo bin=$?
330
331# Octal
332$SH -c '
333var x = 0o77
334pp test_ (x)
335'
336echo oct=$?
337
338$SH -c '
339var x = 0o1111_2222_3333_4444_5555_6666
340pp test_ (x)
341'
342echo oct=$?
343
344# Hex
345$SH -c '
346var x = 0xff
347pp test_ (x)
348'
349echo hex=$?
350
351$SH -c '
352var x = 0xaaaa_bbbb_cccc_dddd_eeee_ffff
353pp test_ (x)
354'
355echo hex=$?
356
357## STDOUT:
358(Int) 1111
359dec=0
360dec=2
361(Int) 3
362bin=0
363bin=2
364(Int) 63
365oct=0
366oct=2
367(Int) 255
368hex=0
369hex=2
370## END
371
372#### Bit shift by negative number is not allowed
373
374shopt -s ysh:upgrade
375
376pp test_ (1 << 1)
377pp test_ (1 << 0)
378try {
379 pp test_ (1 << -1)
380}
381echo failed $[_error.code]
382echo
383
384pp test_ (16 >> 2)
385pp test_ (16 >> 1)
386pp test_ (16 >> 0)
387try {
388 pp test_ (16 >> -1)
389}
390echo failed $[_error.code]
391
392## STDOUT:
393(Int) 2
394(Int) 1
395failed 3
396
397(Int) 4
398(Int) 8
399(Int) 16
400failed 3
401## END
402
403#### 64-bit operations
404
405shopt -s ysh:upgrade
406
407var i = 1 << 32
408var s = str(i)
409
410echo "i = $i, s = $s"
411
412if (s ~== i) {
413 echo equal
414}
415
416## STDOUT:
417i = 4294967296, s = 4294967296
418equal
419## END
420
421#### 64-bit integer doesn't overflow
422
423# same as spec/arith.test.sh case 38
424
425var a= 1 << 31
426echo $a
427
428var b = a + a
429echo $b
430
431var c = b + a
432echo $c
433
434var x = 1 << 62
435var y = x - 1
436echo "max positive = $[ x + y ]"
437
438#echo "overflow $[ x + x ]"
439
440## STDOUT:
4412147483648
4424294967296
4436442450944
444max positive = 9223372036854775807
445## END
446
447#### Integer literals
448var d = 123
449var b = 0b11
450var o = 0o123
451var h = 0xff
452echo $d $b $o $h
453## STDOUT:
454123 3 83 255
455## END
456
457#### Integer literals with underscores
458const dec = 65_536
459const bin = 0b0001_0101
460const oct = 0o001_755
461const hex = 0x0001_000f
462
463echo SHELL
464echo $dec
465echo $bin
466echo $oct
467echo $hex
468const x = 1_1 + 0b1_1 + 0o1_1 + 0x1_1
469echo sum $x
470
471# This works under Python 3.6, but the continuous build has earlier versions
472if false; then
473 echo ---
474 echo PYTHON
475
476 python3 -c '
477 print(65_536)
478 print(0b0001_0101)
479 print(0o001_755)
480 print(0x0001_000f)
481
482 # Weird syntax
483 print("sum", 1_1 + 0b1_1 + 0o1_1 + 0x1_1)
484 '
485fi
486
487## STDOUT:
488SHELL
48965536
49021
4911005
49265551
493sum 40
494## END
495
496#### Exponentiation with **
497var x = 2**3
498echo $x
499
500var y = 2.0 ** 3.0 # NOT SUPPORTED
501echo 'should not get here'
502
503## status: 3
504## STDOUT:
5058
506## END
507
508#### Float Division
509pp test_ (5/2)
510pp test_ (-5/2)
511pp test_ (5/-2)
512pp test_ (-5/-2)
513
514echo ---
515
516var x = 9
517setvar x /= 2
518pp test_ (x)
519
520var x = -9
521setvar x /= 2
522pp test_ (x)
523
524var x = 9
525setvar x /= -2
526pp test_ (x)
527
528var x = -9
529setvar x /= -2
530pp test_ (x)
531
532
533## STDOUT:
534(Float) 2.5
535(Float) -2.5
536(Float) -2.5
537(Float) 2.5
538---
539(Float) 4.5
540(Float) -4.5
541(Float) -4.5
542(Float) 4.5
543## END
544
545#### Integer Division (rounds toward zero)
546pp test_ (5//2)
547pp test_ (-5//2)
548pp test_ (5//-2)
549pp test_ (-5//-2)
550
551echo ---
552
553var x = 9
554setvar x //= 2
555pp test_ (x)
556
557var x = -9
558setvar x //= 2
559pp test_ (x)
560
561var x = 9
562setvar x //= -2
563pp test_ (x)
564
565var x = -9
566setvar x //= -2
567pp test_ (x)
568
569## STDOUT:
570(Int) 2
571(Int) -2
572(Int) -2
573(Int) 2
574---
575(Int) 4
576(Int) -4
577(Int) -4
578(Int) 4
579## END
580
581#### % operator is remainder
582pp test_ ( 5 % 3)
583pp test_ (-5 % 3)
584
585# negative divisor illegal (tested in test/ysh-runtime-errors.sh)
586#pp test_ ( 5 % -3)
587#pp test_ (-5 % -3)
588
589var z = 10
590setvar z %= 3
591pp test_ (z)
592
593var z = -10
594setvar z %= 3
595pp test_ (z)
596
597## STDOUT:
598(Int) 2
599(Int) -2
600(Int) 1
601(Int) -1
602## END
603
604#### Bitwise logical
605var a = 0b0101 & 0b0011
606echo $a
607var b = 0b0101 | 0b0011
608echo $b
609var c = 0b0101 ^ 0b0011
610echo $c
611var d = ~b
612echo $d
613## STDOUT:
6141
6157
6166
617-8
618## END
619
620#### Shift operators
621var a = 1 << 4
622echo $a
623var b = 16 >> 4
624echo $b
625## STDOUT:
62616
6271
628## END
629
630#### multiline strings, list, tuple syntax for list, etc.
631var dq = "
632dq
6332
634"
635echo dq=$[len(dq)]
636
637var sq = '
638sq
6392
640'
641echo sq=$[len(sq)]
642
643var mylist = [
644 1,
645 2,
646 3,
647]
648echo mylist=$[len(mylist)]
649
650var mytuple = (1,
651 2, 3)
652echo mytuple=$[len(mytuple)]
653
654## STDOUT:
655dq=6
656sq=6
657mylist=3
658mytuple=3
659## END
660