Trim bootstrap jimsh
[jimtcl.git] / tests / expr-pow.test
blob6389b793d88b3b10cd8ffa313db153cbc13cb306
1 # Commands covered: expr
3 # This file contains a collection of tests for one or more of the Tcl
4 # built-in commands. Sourcing this file into Tcl runs the tests and
5 # generates output for errors.  No output means no errors were found.
7 # Copyright (c) 1996-1997 Sun Microsystems, Inc.
8 # Copyright (c) 1998-1999 by Scriptics Corporation.
10 # See the file "license.terms" for information on usage and redistribution
11 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 # RCS: @(#) $Id: expr.test,v 1.9 2000/04/10 17:18:59 ericm Exp $
15 source [file dirname [info script]]/testing.tcl
17 # Jim Tcl may have no math functions, and may not have specific math functions
18 if {[catch {expr pow(1,0)}]} {
19     testConstraint pow 0
20 } else {
21     testConstraint pow 1
24 # Tests for exponentiation handling
25 test expr-23.1 {CompileExponentialExpr: just exponential expr} {expr 4**2} 16
26 test expr-23.2 {CompileExponentialExpr: just exponential expr} {expr 0xff**2} 65025
27 test expr-23.3 {CompileExponentialExpr: just exponential expr} {expr -1**2} 1
28 test expr-23.4 {CompileExponentialExpr: just exponential expr} {expr 18**07} 612220032
29 test expr-23.5 {CompileExponentialExpr: error in exponential expr} -body {
30     expr x**3
31 } -returnCodes error -match glob -result *
32 test expr-23.6 {CompileExponentialExpr: simple expo exprs} {expr 0xff**0x3} 16581375
33 test expr-23.7 {CompileExponentialExpr: error compiling expo arm} -body {
34     expr (-3-)**6
35 } -returnCodes error -match glob -result *
36 test expr-23.8 {CompileExponentialExpr: error compiling expo arm} -body {
37     expr 2**x
38 } -returnCodes error -match glob -result *
39 test expr-23.9 {CompileExponentialExpr: runtime error} tcl {
40     list [catch {expr {24.0**"xx"}} msg] $msg
41 } {1 {can't use non-numeric string as operand of "**"}}
42 test expr-23.10 {CompileExponentialExpr: runtime error} tcl {
43     list [catch {expr {"a"**2}} msg] $msg
44 } {1 {can't use non-numeric string as operand of "**"}}
45 test expr-23.11 {CompileExponentialExpr: runtime error} {
46     list [catch {expr {0**-1}} msg] $msg
47 } {1 {exponentiation of zero by negative power}}
48 test expr-23.12 {CompileExponentialExpr: runtime error} tcl {
49     list [catch {expr {0.0**-1.0}} msg] $msg
50 } {1 {exponentiation of zero by negative power}}
51 test expr-23.13 {CompileExponentialExpr: runtime error} {
52     list [catch {expr {wide(0)**wide(-1)}} msg] $msg
53 } {1 {exponentiation of zero by negative power}}
54 test expr-23.14 {INST_EXPON: special cases} {expr {0**1}} 0
55 test expr-23.15 {INST_EXPON: special cases} {expr {0**0}} 1
56 test expr-23.16 {INST_EXPON: special cases} {expr {-2**-1}} 0
57 test expr-23.17 {INST_EXPON: special cases} {expr {-2**0}} 1
58 test expr-23.18 {INST_EXPON: special cases} {expr {-1**1}} -1
59 test expr-23.19 {INST_EXPON: special cases} {expr {-1**0}} 1
60 test expr-23.20 {INST_EXPON: special cases} {expr {-1**2}} 1
61 test expr-23.21 {INST_EXPON: special cases} {expr {-1**-1}} -1
62 test expr-23.22 {INST_EXPON: special cases} {expr {1**1234567}} 1
63 test expr-23.23 {INST_EXPON: special cases} {expr {2**-2}} 0
64 test expr-23.24 {INST_EXPON: special cases} {expr {wide(0)**wide(1)}} 0
65 test expr-23.25 {INST_EXPON: special cases} {expr {wide(0)**wide(0)}} 1
66 test expr-23.26 {INST_EXPON: special cases} {expr {wide(-2)**wide(-1)}} 0
67 test expr-23.27 {INST_EXPON: special cases} {expr {wide(-2)**wide(0)}} 1
68 test expr-23.28 {INST_EXPON: special cases} {expr {wide(-1)**wide(1)}} -1
69 test expr-23.29 {INST_EXPON: special cases} {expr {wide(-1)**wide(0)}} 1
70 test expr-23.30 {INST_EXPON: special cases} {expr {wide(-1)**wide(2)}} 1
71 test expr-23.31 {INST_EXPON: special cases} {expr {wide(-1)**wide(-1)}} -1
72 test expr-23.32 {INST_EXPON: special cases} {expr {wide(1)**wide(1234567)}} 1
73 test expr-23.33 {INST_EXPON: special cases} {expr {wide(2)**wide(-2)}} 0
74 test expr-23.34 {INST_EXPON: special cases} {expr {2**0}} 1
75 test expr-23.35 {INST_EXPON: special cases} {expr {wide(2)**0}} 1
76 test expr-23.36 {INST_EXPON: big integer} {expr {10**17}} 1[string repeat 0 17]
77 test expr-23.37 {INST_EXPON: big integer} {expr {10**18}} 1[string repeat 0 18]
78 test expr-23.38 {INST_EXPON: big integer} tcl {expr {10**19}} 1[string repeat 0 19]
79 test expr-23.39 {INST_EXPON: big integer} tcl {
80     expr 1[string repeat 0 30]**2
81 } 1[string repeat 0 60]
82 test expr-23.40 {INST_EXPON: overflow to big integer} {expr {(-10)**3}} -1000
83 test expr-23.41 {INST_EXPON: overflow to big integer} tcl {expr 2**64} [expr 1<<64]
84 test expr-23.42 {INST_EXPON: overflow to big integer} tcl {expr 4**32} [expr 1<<64]
85 test expr-23.43 {INST_EXPON: overflow to big integer} tcl {expr 16**16} [expr 1<<64]
86 test expr-23.44 {INST_EXPON: overflow to big integer} tcl {expr 256**8} [expr 1<<64]
87 test expr-23.45 {INST_EXPON: Bug 1555371} {expr 2**1} 2
88 test expr-23.46 {INST_EXPON: Bug 1561260} -constraints tcl -body {
89     expr 5**28
90 } -match glob -result *5
91 test expr-23.47 {INST_EXPON: Bug 1561260} tcl {
92     expr 2**32*5**32
93 } 1[string repeat 0 32]
94 test expr-23.48 {INST_EXPON: TIP 274: right assoc} {
95 expr 2**1**3
96 } 2
97 test expr-23.49 {INST_EXPON: optimize powers of 2} {
98     set trouble {test powers of 2}
99     for {set tval 0} {$tval <= 63} {incr tval} {
100         set is [expr {2 ** $tval}]
101         set sb [expr {1 << $tval}]
102         if {$is != $sb} {
103             append trouble \n "2**" $tval " is " $is " should be " $sb
104         }
105         if {$tval >= 1} {
106             set is [expr {-2 ** $tval}]
107             set sb [expr {1 << $tval}]
108             if {$tval & 1} {
109                 set sb [expr {-$sb}]
110             }
111             if {$is != $sb} {
112                 append trouble \n "-2**" $tval " is " $is " should be " $sb
113             }
114         }
115     }
116     set trouble
117 } {test powers of 2}
118 test expr-23.50 {INST_EXPON: small powers of 32-bit integers} {
119     set trouble {test small powers of 32-bit ints}
120     for {set base 3} {$base <= 45} {incr base} {
121         set sb $base
122         set sbm [expr {-$base}]
123         for {set expt 2} {$expt <= 8} {incr expt} {
124             set sb [expr {$sb * $base}]
125             set is [expr {$base ** $expt}]
126             if {$sb != $is} {
127                 append trouble \n $base ** $expt " is " $is " should be " $sb
128             }
129             set sbm [expr {-$sbm * $base}]
130             set ism [expr {(-$base) ** $expt}]
131             if {$sbm != $ism} {
132                 append trouble \n - $base ** $expt " is " $ism \
133                     " should be " $sbm
134             }
135         }
136     }
137     set trouble
138 } {test small powers of 32-bit ints}
139 test expr-23.51 {INST_EXPON: intermediate powers of 32-bit integers} {
140     set trouble {test intermediate powers of 32-bit ints}
141     for {set base 3} {$base <= 11} {incr base} {
142         set sb [expr {$base ** 8}]
143         set sbm $sb
144         for {set expt 9} {$expt <= 21} {incr expt} {
145             set sb [expr {$sb * $base}]
146             set sbm [expr {$sbm * -$base}]
147             set is [expr {$base ** $expt}]
148             set ism [expr {-$base ** $expt}]
149             if {$sb != $is} {
150                 append trouble \n $base ** $expt " is " $is " should be " $sb
151             }
152             if {$sbm != $ism} {
153                 append trouble \n - $base ** $expt " is " $ism  \
154                     " should be " $sbm
155             }
156         }
157     }
158     set trouble
159 } {test intermediate powers of 32-bit ints}
160 test expr-23.52 {INST_EXPON: small integer powers with 64-bit results} pow {
161     set trouble {test small int powers with 64-bit results}
162     for {set exp 2} {$exp <= 16} {incr exp} {
163         set base [expr {wide(pow(double(0x7fffffffffffffff),(1.0/$exp)))}]
164         set sb 1
165         set sbm 1
166         for {set i 0} {$i < $exp} {incr i} {
167             set sb [expr {$sb * $base}]
168             set sbm [expr {$sbm * -$base}]
169         }
170         set is [expr {$base ** $exp}]
171         set ism [expr {-$base ** $exp}]
172         if {$sb != $is} {
173             append trouble \n $base ** $exp " is " $is " should be " $sb
174         }
175         if {$sbm != $ism} {
176             append trouble \n - $base ** $exp " is " $ism " should be " $sbm
177         }
178         incr base
179         set sb 1
180         set sbm 1
181         for {set i 0} {$i < $exp} {incr i} {
182             set sb [expr {$sb * $base}]
183             set sbm [expr {$sbm * -$base}]
184         }
185         set is [expr {$base ** $exp}]
186         set ism [expr {-$base ** $exp}]
187         if {$sb != $is} {
188             append trouble \n $base ** $exp " is " $is " should be " $sb
189         }
190         if {$sbm != $ism} {
191             append trouble \n - $base ** $exp " is " $ism " should be " $sbm
192         }
193     }
194     set trouble
195 } {test small int powers with 64-bit results}
196 test expr-23.53 {INST_EXPON: intermediate powers of 64-bit integers} {
197     set trouble {test intermediate powers of 64-bit ints}
198     for {set base 3} {$base <= 13} {incr base} {
199         set sb [expr {$base ** 15}]
200         set sbm [expr {-$sb}]
201         for {set expt 16} {$expt <= 39} {incr expt} {
202             set sb [expr {$sb * $base}]
203             set sbm [expr {$sbm * -$base}]
204             set is [expr {$base ** $expt}]
205             set ism [expr {-$base ** $expt}]
206             if {$sb != $is} {
207                 append trouble \n $base ** $expt " is " $is " should be " $sb
208             }
209             if {$sbm != $ism} {
210                 append trouble \n - $base ** $expt " is " $ism  \
211                     " should be " $sbm
212             }
213         }
214     }
215     set trouble
216 } {test intermediate powers of 64-bit ints}
217 test expr-23.54.0 {INST_EXPON: Bug 2798543} {
218     expr {3**9 == 3**65545}
219 } 0
220 test expr-23.54.1 {INST_EXPON: Bug 2798543} {
221     expr {3**10 == 3**65546}
222 } 0
223 test expr-23.54.2 {INST_EXPON: Bug 2798543} {
224     expr {3**11 == 3**65547}
225 } 0
226 test expr-23.54.3 {INST_EXPON: Bug 2798543} {
227     expr {3**12 == 3**65548}
228 } 0
229 test expr-23.54.4 {INST_EXPON: Bug 2798543} {
230     expr {3**13 == 3**65549}
231 } 0
232 test expr-23.54.5 {INST_EXPON: Bug 2798543} {
233     expr {3**14 == 3**65550}
234 } 0
235 test expr-23.54.6 {INST_EXPON: Bug 2798543} {
236     expr {3**15 == 3**65551}
237 } 0
238 test expr-23.54.7 {INST_EXPON: Bug 2798543} {
239     expr {3**16 == 3**65552}
240 } 0
241 test expr-23.54.8 {INST_EXPON: Bug 2798543} {
242     expr {3**17 == 3**65553}
243 } 0
244 test expr-23.54.9 {INST_EXPON: Bug 2798543} {
245     expr {3**18 == 3**65554}
246 } 0
247 test expr-23.54.10 {INST_EXPON: Bug 2798543} {
248     expr {3**19 == 3**65555}
249 } 0
250 test expr-23.54.11 {INST_EXPON: Bug 2798543} {
251     expr {3**9 == 3**131081}
252 } 0
253 test expr-23.54.13 {INST_EXPON: Bug 2798543} {
254     expr {(-3)**9 == (-3)**65545}
255 } 0
256 test expr-23.55.0 {INST_EXPON: Bug 2798543} {
257     expr {4**9 == 4**65545}
258 } 0
259 test expr-23.55.1 {INST_EXPON: Bug 2798543} {
260     expr {4**15 == 4**65551}
261 } 0
262 test expr-23.55.2 {INST_EXPON: Bug 2798543} {
263     expr {4**9 == 4**131081}
264 } 0
265 test expr-23.55.4 {INST_EXPON: Bug 2798543} {
266     expr {(-4)**9 == (-4)**65545}
267 } 0
268 test expr-23.56.0 {INST_EXPON: Bug 2798543} {
269     expr {5**9 == 5**65545}
270 } 0
271 test expr-23.56.1 {INST_EXPON: Bug 2798543} {
272     expr {5**13 == 5**65549}
273 } 0
274 test expr-23.56.2 {INST_EXPON: Bug 2798543} {
275     expr {5**9 == 5**131081}
276 } 0
277 test expr-23.56.4 {INST_EXPON: Bug 2798543} {
278     expr {(-5)**9 == (-5)**65545}
279 } 0
280 test expr-23.57.0 {INST_EXPON: Bug 2798543} {
281     expr {6**9 == 6**65545}
282 } 0
283 test expr-23.57.1 {INST_EXPON: Bug 2798543} {
284     expr {6**11 == 6**65547}
285 } 0
286 test expr-23.57.2 {INST_EXPON: Bug 2798543} {
287     expr {6**9 == 6**131081}
288 } 0
289 test expr-23.57.4 {INST_EXPON: Bug 2798543} {
290     expr {(-6)**9 == (-6)**65545}
291 } 0
292 test expr-23.58.0 {INST_EXPON: Bug 2798543} {
293     expr {7**9 == 7**65545}
294 } 0
295 test expr-23.58.1 {INST_EXPON: Bug 2798543} {
296     expr {7**11 == 7**65547}
297 } 0
298 test expr-23.58.2 {INST_EXPON: Bug 2798543} {
299     expr {7**9 == 7**131081}
300 } 0
301 test expr-23.58.4 {INST_EXPON: Bug 2798543} {
302     expr {(-7)**9 == (-7)**65545}
303 } 0
304 test expr-23.59.0 {INST_EXPON: Bug 2798543} {
305     expr {8**9 == 8**65545}
306 } 0
307 test expr-23.59.1 {INST_EXPON: Bug 2798543} {
308     expr {8**10 == 8**65546}
309 } 0
310 test expr-23.59.2 {INST_EXPON: Bug 2798543} {
311     expr {8**9 == 8**131081}
312 } 0
313 test expr-23.59.4 {INST_EXPON: Bug 2798543} {
314     expr {(-8)**9 == (-8)**65545}
315 } 0
316 test expr-23.60.0 {INST_EXPON: Bug 2798543} {
317     expr {9**9 == 9**65545}
318 } 0
319 test expr-23.60.1 {INST_EXPON: Bug 2798543} {
320     expr {9**9 == 9**131081}
321 } 0
322 test expr-23.60.3 {INST_EXPON: Bug 2798543} {
323     expr {(-9)**9 == (-9)**65545}
324 } 0
325 test expr-23.61.0 {INST_EXPON: Bug 2798543} {
326     expr {10**9 == 10**65545}
327 } 0
328 test expr-23.61.1 {INST_EXPON: Bug 2798543} {
329     expr {10**9 == 10**131081}
330 } 0
331 test expr-23.61.3 {INST_EXPON: Bug 2798543} {
332     expr {(-10)**9 == (-10)**65545}
333 } 0
334 test expr-23.62.0 {INST_EXPON: Bug 2798543} {
335     expr {11**9 == 11**65545}
336 } 0
337 test expr-23.62.1 {INST_EXPON: Bug 2798543} {
338     expr {11**9 == 11**131081}
339 } 0
340 test expr-23.62.3 {INST_EXPON: Bug 2798543} {
341     expr {(-11)**9 == (-11)**65545}
342 } 0
343 test expr-23.63.0 {INST_EXPON: Bug 2798543} {
344     expr {3**20 == 3**65556}
345 } 0
346 test expr-23.63.1 {INST_EXPON: Bug 2798543} {
347     expr {3**39 == 3**65575}
348 } 0
349 test expr-23.63.2 {INST_EXPON: Bug 2798543} {
350     expr {3**20 == 3**131092}
351 } 0
352 test expr-23.63.4 {INST_EXPON: Bug 2798543} {
353     expr {(-3)**20 == (-3)**65556}
354 } 0
355 test expr-23.64.0 {INST_EXPON: Bug 2798543} {
356     expr {4**17 == 4**65553}
357 } 0
358 test expr-23.64.1 {INST_EXPON: Bug 2798543} {
359     expr {4**31 == 4**65567}
360 } 0
361 test expr-23.64.2 {INST_EXPON: Bug 2798543} {
362     expr {4**17 == 4**131089}
363 } 0
364 test expr-23.64.4 {INST_EXPON: Bug 2798543} {
365     expr {(-4)**17 == (-4)**65553}
366 } 0
367 test expr-23.65.0 {INST_EXPON: Bug 2798543} {
368     expr {5**17 == 5**65553}
369 } 0
370 test expr-23.65.1 {INST_EXPON: Bug 2798543} {
371     expr {5**27 == 5**65563}
372 } 0
373 test expr-23.65.2 {INST_EXPON: Bug 2798543} {
374     expr {5**17 == 5**131089}
375 } 0
376 test expr-23.65.4 {INST_EXPON: Bug 2798543} {
377     expr {(-5)**17 == (-5)**65553}
378 } 0
379 test expr-23.66.0 {INST_EXPON: Bug 2798543} {
380     expr {6**17 == 6**65553}
381 } 0
382 test expr-23.66.1 {INST_EXPON: Bug 2798543} {
383     expr {6**24 == 6**65560}
384 } 0
385 test expr-23.66.2 {INST_EXPON: Bug 2798543} {
386     expr {6**17 == 6**131089}
387 } 0
388 test expr-23.66.4 {INST_EXPON: Bug 2798543} {
389     expr {(-6)**17 == (-6)**65553}
390 } 0
391 test expr-23.67.0 {INST_EXPON: Bug 2798543} {
392     expr {7**17 == 7**65553}
393 } 0
394 test expr-23.67.1 {INST_EXPON: Bug 2798543} {
395     expr {7**22 == 7**65558}
396 } 0
397 test expr-23.67.2 {INST_EXPON: Bug 2798543} {
398     expr {7**17 == 7**131089}
399 } 0
400 test expr-23.67.4 {INST_EXPON: Bug 2798543} {
401     expr {(-7)**17 == (-7)**65553}
402 } 0
403 test expr-23.68.0 {INST_EXPON: Bug 2798543} {
404     expr {8**17 == 8**65553}
405 } 0
406 test expr-23.68.1 {INST_EXPON: Bug 2798543} {
407     expr {8**20 == 8**65556}
408 } 0
409 test expr-23.68.2 {INST_EXPON: Bug 2798543} {
410     expr {8**17 == 8**131089}
411 } 0
412 test expr-23.68.4 {INST_EXPON: Bug 2798543} {
413     expr {(-8)**17 == (-8)**65553}
414 } 0
415 test expr-23.69.0 {INST_EXPON: Bug 2798543} {
416     expr {9**17 == 9**65553}
417 } 0
418 test expr-23.69.1 {INST_EXPON: Bug 2798543} {
419     expr {9**19 == 9**65555}
420 } 0
421 test expr-23.69.2 {INST_EXPON: Bug 2798543} {
422     expr {9**17 == 9**131089}
423 } 0
424 test expr-23.69.4 {INST_EXPON: Bug 2798543} {
425     expr {(-9)**17 == (-9)**65553}
426 } 0
427 test expr-23.70.0 {INST_EXPON: Bug 2798543} {
428     expr {10**17 == 10**65553}
429 } 0
430 test expr-23.70.1 {INST_EXPON: Bug 2798543} {
431     expr {10**18 == 10**65554}
432 } 0
433 test expr-23.70.2 {INST_EXPON: Bug 2798543} {
434     expr {10**17 == 10**131089}
435 } 0
436 test expr-23.70.4 {INST_EXPON: Bug 2798543} {
437     expr {(-10)**17 == (-10)**65553}
438 } 0
439 test expr-23.71.0 {INST_EXPON: Bug 2798543} {
440     expr {11**17 == 11**65553}
441 } 0
442 test expr-23.71.1 {INST_EXPON: Bug 2798543} {
443     expr {11**18 == 11**65554}
444 } 0
445 test expr-23.71.2 {INST_EXPON: Bug 2798543} {
446     expr {11**17 == 11**131089}
447 } 0
448 test expr-23.71.4 {INST_EXPON: Bug 2798543} {
449     expr {(-11)**17 == (-11)**65553}
450 } 0
451 test expr-23.72.0 {INST_EXPON: Bug 2798543} {
452     expr {12**17 == 12**65553}
453 } 0
454 test expr-23.72.1 {INST_EXPON: Bug 2798543} {
455     expr {12**17 == 12**131089}
456 } 0
457 test expr-23.72.3 {INST_EXPON: Bug 2798543} {
458     expr {(-12)**17 == (-12)**65553}
459 } 0
460 test expr-23.73.0 {INST_EXPON: Bug 2798543} {
461     expr {13**17 == 13**65553}
462 } 0
463 test expr-23.73.1 {INST_EXPON: Bug 2798543} {
464     expr {13**17 == 13**131089}
465 } 0
466 test expr-23.73.3 {INST_EXPON: Bug 2798543} {
467     expr {(-13)**17 == (-13)**65553}
468 } 0
469 test expr-23.74.0 {INST_EXPON: Bug 2798543} {
470     expr {14**17 == 14**65553}
471 } 0
472 test expr-23.74.1 {INST_EXPON: Bug 2798543} {
473     expr {14**17 == 14**131089}
474 } 0
475 test expr-23.74.3 {INST_EXPON: Bug 2798543} {
476     expr {(-14)**17 == (-14)**65553}
477 } 0
479         
480 # Some compilers get this wrong; ensure that we work around it correctly
481 test expr-24.1 {expr edge cases; shifting} {expr int(5)>>32} 0
482 test expr-24.2 {expr edge cases; shifting} {expr int(5)>>63} 0
483 test expr-24.3 {expr edge cases; shifting} {expr wide(5)>>32} 0
484 test expr-24.4 {expr edge cases; shifting} {expr wide(5)>>63} 0
485 test expr-24.7 {expr edge cases; shifting} {expr wide(5)<<32} 21474836480
486 test expr-24.8 {expr edge cases; shifting} {expr wide(10<<63)} 0
487 test expr-24.9 {expr edge cases; shifting} {expr 5>>32} 0
489 testreport