home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tcl2-73c.zip / tcl7.3 / tests / expr.test < prev    next >
Text File  |  1993-09-08  |  32KB  |  823 lines

  1. # Commands covered:  expr
  2. #
  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.
  6. #
  7. # Copyright (c) 1991-1993 The Regents of the University of California.
  8. # All rights reserved.
  9. #
  10. # Permission is hereby granted, without written agreement and without
  11. # license or royalty fees, to use, copy, modify, and distribute this
  12. # software and its documentation for any purpose, provided that the
  13. # above copyright notice and the following two paragraphs appear in
  14. # all copies of this software.
  15. #
  16. # IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  17. # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  18. # OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  19. # CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. #
  21. # THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  22. # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  23. # AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  24. # ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  25. # PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  26. #
  27. # $Header: /user6/ouster/tcl/tests/RCS/expr.test,v 1.30 93/09/08 16:46:45 ouster Exp $ (Berkeley)
  28.  
  29. if {[string compare test [info procs test]] == 1} then {source defs}
  30.  
  31. if {([catch {expr T1()} msg] == 1) && ($msg == {unknown math function "T1"})} {
  32.     set gotT1 0
  33.     puts "This application hasn't been compiled with the \"T1\" and"
  34.     puts "\"T2\" math functions, so I'll skip some of the expr tests."
  35. } else {
  36.     set gotT1 1
  37. }
  38.  
  39. # First, test all of the integer operators individually.
  40.  
  41. test expr-1.1 {integer operators} {expr -4} -4
  42. test expr-1.2 {integer operators} {expr -(1+4)} -5
  43. test expr-1.3 {integer operators} {expr ~3} -4
  44. test expr-1.4 {integer operators} {expr !2} 0
  45. test expr-1.5 {integer operators} {expr !0} 1
  46. test expr-1.6 {integer operators} {expr 4*6} 24
  47. test expr-1.7 {integer operators} {expr 36/12} 3
  48. test expr-1.8 {integer operators} {expr 27/4} 6
  49. test expr-1.9 {integer operators} {expr 27%4} 3
  50. test expr-1.10 {integer operators} {expr 2+2} 4
  51. test expr-1.11 {integer operators} {expr 2-6} -4
  52. test expr-1.12 {integer operators} {expr 1<<3} 8
  53. test expr-1.13 {integer operators} {expr 0xff>>2} 63
  54. test expr-1.14 {integer operators} {expr -1>>2} -1
  55. test expr-1.15 {integer operators} {expr 3>2} 1
  56. test expr-1.16 {integer operators} {expr 2>2} 0
  57. test expr-1.17 {integer operators} {expr 1>2} 0
  58. test expr-1.18 {integer operators} {expr 3<2} 0
  59. test expr-1.19 {integer operators} {expr 2<2} 0
  60. test expr-1.20 {integer operators} {expr 1<2} 1
  61. test expr-1.21 {integer operators} {expr 3>=2} 1
  62. test expr-1.22 {integer operators} {expr 2>=2} 1
  63. test expr-1.23 {integer operators} {expr 1>=2} 0
  64. test expr-1.24 {integer operators} {expr 3<=2} 0
  65. test expr-1.25 {integer operators} {expr 2<=2} 1
  66. test expr-1.26 {integer operators} {expr 1<=2} 1
  67. test expr-1.27 {integer operators} {expr 3==2} 0
  68. test expr-1.28 {integer operators} {expr 2==2} 1
  69. test expr-1.29 {integer operators} {expr 3!=2} 1
  70. test expr-1.30 {integer operators} {expr 2!=2} 0
  71. test expr-1.31 {integer operators} {expr 7&0x13} 3
  72. test expr-1.32 {integer operators} {expr 7^0x13} 20
  73. test expr-1.33 {integer operators} {expr 7|0x13} 23
  74. test expr-1.34 {integer operators} {expr 0&&1} 0
  75. test expr-1.35 {integer operators} {expr 0&&0} 0
  76. test expr-1.36 {integer operators} {expr 1&&3} 1
  77. test expr-1.37 {integer operators} {expr 0||1} 1
  78. test expr-1.38 {integer operators} {expr 3||0} 1
  79. test expr-1.39 {integer operators} {expr 0||0} 0
  80. test expr-1.40 {integer operators} {expr 3>2?44:66} 44
  81. test expr-1.41 {integer operators} {expr 2>3?44:66} 66
  82. test expr-1.42 {integer operators} {expr 36/5} 7
  83. test expr-1.43 {integer operators} {expr 36%5} 1
  84. test expr-1.44 {integer operators} {expr -36/5} -8
  85. test expr-1.45 {integer operators} {expr -36%5} 4
  86. test expr-1.46 {integer operators} {expr 36/-5} -8
  87. test expr-1.47 {integer operators} {expr 36%-5} -4
  88. test expr-1.48 {integer operators} {expr -36/-5} 7
  89. test expr-1.49 {integer operators} {expr -36%-5} -1
  90.  
  91. # Check the floating-point operators individually, along with
  92. # automatic conversion to integers where needed.
  93.  
  94. test expr-2.1 {floating-point operators} {expr -4.2} -4.2
  95. test expr-2.2 {floating-point operators} {expr -(1.1+4.2)} -5.3
  96. test expr-2.3 {floating-point operators} {expr !2.1} 0
  97. test expr-2.4 {floating-point operators} {expr !0.0} 1
  98. test expr-2.5 {floating-point operators} {expr 4.2*6.3} 26.46
  99. test expr-2.6 {floating-point operators} {expr 36.0/12.0} 3.0
  100. test expr-2.7 {floating-point operators} {expr 27/4.0} 6.75
  101. test expr-2.8 {floating-point operators} {expr 2.3+2.1} 4.4
  102. test expr-2.9 {floating-point operators} {expr 2.3-6.5} -4.2
  103. test expr-2.10 {floating-point operators} {expr 3.1>2.1} 1
  104. test expr-2.11 {floating-point operators} {expr {2.1 > 2.1}} 0
  105. test expr-2.12 {floating-point operators} {expr 1.23>2.34e+1} 0
  106. test expr-2.13 {floating-point operators} {expr 3.45<2.34} 0
  107. test expr-2.14 {floating-point operators} {expr 0.002e3<--200e-2} 0
  108. test expr-2.15 {floating-point operators} {expr 1.1<2.1} 1
  109. test expr-2.16 {floating-point operators} {expr 3.1>=2.2} 1
  110. test expr-2.17 {floating-point operators} {expr 2.345>=2.345} 1
  111. test expr-2.18 {floating-point operators} {expr 1.1>=2.2} 0
  112. test expr-2.19 {floating-point operators} {expr 3.0<=2.0} 0
  113. test expr-2.20 {floating-point operators} {expr 2.2<=2.2} 1
  114. test expr-2.21 {floating-point operators} {expr 2.2<=2.2001} 1
  115. test expr-2.22 {floating-point operators} {expr 3.2==2.2} 0
  116. test expr-2.23 {floating-point operators} {expr 2.2==2.2} 1
  117. test expr-2.24 {floating-point operators} {expr 3.2!=2.2} 1
  118. test expr-2.25 {floating-point operators} {expr 2.2!=2.2} 0
  119. test expr-2.26 {floating-point operators} {expr 0.0&&0.0} 0
  120. test expr-2.27 {floating-point operators} {expr 0.0&&1.3} 0
  121. test expr-2.28 {floating-point operators} {expr 1.3&&0.0} 0
  122. test expr-2.29 {floating-point operators} {expr 1.3&&3.3} 1
  123. test expr-2.30 {floating-point operators} {expr 0.0||0.0} 0
  124. test expr-2.31 {floating-point operators} {expr 0.0||1.3} 1
  125. test expr-2.32 {floating-point operators} {expr 1.3||0.0} 1
  126. test expr-2.33 {floating-point operators} {expr 3.3||0.0} 1
  127. test expr-2.34 {floating-point operators} {expr 3.3>2.3?44.3:66.3} 44.3
  128. test expr-2.35 {floating-point operators} {expr 2.3>3.3?44.3:66.3} 66.3
  129. test expr-2.36 {floating-point operators} {
  130.     list [catch {expr 028.1 + 09.2} msg] $msg
  131. } {0 37.3}
  132.  
  133. # Operators that aren't legal on floating-point numbers
  134.  
  135. test expr-3.1 {illegal floating-point operations} {
  136.     list [catch {expr ~4.0} msg] $msg
  137. } {1 {can't use floating-point value as operand of "~"}}
  138. test expr-3.2 {illegal floating-point operations} {
  139.     list [catch {expr 27%4.0} msg] $msg
  140. } {1 {can't use floating-point value as operand of "%"}}
  141. test expr-3.3 {illegal floating-point operations} {
  142.     list [catch {expr 27.0%4} msg] $msg
  143. } {1 {can't use floating-point value as operand of "%"}}
  144. test expr-3.4 {illegal floating-point operations} {
  145.     list [catch {expr 1.0<<3} msg] $msg
  146. } {1 {can't use floating-point value as operand of "<<"}}
  147. test expr-3.5 {illegal floating-point operations} {
  148.     list [catch {expr 3<<1.0} msg] $msg
  149. } {1 {can't use floating-point value as operand of "<<"}}
  150. test expr-3.6 {illegal floating-point operations} {
  151.     list [catch {expr 24.0>>3} msg] $msg
  152. } {1 {can't use floating-point value as operand of ">>"}}
  153. test expr-3.7 {illegal floating-point operations} {
  154.     list [catch {expr 24>>3.0} msg] $msg
  155. } {1 {can't use floating-point value as operand of ">>"}}
  156. test expr-3.8 {illegal floating-point operations} {
  157.     list [catch {expr 24&3.0} msg] $msg
  158. } {1 {can't use floating-point value as operand of "&"}}
  159. test expr-3.9 {illegal floating-point operations} {
  160.     list [catch {expr 24.0|3} msg] $msg
  161. } {1 {can't use floating-point value as operand of "|"}}
  162. test expr-3.10 {illegal floating-point operations} {
  163.     list [catch {expr 24.0^3} msg] $msg
  164. } {1 {can't use floating-point value as operand of "^"}}
  165.  
  166. # Check the string operators individually.
  167.  
  168. test expr-4.1 {string operators} {expr {"abc" > "def"}} 0
  169. test expr-4.2 {string operators} {expr {"def" > "def"}} 0
  170. test expr-4.3 {string operators} {expr {"g" > "def"}} 1
  171. test expr-4.4 {string operators} {expr {"abc" < "abd"}} 1
  172. test expr-4.5 {string operators} {expr {"abd" < "abd"}} 0
  173. test expr-4.6 {string operators} {expr {"abe" < "abd"}} 0
  174. test expr-4.7 {string operators} {expr {"abc" >= "def"}} 0
  175. test expr-4.8 {string operators} {expr {"def" >= "def"}} 1
  176. test expr-4.9 {string operators} {expr {"g" >= "def"}} 1
  177. test expr-4.10 {string operators} {expr {"abc" <= "abd"}} 1
  178. test expr-4.11 {string operators} {expr {"abd" <= "abd"}} 1
  179. test expr-4.12 {string operators} {expr {"abe" <= "abd"}} 0
  180. test expr-4.13 {string operators} {expr {"abc" == "abd"}} 0
  181. test expr-4.14 {string operators} {expr {"abd" == "abd"}} 1
  182. test expr-4.15 {string operators} {expr {"abc" != "abd"}} 1
  183. test expr-4.16 {string operators} {expr {"abd" != "abd"}} 0
  184. test expr-4.17 {string operators} {expr {"0y" < "0x12"}} 1
  185. test expr-4.18 {string operators} {expr {"." < " "}} 0
  186. test expr-4.19 {string operators} {expr {"0" == "+"}} 0
  187. test expr-4.20 {string operators} {expr {"0" == "-"}} 0
  188. test expr-4.21 {string operators} {expr {1?"foo":"bar"}} foo
  189. test expr-4.22 {string operators} {expr {0?"foo":"bar"}} bar
  190.  
  191. # Operators that aren't legal on string operands.
  192.  
  193. test expr-5.1 {illegal string operations} {
  194.     list [catch {expr {-"a"}} msg] $msg
  195. } {1 {can't use non-numeric string as operand of "-"}}
  196. test expr-5.2 {illegal string operations} {
  197.     list [catch {expr {~"a"}} msg] $msg
  198. } {1 {can't use non-numeric string as operand of "~"}}
  199. test expr-5.3 {illegal string operations} {
  200.     list [catch {expr {!"a"}} msg] $msg
  201. } {1 {can't use non-numeric string as operand of "!"}}
  202. test expr-5.4 {illegal string operations} {
  203.     list [catch {expr {"a"*"b"}} msg] $msg
  204. } {1 {can't use non-numeric string as operand of "*"}}
  205. test expr-5.5 {illegal string operations} {
  206.     list [catch {expr {"a"/"b"}} msg] $msg
  207. } {1 {can't use non-numeric string as operand of "/"}}
  208. test expr-5.6 {illegal string operations} {
  209.     list [catch {expr {"a"%"b"}} msg] $msg
  210. } {1 {can't use non-numeric string as operand of "%"}}
  211. test expr-5.7 {illegal string operations} {
  212.     list [catch {expr {"a"+"b"}} msg] $msg
  213. } {1 {can't use non-numeric string as operand of "+"}}
  214. test expr-5.8 {illegal string operations} {
  215.     list [catch {expr {"a"-"b"}} msg] $msg
  216. } {1 {can't use non-numeric string as operand of "-"}}
  217. test expr-5.9 {illegal string operations} {
  218.     list [catch {expr {"a"<<"b"}} msg] $msg
  219. } {1 {can't use non-numeric string as operand of "<<"}}
  220. test expr-5.10 {illegal string operations} {
  221.     list [catch {expr {"a">>"b"}} msg] $msg
  222. } {1 {can't use non-numeric string as operand of ">>"}}
  223. test expr-5.11 {illegal string operations} {
  224.     list [catch {expr {"a"&"b"}} msg] $msg
  225. } {1 {can't use non-numeric string as operand of "&"}}
  226. test expr-5.12 {illegal string operations} {
  227.     list [catch {expr {"a"^"b"}} msg] $msg
  228. } {1 {can't use non-numeric string as operand of "^"}}
  229. test expr-5.13 {illegal string operations} {
  230.     list [catch {expr {"a"|"b"}} msg] $msg
  231. } {1 {can't use non-numeric string as operand of "|"}}
  232. test expr-5.14 {illegal string operations} {
  233.     list [catch {expr {"a"&&"b"}} msg] $msg
  234. } {1 {can't use non-numeric string as operand of "&&"}}
  235. test expr-5.15 {illegal string operations} {
  236.     list [catch {expr {"a"||"b"}} msg] $msg
  237. } {1 {can't use non-numeric string as operand of "||"}}
  238. test expr-5.16 {illegal string operations} {
  239.     list [catch {expr {"a"?4:2}} msg] $msg
  240. } {1 {can't use non-numeric string as operand of "?"}}
  241.  
  242. # Check precedence pairwise.
  243.  
  244. test expr-6.1 {precedence checks} {expr -~3} 4
  245. test expr-6.2 {precedence checks} {expr -!3} 0
  246. test expr-6.3 {precedence checks} {expr -~0} 1
  247.  
  248. test expr-7.1 {precedence checks} {expr 2*4/6} 1
  249. test expr-7.2 {precedence checks} {expr 24/6*3} 12
  250. test expr-7.3 {precedence checks} {expr 24/6/2} 2
  251.  
  252. test expr-8.1 {precedence checks} {expr -2+4} 2
  253. test expr-8.2 {precedence checks} {expr -2-4} -6
  254.  
  255. test expr-9.1 {precedence checks} {expr 2*3+4} 10
  256. test expr-9.2 {precedence checks} {expr 8/2+4} 8
  257. test expr-9.3 {precedence checks} {expr 8%3+4} 6
  258. test expr-9.4 {precedence checks} {expr 2*3-1} 5
  259. test expr-9.5 {precedence checks} {expr 8/2-1} 3
  260. test expr-9.6 {precedence checks} {expr 8%3-1} 1
  261.  
  262. test expr-10.1 {precedence checks} {expr 6-3-2} 1
  263.  
  264. test expr-11.1 {precedence checks} {expr 7+1>>2} 2
  265. test expr-11.2 {precedence checks} {expr 7+1<<2} 32
  266. test expr-11.3 {precedence checks} {expr 7>>3-2} 3
  267. test expr-11.4 {precedence checks} {expr 7<<3-2} 14
  268.  
  269. test expr-12.1 {precedence checks} {expr 6>>1>4} 0
  270. test expr-12.2 {precedence checks} {expr 6>>1<2} 0
  271. test expr-12.3 {precedence checks} {expr 6>>1>=3} 1
  272. test expr-12.4 {precedence checks} {expr 6>>1<=2} 0
  273. test expr-12.5 {precedence checks} {expr 6<<1>5} 1
  274. test expr-12.6 {precedence checks} {expr 6<<1<5} 0
  275. test expr-12.7 {precedence checks} {expr 5<=6<<1} 1
  276. test expr-12.8 {precedence checks} {expr 5>=6<<1} 0
  277.  
  278. test expr-13.1 {precedence checks} {expr 2<3<4} 1
  279. test expr-13.2 {precedence checks} {expr 0<4>2} 0
  280. test expr-13.3 {precedence checks} {expr 4>2<1} 0
  281. test expr-13.4 {precedence checks} {expr 4>3>2} 0
  282. test expr-13.5 {precedence checks} {expr 4>3>=2} 0
  283. test expr-13.6 {precedence checks} {expr 4>=3>2} 0
  284. test expr-13.7 {precedence checks} {expr 4>=3>=2} 0
  285. test expr-13.8 {precedence checks} {expr 0<=4>=2} 0
  286. test expr-13.9 {precedence checks} {expr 4>=2<=0} 0
  287. test expr-10.10 {precedence checks} {expr 2<=3<=4} 1
  288.  
  289. test expr-14.1 {precedence checks} {expr 1==4>3} 1
  290. test expr-14.2 {precedence checks} {expr 0!=4>3} 1
  291. test expr-14.3 {precedence checks} {expr 1==3<4} 1
  292. test expr-14.4 {precedence checks} {expr 0!=3<4} 1
  293. test expr-14.5 {precedence checks} {expr 1==4>=3} 1
  294. test expr-14.6 {precedence checks} {expr 0!=4>=3} 1
  295. test expr-14.7 {precedence checks} {expr 1==3<=4} 1
  296. test expr-14.8 {precedence checks} {expr 0!=3<=4} 1
  297.  
  298. test expr-15.1 {precedence checks} {expr 1==3==3} 0
  299. test expr-15.2 {precedence checks} {expr 3==3!=2} 1
  300. test expr-15.3 {precedence checks} {expr 2!=3==3} 0
  301. test expr-15.4 {precedence checks} {expr 2!=1!=1} 0
  302.  
  303. test expr-16.1 {precedence checks} {expr 2&3==2} 0
  304. test expr-16.2 {precedence checks} {expr 1&3!=3} 0
  305.  
  306. test expr-17.1 {precedence checks} {expr 7&3^0x10} 19
  307. test expr-17.2 {precedence checks} {expr 7^0x10&3} 7
  308.  
  309. test expr-18.1 {precedence checks} {expr 7^0x10|3} 23
  310. test expr-18.2 {precedence checks} {expr 7|0x10^3} 23
  311.  
  312. test expr-19.1 {precedence checks} {expr 7|3&&1} 1
  313. test expr-19.2 {precedence checks} {expr 1&&3|7} 1
  314. test expr-19.3 {precedence checks} {expr 0&&1||1} 1
  315. test expr-19.4 {precedence checks} {expr 1||1&&0} 1
  316.  
  317. test expr-20.1 {precedence checks} {expr 1||0?3:4} 3
  318. test expr-20.2 {precedence checks} {expr 1?0:4||1} 0
  319.  
  320. # Parentheses.
  321.  
  322. test expr-21.1 {parenthesization} {expr (2+4)*6} 36
  323. test expr-21.2 {parenthesization} {expr (1?0:4)||1} 1
  324.  
  325. # Embedded commands and variable names.
  326.  
  327. set a 16
  328. test expr-22.1 {embedded variables} {expr {2*$a}} 32
  329. test expr-22.2 {embedded variables} {
  330.     set x -5
  331.     set y 10
  332.     expr {$x + $y}
  333. } {5}
  334. test expr-22.3 {embedded variables} {
  335.     set x "  -5"
  336.     set y "  +10"
  337.     expr {$x + $y}
  338. } {5}
  339. test expr-22.4 {embedded commands and variables} {expr {[set a] - 14}} 2
  340. test expr-22.5 {embedded commands and variables} {
  341.     list [catch {expr {12 - [bad_command_name]}} msg] $msg
  342. } {1 {invalid command name: "bad_command_name"}}
  343.  
  344. # Double-quotes and things inside them.
  345.  
  346. test expr-23.1 {double-quotes} {expr {"abc"}} abc
  347. test expr-23.2 {double-quotes} {
  348.     set a 189
  349.     expr {"$a.bc"}
  350. } 189.bc
  351. test expr-23.3 {double-quotes} {
  352.     set b2 xyx
  353.     expr {"$b2$b2$b2.[set b2].[set b2]"}
  354. } xyxxyxxyx.xyx.xyx
  355. test expr-23.4 {double-quotes} {expr {"11\}\}22"}} 11}}22
  356. test expr-23.5 {double-quotes} {expr {"\*bc"}} {*bc}
  357. test expr-23.6 {double-quotes} {
  358.     catch {unset bogus__}
  359.     list [catch {expr {"$bogus__"}} msg] $msg
  360. } {1 {can't read "bogus__": no such variable}}
  361. test expr-23.7 {double-quotes} {
  362.     list [catch {expr {"a[error Testing]bc"}} msg] $msg
  363. } {1 Testing}
  364.  
  365. # Numbers in various bases.
  366.  
  367. test expr-24.1 {numbers in different bases} {expr 0x20} 32
  368. test expr-24.2 {numbers in different bases} {expr 015} 13
  369.  
  370. # Conversions between various data types.
  371.  
  372. test expr-25.1 {type conversions} {expr 2+2.5} 4.5
  373. test expr-25.2 {type conversions} {expr 2.5+2} 4.5
  374. test expr-25.3 {type conversions} {expr 2-2.5} -0.5
  375. test expr-25.4 {type conversions} {expr 2/2.5} 0.8
  376. test expr-25.5 {type conversions} {expr 2>2.5} 0
  377. test expr-25.6 {type conversions} {expr 2.5>2} 1
  378. test expr-25.7 {type conversions} {expr 2<2.5} 1
  379. test expr-25.8 {type conversions} {expr 2>=2.5} 0
  380. test expr-25.9 {type conversions} {expr 2<=2.5} 1
  381. test expr-25.10 {type conversions} {expr 2==2.5} 0
  382. test expr-25.11 {type conversions} {expr 2!=2.5} 1
  383. test expr-25.12 {type conversions} {expr 2>"ab"} 0
  384. test expr-25.13 {type conversions} {expr {2>" "}} 1
  385. test expr-25.14 {type conversions} {expr {"24.1a" > 24.1}} 1
  386. test expr-25.15 {type conversions} {expr {24.1 > "24.1a"}} 0
  387. test expr-25.16 {type conversions} {expr 2+2.5} 4.5
  388. test expr-25.17 {type conversions} {expr 2+2.5} 4.5
  389. test expr-25.18 {type conversions} {expr 2.0e2} 200.0
  390. test expr-25.19 {type conversions} {expr 2.0e15} 2e+15
  391. test expr-25.20 {type conversions} {expr 10.0} 10.0
  392.  
  393. # Various error conditions.
  394.  
  395. test expr-26.1 {error conditions} {
  396.     list [catch {expr 2+"a"} msg] $msg
  397. } {1 {can't use non-numeric string as operand of "+"}}
  398. test expr-26.2 {error conditions} {
  399.     list [catch {expr 2+4*} msg] $msg
  400. } {1 {syntax error in expression "2+4*"}}
  401. test expr-26.3 {error conditions} {
  402.     list [catch {expr 2+4*(} msg] $msg
  403. } {1 {syntax error in expression "2+4*("}}
  404. catch {unset _non_existent_}
  405. test expr-26.4 {error conditions} {
  406.     list [catch {expr 2+$_non_existent_} msg] $msg
  407. } {1 {can't read "_non_existent_": no such variable}}
  408. set a xx
  409. test expr-26.5 {error conditions} {
  410.     list [catch {expr {2+$a}} msg] $msg
  411. } {1 {can't use non-numeric string as operand of "+"}}
  412. test expr-26.6 {error conditions} {
  413.     list [catch {expr {2+[set a]}} msg] $msg
  414. } {1 {can't use non-numeric string as operand of "+"}}
  415. test expr-26.7 {error conditions} {
  416.     list [catch {expr {2+(4}} msg] $msg
  417. } {1 {unmatched parentheses in expression "2+(4"}}
  418. test expr-26.8 {error conditions} {
  419.     list [catch {expr 2/0} msg] $msg $errorCode
  420. } {1 {divide by zero} {ARITH DIVZERO {divide by zero}}}
  421. test expr-26.9 {error conditions} {
  422.     list [catch {expr 2%0} msg] $msg $errorCode
  423. } {1 {divide by zero} {ARITH DIVZERO {divide by zero}}}
  424. test expr-26.10 {error conditions} {
  425.     list [catch {expr 2.0/0.0} msg] $msg $errorCode
  426. } {1 {divide by zero} {ARITH DIVZERO {divide by zero}}}
  427. test expr-26.11 {error conditions} {
  428.     list [catch {expr 2#} msg] $msg
  429. } {1 {syntax error in expression "2#"}}
  430. test expr-26.12 {error conditions} {
  431.     list [catch {expr a.b} msg] $msg
  432. } {1 {syntax error in expression "a.b"}}
  433. test expr-26.13 {error conditions} {
  434.     list [catch {expr {"a"/"b"}} msg] $msg
  435. } {1 {can't use non-numeric string as operand of "/"}}
  436. test expr-26.14 {error conditions} {
  437.     list [catch {expr 2:3} msg] $msg
  438. } {1 {can't have : operator without ? first}}
  439. test expr-26.15 {error conditions} {
  440.     list [catch {expr a@b} msg] $msg
  441. } {1 {syntax error in expression "a@b"}}
  442. test expr-26.16 {error conditions} {
  443.     list [catch {expr a[b} msg] $msg
  444. } {1 {missing close-bracket}}
  445. test expr-26.17 {error conditions} {
  446.     list [catch {expr a`b} msg] $msg
  447. } {1 {syntax error in expression "a`b"}}
  448. test expr-26.18 {error conditions} {
  449.     list [catch {expr \"a\"\{b} msg] $msg
  450. } {1 {missing close-brace}}
  451. test expr-26.19 {error conditions} {
  452.     list [catch {expr a} msg] $msg
  453. } {1 {syntax error in expression "a"}}
  454. test expr-26.20 {error conditions} {
  455.     list [catch expr msg] $msg
  456. } {1 {wrong # args: should be "expr arg ?arg ...?"}}
  457.  
  458. # Cancelled evaluation.
  459.  
  460. test expr-27.1 {cancelled evaluation} {
  461.     set a 1
  462.     expr {0&&[set a 2]}
  463.     set a
  464. } 1
  465. test expr-27.2 {cancelled evaluation} {
  466.     set a 1
  467.     expr {1||[set a 2]}
  468.     set a
  469. } 1
  470. test expr-27.3 {cancelled evaluation} {
  471.     set a 1
  472.     expr {0?[set a 2]:1}
  473.     set a
  474. } 1
  475. test expr-27.4 {cancelled evaluation} {
  476.     set a 1
  477.     expr {1?2:[set a 2]}
  478.     set a
  479. } 1
  480. catch {unset x}
  481. test expr-27.5 {cancelled evaluation} {
  482.     list [catch {expr {[info exists x] && $x}} msg] $msg
  483. } {0 0}
  484. test expr-27.6 {cancelled evaluation} {
  485.     list [catch {expr {0 && [concat $x]}} msg] $msg
  486. } {0 0}
  487.  
  488. # Tcl_ExprBool as used in "if" statements
  489.  
  490. test expr-28.1 {Tcl_ExprBoolean usage} {
  491.     set a 1
  492.     if {2} {set a 2}
  493.     set a
  494. } 2
  495. test expr-28.2 {Tcl_ExprBoolean usage} {
  496.     set a 1
  497.     if {0} {set a 2}
  498.     set a
  499. } 1
  500. test expr-28.3 {Tcl_ExprBoolean usage} {
  501.     set a 1
  502.     if {1.2} {set a 2}
  503.     set a
  504. } 2
  505. test expr-28.4 {Tcl_ExprBoolean usage} {
  506.     set a 1
  507.     if {-1.1} {set a 2}
  508.     set a
  509. } 2
  510. test expr-28.5 {Tcl_ExprBoolean usage} {
  511.     set a 1
  512.     if {0.0} {set a 2}
  513.     set a
  514. } 1
  515. test expr-28.6 {Tcl_ExprBoolean usage} {
  516.     set a 1
  517.     if {"YES"} {set a 2}
  518.     set a
  519. } 2
  520. test expr-28.7 {Tcl_ExprBoolean usage} {
  521.     set a 1
  522.     if {"no"} {set a 2}
  523.     set a
  524. } 1
  525. test expr-28.8 {Tcl_ExprBoolean usage} {
  526.     set a 1
  527.     if {"true"} {set a 2}
  528.     set a
  529. } 2
  530. test expr-28.9 {Tcl_ExprBoolean usage} {
  531.     set a 1
  532.     if {"fAlse"} {set a 2}
  533.     set a
  534. } 1
  535. test expr-28.10 {Tcl_ExprBoolean usage} {
  536.     set a 1
  537.     if {"on"} {set a 2}
  538.     set a
  539. } 2
  540. test expr-28.11 {Tcl_ExprBoolean usage} {
  541.     set a 1
  542.     if {"Off"} {set a 2}
  543.     set a
  544. } 1
  545. test expr-28.12 {Tcl_ExprBool usage} {
  546.     list [catch {if {"abc"} {}} msg] $msg
  547. } {1 {expected boolean value but got "abc"}}
  548.  
  549. # Operands enclosed in braces
  550.  
  551. test expr-29.1 {braces} {expr {{abc}}} abc
  552. test expr-29.2 {braces} {expr {{00010}}} 8
  553. test expr-29.3 {braces} {expr {{3.1200000}}} 3.12
  554. test expr-29.4 {braces} {expr {{a{b}{1 {2 3}}c}}} "a{b}{1 {2 3}}c"
  555. test expr-29.5 {braces} {
  556.     list [catch {expr "\{abc"} msg] $msg
  557. } {1 {missing close-brace}}
  558.  
  559. # Very long values
  560.  
  561. test expr-30.1 {long values} {
  562.     set a "0000 1111 2222 3333 4444"
  563.     set a "$a | $a | $a | $a | $a"
  564.     set a "$a || $a || $a || $a || $a"
  565.     expr {$a}
  566. } {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}
  567. test expr-30.2 {long values} {
  568.     set a "000000000000000000000000000000"
  569.     set a "$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a${a}5"
  570.     expr $a
  571. } 5
  572.  
  573. # Expressions spanning multiple arguments
  574.  
  575. test expr-31.1 {multiple arguments to expr command} {
  576.     expr 4 + ( 6 *12) -3
  577. } 73
  578. test expr-31.2 {multiple arguments to expr command} {
  579.     list [catch {expr 2 + (3 + 4} msg] $msg
  580. } {1 {unmatched parentheses in expression "2 + (3 + 4"}}
  581. test expr-31.3 {multiple arguments to expr command} {
  582.     list [catch {expr 2 + 3 +} msg] $msg
  583. } {1 {syntax error in expression "2 + 3 +"}}
  584. test expr-31.4 {multiple arguments to expr command} {
  585.     list [catch {expr 2 + 3 )} msg] $msg
  586. } {1 {syntax error in expression "2 + 3 )"}}
  587.  
  588. # Math functions
  589.  
  590. test expr-32.1 {math functions in expressions} {
  591.     expr acos(0.5)
  592. } {1.0472}
  593. test expr-32.2 {math functions in expressions} {
  594.     expr asin(0.5)
  595. } {0.523599}
  596. test expr-32.3 {math functions in expressions} {
  597.     expr atan(1.0)
  598. } {0.785398}
  599. test expr-32.4 {math functions in expressions} {
  600.     expr atan2(2.0, 2.0)
  601. } {0.785398}
  602. test expr-32.5 {math functions in expressions} {
  603.     expr ceil(1.999)
  604. } {2.0}
  605. test expr-32.6 {math functions in expressions} {
  606.     expr cos(.1)
  607. } {0.995004}
  608. test expr-32.7 {math functions in expressions} {
  609.     expr cosh(.1)
  610. } {1.005}
  611. test expr-32.8 {math functions in expressions} {
  612.     expr exp(1.0)
  613. } {2.71828}
  614. test expr-32.9 {math functions in expressions} {
  615.     expr floor(2.000)
  616. } {2.0}
  617. test expr-32.10 {math functions in expressions} {
  618.     expr floor(2.001)
  619. } {2.0}
  620. test expr-32.11 {math functions in expressions} {
  621.     expr fmod(7.3, 3.2)
  622. } {0.9}
  623. test expr-32.12 {math functions in expressions} {
  624.     expr hypot(3.0, 4.0)
  625. } {5.0}
  626. test expr-32.13 {math functions in expressions} {
  627.     expr log(2.8)
  628. } {1.02962}
  629. test expr-32.14 {math functions in expressions} {
  630.     expr log10(2.8)
  631. } {0.447158}
  632. test expr-32.15 {math functions in expressions} {
  633.     expr pow(2.1, 3.1)
  634. } {9.97424}
  635. test expr-32.16 {math functions in expressions} {
  636.     expr sin(.1)
  637. } {0.0998334}
  638. test expr-32.17 {math functions in expressions} {
  639.     expr sinh(.1)
  640. } {0.100167}
  641. test expr-32.18 {math functions in expressions} {
  642.     expr sqrt(2.0)
  643. } {1.41421}
  644. test expr-32.19 {math functions in expressions} {
  645.     expr tan(0.8)
  646. } {1.02964}
  647. test expr-32.20 {math functions in expressions} {
  648.     expr tanh(0.8)
  649. } {0.664037}
  650. test expr-32.21 {math functions in expressions} {
  651.     expr abs(-1.8)
  652. } {1.8}
  653. test expr-32.22 {math functions in expressions} {
  654.     expr abs(10.0)
  655. } {10.0}
  656. test expr-32.23 {math functions in expressions} {
  657.     expr abs(-4)
  658. } {4}
  659. test expr-32.24 {math functions in expressions} {
  660.     expr abs(66)
  661. } {66}
  662. if ($atBerkeley) {
  663.     test expr-32.25 {math functions in expressions} {
  664.     list [catch {expr abs(0x80000000)} msg] $msg
  665.     } {1 {integer value too large to represent}}
  666. }
  667. test expr-32.26 {math functions in expressions} {
  668.     expr double(1)
  669. } {1.0}
  670. test expr-32.27 {math functions in expressions} {
  671.     expr double(1.1)
  672. } {1.1}
  673. test expr-32.28 {math functions in expressions} {
  674.     expr int(1)
  675. } {1}
  676. test expr-32.29 {math functions in expressions} {
  677.     expr int(1.4)
  678. } {1}
  679. test expr-32.30 {math functions in expressions} {
  680.     expr int(1.6)
  681. } {1}
  682. test expr-32.31 {math functions in expressions} {
  683.     expr int(-1.4)
  684. } {-1}
  685. test expr-32.32 {math functions in expressions} {
  686.     expr int(-1.6)
  687. } {-1}
  688. test expr-32.33 {math functions in expressions} {
  689.     list [catch {expr int(1e60)} msg] $msg
  690. } {1 {integer value too large to represent}}
  691. test expr-32.34 {math functions in expressions} {
  692.     list [catch {expr int(-1e60)} msg] $msg
  693. } {1 {integer value too large to represent}}
  694. test expr-32.35 {math functions in expressions} {
  695.     expr round(1.49)
  696. } {1}
  697. test expr-32.36 {math functions in expressions} {
  698.     expr round(1.51)
  699. } {2}
  700. test expr-32.37 {math functions in expressions} {
  701.     expr round(-1.49)
  702. } {-1}
  703. test expr-32.38 {math functions in expressions} {
  704.     expr round(-1.51)
  705. } {-2}
  706. test expr-32.39 {math functions in expressions} {
  707.     list [catch {expr round(1e60)} msg] $msg
  708. } {1 {integer value too large to represent}}
  709. test expr-32.40 {math functions in expressions} {
  710.     list [catch {expr round(-1e60)} msg] $msg
  711. } {1 {integer value too large to represent}}
  712. test expr-32.41 {math functions in expressions} {
  713.     list [catch {expr pow(1.0 + 3.0 - 2, .8 * 5)} msg] $msg
  714. } {0 16.0}
  715. test expr-32.42 {math functions in expressions} {
  716.     list [catch {expr hypot(5*.8,3)} msg] $msg
  717. } {0 5.0}
  718. if $gotT1 {
  719.     test expr-32.43 {math functions in expressions} {
  720.     expr 2*T1()
  721.     } 246
  722.     test expr-32.44 {math functions in expressions} {
  723.     expr T2()*3
  724.     } 1035
  725. }
  726.  
  727. test expr-33.1 {conversions and fancy args to math functions} {
  728.     expr hypot ( 3 , 4 )
  729. } 5.0
  730. test expr-33.2 {conversions and fancy args to math functions} {
  731.     expr hypot ( (2.0+1.0) , 4 )
  732. } 5.0
  733. test expr-33.3 {conversions and fancy args to math functions} {
  734.     expr hypot ( 3 , (3.0 + 1.0) )
  735. } 5.0
  736. test expr-33.4 {conversions and fancy args to math functions} {
  737.     expr cos(acos(0.1))
  738. } 0.1
  739.  
  740. test expr-34.1 {errors in math functions} {
  741.     list [catch {expr func_2(1.0)} msg] $msg
  742. } {1 {unknown math function "func_2"}}
  743. test expr-34.2 {errors in math functions} {
  744.     list [catch {expr func|(1.0)} msg] $msg
  745. } {1 {syntax error in expression "func|(1.0)"}}
  746. test expr-34.3 {errors in math functions} {
  747.     list [catch {expr {hypot("a b", 2.0)}} msg] $msg
  748. } {1 {argument to math function didn't have numeric value}}
  749. test expr-34.4 {errors in math functions} {
  750.     list [catch {expr hypot(1.0 2.0)} msg] $msg
  751. } {1 {syntax error in expression "hypot(1.0 2.0)"}}
  752. test expr-34.5 {errors in math functions} {
  753.     list [catch {expr hypot(1.0, 2.0} msg] $msg
  754. } {1 {syntax error in expression "hypot(1.0, 2.0"}}
  755. test expr-34.6 {errors in math functions} {
  756.     list [catch {expr hypot(1.0 ,} msg] $msg
  757. } {1 {syntax error in expression "hypot(1.0 ,"}}
  758. test expr-34.7 {errors in math functions} {
  759.     list [catch {expr hypot(1.0)} msg] $msg
  760. } {1 {too few arguments for math function}}
  761. test expr-34.8 {errors in math functions} {
  762.     list [catch {expr hypot(1.0, 2.0, 3.0)} msg] $msg
  763. } {1 {too many arguments for math function}}
  764. test expr-34.9 {errors in math functions} {
  765.     list [catch {expr acos(-2.0)} msg] $msg $errorCode
  766. } {1 {domain error: argument not in valid range} {ARITH DOMAIN {domain error: argument not in valid range}}}
  767. if $atBerkeley {
  768.     test expr-34.10 {errors in math functions} {
  769.     list [catch {expr pow(-3, 1000001)} msg] $msg $errorCode
  770.     } {1 {floating-point value too large to represent} {ARITH OVERFLOW {floating-point value too large to represent}}}
  771. }
  772. test expr-34.11 {errors in math functions} {
  773.     list [catch {expr pow(3, 1000001)} msg] $msg $errorCode
  774. } {1 {floating-point value too large to represent} {ARITH OVERFLOW {floating-point value too large to represent}}}
  775. test expr-34.12 {errors in math functions} {
  776.     list [catch {expr -14.0*exp(100000)} msg] $msg $errorCode
  777. } {1 {floating-point value too large to represent} {ARITH OVERFLOW {floating-point value too large to represent}}}
  778. test expr-34.13 {errors in math functions} {
  779.     list [catch {expr int(1.0e30)} msg] $msg $errorCode
  780. } {1 {integer value too large to represent} {ARITH IOVERFLOW {integer value too large to represent}}}
  781. test expr-34.14 {errors in math functions} {
  782.     list [catch {expr int(-1.0e30)} msg] $msg $errorCode
  783. } {1 {integer value too large to represent} {ARITH IOVERFLOW {integer value too large to represent}}}
  784. test expr-34.15 {errors in math functions} {
  785.     list [catch {expr round(1.0e30)} msg] $msg $errorCode
  786. } {1 {integer value too large to represent} {ARITH IOVERFLOW {integer value too large to represent}}}
  787. test expr-34.16 {errors in math functions} {
  788.     list [catch {expr round(-1.0e30)} msg] $msg $errorCode
  789. } {1 {integer value too large to represent} {ARITH IOVERFLOW {integer value too large to represent}}}
  790. if $gotT1 {
  791.     test expr-34.17 {errors in math functions} {
  792.     list [catch {expr T1(4)} msg] $msg
  793.     } {1 {syntax error in expression "T1(4)"}}
  794. }
  795.  
  796. catch {unset tcl_precision}
  797. test expr-35.1 {tcl_precision variable} {
  798.     expr 2.0/3
  799. } 0.666667
  800. set tcl_precision 1
  801. test expr-35.2 {tcl_precision variable} {
  802.     expr 2.0/3
  803. } 0.7
  804. test expr-35.3 {tcl_precision variable} {
  805.     expr 2.0/3
  806. } 0.7
  807. test expr-35.4 {tcl_precision variable} {
  808.     list [catch {set tcl_precision 0} msg] $msg [expr 2.0/3]
  809. } {1 {can't set "tcl_precision": improper value for precision} 0.7}
  810. test expr-35.5 {tcl_precision variable} {
  811.     list [catch {set tcl_precision 101} msg] $msg [expr 2.0/3]
  812. } {1 {can't set "tcl_precision": improper value for precision} 0.7}
  813. test expr-35.6 {tcl_precision variable} {
  814.     list [catch {set tcl_precision {}} msg] $msg [expr 2.0/3]
  815. } {1 {can't set "tcl_precision": improper value for precision} 0.7}
  816. test expr-35.7 {tcl_precision variable} {
  817.     list [catch {set tcl_precision {1 2 3}} msg] $msg [expr 2.0/3]
  818. } {1 {can't set "tcl_precision": improper value for precision} 0.7}
  819. catch {unset tcl_precision}
  820. test expr-35.8 {tcl_precision variable} {
  821.     expr 2.0/3
  822. } 0.666667
  823.