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

369 lines, 181 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 line (x)
223'
224echo float=$?
225
226$SH -c '
227# Becomes infinity
228var x = 0.123456789e1234567
229pp line (x)
230
231var x = -0.123456789e1234567
232pp line (x)
233'
234echo float=$?
235
236$SH -c '
237# Becomes infinity
238var x = 0.123456789e-1234567
239pp line (x)
240
241var x = -0.123456789e-1234567
242pp line (x)
243'
244echo float=$?
245
246## STDOUT:
247(Float) 0.12345
248float=0
249(Float) inf
250(Float) -inf
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 line (x)
263'
264echo dec=$?
265
266$SH -c '
267var x = 1111_2222_3333_4444_5555_6666
268pp line (x)
269'
270echo dec=$?
271
272# Binary
273$SH -c '
274var x = 0b11
275pp line (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 line (x)
282'
283echo bin=$?
284
285# Octal
286$SH -c '
287var x = 0o77
288pp line (x)
289'
290echo oct=$?
291
292$SH -c '
293var x = 0o1111_2222_3333_4444_5555_6666
294pp line (x)
295'
296echo oct=$?
297
298# Hex
299$SH -c '
300var x = 0xff
301pp line (x)
302'
303echo hex=$?
304
305$SH -c '
306var x = 0xaaaa_bbbb_cccc_dddd_eeee_ffff
307pp line (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#### 64-bit operations
327
328shopt -s ysh:upgrade
329
330var i = 1 << 32
331var s = str(i)
332
333echo "i = $i, s = $s"
334
335if (s ~== i) {
336 echo equal
337}
338
339## STDOUT:
340i = 4294967296, s = 4294967296
341equal
342## END
343
344#### 64-bit integer doesn't overflow
345
346# same as spec/arith.test.sh case 38
347
348var a= 1 << 31
349echo $a
350
351var b = a + a
352echo $b
353
354var c = b + a
355echo $c
356
357var x = 1 << 62
358var y = x - 1
359echo "max positive = $[ x + y ]"
360
361#echo "overflow $[ x + x ]"
362
363## STDOUT:
3642147483648
3654294967296
3666442450944
367max positive = 9223372036854775807
368## END
369