os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/compExpr.test
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/compExpr.test Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,329 @@
1.4 +# This file contains a collection of tests for the procedures in the
1.5 +# file tclCompExpr.c. Sourcing this file into Tcl runs the tests and
1.6 +# generates output for errors. No output means no errors were found.
1.7 +#
1.8 +# Copyright (c) 1997 Sun Microsystems, Inc.
1.9 +# Copyright (c) 1998-1999 by Scriptics Corporation.
1.10 +#
1.11 +# See the file "license.terms" for information on usage and redistribution
1.12 +# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
1.13 +#
1.14 +# RCS: @(#) $Id: compExpr.test,v 1.6 2001/12/06 10:59:17 dkf Exp $
1.15 +
1.16 +if {[lsearch [namespace children] ::tcltest] == -1} {
1.17 + package require tcltest
1.18 + namespace import -force ::tcltest::*
1.19 +}
1.20 +
1.21 +if {([catch {expr T1()} msg] == 1) && ($msg == {unknown math function "T1"})} {
1.22 + set gotT1 0
1.23 + puts "This application hasn't been compiled with the \"T1\" and"
1.24 + puts "\"T2\" math functions, so I'll skip some of the expr tests."
1.25 +} else {
1.26 + set gotT1 1
1.27 +}
1.28 +
1.29 +catch {unset a}
1.30 +
1.31 +test compExpr-1.1 {TclCompileExpr procedure, successful expr parse and compile} {
1.32 + expr 1+2
1.33 +} 3
1.34 +test compExpr-1.2 {TclCompileExpr procedure, error parsing expr} {
1.35 + list [catch {expr 1+2+} msg] $msg
1.36 +} {1 {syntax error in expression "1+2+": premature end of expression}}
1.37 +test compExpr-1.3 {TclCompileExpr procedure, error compiling expr} {
1.38 + list [catch {expr "foo(123)"} msg] $msg
1.39 +} {1 {unknown math function "foo"}}
1.40 +test compExpr-1.4 {TclCompileExpr procedure, expr has no operators} {
1.41 + set a {000123}
1.42 + expr {$a}
1.43 +} 83
1.44 +
1.45 +test compExpr-2.1 {CompileSubExpr procedure, TCL_TOKEN_WORD parse token} {
1.46 + catch {unset a}
1.47 + set a 27
1.48 + expr {"foo$a" < "bar"}
1.49 +} 0
1.50 +test compExpr-2.2 {CompileSubExpr procedure, error compiling TCL_TOKEN_WORD parse token} {
1.51 + list [catch {expr {"00[expr 1+]" + 17}} msg] $msg
1.52 +} {1 {syntax error in expression "1+": premature end of expression}}
1.53 +test compExpr-2.3 {CompileSubExpr procedure, TCL_TOKEN_TEXT parse token} {
1.54 + expr {{12345}}
1.55 +} 12345
1.56 +test compExpr-2.4 {CompileSubExpr procedure, empty TCL_TOKEN_TEXT parse token} {
1.57 + expr {{}}
1.58 +} {}
1.59 +test compExpr-2.5 {CompileSubExpr procedure, TCL_TOKEN_BS parse token} {
1.60 + expr "\{ \\
1.61 + +123 \}"
1.62 +} 123
1.63 +test compExpr-2.6 {CompileSubExpr procedure, TCL_TOKEN_COMMAND parse token} {
1.64 + expr {[info tclversion] != ""}
1.65 +} 1
1.66 +test compExpr-2.7 {CompileSubExpr procedure, TCL_TOKEN_COMMAND parse token} {
1.67 + expr {[]}
1.68 +} {}
1.69 +test compExpr-2.8 {CompileSubExpr procedure, error in TCL_TOKEN_COMMAND parse token} {
1.70 + list [catch {expr {[foo "bar"xxx] + 17}} msg] $msg
1.71 +} {1 {extra characters after close-quote}}
1.72 +test compExpr-2.9 {CompileSubExpr procedure, TCL_TOKEN_VARIABLE parse token} {
1.73 + catch {unset a}
1.74 + set a 123
1.75 + expr {$a*2}
1.76 +} 246
1.77 +test compExpr-2.10 {CompileSubExpr procedure, TCL_TOKEN_VARIABLE parse token} {
1.78 + catch {unset a}
1.79 + catch {unset b}
1.80 + set a(george) martha
1.81 + set b geo
1.82 + expr {$a(${b}rge)}
1.83 +} martha
1.84 +test compExpr-2.11 {CompileSubExpr procedure, error in TCL_TOKEN_VARIABLE parse token} {
1.85 + catch {unset a}
1.86 + list [catch {expr {$a + 17}} msg] $msg
1.87 +} {1 {can't read "a": no such variable}}
1.88 +test compExpr-2.12 {CompileSubExpr procedure, TCL_TOKEN_SUB_EXPR parse token} {
1.89 + expr {27||3? 3<<(1+4) : 4&&9}
1.90 +} 96
1.91 +test compExpr-2.13 {CompileSubExpr procedure, error in TCL_TOKEN_SUB_EXPR parse token} {
1.92 + catch {unset a}
1.93 + set a 15
1.94 + list [catch {expr {27 || "$a[expr 1+]00"}} msg] $msg
1.95 +} {1 {syntax error in expression "1+": premature end of expression}}
1.96 +test compExpr-2.14 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, op found} {
1.97 + expr {5*6}
1.98 +} 30
1.99 +test compExpr-2.15 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, math function found} {
1.100 + format %.6g [expr {sin(2.0)}]
1.101 +} 0.909297
1.102 +test compExpr-2.16 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, math function not found} {
1.103 + list [catch {expr {fred(2.0)}} msg] $msg
1.104 +} {1 {unknown math function "fred"}}
1.105 +test compExpr-2.17 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
1.106 + expr {4*2}
1.107 +} 8
1.108 +test compExpr-2.18 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
1.109 + expr {4/2}
1.110 +} 2
1.111 +test compExpr-2.19 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
1.112 + expr {4%2}
1.113 +} 0
1.114 +test compExpr-2.20 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
1.115 + expr {4<<2}
1.116 +} 16
1.117 +test compExpr-2.21 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
1.118 + expr {4>>2}
1.119 +} 1
1.120 +test compExpr-2.22 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
1.121 + expr {4<2}
1.122 +} 0
1.123 +test compExpr-2.23 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
1.124 + expr {4>2}
1.125 +} 1
1.126 +test compExpr-2.24 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
1.127 + expr {4<=2}
1.128 +} 0
1.129 +test compExpr-2.25 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
1.130 + expr {4>=2}
1.131 +} 1
1.132 +test compExpr-2.26 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
1.133 + expr {4==2}
1.134 +} 0
1.135 +test compExpr-2.27 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
1.136 + expr {4!=2}
1.137 +} 1
1.138 +test compExpr-2.28 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
1.139 + expr {4&2}
1.140 +} 0
1.141 +test compExpr-2.29 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
1.142 + expr {4^2}
1.143 +} 6
1.144 +test compExpr-2.30 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator} {
1.145 + expr {4|2}
1.146 +} 6
1.147 +test compExpr-2.31 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator, 1 operand} {
1.148 + expr {!4}
1.149 +} 0
1.150 +test compExpr-2.32 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator, 1 operand} {
1.151 + expr {~4}
1.152 +} -5
1.153 +test compExpr-2.33 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, normal operator, comparison} {
1.154 + catch {unset a}
1.155 + set a 15
1.156 + expr {$a==15} ;# compiled out-of-line to runtime call on Tcl_ExprObjCmd
1.157 +} 1
1.158 +test compExpr-2.34 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, special operator} {
1.159 + expr {+2}
1.160 +} 2
1.161 +test compExpr-2.35 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, error in special operator} {
1.162 + list [catch {expr {+[expr 1+]}} msg] $msg
1.163 +} {1 {syntax error in expression "1+": premature end of expression}}
1.164 +test compExpr-2.36 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, special operator} {
1.165 + expr {4+2}
1.166 +} 6
1.167 +test compExpr-2.37 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, error in special operator} {
1.168 + list [catch {expr {[expr 1+]+5}} msg] $msg
1.169 +} {1 {syntax error in expression "1+": premature end of expression}}
1.170 +test compExpr-2.38 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, error in special operator} {
1.171 + list [catch {expr {5+[expr 1+]}} msg] $msg
1.172 +} {1 {syntax error in expression "1+": premature end of expression}}
1.173 +test compExpr-2.39 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, special operator} {
1.174 + expr {-2}
1.175 +} -2
1.176 +test compExpr-2.40 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, special operator} {
1.177 + expr {4-2}
1.178 +} 2
1.179 +test compExpr-2.41 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, special operator} {
1.180 + catch {unset a}
1.181 + set a true
1.182 + expr {0||$a}
1.183 +} 1
1.184 +test compExpr-2.42 {CompileSubExpr procedure, error in TCL_TOKEN_SUB_EXPR parse token} {
1.185 + catch {unset a}
1.186 + set a 15
1.187 + list [catch {expr {27 || "$a[expr 1+]00"}} msg] $msg
1.188 +} {1 {syntax error in expression "1+": premature end of expression}}
1.189 +test compExpr-2.43 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, special operator} {
1.190 + catch {unset a}
1.191 + set a false
1.192 + expr {3&&$a}
1.193 +} 0
1.194 +test compExpr-2.44 {CompileSubExpr procedure, TCL_TOKEN_OPERATOR token, special operator} {
1.195 + catch {unset a}
1.196 + set a false
1.197 + expr {$a||1? 1 : 0}
1.198 +} 1
1.199 +test compExpr-2.45 {CompileSubExpr procedure, error in TCL_TOKEN_SUB_EXPR parse token} {
1.200 + catch {unset a}
1.201 + set a 15
1.202 + list [catch {expr {1? 54 : "$a[expr 1+]00"}} msg] $msg
1.203 +} {1 {syntax error in expression "1+": premature end of expression}}
1.204 +
1.205 +test compExpr-3.1 {CompileLandOrLorExpr procedure, numeric 1st operand} {
1.206 + catch {unset a}
1.207 + set a 2
1.208 + expr {[set a]||0}
1.209 +} 1
1.210 +test compExpr-3.2 {CompileLandOrLorExpr procedure, nonnumeric 1st operand} {
1.211 + catch {unset a}
1.212 + set a no
1.213 + expr {$a&&1}
1.214 +} 0
1.215 +test compExpr-3.3 {CompileSubExpr procedure, error in 1st operand} {
1.216 + list [catch {expr {[expr *2]||0}} msg] $msg
1.217 +} {1 {syntax error in expression "*2": unexpected operator *}}
1.218 +test compExpr-3.4 {CompileLandOrLorExpr procedure, result is 1 or 0} {
1.219 + catch {unset a}
1.220 + catch {unset b}
1.221 + set a no
1.222 + set b true
1.223 + expr {$a || $b}
1.224 +} 1
1.225 +test compExpr-3.5 {CompileLandOrLorExpr procedure, short-circuit semantics} {
1.226 + catch {unset a}
1.227 + set a yes
1.228 + expr {$a || [exit]}
1.229 +} 1
1.230 +test compExpr-3.6 {CompileLandOrLorExpr procedure, short-circuit semantics} {
1.231 + catch {unset a}
1.232 + set a no
1.233 + expr {$a && [exit]}
1.234 +} 0
1.235 +test compExpr-3.7 {CompileLandOrLorExpr procedure, numeric 2nd operand} {
1.236 + catch {unset a}
1.237 + set a 2
1.238 + expr {0||[set a]}
1.239 +} 1
1.240 +test compExpr-3.8 {CompileLandOrLorExpr procedure, nonnumeric 2nd operand} {
1.241 + catch {unset a}
1.242 + set a no
1.243 + expr {1&&$a}
1.244 +} 0
1.245 +test compExpr-3.9 {CompileLandOrLorExpr procedure, error in 2nd operand} {
1.246 + list [catch {expr {0||[expr %2]}} msg] $msg
1.247 +} {1 {syntax error in expression "%2": unexpected operator %}}
1.248 +test compExpr-3.10 {CompileLandOrLorExpr procedure, long lor/land arm} {
1.249 + set a "abcdefghijkl"
1.250 + set i 7
1.251 + expr {[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]] || [string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]] || [string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]] || [string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]&&[string compare [format %c $i] [string index $a $i]]}
1.252 +} 1
1.253 +
1.254 +test compExpr-4.1 {CompileCondExpr procedure, simple test} {
1.255 + catch {unset a}
1.256 + set a 2
1.257 + expr {($a > 1)? "ok" : "nope"}
1.258 +} ok
1.259 +test compExpr-4.2 {CompileCondExpr procedure, complex test, convert to numeric} {
1.260 + catch {unset a}
1.261 + set a no
1.262 + expr {[set a]? 27 : -54}
1.263 +} -54
1.264 +test compExpr-4.3 {CompileCondExpr procedure, error in test} {
1.265 + list [catch {expr {[expr *2]? +1 : -1}} msg] $msg
1.266 +} {1 {syntax error in expression "*2": unexpected operator *}}
1.267 +test compExpr-4.4 {CompileCondExpr procedure, simple "true" clause} {
1.268 + catch {unset a}
1.269 + set a no
1.270 + expr {1? (27-2) : -54}
1.271 +} 25
1.272 +test compExpr-4.5 {CompileCondExpr procedure, convert "true" clause to numeric} {
1.273 + catch {unset a}
1.274 + set a no
1.275 + expr {1? $a : -54}
1.276 +} no
1.277 +test compExpr-4.6 {CompileCondExpr procedure, error in "true" clause} {
1.278 + list [catch {expr {1? [expr *2] : -127}} msg] $msg
1.279 +} {1 {syntax error in expression "*2": unexpected operator *}}
1.280 +test compExpr-4.7 {CompileCondExpr procedure, simple "false" clause} {
1.281 + catch {unset a}
1.282 + set a no
1.283 + expr {(2-2)? -3.14159 : "nope"}
1.284 +} nope
1.285 +test compExpr-4.8 {CompileCondExpr procedure, convert "false" clause to numeric} {
1.286 + catch {unset a}
1.287 + set a 00123
1.288 + expr {0? 42 : $a}
1.289 +} 83
1.290 +test compExpr-4.9 {CompileCondExpr procedure, error in "false" clause} {
1.291 + list [catch {expr {1? 15 : [expr *2]}} msg] $msg
1.292 +} {1 {syntax error in expression "*2": unexpected operator *}}
1.293 +
1.294 +test compExpr-5.1 {CompileMathFuncCall procedure, math function found} {
1.295 + format %.6g [expr atan2(1.0, 2.0)]
1.296 +} 0.463648
1.297 +test compExpr-5.2 {CompileMathFuncCall procedure, math function not found} {
1.298 + list [catch {expr {do_it()}} msg] $msg
1.299 +} {1 {unknown math function "do_it"}}
1.300 +if $gotT1 {
1.301 + test compExpr-5.3 {CompileMathFuncCall: call registered math function} {
1.302 + expr 3*T1()-1
1.303 + } 368
1.304 + test compExpr-5.4 {CompileMathFuncCall: call registered math function} {
1.305 + expr T2()*3
1.306 + } 1035
1.307 +}
1.308 +test compExpr-5.5 {CompileMathFuncCall procedure, too few arguments} {
1.309 + list [catch {expr {atan2(1.0)}} msg] $msg
1.310 +} {1 {too few arguments for math function}}
1.311 +test compExpr-5.6 {CompileMathFuncCall procedure, complex argument} {
1.312 + format %.6g [expr pow(2.1, 27.5-(24.4*(5%2)))]
1.313 +} 9.97424
1.314 +test compExpr-5.7 {CompileMathFuncCall procedure, error in argument} {
1.315 + list [catch {expr {sinh(2.*)}} msg] $msg
1.316 +} {1 {syntax error in expression "sinh(2.*)": unexpected close parenthesis}}
1.317 +test compExpr-5.8 {CompileMathFuncCall procedure, too many arguments} {
1.318 + list [catch {expr {sinh(2.0, 3.0)}} msg] $msg
1.319 +} {1 {too many arguments for math function}}
1.320 +test compExpr-5.9 {CompileMathFuncCall procedure, too many arguments} {
1.321 + list [catch {expr {0 <= rand(5.2)}} msg] $msg
1.322 +} {1 {too many arguments for math function}}
1.323 +
1.324 +test compExpr-6.1 {LogSyntaxError procedure, error in expr longer than 60 chars} {
1.325 + list [catch {expr {(+0123456)*(+0123456)*(+0123456)*(+0123456)*(+0123456)*(+0123456)*(+0123456)/} -1 foo 3} msg] $msg
1.326 +} {1 {syntax error in expression "(+0123456)*(+0123456)*(+0123456)*(+0123456)*(+0123456)*(+012...": extra tokens at end of expression}}
1.327 +
1.328 +# cleanup
1.329 +catch {unset a}
1.330 +catch {unset b}
1.331 +::tcltest::cleanupTests
1.332 +return