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

  1. # Commands covered:  proc, return, global
  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/proc.test,v 1.15 93/08/03 16:10:28 ouster Exp $ (Berkeley)
  28.  
  29. if {[string compare test [info procs test]] == 1} then {source defs}
  30.  
  31. proc tproc {} {return a; return b}
  32. test proc-1.1 {simple procedure call and return} {tproc} a
  33. proc tproc x {
  34.     set x [expr $x+1]
  35.     return $x
  36. }
  37. test proc-1.2 {simple procedure call and return} {tproc 2} 3
  38. test proc-1.3 {simple procedure call and return} {
  39.     proc tproc {} {return foo}
  40. } {}
  41. test proc-1.4 {simple procedure call and return} {
  42.     proc tproc {} {return}
  43.     tproc
  44. } {}
  45.  
  46. test proc-2.1 {local and global variables} {
  47.     proc tproc x {
  48.     set x [expr $x+1]
  49.     return $x
  50.     }
  51.     set x 42
  52.     list [tproc 6] $x
  53. } {7 42}
  54. test proc-2.2 {local and global variables} {
  55.     proc tproc x {
  56.     set y [expr $x+1]
  57.     return $y
  58.     }
  59.     set y 18
  60.     list [tproc 6] $y
  61. } {7 18}
  62. test proc-2.3 {local and global variables} {
  63.     proc tproc x {
  64.     global y
  65.     set y [expr $x+1]
  66.     return $y
  67.     }
  68.     set y 189
  69.     list [tproc 6] $y
  70. } {7 7}
  71. test proc-2.4 {local and global variables} {
  72.     proc tproc x {
  73.     global y
  74.     return [expr $x+$y]
  75.     }
  76.     set y 189
  77.     list [tproc 6] $y
  78. } {195 189}
  79. catch {unset _undefined_}
  80. test proc-2.5 {local and global variables} {
  81.     proc tproc x {
  82.     global _undefined_
  83.     return $_undefined_
  84.     }
  85.     list [catch {tproc xxx} msg] $msg
  86. } {1 {can't read "_undefined_": no such variable}}
  87. test proc-2.6 {local and global variables} {
  88.     set a 114
  89.     set b 115
  90.     global a b
  91.     list $a $b
  92. } {114 115}
  93.  
  94. proc do {cmd} {eval $cmd}
  95. test proc-3.1 {local and global arrays} {
  96.     catch {unset a}
  97.     set a(0) 22
  98.     list [catch {do {global a; set a(0)}} msg] $msg
  99. } {0 22}
  100. test proc-3.2 {local and global arrays} {
  101.     catch {unset a}
  102.     set a(x) 22
  103.     list [catch {do {global a; set a(x) newValue}} msg] $msg $a(x)
  104. } {0 newValue newValue}
  105. test proc-3.3 {local and global arrays} {
  106.     catch {unset a}
  107.     set a(x) 22
  108.     set a(y) 33
  109.     list [catch {do {global a; unset a(y)}; array names a} msg] $msg
  110. } {0 x}
  111. test proc-3.4 {local and global arrays} {
  112.     catch {unset a}
  113.     set a(x) 22
  114.     set a(y) 33
  115.     list [catch {do {global a; unset a; info exists a}} msg] $msg \
  116.         [info exists a]
  117. } {0 0 0}
  118. test proc-3.5 {local and global arrays} {
  119.     catch {unset a}
  120.     set a(x) 22
  121.     set a(y) 33
  122.     list [catch {do {global a; unset a(y); array names a}} msg] $msg
  123. } {0 x}
  124. catch {unset a}
  125. test proc-3.6 {local and global arrays} {
  126.     catch {unset a}
  127.     set a(x) 22
  128.     set a(y) 33
  129.     do {global a; do {global a; unset a}; set a(z) 22}
  130.     list [catch {array names a} msg] $msg
  131. } {0 z}
  132. test proc-3.7 {local and global arrays} {
  133.     proc t1 {args} {global info; set info 1}
  134.     catch {unset a}
  135.     set info {}
  136.     do {global a; trace var a(1) w t1}
  137.     set a(1) 44
  138.     set info
  139. } 1
  140. test proc-3.8 {local and global arrays} {
  141.     proc t1 {args} {global info; set info 1}
  142.     catch {unset a}
  143.     trace var a(1) w t1
  144.     set info {}
  145.     do {global a; trace vdelete a(1) w t1}
  146.     set a(1) 44
  147.     set info
  148. } {}
  149. test proc-3.9 {local and global arrays} {
  150.     proc t1 {args} {global info; set info 1}
  151.     catch {unset a}
  152.     trace var a(1) w t1
  153.     do {global a; trace vinfo a(1)}
  154. } {{w t1}}
  155. catch {unset a}
  156.  
  157. test proc-3.1 {arguments and defaults} {
  158.     proc tproc {x y z} {
  159.     return [list $x $y $z]
  160.     }
  161.     tproc 11 12 13
  162. } {11 12 13}
  163. test proc-3.2 {arguments and defaults} {
  164.     proc tproc {x y z} {
  165.     return [list $x $y $z]
  166.     }
  167.     list [catch {tproc 11 12} msg] $msg
  168. } {1 {no value given for parameter "z" to "tproc"}}
  169. test proc-3.3 {arguments and defaults} {
  170.     proc tproc {x y z} {
  171.     return [list $x $y $z]
  172.     }
  173.     list [catch {tproc 11 12 13 14} msg] $msg
  174. } {1 {called "tproc" with too many arguments}}
  175. test proc-3.4 {arguments and defaults} {
  176.     proc tproc {x {y y-default} {z z-default}} {
  177.     return [list $x $y $z]
  178.     }
  179.     tproc 11 12 13
  180. } {11 12 13}
  181. test proc-3.5 {arguments and defaults} {
  182.     proc tproc {x {y y-default} {z z-default}} {
  183.     return [list $x $y $z]
  184.     }
  185.     tproc 11 12
  186. } {11 12 z-default}
  187. test proc-3.6 {arguments and defaults} {
  188.     proc tproc {x {y y-default} {z z-default}} {
  189.     return [list $x $y $z]
  190.     }
  191.     tproc 11
  192. } {11 y-default z-default}
  193. test proc-3.7 {arguments and defaults} {
  194.     proc tproc {x {y y-default} {z z-default}} {
  195.     return [list $x $y $z]
  196.     }
  197.     list [catch {tproc} msg] $msg
  198. } {1 {no value given for parameter "x" to "tproc"}}
  199. test proc-3.8 {arguments and defaults} {
  200.     list [catch {
  201.     proc tproc {x {y y-default} z} {
  202.         return [list $x $y $z]
  203.     }
  204.     tproc 2 3
  205.     } msg] $msg
  206. } {1 {no value given for parameter "z" to "tproc"}}
  207. test proc-3.9 {arguments and defaults} {
  208.     proc tproc {x {y y-default} args} {
  209.     return [list $x $y $args]
  210.     }
  211.     tproc 2 3 4 5
  212. } {2 3 {4 5}}
  213. test proc-3.10 {arguments and defaults} {
  214.     proc tproc {x {y y-default} args} {
  215.     return [list $x $y $args]
  216.     }
  217.     tproc 2 3
  218. } {2 3 {}}
  219. test proc-3.11 {arguments and defaults} {
  220.     proc tproc {x {y y-default} args} {
  221.     return [list $x $y $args]
  222.     }
  223.     tproc 2
  224. } {2 y-default {}}
  225. test proc-3.12 {arguments and defaults} {
  226.     proc tproc {x {y y-default} args} {
  227.     return [list $x $y $args]
  228.     }
  229.     list [catch {tproc} msg] $msg
  230. } {1 {no value given for parameter "x" to "tproc"}}
  231.  
  232. test proc-4.1 {variable numbers of arguments} {
  233.     proc tproc args {return $args}
  234.     tproc
  235. } {}
  236. test proc-4.2 {variable numbers of arguments} {
  237.     proc tproc args {return $args}
  238.     tproc 1 2 3 4 5 6 7 8
  239. } {1 2 3 4 5 6 7 8}
  240. test proc-4.3 {variable numbers of arguments} {
  241.     proc tproc args {return $args}
  242.     tproc 1 {2 3} {4 {5 6} {{{7}}}} 8
  243. } {1 {2 3} {4 {5 6} {{{7}}}} 8}
  244. test proc-4.4 {variable numbers of arguments} {
  245.     proc tproc {x y args} {return $args}
  246.     tproc 1 2 3 4 5 6 7
  247. } {3 4 5 6 7}
  248. test proc-4.5 {variable numbers of arguments} {
  249.     proc tproc {x y args} {return $args}
  250.     tproc 1 2
  251. } {}
  252. test proc-4.6 {variable numbers of arguments} {
  253.     proc tproc {x missing args} {return $args}
  254.     list [catch {tproc 1} msg] $msg
  255. } {1 {no value given for parameter "missing" to "tproc"}}
  256.  
  257. test proc-5.1 {error conditions} {
  258.     list [catch {proc} msg] $msg
  259. } {1 {wrong # args: should be "proc name args body"}}
  260. test proc-5.2 {error conditions} {
  261.     list [catch {proc tproc b} msg] $msg
  262. } {1 {wrong # args: should be "proc name args body"}}
  263. test proc-5.3 {error conditions} {
  264.     list [catch {proc tproc b c d e} msg] $msg
  265. } {1 {wrong # args: should be "proc name args body"}}
  266. test proc-5.4 {error conditions} {
  267.     list [catch {proc tproc \{xyz {return foo}} msg] $msg
  268. } {1 {unmatched open brace in list}}
  269. test proc-5.5 {error conditions} {
  270.     list [catch {proc tproc {{} y} {return foo}} msg] $msg
  271. } {1 {procedure "tproc" has argument with no name}}
  272. test proc-5.6 {error conditions} {
  273.     list [catch {proc tproc {{} y} {return foo}} msg] $msg
  274. } {1 {procedure "tproc" has argument with no name}}
  275. test proc-5.7 {error conditions} {
  276.     list [catch {proc tproc {{x 1 2} y} {return foo}} msg] $msg
  277. } {1 {too many fields in argument specifier "x 1 2"}}
  278. test proc-5.8 {error conditions} {
  279.     catch {return}
  280. } 2
  281. test proc-5.9 {error conditions} {
  282.     list [catch {global} msg] $msg
  283. } {1 {wrong # args: should be "global varName ?varName ...?"}}
  284. proc tproc {} {
  285.     set a 22
  286.     global a
  287. }
  288. test proc-5.10 {error conditions} {
  289.     list [catch {tproc} msg] $msg
  290. } {1 {variable "a" already exists}}
  291. test proc-5.11 {error conditions} {
  292.     catch {rename tproc {}}
  293.     catch {
  294.     proc tproc {x {} z} {return foo}
  295.     }
  296.     list [catch {tproc 1} msg] $msg
  297. } {1 {invalid command name: "tproc"}}
  298. test proc-5.12 {error conditions} {
  299.     proc tproc {} {
  300.     set a 22
  301.     error "error in procedure"
  302.     return
  303.     }
  304.     list [catch tproc msg] $msg
  305. } {1 {error in procedure}}
  306. test proc-5.13 {error conditions} {
  307.     proc tproc {} {
  308.     set a 22
  309.     error "error in procedure"
  310.     return
  311.     }
  312.     catch tproc msg
  313.     set errorInfo
  314. } {error in procedure
  315.     while executing
  316. "error "error in procedure""
  317.     (procedure "tproc" line 3)
  318.     invoked from within
  319. "tproc"}
  320. test proc-5.14 {error conditions} {
  321.     proc tproc {} {
  322.     set a 22
  323.     break
  324.     return
  325.     }
  326.     catch tproc msg
  327.     set errorInfo
  328. } {invoked "break" outside of a loop
  329.     while executing
  330. "tproc"}
  331. test proc-5.15 {error conditions} {
  332.     proc tproc {} {
  333.     set a 22
  334.     continue
  335.     return
  336.     }
  337.     catch tproc msg
  338.     set errorInfo
  339. } {invoked "continue" outside of a loop
  340.     while executing
  341. "tproc"}
  342.  
  343. # The tests below will really only be useful when run under Purify or
  344. # some other system that can detect accesses to freed memory...
  345.  
  346. test proc-6.1 {procedure that redefines itself} {
  347.     proc tproc {} {
  348.     proc tproc {} {
  349.         return 44
  350.     }
  351.     return 45
  352.     }
  353.     tproc
  354. } 45
  355. test proc-6.2 {procedure that deletes itself} {
  356.     proc tproc {} {
  357.     rename tproc {}
  358.     return 45
  359.     }
  360.     tproc
  361. } 45
  362.  
  363. proc tproc code {
  364.     return -code $code abc
  365. }
  366. test proc-7.1 {return with special completion code} {
  367.     list [catch {tproc ok} msg] $msg
  368. } {0 abc}
  369. test proc-7.2 {return with special completion code} {
  370.     list [catch {tproc error} msg] $msg $errorInfo $errorCode
  371. } {1 abc {abc
  372.     while executing
  373. "tproc error"} NONE}
  374. test proc-7.3 {return with special completion code} {
  375.     list [catch {tproc return} msg] $msg
  376. } {2 abc}
  377. test proc-7.4 {return with special completion code} {
  378.     list [catch {tproc break} msg] $msg
  379. } {3 abc}
  380. test proc-7.5 {return with special completion code} {
  381.     list [catch {tproc continue} msg] $msg
  382. } {4 abc}
  383. test proc-7.6 {return with special completion code} {
  384.     list [catch {tproc -14} msg] $msg
  385. } {-14 abc}
  386. test proc-7.7 {return with special completion code} {
  387.     list [catch {tproc gorp} msg] $msg
  388. } {1 {bad completion code "gorp": must be ok, error, return, break, continue, or an integer}}
  389. test proc-7.8 {return with special completion code} {
  390.     list [catch {tproc 10b} msg] $msg
  391. } {1 {bad completion code "10b": must be ok, error, return, break, continue, or an integer}}
  392. test proc-7.9 {return with special completion code} {
  393.     proc tproc2 {} {
  394.     tproc return
  395.     }
  396.     list [catch tproc2 msg] $msg
  397. } {0 abc}
  398. test proc-7.10 {return with special completion code} {
  399.     proc tproc2 {} {
  400.     return -code error
  401.     }
  402.     list [catch tproc2 msg] $msg
  403. } {1 {}}
  404. test proc-7.11 {return with special completion code} {
  405.     proc tproc2 {} {
  406.     global errorCode errorInfo
  407.     catch {open _bad_file_name r} msg
  408.     return -code error -errorinfo $errorInfo -errorcode $errorCode $msg
  409.     }
  410.     string tolower [list [catch tproc2 msg] $msg $errorInfo $errorCode]
  411. } {1 {couldn't open "_bad_file_name": no such file or directory} {couldn't open "_bad_file_name": no such file or directory
  412.     while executing
  413. "open _bad_file_name r"
  414.     invoked from within
  415. "tproc2"} {posix enoent {no such file or directory}}}
  416. test proc-7.12 {return with special completion code} {
  417.     proc tproc2 {} {
  418.     global errorCode errorInfo
  419.     catch {open _bad_file_name r} msg
  420.     return -code error -errorcode $errorCode $msg
  421.     }
  422.     string tolower [list [catch tproc2 msg] $msg $errorInfo $errorCode]
  423. } {1 {couldn't open "_bad_file_name": no such file or directory} {couldn't open "_bad_file_name": no such file or directory
  424.     while executing
  425. "tproc2"} {posix enoent {no such file or directory}}}
  426. test proc-7.13 {return with special completion code} {
  427.     proc tproc2 {} {
  428.     global errorCode errorInfo
  429.     catch {open _bad_file_name r} msg
  430.     return -code error -errorinfo $errorInfo $msg
  431.     }
  432.     string tolower [list [catch tproc2 msg] $msg $errorInfo $errorCode]
  433. } {1 {couldn't open "_bad_file_name": no such file or directory} {couldn't open "_bad_file_name": no such file or directory
  434.     while executing
  435. "open _bad_file_name r"
  436.     invoked from within
  437. "tproc2"} none}
  438. test proc-7.14 {return with special completion code} {
  439.     proc tproc2 {} {
  440.     global errorCode errorInfo
  441.     catch {open _bad_file_name r} msg
  442.     return -code error $msg
  443.     }
  444.     string tolower [list [catch tproc2 msg] $msg $errorInfo $errorCode]
  445. } {1 {couldn't open "_bad_file_name": no such file or directory} {couldn't open "_bad_file_name": no such file or directory
  446.     while executing
  447. "tproc2"} none}
  448. test proc-7.14 {return with special completion code} {
  449.     list [catch {return -badOption foo message} msg] $msg
  450. } {1 {bad option "-badOption: must be -code, -errorcode, or -errorinfo}}
  451.