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