1# Commands covered: expr 2# 3# This file contains the original set of tests for Tcl's expr command. 4# Since the expr command is now compiled, a new set of tests covering 5# the new implementation are in the files "parseExpr.test" and 6# "compExpr.test". Sourcing this file into Tcl runs the tests and generates 7# output for errors. No output means no errors were found. 8# 9# Copyright © 1991-1994 The Regents of the University of California. 10# Copyright © 1994-1997 Sun Microsystems, Inc. 11# Copyright © 1998-2000 Scriptics Corporation. 12# 13# See the file "license.terms" for information on usage and redistribution 14# of this file, and for a DISCLAIMER OF ALL WARRANTIES. 15 16if {"::tcltest" ni [namespace children]} { 17 package require tcltest 2.5 18 namespace import -force ::tcltest::* 19} 20 21::tcltest::loadTestedCommands 22catch [list package require -exact tcl::test [info patchlevel]] 23 24testConstraint testexprlong [llength [info commands testexprlong]] 25testConstraint testexprdouble [llength [info commands testexprdouble]] 26testConstraint testexprstring [llength [info commands testexprstring]] 27testConstraint longIs32bit [expr {$tcl_platform(wordSize) == 4}] 28testConstraint longIs64bit [expr {$tcl_platform(wordSize) == 8}] 29 30# Big test for correct ordering of data in [expr] 31 32proc testIEEE {} { 33 variable ieeeValues 34 binary scan [binary format dd -1.0 1.0] c* c 35 switch -exact -- $c { 36 {0 0 0 0 0 0 -16 -65 0 0 0 0 0 0 -16 63} { 37 # little endian 38 binary scan \x00\x00\x00\x00\x00\x00\xF0\xFF d \ 39 ieeeValues(-Infinity) 40 binary scan \x00\x00\x00\x00\x00\x00\xF0\xBF d \ 41 ieeeValues(-Normal) 42 binary scan \x00\x00\x00\x00\x00\x00\x08\x80 d \ 43 ieeeValues(-Subnormal) 44 binary scan \x00\x00\x00\x00\x00\x00\x00\x80 d \ 45 ieeeValues(-0) 46 binary scan \x00\x00\x00\x00\x00\x00\x00\x00 d \ 47 ieeeValues(+0) 48 binary scan \x00\x00\x00\x00\x00\x00\x08\x00 d \ 49 ieeeValues(+Subnormal) 50 binary scan \x00\x00\x00\x00\x00\x00\xF0\x3F d \ 51 ieeeValues(+Normal) 52 binary scan \x00\x00\x00\x00\x00\x00\xF0\x7F d \ 53 ieeeValues(+Infinity) 54 binary scan \x00\x00\x00\x00\x00\x00\xF8\x7F d \ 55 ieeeValues(NaN) 56 set ieeeValues(littleEndian) 1 57 return 1 58 } 59 {-65 -16 0 0 0 0 0 0 63 -16 0 0 0 0 0 0} { 60 binary scan \xFF\xF0\x00\x00\x00\x00\x00\x00 d \ 61 ieeeValues(-Infinity) 62 binary scan \xBF\xF0\x00\x00\x00\x00\x00\x00 d \ 63 ieeeValues(-Normal) 64 binary scan \x80\x08\x00\x00\x00\x00\x00\x00 d \ 65 ieeeValues(-Subnormal) 66 binary scan \x80\x00\x00\x00\x00\x00\x00\x00 d \ 67 ieeeValues(-0) 68 binary scan \x00\x00\x00\x00\x00\x00\x00\x00 d \ 69 ieeeValues(+0) 70 binary scan \x00\x08\x00\x00\x00\x00\x00\x00 d \ 71 ieeeValues(+Subnormal) 72 binary scan \x3F\xF0\x00\x00\x00\x00\x00\x00 d \ 73 ieeeValues(+Normal) 74 binary scan \x7F\xF0\x00\x00\x00\x00\x00\x00 d \ 75 ieeeValues(+Infinity) 76 binary scan \x7F\xF8\x00\x00\x00\x00\x00\x00 d \ 77 ieeeValues(NaN) 78 set ieeeValues(littleEndian) 0 79 return 1 80 } 81 default { 82 return 0 83 } 84 } 85} 86testConstraint ieeeFloatingPoint [testIEEE] 87 88# First, test all of the integer operators individually. 89 90test expr-old-1.1 {integer operators} {expr -4} -4 91test expr-old-1.2 {integer operators} {expr -(1+4)} -5 92test expr-old-1.3 {integer operators} {expr ~3} -4 93test expr-old-1.4 {integer operators} {expr !2} 0 94test expr-old-1.5 {integer operators} {expr !0} 1 95test expr-old-1.6 {integer operators} {expr 4*6} 24 96test expr-old-1.7 {integer operators} {expr 36/12} 3 97test expr-old-1.8 {integer operators} {expr 27/4} 6 98test expr-old-1.9 {integer operators} {expr 27%4} 3 99test expr-old-1.10 {integer operators} {expr 2+2} 4 100test expr-old-1.11 {integer operators} {expr 2-6} -4 101test expr-old-1.12 {integer operators} {expr 1<<3} 8 102test expr-old-1.13 {integer operators} {expr 0xff>>2} 63 103test expr-old-1.14 {integer operators} {expr -1>>2} -1 104test expr-old-1.15 {integer operators} {expr 3>2} 1 105test expr-old-1.16 {integer operators} {expr 2>2} 0 106test expr-old-1.17 {integer operators} {expr 1>2} 0 107test expr-old-1.18 {integer operators} {expr 3<2} 0 108test expr-old-1.19 {integer operators} {expr 2<2} 0 109test expr-old-1.20 {integer operators} {expr 1<2} 1 110test expr-old-1.21 {integer operators} {expr 3>=2} 1 111test expr-old-1.22 {integer operators} {expr 2>=2} 1 112test expr-old-1.23 {integer operators} {expr 1>=2} 0 113test expr-old-1.24 {integer operators} {expr 3<=2} 0 114test expr-old-1.25 {integer operators} {expr 2<=2} 1 115test expr-old-1.26 {integer operators} {expr 1<=2} 1 116test expr-old-1.27 {integer operators} {expr 3==2} 0 117test expr-old-1.28 {integer operators} {expr 2==2} 1 118test expr-old-1.29 {integer operators} {expr 3!=2} 1 119test expr-old-1.30 {integer operators} {expr 2!=2} 0 120test expr-old-1.31 {integer operators} {expr 7&0x13} 3 121test expr-old-1.32 {integer operators} {expr 7^0x13} 20 122test expr-old-1.33 {integer operators} {expr 7|0x13} 23 123test expr-old-1.34 {integer operators} {expr 0&&1} 0 124test expr-old-1.35 {integer operators} {expr 0&&0} 0 125test expr-old-1.36 {integer operators} {expr 1&&3} 1 126test expr-old-1.37 {integer operators} {expr 0||1} 1 127test expr-old-1.38 {integer operators} {expr 3||0} 1 128test expr-old-1.39 {integer operators} {expr 0||0} 0 129test expr-old-1.40 {integer operators} {expr 3>2?44:66} 44 130test expr-old-1.41 {integer operators} {expr 2>3?44:66} 66 131test expr-old-1.42 {integer operators} {expr 36/5} 7 132test expr-old-1.43 {integer operators} {expr 36%5} 1 133test expr-old-1.44 {integer operators} {expr -36/5} -8 134test expr-old-1.45 {integer operators} {expr -36%5} 4 135test expr-old-1.46 {integer operators} {expr 36/-5} -8 136test expr-old-1.47 {integer operators} {expr 36%-5} -4 137test expr-old-1.48 {integer operators} {expr -36/-5} 7 138test expr-old-1.49 {integer operators} {expr -36%-5} -1 139test expr-old-1.50 {integer operators} {expr +36} 36 140test expr-old-1.51 {integer operators} {expr +--++36} 36 141test expr-old-1.52 {integer operators} {expr +36%+5} 1 142test expr-old-1.53 {integer operators} { 143 unset -nocomplain x 144 set x yes 145 list [expr {1 && $x}] [expr {$x && 1}] \ 146 [expr {0 || $x}] [expr {$x || 0}] 147} {1 1 1 1} 148 149# Check the floating-point operators individually, along with 150# automatic conversion to integers where needed. 151 152test expr-old-2.1 {floating-point operators} {expr -4.2} -4.2 153test expr-old-2.2 {floating-point operators} {expr -(1.125+4.25)} -5.375 154test expr-old-2.3 {floating-point operators} {expr +5.7} 5.7 155test expr-old-2.4 {floating-point operators} {expr +--+-62.0} -62.0 156test expr-old-2.5 {floating-point operators} {expr !2.1} 0 157test expr-old-2.6 {floating-point operators} {expr !0.0} 1 158test expr-old-2.7 {floating-point operators} {expr 4.2*6.3} 26.46 159test expr-old-2.8 {floating-point operators} {expr 36.0/12.0} 3.0 160test expr-old-2.9 {floating-point operators} {expr 27/4.0} 6.75 161test expr-old-2.10 {floating-point operators} {expr 2.3+2.1} 4.4 162test expr-old-2.11 {floating-point operators} {expr 2.3-6.5} -4.2 163test expr-old-2.12 {floating-point operators} {expr 3.1>2.1} 1 164test expr-old-2.13 {floating-point operators} {expr {2.1 > 2.1}} 0 165test expr-old-2.14 {floating-point operators} {expr 1.23>2.34e+1} 0 166test expr-old-2.15 {floating-point operators} {expr 3.45<2.34} 0 167test expr-old-2.16 {floating-point operators} {expr 0.002e3<--200e-2} 0 168test expr-old-2.17 {floating-point operators} {expr 1.1<2.1} 1 169test expr-old-2.18 {floating-point operators} {expr 3.1>=2.2} 1 170test expr-old-2.19 {floating-point operators} {expr 2.345>=2.345} 1 171test expr-old-2.20 {floating-point operators} {expr 1.1>=2.2} 0 172test expr-old-2.21 {floating-point operators} {expr 3.0<=2.0} 0 173test expr-old-2.22 {floating-point operators} {expr 2.2<=2.2} 1 174test expr-old-2.23 {floating-point operators} {expr 2.2<=2.2001} 1 175test expr-old-2.24 {floating-point operators} {expr 3.2==2.2} 0 176test expr-old-2.25 {floating-point operators} {expr 2.2==2.2} 1 177test expr-old-2.26 {floating-point operators} {expr 3.2!=2.2} 1 178test expr-old-2.27 {floating-point operators} {expr 2.2!=2.2} 0 179test expr-old-2.28 {floating-point operators} {expr 0.0&&0.0} 0 180test expr-old-2.29 {floating-point operators} {expr 0.0&&1.3} 0 181test expr-old-2.30 {floating-point operators} {expr 1.3&&0.0} 0 182test expr-old-2.31 {floating-point operators} {expr 1.3&&3.3} 1 183test expr-old-2.32 {floating-point operators} {expr 0.0||0.0} 0 184test expr-old-2.33 {floating-point operators} {expr 0.0||1.3} 1 185test expr-old-2.34 {floating-point operators} {expr 1.3||0.0} 1 186test expr-old-2.35 {floating-point operators} {expr 3.3||0.0} 1 187test expr-old-2.36 {floating-point operators} {expr 3.3>2.3?44.3:66.3} 44.3 188test expr-old-2.37 {floating-point operators} {expr 2.3>3.3?44.3:66.3} 66.3 189test expr-old-2.38 {floating-point operators} { 190 list [catch {expr 028.1 + 09.2} msg] $msg 191} {0 37.3} 192 193# Operators that aren't legal on floating-point numbers 194 195test expr-old-3.1 {illegal floating-point operations} { 196 list [catch {expr ~4.0} msg] $msg 197} {1 {can't use floating-point value as operand of "~"}} 198test expr-old-3.2 {illegal floating-point operations} { 199 list [catch {expr 27%4.0} msg] $msg 200} {1 {can't use floating-point value as operand of "%"}} 201test expr-old-3.3 {illegal floating-point operations} { 202 list [catch {expr 27.0%4} msg] $msg 203} {1 {can't use floating-point value as operand of "%"}} 204test expr-old-3.4 {illegal floating-point operations} { 205 list [catch {expr 1.0<<3} msg] $msg 206} {1 {can't use floating-point value as operand of "<<"}} 207test expr-old-3.5 {illegal floating-point operations} { 208 list [catch {expr 3<<1.0} msg] $msg 209} {1 {can't use floating-point value as operand of "<<"}} 210test expr-old-3.6 {illegal floating-point operations} { 211 list [catch {expr 24.0>>3} msg] $msg 212} {1 {can't use floating-point value as operand of ">>"}} 213test expr-old-3.7 {illegal floating-point operations} { 214 list [catch {expr 24>>3.0} msg] $msg 215} {1 {can't use floating-point value as operand of ">>"}} 216test expr-old-3.8 {illegal floating-point operations} { 217 list [catch {expr 24&3.0} msg] $msg 218} {1 {can't use floating-point value as operand of "&"}} 219test expr-old-3.9 {illegal floating-point operations} { 220 list [catch {expr 24.0|3} msg] $msg 221} {1 {can't use floating-point value as operand of "|"}} 222test expr-old-3.10 {illegal floating-point operations} { 223 list [catch {expr 24.0^3} msg] $msg 224} {1 {can't use floating-point value as operand of "^"}} 225 226# Check the string operators individually. 227 228test expr-old-4.1 {string operators} {expr {"abc" > "def"}} 0 229test expr-old-4.2 {string operators} {expr {"def" > "def"}} 0 230test expr-old-4.3 {string operators} {expr {"g" > "def"}} 1 231test expr-old-4.4 {string operators} {expr {"abc" < "abd"}} 1 232test expr-old-4.5 {string operators} {expr {"abd" < "abd"}} 0 233test expr-old-4.6 {string operators} {expr {"abe" < "abd"}} 0 234test expr-old-4.7 {string operators} {expr {"abc" >= "def"}} 0 235test expr-old-4.8 {string operators} {expr {"def" >= "def"}} 1 236test expr-old-4.9 {string operators} {expr {"g" >= "def"}} 1 237test expr-old-4.10 {string operators} {expr {"abc" <= "abd"}} 1 238test expr-old-4.11 {string operators} {expr {"abd" <= "abd"}} 1 239test expr-old-4.12 {string operators} {expr {"abe" <= "abd"}} 0 240test expr-old-4.13 {string operators} {expr {"abc" == "abd"}} 0 241test expr-old-4.14 {string operators} {expr {"abd" == "abd"}} 1 242test expr-old-4.15 {string operators} {expr {"abc" != "abd"}} 1 243test expr-old-4.16 {string operators} {expr {"abd" != "abd"}} 0 244test expr-old-4.17 {string operators} {expr {"0y" < "0x12"}} 0 245test expr-old-4.18 {string operators} {expr {"." < " "}} 0 246test expr-old-4.19 {string operators} {expr {"abc" eq "abd"}} 0 247test expr-old-4.20 {string operators} {expr {"abd" eq "abd"}} 1 248test expr-old-4.21 {string operators} {expr {"abc" ne "abd"}} 1 249test expr-old-4.22 {string operators} {expr {"abd" ne "abd"}} 0 250test expr-old-4.23 {string operators} {expr {"" eq "abd"}} 0 251test expr-old-4.24 {string operators} {expr {"" eq ""}} 1 252test expr-old-4.25 {string operators} {expr {"abd" ne ""}} 1 253test expr-old-4.26 {string operators} {expr {"" ne ""}} 0 254test expr-old-4.27 {string operators} {expr {"longerstring" eq "shorter"}} 0 255test expr-old-4.28 {string operators} {expr {"longerstring" ne "shorter"}} 1 256test expr-old-4.29 {string operators} {expr {"0" == "+"}} 0 257test expr-old-4.30 {string operators} {expr {"0" == "-"}} 0 258test expr-old-4.31 {string operators} {expr {1?"foo":"bar"}} foo 259test expr-old-4.32 {string operators} {expr {0?"foo":"bar"}} bar 260 261# Operators that aren't legal on string operands. 262 263test expr-old-5.1 {illegal string operations} { 264 list [catch {expr {-"a"}} msg] $msg 265} {1 {can't use non-numeric string as operand of "-"}} 266test expr-old-5.2 {illegal string operations} { 267 list [catch {expr {+"a"}} msg] $msg 268} {1 {can't use non-numeric string as operand of "+"}} 269test expr-old-5.3 {illegal string operations} { 270 list [catch {expr {~"a"}} msg] $msg 271} {1 {can't use non-numeric string as operand of "~"}} 272test expr-old-5.4 {illegal string operations} { 273 list [catch {expr {!"a"}} msg] $msg 274} {1 {can't use non-numeric string as operand of "!"}} 275test expr-old-5.5 {illegal string operations} { 276 list [catch {expr {"a"*"b"}} msg] $msg 277} {1 {can't use non-numeric string as operand of "*"}} 278test expr-old-5.6 {illegal string operations} { 279 list [catch {expr {"a"/"b"}} msg] $msg 280} {1 {can't use non-numeric string as operand of "/"}} 281test expr-old-5.7 {illegal string operations} { 282 list [catch {expr {"a"%"b"}} msg] $msg 283} {1 {can't use non-numeric string as operand of "%"}} 284test expr-old-5.8 {illegal string operations} { 285 list [catch {expr {"a"+"b"}} msg] $msg 286} {1 {can't use non-numeric string as operand of "+"}} 287test expr-old-5.9 {illegal string operations} { 288 list [catch {expr {"a"-"b"}} msg] $msg 289} {1 {can't use non-numeric string as operand of "-"}} 290test expr-old-5.10 {illegal string operations} { 291 list [catch {expr {"a"<<"b"}} msg] $msg 292} {1 {can't use non-numeric string as operand of "<<"}} 293test expr-old-5.11 {illegal string operations} { 294 list [catch {expr {"a">>"b"}} msg] $msg 295} {1 {can't use non-numeric string as operand of ">>"}} 296test expr-old-5.12 {illegal string operations} { 297 list [catch {expr {"a"&"b"}} msg] $msg 298} {1 {can't use non-numeric string as operand of "&"}} 299test expr-old-5.13 {illegal string operations} { 300 list [catch {expr {"a"^"b"}} msg] $msg 301} {1 {can't use non-numeric string as operand of "^"}} 302test expr-old-5.14 {illegal string operations} { 303 list [catch {expr {"a"|"b"}} msg] $msg 304} {1 {can't use non-numeric string as operand of "|"}} 305test expr-old-5.15 {illegal string operations} { 306 list [catch {expr {"a"&&"b"}} msg] $msg 307} {1 {expected boolean value but got "a"}} 308test expr-old-5.16 {illegal string operations} { 309 list [catch {expr {"a"||"b"}} msg] $msg 310} {1 {expected boolean value but got "a"}} 311test expr-old-5.17 {illegal string operations} { 312 list [catch {expr {"a"?4:2}} msg] $msg 313} {1 {expected boolean value but got "a"}} 314 315# Check precedence pairwise. 316 317test expr-old-6.1 {precedence checks} {expr -~3} 4 318test expr-old-6.2 {precedence checks} {expr -!3} 0 319test expr-old-6.3 {precedence checks} {expr -~0} 1 320 321test expr-old-7.1 {precedence checks} {expr 2*4/6} 1 322test expr-old-7.2 {precedence checks} {expr 24/6*3} 12 323test expr-old-7.3 {precedence checks} {expr 24/6/2} 2 324 325test expr-old-8.1 {precedence checks} {expr -2+4} 2 326test expr-old-8.2 {precedence checks} {expr -2-4} -6 327test expr-old-8.3 {precedence checks} {expr +2-4} -2 328 329test expr-old-9.1 {precedence checks} {expr 2*3+4} 10 330test expr-old-9.2 {precedence checks} {expr 8/2+4} 8 331test expr-old-9.3 {precedence checks} {expr 8%3+4} 6 332test expr-old-9.4 {precedence checks} {expr 2*3-1} 5 333test expr-old-9.5 {precedence checks} {expr 8/2-1} 3 334test expr-old-9.6 {precedence checks} {expr 8%3-1} 1 335 336test expr-old-10.1 {precedence checks} {expr 6-3-2} 1 337 338test expr-old-11.1 {precedence checks} {expr 7+1>>2} 2 339test expr-old-11.2 {precedence checks} {expr 7+1<<2} 32 340test expr-old-11.3 {precedence checks} {expr 7>>3-2} 3 341test expr-old-11.4 {precedence checks} {expr 7<<3-2} 14 342 343test expr-old-12.1 {precedence checks} {expr 6>>1>4} 0 344test expr-old-12.2 {precedence checks} {expr 6>>1<2} 0 345test expr-old-12.3 {precedence checks} {expr 6>>1>=3} 1 346test expr-old-12.4 {precedence checks} {expr 6>>1<=2} 0 347test expr-old-12.5 {precedence checks} {expr 6<<1>5} 1 348test expr-old-12.6 {precedence checks} {expr 6<<1<5} 0 349test expr-old-12.7 {precedence checks} {expr 5<=6<<1} 1 350test expr-old-12.8 {precedence checks} {expr 5>=6<<1} 0 351 352test expr-old-13.1 {precedence checks} {expr 2<3<4} 1 353test expr-old-13.2 {precedence checks} {expr 0<4>2} 0 354test expr-old-13.3 {precedence checks} {expr 4>2<1} 0 355test expr-old-13.4 {precedence checks} {expr 4>3>2} 0 356test expr-old-13.5 {precedence checks} {expr 4>3>=2} 0 357test expr-old-13.6 {precedence checks} {expr 4>=3>2} 0 358test expr-old-13.7 {precedence checks} {expr 4>=3>=2} 0 359test expr-old-13.8 {precedence checks} {expr 0<=4>=2} 0 360test expr-old-13.9 {precedence checks} {expr 4>=2<=0} 0 361test expr-old-13.10 {precedence checks} {expr 2<=3<=4} 1 362 363test expr-old-14.1 {precedence checks} {expr 1==4>3} 1 364test expr-old-14.2 {precedence checks} {expr 0!=4>3} 1 365test expr-old-14.3 {precedence checks} {expr 1==3<4} 1 366test expr-old-14.4 {precedence checks} {expr 0!=3<4} 1 367test expr-old-14.5 {precedence checks} {expr 1==4>=3} 1 368test expr-old-14.6 {precedence checks} {expr 0!=4>=3} 1 369test expr-old-14.7 {precedence checks} {expr 1==3<=4} 1 370test expr-old-14.8 {precedence checks} {expr 0!=3<=4} 1 371test expr-old-14.9 {precedence checks} {expr 1eq4>3} 1 372test expr-old-14.10 {precedence checks} {expr 0ne4>3} 1 373test expr-old-14.11 {precedence checks} {expr 1eq3<4} 1 374test expr-old-14.12 {precedence checks} {expr 0ne3<4} 1 375test expr-old-14.13 {precedence checks} {expr 1eq4>=3} 1 376test expr-old-14.14 {precedence checks} {expr 0ne4>=3} 1 377test expr-old-14.15 {precedence checks} {expr 1eq3<=4} 1 378test expr-old-14.16 {precedence checks} {expr 0ne3<=4} 1 379 380test expr-old-15.1 {precedence checks} {expr 1==3==3} 0 381test expr-old-15.2 {precedence checks} {expr 3==3!=2} 1 382test expr-old-15.3 {precedence checks} {expr 2!=3==3} 0 383test expr-old-15.4 {precedence checks} {expr 2!=1!=1} 0 384test expr-old-15.5 {precedence checks} {expr 1eq3eq3} 0 385test expr-old-15.6 {precedence checks} {expr 3eq3ne2} 1 386test expr-old-15.7 {precedence checks} {expr 2ne3eq3} 0 387test expr-old-15.8 {precedence checks} {expr 2ne1ne1} 0 388 389test expr-old-16.1 {precedence checks} {expr 2&3eq2} 0 390test expr-old-16.2 {precedence checks} {expr 1&3ne3} 0 391test expr-old-16.3 {precedence checks} {expr 2&3eq2} 0 392test expr-old-16.4 {precedence checks} {expr 1&3ne3} 0 393 394test expr-old-17.1 {precedence checks} {expr 7&3^0x10} 19 395test expr-old-17.2 {precedence checks} {expr 7^0x10&3} 7 396 397test expr-old-18.1 {precedence checks} {expr 7^0x10|3} 23 398test expr-old-18.2 {precedence checks} {expr 7|0x10^3} 23 399 400test expr-old-19.1 {precedence checks} {expr 7|3&&1} 1 401test expr-old-19.2 {precedence checks} {expr 1&&3|7} 1 402test expr-old-19.3 {precedence checks} {expr 0&&1||1} 1 403test expr-old-19.4 {precedence checks} {expr 1||1&&0} 1 404 405test expr-old-20.1 {precedence checks} {expr 1||0?3:4} 3 406test expr-old-20.2 {precedence checks} {expr 1?0:4||1} 0 407test expr-old-20.3 {precedence checks} {expr 1?2:0?3:4} 2 408test expr-old-20.4 {precedence checks} {expr 0?2:0?3:4} 4 409test expr-old-20.5 {precedence checks} {expr 1?2?3:4:0} 3 410test expr-old-20.6 {precedence checks} {expr 0?2?3:4:0} 0 411 412# Parentheses. 413 414test expr-old-21.1 {parenthesization} {expr (2+4)*6} 36 415test expr-old-21.2 {parenthesization} {expr (1?0:4)||1} 1 416test expr-old-21.3 {parenthesization} {expr +(3-4)} -1 417 418# Embedded commands and variable names. 419 420set a 16 421test expr-old-22.1 {embedded variables} {expr {2*$a}} 32 422test expr-old-22.2 {embedded variables} { 423 set x -5 424 set y 10 425 expr {$x + $y} 426} {5} 427test expr-old-22.3 {embedded variables} { 428 set x " -5" 429 set y " +10" 430 expr {$x + $y} 431} {5} 432test expr-old-22.4 {embedded commands and variables} {expr {[set a] - 14}} 2 433test expr-old-22.5 {embedded commands and variables} { 434 list [catch {expr {12 - [bad_command_name]}} msg] $msg 435} {1 {invalid command name "bad_command_name"}} 436 437# Double-quotes and things inside them. 438 439test expr-old-23.1 {double quotes} {expr {"abc"}} abc 440test expr-old-23.2 {double quotes} { 441 set a 189 442 expr {"$a.bc"} 443} 189.bc 444test expr-old-23.3 {double quotes} { 445 set b2 xyx 446 expr {"$b2$b2$b2.[set b2].[set b2]"} 447} xyxxyxxyx.xyx.xyx 448test expr-old-23.4 {double quotes} {expr {"11\}\}22"}} 11}}22 449test expr-old-23.5 {double quotes} {expr {"\*bc"}} {*bc} 450test expr-old-23.6 {double quotes} { 451 unset -nocomplain bogus__ 452 list [catch {expr {"$bogus__"}} msg] $msg 453} {1 {can't read "bogus__": no such variable}} 454test expr-old-23.7 {double quotes} { 455 list [catch {expr {"a[error Testing]bc"}} msg] $msg 456} {1 Testing} 457test expr-old-23.8 {double quotes} { 458 list [catch {expr {"12398712938788234-1298379" != ""}} msg] $msg 459} {0 1} 460 461# Numbers in various bases. 462 463test expr-old-24.1 {numbers in different bases} {expr 0x20} 32 464test expr-old-24.2 {numbers in different bases} {expr 0o15} 13 465 466# Conversions between various data types. 467 468test expr-old-25.1 {type conversions} {expr 2+2.5} 4.5 469test expr-old-25.2 {type conversions} {expr 2.5+2} 4.5 470test expr-old-25.3 {type conversions} {expr 2-2.5} -0.5 471test expr-old-25.4 {type conversions} {expr 2/2.5} 0.8 472test expr-old-25.5 {type conversions} {expr 2>2.5} 0 473test expr-old-25.6 {type conversions} {expr 2.5>2} 1 474test expr-old-25.7 {type conversions} {expr 2<2.5} 1 475test expr-old-25.8 {type conversions} {expr 2>=2.5} 0 476test expr-old-25.9 {type conversions} {expr 2<=2.5} 1 477test expr-old-25.10 {type conversions} {expr 2==2.5} 0 478test expr-old-25.11 {type conversions} {expr 2!=2.5} 1 479test expr-old-25.12 {type conversions} {expr 2>"ab"} 0 480test expr-old-25.13 {type conversions} {expr {2>" "}} 1 481test expr-old-25.14 {type conversions} {expr {"24.1a" > 24.1}} 1 482test expr-old-25.15 {type conversions} {expr {24.1 > "24.1a"}} 0 483test expr-old-25.16 {type conversions} {expr 2+2.5} 4.5 484test expr-old-25.17 {type conversions} {expr 2+2.5} 4.5 485test expr-old-25.18 {type conversions} {expr 2.0e2} 200.0 486test expr-old-25.19 {type conversions} {expr 2.0e15} 2000000000000000.0 487test expr-old-25.20 {type conversions} {expr 10.0} 10.0 488 489# Various error conditions. 490 491test expr-old-26.1 {error conditions} { 492 list [catch {expr 2+"a"} msg] $msg 493} {1 {can't use non-numeric string as operand of "+"}} 494test expr-old-26.2 {error conditions} -body { 495 expr 2+4* 496} -returnCodes error -match glob -result * 497test expr-old-26.3 {error conditions} -body { 498 expr 2+4*( 499} -returnCodes error -match glob -result * 500unset -nocomplain _non_existent_ 501test expr-old-26.4 {error conditions} { 502 list [catch {expr 2+$_non_existent_} msg] $msg 503} {1 {can't read "_non_existent_": no such variable}} 504set a xx 505test expr-old-26.5 {error conditions} { 506 list [catch {expr {2+$a}} msg] $msg 507} {1 {can't use non-numeric string as operand of "+"}} 508test expr-old-26.6 {error conditions} { 509 list [catch {expr {2+[set a]}} msg] $msg 510} {1 {can't use non-numeric string as operand of "+"}} 511test expr-old-26.7 {error conditions} -body { 512 expr {2+(4} 513} -returnCodes error -match glob -result * 514test expr-old-26.8 {error conditions} { 515 list [catch {expr 2/0} msg] $msg $errorCode 516} {1 {divide by zero} {ARITH DIVZERO {divide by zero}}} 517test expr-old-26.9 {error conditions} { 518 list [catch {expr 2%0} msg] $msg $errorCode 519} {1 {divide by zero} {ARITH DIVZERO {divide by zero}}} 520test expr-old-26.10a {error conditions} !ieeeFloatingPoint { 521 list [catch {expr 2.0/0.0} msg] $msg $errorCode 522} {1 {divide by zero} {ARITH DIVZERO {divide by zero}}} 523test expr-old-26.10b {error conditions} ieeeFloatingPoint { 524 list [catch {expr 2.0/0.0} msg] $msg 525} {0 Inf} 526test expr-old-26.11 {error conditions} -body { 527 expr 2` 528} -returnCodes error -match glob -result * 529test expr-old-26.12 {error conditions} -body { 530 expr a.b 531} -returnCodes error -match glob -result * 532test expr-old-26.13 {error conditions} { 533 list [catch {expr {"a"/"b"}} msg] $msg 534} {1 {can't use non-numeric string as operand of "/"}} 535test expr-old-26.14 {error conditions} -body { 536 expr 2:3 537} -returnCodes error -match glob -result * 538test expr-old-26.15 {error conditions} -body { 539 expr a@b 540} -returnCodes error -match glob -result * 541test expr-old-26.16 {error conditions} { 542 list [catch {expr a[b} msg] $msg 543} {1 {missing close-bracket}} 544test expr-old-26.17 {error conditions} -body { 545 expr a`b 546} -returnCodes error -match glob -result * 547test expr-old-26.18 {error conditions} -body { 548 expr \"a\"\{b 549} -returnCodes error -match glob -result * 550test expr-old-26.19 {error conditions} -body { 551 expr a 552} -returnCodes error -match glob -result * 553test expr-old-26.20 {error conditions} { 554 list [catch expr msg] $msg 555} {1 {wrong # args: should be "expr arg ?arg ...?"}} 556 557# Cancelled evaluation. 558 559test expr-old-27.1 {cancelled evaluation} { 560 set a 1 561 expr {0&&[set a 2]} 562 set a 563} 1 564test expr-old-27.2 {cancelled evaluation} { 565 set a 1 566 expr {1||[set a 2]} 567 set a 568} 1 569test expr-old-27.3 {cancelled evaluation} { 570 set a 1 571 expr {0?[set a 2]:1} 572 set a 573} 1 574test expr-old-27.4 {cancelled evaluation} { 575 set a 1 576 expr {1?2:[set a 2]} 577 set a 578} 1 579unset -nocomplain x 580test expr-old-27.5 {cancelled evaluation} { 581 list [catch {expr {[info exists x] && $x}} msg] $msg 582} {0 0} 583test expr-old-27.6 {cancelled evaluation} { 584 list [catch {expr {0 && [concat $x]}} msg] $msg 585} {0 0} 586test expr-old-27.7 {cancelled evaluation} { 587 set one 1 588 list [catch {expr {1 || 1/$one}} msg] $msg 589} {0 1} 590test expr-old-27.8 {cancelled evaluation} { 591 list [catch {expr {1 || -"string"}} msg] $msg 592} {0 1} 593test expr-old-27.9 {cancelled evaluation} { 594 list [catch {expr {1 || ("string" * ("x" && "y"))}} msg] $msg 595} {0 1} 596test expr-old-27.10 {cancelled evaluation} { 597 set x -1.0 598 list [catch {expr {($x > 0) ? round(log($x)) : 0}} msg] $msg 599} {0 0} 600test expr-old-27.11 {cancelled evaluation} -body { 601 expr {0 && foo} 602} -returnCodes error -match glob -result * 603test expr-old-27.12 {cancelled evaluation} -body { 604 expr {0 ? 1 : foo} 605} -returnCodes error -match glob -result * 606 607# Tcl_ExprBool as used in "if" statements 608 609test expr-old-28.1 {Tcl_ExprBoolean usage} { 610 set a 1 611 if {2} {set a 2} 612 set a 613} 2 614test expr-old-28.2 {Tcl_ExprBoolean usage} { 615 set a 1 616 if {0} {set a 2} 617 set a 618} 1 619test expr-old-28.3 {Tcl_ExprBoolean usage} { 620 set a 1 621 if {1.2} {set a 2} 622 set a 623} 2 624test expr-old-28.4 {Tcl_ExprBoolean usage} { 625 set a 1 626 if {-1.1} {set a 2} 627 set a 628} 2 629test expr-old-28.5 {Tcl_ExprBoolean usage} { 630 set a 1 631 if {0.0} {set a 2} 632 set a 633} 1 634test expr-old-28.6 {Tcl_ExprBoolean usage} { 635 set a 1 636 if {"YES"} {set a 2} 637 set a 638} 2 639test expr-old-28.7 {Tcl_ExprBoolean usage} { 640 set a 1 641 if {"no"} {set a 2} 642 set a 643} 1 644test expr-old-28.8 {Tcl_ExprBoolean usage} { 645 set a 1 646 if {"true"} {set a 2} 647 set a 648} 2 649test expr-old-28.9 {Tcl_ExprBoolean usage} { 650 set a 1 651 if {"fAlse"} {set a 2} 652 set a 653} 1 654test expr-old-28.10 {Tcl_ExprBoolean usage} { 655 set a 1 656 if {"on"} {set a 2} 657 set a 658} 2 659test expr-old-28.11 {Tcl_ExprBoolean usage} { 660 set a 1 661 if {"Off"} {set a 2} 662 set a 663} 1 664test expr-old-28.12 {Tcl_ExprBool usage} { 665 list [catch {if {"abc"} {}} msg] $msg 666} {1 {expected boolean value but got "abc"}} 667test expr-old-28.13 {Tcl_ExprBool usage} { 668 list [catch {if {"ogle"} {}} msg] $msg 669} {1 {expected boolean value but got "ogle"}} 670test expr-old-28.14 {Tcl_ExprBool usage} { 671 list [catch {if {"o"} {}} msg] $msg 672} {1 {expected boolean value but got "o"}} 673 674# Operands enclosed in braces 675 676test expr-old-29.1 {braces} {expr {{abc}}} abc 677test expr-old-29.2 {braces} {expr {{0o0010}}} 8 678test expr-old-29.3 {braces} {expr {{3.1200000}}} 3.12 679test expr-old-29.4 {braces} {expr {{a{b}{1 {2 3}}c}}} "a{b}{1 {2 3}}c" 680test expr-old-29.5 {braces} -body { 681 expr "\{abc" 682} -returnCodes error -match glob -result * 683 684# Very long values 685 686test expr-old-30.1 {long values} { 687 set a "0000 1111 2222 3333 4444" 688 set a "$a | $a | $a | $a | $a" 689 set a "$a || $a || $a || $a || $a" 690 expr {$a} 691} {0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 || 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 || 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 || 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 || 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444 | 0000 1111 2222 3333 4444} 692test expr-old-30.2 {long values} { 693 set a "000000000000000000000000000000" 694 set a "$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a${a}5" 695 expr $a 696} 5 697 698# Expressions spanning multiple arguments 699 700test expr-old-31.1 {multiple arguments to expr command} { 701 expr 4 + ( 6 *12) -3 702} 73 703test expr-old-31.2 {multiple arguments to expr command} -body { 704 expr 2 + (3 + 4 705} -returnCodes error -match glob -result * 706test expr-old-31.3 {multiple arguments to expr command} -body { 707 expr 2 + 3 + 708} -returnCodes error -match glob -result * 709test expr-old-31.4 {multiple arguments to expr command} -body { 710 expr 2 + 3 ) 711} -returnCodes error -match glob -result * 712 713# Math functions 714 715test expr-old-32.1 {math functions in expressions} { 716 format %.6g [expr acos(0.5)] 717} {1.0472} 718test expr-old-32.2 {math functions in expressions} { 719 format %.6g [expr asin(0.5)] 720} {0.523599} 721test expr-old-32.3 {math functions in expressions} { 722 format %.6g [expr atan(1.0)] 723} {0.785398} 724test expr-old-32.4 {math functions in expressions} { 725 format %.6g [expr atan2(2.0, 2.0)] 726} {0.785398} 727test expr-old-32.5 {math functions in expressions} { 728 format %.6g [expr ceil(1.999)] 729} {2} 730test expr-old-32.6 {math functions in expressions} { 731 format %.6g [expr cos(.1)] 732} {0.995004} 733test expr-old-32.7 {math functions in expressions} { 734 format %.6g [expr cosh(.1)] 735} {1.005} 736test expr-old-32.8 {math functions in expressions} { 737 format %.6g [expr exp(1.0)] 738} {2.71828} 739test expr-old-32.9 {math functions in expressions} { 740 format %.6g [expr floor(2.000)] 741} {2} 742test expr-old-32.10 {math functions in expressions} { 743 format %.6g [expr floor(2.001)] 744} {2} 745test expr-old-32.11 {math functions in expressions} { 746 format %.6g [expr fmod(7.3, 3.2)] 747} {0.9} 748test expr-old-32.12 {math functions in expressions} { 749 format %.6g [expr hypot(3.0, 4.0)] 750} {5} 751test expr-old-32.13 {math functions in expressions} { 752 format %.6g [expr log(2.8)] 753} {1.02962} 754test expr-old-32.14 {math functions in expressions} { 755 format %.6g [expr log10(2.8)] 756} {0.447158} 757test expr-old-32.15 {math functions in expressions} { 758 format %.6g [expr pow(2.1, 3.1)] 759} {9.97424} 760test expr-old-32.16 {math functions in expressions} { 761 format %.6g [expr sin(.1)] 762} {0.0998334} 763test expr-old-32.17 {math functions in expressions} { 764 format %.6g [expr sinh(.1)] 765} {0.100167} 766test expr-old-32.18 {math functions in expressions} { 767 format %.6g [expr sqrt(2.0)] 768} {1.41421} 769test expr-old-32.19 {math functions in expressions} { 770 format %.6g [expr tan(0.8)] 771} {1.02964} 772test expr-old-32.20 {math functions in expressions} { 773 format %.6g [expr tanh(0.8)] 774} {0.664037} 775test expr-old-32.21 {math functions in expressions} { 776 format %.6g [expr abs(-1.8)] 777} {1.8} 778test expr-old-32.22 {math functions in expressions} { 779 expr abs(10.0) 780} {10.0} 781test expr-old-32.23 {math functions in expressions} { 782 format %.6g [expr abs(-4)] 783} {4} 784test expr-old-32.24 {math functions in expressions} { 785 format %.6g [expr abs(66)] 786} {66} 787 788test expr-old-32.25a {math functions in expressions} { 789 expr abs(0x8000000000000000) 790} [expr 1<<63] 791 792test expr-old-32.25b {math functions in expressions} { 793 expr abs(0x80000000) 794} 2147483648 795 796test expr-old-32.26 {math functions in expressions} { 797 expr double(1) 798} {1.0} 799test expr-old-32.27 {math functions in expressions} { 800 expr double(1.1) 801} {1.1} 802test expr-old-32.28 {math functions in expressions} { 803 expr int(1) 804} {1} 805test expr-old-32.29 {math functions in expressions} { 806 expr int(1.4) 807} {1} 808test expr-old-32.30 {math functions in expressions} { 809 expr int(1.6) 810} {1} 811test expr-old-32.31 {math functions in expressions} { 812 expr int(-1.4) 813} {-1} 814test expr-old-32.32 {math functions in expressions} { 815 expr int(-1.6) 816} {-1} 817test expr-old-32.33 {math functions in expressions} { 818 expr int(1e60) 819} 999999999999999949387135297074018866963645011013410073083904 820test expr-old-32.34 {math functions in expressions} { 821 expr int(-1e60) 822} -999999999999999949387135297074018866963645011013410073083904 823test expr-old-32.35 {math functions in expressions} { 824 expr round(1.49) 825} {1} 826test expr-old-32.36 {math functions in expressions} { 827 expr round(1.51) 828} {2} 829test expr-old-32.37 {math functions in expressions} { 830 expr round(-1.49) 831} {-1} 832test expr-old-32.38 {math functions in expressions} { 833 expr round(-1.51) 834} {-2} 835test expr-old-32.39 {math functions in expressions} { 836 expr round(1e60) 837} 999999999999999949387135297074018866963645011013410073083904 838test expr-old-32.40 {math functions in expressions} { 839 expr round(-1e60) 840} -999999999999999949387135297074018866963645011013410073083904 841test expr-old-32.41 {math functions in expressions} { 842 list [catch {expr pow(1.0 + 3.0 - 2, .8 * 5)} msg] $msg 843} {0 16.0} 844test expr-old-32.42 {math functions in expressions} { 845 list [catch {expr hypot(5*.8,3)} msg] $msg 846} {0 5.0} 847test expr-old-32.45 {math functions in expressions} { 848 expr (0 <= rand()) && (rand() < 1) 849} {1} 850test expr-old-32.46 {math functions in expressions} -body { 851 list [catch {expr rand(24)} msg] $msg 852} -match glob -result {1 {too many arguments for math function*}} 853test expr-old-32.47 {math functions in expressions} -body { 854 list [catch {expr srand()} msg] $msg 855} -match glob -result {1 {not enough arguments for math function*}} 856test expr-old-32.48 {math functions in expressions} -body { 857 expr srand(3.79) 858} -returnCodes error -match glob -result * 859test expr-old-32.49 {math functions in expressions} -body { 860 expr srand("") 861} -returnCodes error -match glob -result * 862test expr-old-32.50 {math functions in expressions} { 863 set result [expr round(srand(12345) * 1000)] 864 for {set i 0} {$i < 10} {incr i} { 865 lappend result [expr round(rand() * 1000)] 866 } 867 set result 868} {97 834 948 36 12 51 766 585 914 784 333} 869test expr-old-32.51 {math functions in expressions} -body { 870 expr {srand([lindex "6ty" 0])} 871} -returnCodes error -match glob -result * 872test expr-old-32.52 {math functions in expressions} { 873 expr {srand(int(1<<37)) < 1} 874} {1} 875test expr-old-32.53 {math functions in expressions} { 876 expr {srand((1<<31) - 1) > 0} 877} {1} 878 879test expr-old-33.1 {conversions and fancy args to math functions} { 880 expr hypot ( 3 , 4 ) 881} 5.0 882test expr-old-33.2 {conversions and fancy args to math functions} { 883 expr hypot ( (2.0+1.0) , 4 ) 884} 5.0 885test expr-old-33.3 {conversions and fancy args to math functions} { 886 expr hypot ( 3 , (3.0 + 1.0) ) 887} 5.0 888test expr-old-33.4 {conversions and fancy args to math functions} { 889 format %.6g [expr cos(acos(0.1))] 890} 0.1 891 892test expr-old-34.1 {errors in math functions} -body { 893 list [catch {expr func_2(1.0)} msg] $msg 894} -match glob -result {1 {* "*func_2"}} 895test expr-old-34.2 {errors in math functions} -body { 896 expr func|(1.0) 897} -returnCodes error -match glob -result * 898test expr-old-34.3 {errors in math functions} { 899 list [catch {expr {hypot("a b", 2.0)}} msg] $msg 900} {1 {expected floating-point number but got "a b"}} 901test expr-old-34.4 {errors in math functions} -body { 902 expr hypot(1.0 2.0) 903} -returnCodes error -match glob -result * 904test expr-old-34.5 {errors in math functions} -body { 905 expr hypot(1.0, 2.0 906} -returnCodes error -match glob -result * 907test expr-old-34.6 {errors in math functions} -body { 908 expr hypot(1.0 , 909} -returnCodes error -match glob -result * 910test expr-old-34.7 {errors in math functions} -body { 911 list [catch {expr hypot(1.0)} msg] $msg 912} -match glob -result {1 {not enough arguments for math function*}} 913test expr-old-34.8 {errors in math functions} -body { 914 list [catch {expr hypot(1.0, 2.0, 3.0)} msg] $msg 915} -match glob -result {1 {too many arguments for math function*}} 916test expr-old-34.9 {errors in math functions} { 917 list [catch {expr acos(-2.0)} msg] $msg $errorCode 918} {1 {domain error: argument not in valid range} {ARITH DOMAIN {domain error: argument not in valid range}}} 919test expr-old-34.10 {errors in math functions} { 920 list [catch {expr pow(-3, 1000001)} msg] $msg 921} {0 -Inf} 922test expr-old-34.11a {errors in math functions} !ieeeFloatingPoint { 923 list [catch {expr pow(3, 1000001)} msg] $msg $errorCode 924} {1 {floating-point value too large to represent} {ARITH OVERFLOW {floating-point value too large to represent}}} 925test expr-old-34.11b {errors in math functions} ieeeFloatingPoint { 926 list [catch {expr pow(3, 1000001)} msg] $msg 927} {0 Inf} 928test expr-old-34.12a {errors in math functions} !ieeeFloatingPoint { 929 list [catch {expr -14.0*exp(100000)} msg] $msg $errorCode 930} {1 {floating-point value too large to represent} {ARITH OVERFLOW {floating-point value too large to represent}}} 931test expr-old-34.12b {errors in math functions} ieeeFloatingPoint { 932 list [catch {expr -14.0*exp(100000)} msg] $msg 933} {0 -Inf} 934test expr-old-34.13 {errors in math functions} { 935 expr wide(1.0e30) 936} 5076964154930102272 937test expr-old-34.14 {errors in math functions} { 938 expr wide(-1.0e30) 939} -5076964154930102272 940test expr-old-34.15 {errors in math functions} { 941 expr round(1.0e30) 942} 1000000000000000019884624838656 943test expr-old-34.16 {errors in math functions} { 944 expr round(-1.0e30) 945} -1000000000000000019884624838656 946test expr-old-36.1 {ExprLooksLikeInt procedure} -body { 947 expr 0o289 948} -returnCodes error -match glob -result {*invalid octal number*} 949test expr-old-36.2 {ExprLooksLikeInt procedure} { 950 set x 0o289 951 list [catch {expr {$x+1}} msg] $msg 952} {1 {can't use invalid octal number as operand of "+"}} 953test expr-old-36.3 {ExprLooksLikeInt procedure} { 954 list [catch {expr 0289.1} msg] $msg 955} {0 289.1} 956test expr-old-36.4 {ExprLooksLikeInt procedure} { 957 set x 0289.1 958 list [catch {expr {$x+1}} msg] $msg 959} {0 290.1} 960test expr-old-36.5 {ExprLooksLikeInt procedure} { 961 set x { +22} 962 list [catch {expr {$x+1}} msg] $msg 963} {0 23} 964test expr-old-36.6 {ExprLooksLikeInt procedure} { 965 set x { -22} 966 list [catch {expr {$x+1}} msg] $msg 967} {0 -21} 968test expr-old-36.7 {ExprLooksLikeInt procedure} { 969 list [catch {expr nan} msg] $msg 970} {1 {domain error: argument not in valid range}} 971test expr-old-36.8 {ExprLooksLikeInt procedure} { 972 list [catch {expr 78e1} msg] $msg 973} {0 780.0} 974test expr-old-36.9 {ExprLooksLikeInt procedure} { 975 list [catch {expr 24E1} msg] $msg 976} {0 240.0} 977test expr-old-36.10 {ExprLooksLikeInt procedure} -body { 978 expr 78e 979} -returnCodes error -match glob -result * 980 981# test for [Bug #542588] 982test expr-old-36.11 {ExprLooksLikeInt procedure} { 983 # define a "too large integer"; this one works also for 64bit arith 984 set x 665802003400000000000000 985 expr {$x+1} 986} 665802003400000000000001 987 988# tests for [Bug #587140] 989test expr-old-36.12 {ExprLooksLikeInt procedure} { 990 set x "10;" 991 list [catch {expr {$x+1}} msg] $msg 992} {1 {can't use non-numeric string as operand of "+"}} 993test expr-old-36.13 {ExprLooksLikeInt procedure} { 994 set x " +" 995 list [catch {expr {$x+1}} msg] $msg 996} {1 {can't use non-numeric string as operand of "+"}} 997test expr-old-36.14 {ExprLooksLikeInt procedure} { 998 set x "123456789012345678901234567890 " 999 expr {$x+1} 1000} 123456789012345678901234567891 1001test expr-old-36.15 {ExprLooksLikeInt procedure} { 1002 set x "0o99 " 1003 list [catch {expr {$x+1}} msg] $msg 1004} {1 {can't use invalid octal number as operand of "+"}} 1005test expr-old-36.16 {ExprLooksLikeInt procedure} { 1006 set x " 0xffffffffffffffffffffffffffffffffffffff " 1007 expr {$x+1} 1008} [expr 0x100000000000000000000000000000000000000] 1009 1010test expr-old-37.1 {Check that Tcl_ExprLong doesn't modify interpreter result if no error} testexprlong { 1011 testexprlong 4+1 1012} {This is a result: 5} 1013#Check for [Bug 1109484] 1014test expr-old-37.2 {Tcl_ExprLong handles wide ints gracefully} testexprlong { 1015 testexprlong wide(1)+2 1016} {This is a result: 3} 1017 1018test expr-old-37.3 {Tcl_ExprLong on the empty string} testexprlong { 1019 testexprlong "" 1020} {This is a result: 0} 1021test expr-old-37.4 {Tcl_ExprLong coerces doubles} testexprlong { 1022 testexprlong 3+.14159 1023} {This is a result: 3} 1024test expr-old-37.5 {Tcl_ExprLong handles overflows} {testexprlong longIs32bit} { 1025 testexprlong 0x80000000 1026} {This is a result: -2147483648} 1027test expr-old-37.6 {Tcl_ExprLong handles overflows} {testexprlong longIs32bit} { 1028 testexprlong 0xffffffff 1029} {This is a result: -1} 1030test expr-old-37.7 {Tcl_ExprLong handles overflows} \ 1031 -constraints {testexprlong longIs32bit} \ 1032 -match glob \ 1033 -body { 1034 list [catch {testexprlong 0x100000000} result] $result 1035 } \ 1036 -result {1 {integer value too large to represent*}} 1037test expr-old-37.8 {Tcl_ExprLong handles overflows} testexprlong { 1038 testexprlong -0x80000000 1039} {This is a result: -2147483648} 1040test expr-old-37.9 {Tcl_ExprLong handles overflows} {testexprlong longIs32bit} { 1041 testexprlong -0x7fffffff 1042} {This is a result: -2147483647} 1043test expr-old-37.10 {Tcl_ExprLong handles overflows} \ 1044 -constraints {testexprlong longIs32bit} \ 1045 -match glob \ 1046 -body { 1047 list [catch {testexprlong -0x100000000} result] $result 1048 } \ 1049 -result {1 {integer value too large to represent*}} 1050test expr-old-37.11 {Tcl_ExprLong handles overflows} {testexprlong longIs32bit} { 1051 testexprlong 2147483648. 1052} {This is a result: -2147483648} 1053test expr-old-37.12 {Tcl_ExprLong handles overflows} {testexprlong longIs32bit} { 1054 testexprlong 4294967295. 1055} {This is a result: -1} 1056test expr-old-37.13 {Tcl_ExprLong handles overflows} \ 1057 -constraints {testexprlong longIs32bit} \ 1058 -match glob \ 1059 -body { 1060 list [catch {testexprlong 4294967296.} result] $result 1061 } \ 1062 -result {1 {integer value too large to represent*}} 1063test expr-old-37.14 {Tcl_ExprLong handles overflows} testexprlong { 1064 testexprlong -2147483648. 1065} {This is a result: -2147483648} 1066test expr-old-37.15 {Tcl_ExprLong handles overflows} \ 1067 -constraints {testexprlong longIs32bit} \ 1068 -match glob \ 1069 -body { 1070 list [catch {testexprlong -2147483649.} result] $result 1071 } \ 1072 -result {1 {integer value too large to represent*}} 1073test expr-old-37.16 {Tcl_ExprLong handles overflows} \ 1074 -constraints {testexprlong longIs32bit} \ 1075 -match glob \ 1076 -body { 1077 list [catch {testexprlong 4294967296.} result] $result 1078 } \ 1079 -result {1 {integer value too large to represent*}} 1080 1081test expr-old-37.17 {Check that Tcl_ExprDouble doesn't modify interpreter result if no error} testexprdouble { 1082 testexprdouble 4.+1. 1083} {This is a result: 5.0} 1084#Check for [Bug 1109484] 1085test expr-old-37.18 {Tcl_ExprDouble on the empty string} testexprdouble { 1086 testexprdouble "" 1087} {This is a result: 0.0} 1088test expr-old-37.19 {Tcl_ExprDouble coerces wides} testexprdouble { 1089 testexprdouble 1[string repeat 0 17] 1090} {This is a result: 1e+17} 1091test expr-old-37.20 {Tcl_ExprDouble coerces bignums} testexprdouble { 1092 testexprdouble 1[string repeat 0 38] 1093} {This is a result: 1e+38} 1094test expr-old-37.21 {Tcl_ExprDouble handles overflows} testexprdouble { 1095 testexprdouble 17976931348623157[string repeat 0 292]. 1096} {This is a result: 1.7976931348623157e+308} 1097test expr-old-37.22 {Tcl_ExprDouble handles overflows that look like int} \ 1098 testexprdouble { 1099 testexprdouble 17976931348623157[string repeat 0 292] 1100 } {This is a result: 1.7976931348623157e+308} 1101test expr-old-37.23 {Tcl_ExprDouble handles overflows} \ 1102 ieeeFloatingPoint&&testexprdouble { 1103 testexprdouble 17976931348623165[string repeat 0 292]. 1104 } {This is a result: Inf} 1105test expr-old-37.24 {Tcl_ExprDouble handles overflows that look like int} \ 1106 ieeeFloatingPoint&&testexprdouble { 1107 testexprdouble 17976931348623165[string repeat 0 292] 1108 } {This is a result: Inf} 1109test expr-old-37.25 {Tcl_ExprDouble and NaN} \ 1110 {ieeeFloatingPoint testexprdouble} { 1111 list [catch {testexprdouble 0.0/0.0} result] $result 1112 } {1 {domain error: argument not in valid range}} 1113 1114test expr-old-38.1 {Verify Tcl_ExprString's basic operation} -constraints {testexprstring} -body { 1115 list [testexprstring "1+4"] [testexprstring "2*3+4.2"] \ 1116 [catch {testexprstring "1+"} msg] $msg 1117} -match glob -result {5 10.2 1 *} 1118test expr-old-38.2 {Tcl_ExprString} testexprstring { 1119 # This one is "magical" 1120 testexprstring {} 1121} 0 1122test expr-old-38.3 {Tcl_ExprString} -constraints testexprstring -body { 1123 testexprstring { } 1124} -returnCodes error -match glob -result * 1125 1126# 1127# Test for bug #908375: rounding numbers that do not fit in a 1128# long but do fit in a wide 1129# 1130 1131test expr-old-39.1 {Rounding with wide result} { 1132 set x 1.0e10 1133 set y [expr $x + 0.1] 1134 catch { 1135 set x [list [expr {$x == round($y)}] [expr $x == -round(-$y)]] 1136 } 1137 set x 1138} {1 1} 1139unset -nocomplain x y 1140 1141# 1142# TIP #255 min and max math functions 1143# 1144 1145test expr-old-40.1 {min math function} -body { 1146 expr {min(0)} 1147} -result 0 1148test expr-old-40.2 {min math function} -body { 1149 expr {min(0.0)} 1150} -result 0.0 1151test expr-old-40.3 {min math function} -body { 1152 expr {min()} 1153} -returnCodes error -result {not enough arguments for math function "min"} 1154test expr-old-40.4 {min math function} -body { 1155 expr {min(wide(-1) << 30, 4.5, -10)} 1156} -result [expr {wide(-1) << 30}] 1157test expr-old-40.5 {min math function} -body { 1158 expr {min("a", 0)} 1159} -returnCodes error -match glob -result * 1160test expr-old-40.6 {min math function} -body { 1161 expr {min(300, "0xFF")} 1162} -result 255 1163test expr-old-40.7 {min math function} -body { 1164 expr min(1[string repeat 0 10000], 1e300) 1165} -result 1e+300 1166test expr-old-40.8 {min math function} -body { 1167 expr {min(0, "a")} 1168} -returnCodes error -match glob -result * 1169 1170test expr-old-41.1 {max math function} -body { 1171 expr {max(0)} 1172} -result 0 1173test expr-old-41.2 {max math function} -body { 1174 expr {max(0.0)} 1175} -result 0.0 1176test expr-old-41.3 {max math function} -body { 1177 expr {max()} 1178} -returnCodes error -result {not enough arguments for math function "max"} 1179test expr-old-41.4 {max math function} -body { 1180 expr {max(wide(1) << 30, 4.5, -10)} 1181} -result [expr {wide(1) << 30}] 1182test expr-old-41.5 {max math function} -body { 1183 expr {max("a", 0)} 1184} -returnCodes error -match glob -result * 1185test expr-old-41.6 {max math function} -body { 1186 expr {max(200, "0xFF")} 1187} -result 255 1188test expr-old-41.7 {max math function} -body { 1189 expr max(1[string repeat 0 10000], 1e300) 1190} -result 1[string repeat 0 10000] 1191test expr-old-41.8 {max math function} -body { 1192 expr {max(0, "a")} 1193} -returnCodes error -match glob -result * 1194 1195# Special test for Pentium arithmetic bug of 1994: 1196 1197if {(4195835.0 - (4195835.0/3145727.0)*3145727.0) == 256.0} { 1198 puts "Warning: this machine contains a defective Pentium processor" 1199 puts "that performs arithmetic incorrectly. I recommend that you" 1200 puts "call Intel customer service immediately at 1-800-628-8686" 1201 puts "to request a replacement processor." 1202} 1203 1204# cleanup 1205::tcltest::cleanupTests 1206return 1207 1208# Local Variables: 1209# mode: tcl 1210# End: 1211