26754: tweak zgetdir() and test for realpath()
[zsh.git] / Test / C01arith.ztst
blob02612bd7993b159a9a0a50401fdffbec665f1cdd
1 # Tests corresponding to the texinfo node `Arithmetic Evaluation'
3 %test
5   integer light there
6   (( light = 42 )) &&
7   let 'there = light' &&
8   print $(( there ))
9 0:basic integer arithmetic
10 >42
12   float light there
13   integer rnd
14   (( light = 3.1415 )) &&
15   let 'there = light' &&
16   print -- $(( rnd = there * 10000 ))
17 # save rounding problems by converting to integer
18 0:basic floating point arithmetic
19 >31415.
21   print $(( 0x10 + 0X01 + 2#1010 ))
22 0:base input
23 >27
25   float light
26   (( light = 4 ))
27   print $light
28   typeset -F light
29   print $light
30 0:conversion to float
31 >4.000000000e+00
32 >4.0000000000
34   integer i
35   (( i = 32.5 ))
36   print $i
37 0:conversion to int
38 >32
40   integer i
41   (( i = 4 - - 3 * 7 << 1 & 7 ^ 1 | 16 ** 2 ))
42   print $i
43 0:precedence (arithmetic)
44 >1591
46   fn() {
47     setopt localoptions c_precedences
48     integer i
49     (( i = 4 - - 3 * 7 << 1 & 7 ^ 1 | 16 ** 2 ))
50     print $i
51   }
52   fn
53 0:precedence (arithmetic, with C_PRECEDENCES)
54 >259
56   print $(( 1 < 2 || 2 < 2 && 3 > 4 ))
57 0:precedence (logical)
60   print $(( 1 + 4 ? 3 + 2 ? 4 + 3 ? 5 + 6 ? 4 * 8 : 0 : 0 : 0 : 0 ))
61 0:precedence (ternary)
62 >32
64   print $(( 3 ? 2 ))
65 1:parsing ternary (1)
66 ?(eval):1: ':' expected
68   print $(( 3 ? 2 : 1 : 4 ))
69 1:parsing ternary (2)
70 ?(eval):1: ':' without '?'
72   print $(( 0, 4 ? 3 : 1, 5 ))
73 0:comma operator
76   foo=000
77   print $(( ##A + ##\C-a + #foo + $#foo ))
78 0:#, ## and $#
79 >117
81   print $((##))
82 1:## without following character
83 ?(eval):1: character missing after ##
85   print $((## ))
86 0:## followed by a space
87 >32
89   integer i
90   (( i = 3 + 5 * 1.75 ))
91   print $i
92 0:promotion to float
93 >11
95   typeset x      &&
96   (( x = 3.5 ))  &&
97   print $x       &&
98   (( x = 4 ))    &&
99   print $x
100 0:use of scalars to store integers and floats
101 >3.5
104   (( newarray[unsetvar] = 1 ))
105 2:error using unset variable as index
106 ?(eval):1: newarray: assignment to invalid subscript range
108   integer setvar=1
109   (( newarray[setvar]++ ))
110   (( newarray[setvar]++ ))
111   print ${(t)newarray} ${#newarray} ${newarray[1]}
112 0:setting array elements in math context
113 >array 1 2
115   xarr=()
116   (( xarr = 3 ))
117   print ${(t)xarr} $xarr
118 0:converting type from array
119 >integer 3
121   print $(( 13 = 42 ))
122 1:bad lvalue
123 ?(eval):1: lvalue required
125   x=/bar
126   (( x = 32 ))
127   print $x
128 0:assigning to scalar which contains non-math string
131   print $(( ))
132 0:empty math parse e.g. $(( )) acts like a zero
135   print $(( a = ))
136 1:empty assignment
137 ?(eval):1: bad math expression: operand expected at `'
139   print $(( 3, ))
140 1:empty right hand of comma
141 ?(eval):1: bad math expression: operand expected at `'
143   print $(( 3,,4 ))
144 1:empty middle of comma
145 ?(eval):1: bad math expression: operand expected at `,4 '
147   print $(( (3 + 7, 4), 5 ))
148 0:commas and parentheses, part 1
151   print $(( 5, (3 + 7, 4) ))
152 0:commas and parentheses, part 1
155   (setopt octalzeroes; print $(( 08#77 )))
156 0:octalzeroes doesn't affect bases
159   print $(( 36#z ))
160 0:bases up to 36 work
163   print $(( 37#z ))
164 1:bases beyond 36 don't work
165 ?(eval):1: invalid base (must be 2 to 36 inclusive): 37
167   print $(( 3 + "fail" ))
168 1:parse failure in arithmetic
169 ?(eval):1: bad math expression: operand expected at `"fail" '
171   alias 3=echo
172   print $(( 3 + "OK"); echo "Worked")
173 0:not a parse failure because not arithmetic
174 >+ OK Worked
176   fn() {
177     emulate -L zsh
178     print $(( [#16] 255 ))
179     print $(( [##16] 255 ))
180     setopt cbases
181     print $(( [#16] 255 ))
182     print $(( [##16] 255 ))
183   }
184   fn
185 0:doubled # in base removes radix
186 >16#FF
188 >0xFF