about summary refs log tree commit diff
path: root/Test/C01arith.ztst
blob: 4c55b961e08d2dd04aa3501868f008cee8256361 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# Tests corresponding to the texinfo node `Arithmetic Evaluation'

%test

  integer light there
  (( light = 42 )) &&
  let 'there = light' &&
  print $(( there ))
0:basic integer arithmetic
>42

  float light there
  integer rnd
  (( light = 3.1415 )) &&
  let 'there = light' &&
  print -- $(( rnd = there * 10000 ))
# save rounding problems by converting to integer
0:basic floating point arithmetic
>31415.

  print $(( 0x10 + 0X01 + 2#1010 ))
0:base input
>27

  float light
  (( light = 4 ))
  print $light
  typeset -F light
  print $light
0:conversion to float
>4.000000000e+00
>4.0000000000

  integer i
  (( i = 32.5 ))
  print $i
0:conversion to int
>32

  integer i
  (( i = 4 - - 3 * 7 << 1 & 7 ^ 1 | 16 ** 2 ))
  print $i
0:precedence (arithmetic)
>1591

  fn() {
    setopt localoptions c_precedences
    integer i
    (( i = 4 - - 3 * 7 << 1 & 7 ^ 1 | 16 ** 2 ))
    print $i
  }
  fn
0:precedence (arithmetic, with C_PRECEDENCES)
>259

  print $(( 1 < 2 || 2 < 2 && 3 > 4 ))
0:precedence (logical)
>1

  print $(( 1 + 4 ? 3 + 2 ? 4 + 3 ? 5 + 6 ? 4 * 8 : 0 : 0 : 0 : 0 ))
0:precedence (ternary)
>32

  print $(( 3 ? 2 ))
1:parsing ternary (1)
?(eval):1: ':' expected

  print $(( 3 ? 2 : 1 : 4 ))
1:parsing ternary (2)
?(eval):1: ':' without '?'

  print $(( 0, 4 ? 3 : 1, 5 ))
0:comma operator
>5

  foo=000
  print $(( ##A + ##\C-a + #foo + $#foo ))
0:#, ## and $#
>117

  print $((##))
1:## without following character
?(eval):1: character missing after ##

  print $((## ))
0:## followed by a space
>32

  integer i
  (( i = 3 + 5 * 1.75 ))
  print $i
0:promotion to float
>11

  typeset x      &&
  (( x = 3.5 ))  &&
  print $x       &&
  (( x = 4 ))    &&
  print $x
0:use of scalars to store integers and floats
>3.5
>4

  (( newarray[unsetvar] = 1 ))
2:error using unset variable as index
?(eval):1: newarray: assignment to invalid subscript range

  integer setvar=1
  (( newarray[setvar]++ ))
  (( newarray[setvar]++ ))
  print ${(t)newarray} ${#newarray} ${newarray[1]}
0:setting array elements in math context
>array 1 2

  xarr=()
  (( xarr = 3 ))
  print ${(t)xarr} $xarr
0:converting type from array
>integer 3

  print $(( 13 = 42 ))
1:bad lvalue
?(eval):1: lvalue required

  x=/bar
  (( x = 32 ))
  print $x
0:assigning to scalar which contains non-math string
>32

  print $(( ))
0:empty math parse e.g. $(( )) acts like a zero
>0

  print $(( a = ))
1:empty assignment
?(eval):1: bad math expression: operand expected at end of string

  print $(( 3, ))
1:empty right hand of comma
?(eval):1: bad math expression: operand expected at end of string

  print $(( 3,,4 ))
1:empty middle of comma
?(eval):1: bad math expression: operand expected at `,4 '

  print $(( (3 + 7, 4), 5 ))
0:commas and parentheses, part 1
>5

  print $(( 5, (3 + 7, 4) ))
0:commas and parentheses, part 1
>4

  print $(( 07.5 ))
  (setopt octalzeroes; print $(( 09.5 )))
0:leading zero doesn't affect floating point
>7.5
>9.5

  (setopt octalzeroes; print $(( 09 )))
1:octalzeroes rejects invalid constants
?(eval):1: bad math expression: operator expected at `9 '

  (setopt octalzeroes; print $(( 08#77 )))
0:octalzeroes doesn't affect bases
>63

  print $(( 36#z ))
0:bases up to 36 work
>35

  print $(( 37#z ))
1:bases beyond 36 don't work
?(eval):1: invalid base (must be 2 to 36 inclusive): 37

  print $(( 3 + "fail" ))
1:parse failure in arithmetic
?(eval):1: bad math expression: operand expected at `"fail" '

  alias 3=echo
  print $(( 3 + "OK"); echo "Worked")
0:not a parse failure because not arithmetic
>+ OK Worked

  fn() {
    emulate -L zsh
    print $(( [#16] 255 ))
    print $(( [##16] 255 ))
    setopt cbases
    print $(( [#16] 255 ))
    print $(( [##16] 255 ))
  }
  fn
0:doubled # in base removes radix
>16#FF
>FF
>0xFF
>FF

  array=(1)
  x=0
  (( array[++x]++ ))
  print $x
  print $#array
  print $array
0:no double increment for subscript
>1
>1
>2

  # This is a bit naughty...  the value of array
  # isn't well defined since there's no sequence point
  # between the increments of x, however we just want
  # to be sure that in this case, unlike the above,
  # x does get incremented twice.
  x=0
  array=(1 2)
  (( array[++x] = array[++x] + 1 ))
  print $x
0:double increment for repeated expression
>2

  # Floating point.  Default precision should take care of rounding errors.
  print $(( 1_0.000_000e0_1 ))
  # Integer.
  print $(( 0x_ff_ff_ ))
  # _ are parts of variable names that don't start with a digit
  __myvar__=42
  print $(( __myvar__ + $__myvar__ ))
  # _ is not part of variable name that does start with a digit
  # (which are substituted before math eval)
  set -- 6
  print $(( $1_000_000 ))
  # Underscores in expressions with no whitespace
  print $(( 3_000_+4_000_/2 ))
  # Underscores may appear in the base descriptor, for what it's worth...
  print $(( 1_6_#f_f_ ))
0:underscores in math constants
>100.
>65535
>84
>6000000
>5000
>255

  # Force floating point.
  for expr in "3/4" "0x100/0x200" "0x30/0x10"; do
    print $(( $expr ))
    setopt force_float
    print $(( $expr ))
    unsetopt force_float
  done
0:Forcing floating point constant evaluation, or not.
>0
>0.75
>0
>0.5
>3
>3.

  print $(( 0x30 + 0.5 ))
  print $(( 077 + 0.5 ))
  (setopt octalzeroes; print $(( 077 + 0.5 )) )
0:Mixed float and non-decimal integer constants
>48.5
>77.5
>63.5

  underscore_integer() {
    setopt cbases localoptions
    print $(( [#_] 1000000 ))
    print $(( [#16_] 65536 ))
    print $(( [#16_4] 65536 * 32768 ))
  }
  underscore_integer
0:Grouping output with underscores: integers
>1_000_000
>0x10_000
>0x8000_0000

  print $(( [#_] (5. ** 10) / 16. ))
0:Grouping output with underscores: floating point
>610_351.562_5

  env SHLVL=1+RANDOM $ZTST_testdir/../Src/zsh -f -c 'print $SHLVL'
0:Imported integer functions are not evaluated
>2