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

400 lines, 203 significant
1## oils_failures_allowed: 1
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