home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tcl8.0 / tests / if.test < prev    next >
Encoding:
Text File  |  1997-08-15  |  11.9 KB  |  506 lines  |  [TEXT/ALFA]

  1. # Commands covered:  if
  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) 1996 Sun Microsystems, Inc.
  8. #
  9. # See the file "license.terms" for information on usage and redistribution
  10. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11. #
  12. # SCCS: @(#) if.test 1.9 97/07/02 16:40:58
  13.  
  14. if {[string compare test [info procs test]] == 1} then {source defs}
  15.  
  16. # Basic "if" operation.
  17.  
  18. catch {unset a}
  19. test if-1.1 {TclCompileIfCmd: missing if/elseif test} {
  20.     list [catch {if} msg] $msg
  21. } {1 {wrong # args: no expression after "if" argument}}
  22. test if-1.2 {TclCompileIfCmd: error in if/elseif test} {
  23.     list [catch {if {[error "error in condition"]} foo} msg] $msg
  24. } {1 {error in condition}}
  25. test if-1.3 {TclCompileIfCmd: error in if/elseif test} {
  26.     list [catch {if {1+}} msg] $msg $errorInfo
  27. } {1 {syntax error in expression "1+"} {syntax error in expression "1+"
  28.     ("if" test expression)
  29.     while compiling
  30. "if {1+}"}}
  31. test if-1.4 {TclCompileIfCmd: if/elseif test in braces} {
  32.     set a {}
  33.     if {1<2} {set a 1}
  34.     set a
  35. } {1}
  36. test if-1.5 {TclCompileIfCmd: if/elseif test not in braces} {
  37.     set a {}
  38.     if 1<2 {set a 1}
  39.     set a
  40. } {1}
  41. test if-1.6 {TclCompileIfCmd: multiline test expr} {
  42.     set a {}
  43.     if {($tcl_platform(platform) != "foobar1") && \
  44.     ($tcl_platform(platform) != "foobar2")} {set a 3} else {set a 4}
  45.     set a
  46. } 3
  47. test if-1.7 {TclCompileIfCmd: "then" after if/elseif test} {
  48.     set a {}
  49.     if 4>3 then {set a 1}
  50.     set a
  51. } {1}
  52. test if-1.8 {TclCompileIfCmd: keyword other than "then" after if/elseif test} {
  53.     set a {}
  54.     catch {if 1<2 therefore {set a 1}} msg 
  55.     set msg
  56. } {invalid command name "therefore"}
  57. test if-1.9 {TclCompileIfCmd: missing "then" body} {
  58.     set a {}
  59.     catch {if 1<2 then} msg 
  60.     set msg
  61. } {wrong # args: no script following "then" argument}
  62. test if-1.10 {TclCompileIfCmd: error in "then" body} {
  63.     set a {}
  64.     list [catch {if {$a!="xxx"} then {set}} msg] $msg $errorInfo
  65. } {1 {wrong # args: should be "set varName ?newValue?"} {wrong # args: should be "set varName ?newValue?"
  66.     while compiling
  67. "set"
  68.     ("if" then script line 1)
  69.     while compiling
  70. "if {$a!="xxx"} then {set}"}}
  71. test if-1.11 {TclCompileIfCmd: error in "then" body} {
  72.     list [catch {if 2 then {[error "error in then clause"]}} msg] $msg
  73. } {1 {error in then clause}}
  74. test if-1.12 {TclCompileIfCmd: "then" body in quotes} {
  75.     set a {}
  76.     if 27>17 "append a x"
  77.     set a
  78. } {x}
  79. test if-1.13 {TclCompileIfCmd: computed "then" body} {
  80.     catch {unset x1}
  81.     catch {unset x2}
  82.     set a {}
  83.     set x1 {append a x1}
  84.     set x2 {; append a x2}
  85.     set a {}
  86.     if 1 $x1$x2
  87.     set a
  88. } {x1x2}
  89. test if-1.14 {TclCompileIfCmd: taking proper branch} {
  90.     set a {}
  91.     if 1<2 {set a 1}
  92.     set a
  93. } 1
  94. test if-1.15 {TclCompileIfCmd: taking proper branch} {
  95.     set a {}
  96.     if 1>2 {set a 1}
  97.     set a
  98. } {}
  99. test if-1.16 {TclCompileIfCmd: test jumpFalse instruction replacement after long "then" body} {
  100.     catch {unset i}
  101.     set a {}
  102.     if 1<2 {
  103.     set a 1
  104.     while {$a != "xxx"} {
  105.         break;
  106.         while {$i >= 0} {
  107.         if {[string compare $a "bar"] < 0} {
  108.             set i $i
  109.             set i [lindex $s $i]
  110.         }
  111.         if {[string compare $a "bar"] < 0} {
  112.             set i $i
  113.             set i [lindex $s $i]
  114.         }
  115.         if {[string compare $a "bar"] < 0} {
  116.             set i $i
  117.             set i [lindex $s $i]
  118.         }
  119.         if {[string compare $a "bar"] < 0} {
  120.             set i $i
  121.             set i [lindex $s $i]
  122.         }
  123.         set i [expr $i-1]
  124.         }
  125.     }
  126.     set a 2
  127.     while {$a != "xxx"} {
  128.         break;
  129.         while {$i >= 0} {
  130.         if {[string compare $a "bar"] < 0} {
  131.             set i $i
  132.             set i [lindex $s $i]
  133.         }
  134.         if {[string compare $a "bar"] < 0} {
  135.             set i $i
  136.             set i [lindex $s $i]
  137.         }
  138.         if {[string compare $a "bar"] < 0} {
  139.             set i $i
  140.             set i [lindex $s $i]
  141.         }
  142.         if {[string compare $a "bar"] < 0} {
  143.             set i $i
  144.             set i [lindex $s $i]
  145.         }
  146.         set i [expr $i-1]
  147.         }
  148.     }
  149.     set a 3
  150.     }
  151.     set a
  152. } 3
  153. test if-1.17 {TclCompileIfCmd: if/elseif test in quotes} {
  154.     set a {}
  155.     list [catch {if {"0 < 3"} {set a 1}} msg] $msg
  156. } {1 {expected boolean value but got "0 < 3"}}
  157.  
  158.  
  159. test if-2.1 {TclCompileIfCmd: "elseif" after if/elseif test} {
  160.     set a {}
  161.     if 3>4 {set a 1} elseif 1 {set a 2}
  162.     set a
  163. } {2}
  164. # Since "else" is optional, the "elwood" below is treated as a command.
  165. # But then there shouldn't be any additional argument words for the "if".
  166. test if-2.2 {TclCompileIfCmd: keyword other than "elseif"} {
  167.     set a {}
  168.     catch {if 1<2 {set a 1} elwood {set a 2}} msg 
  169.     set msg
  170. } {wrong # args: extra words after "else" clause in "if" command}
  171. test if-2.3 {TclCompileIfCmd: missing expression after "elseif"} {
  172.     set a {}
  173.     catch {if 1<2 {set a 1} elseif} msg 
  174.     set msg
  175. } {wrong # args: no expression after "elseif" argument}
  176. test if-2.4 {TclCompileIfCmd: error in expression after "elseif"} {
  177.     set a {}
  178.     list [catch {if 3>4 {set a 1} elseif {1>}} msg] $msg $errorInfo
  179. } {1 {syntax error in expression "1>"} {syntax error in expression "1>"
  180.     ("if" test expression)
  181.     while compiling
  182. "if 3>4 {set a 1} elseif {1>}"}}
  183. test if-2.5 {TclCompileIfCmd: test jumpFalse instruction replacement after long "elseif" body} {
  184.     catch {unset i}
  185.     set a {}
  186.     if 1>2 {
  187.     set a 1
  188.     while {$a != "xxx"} {
  189.         break;
  190.         while {$i >= 0} {
  191.         if {[string compare $a "bar"] < 0} {
  192.             set i $i
  193.             set i [lindex $s $i]
  194.         }
  195.         if {[string compare $a "bar"] < 0} {
  196.             set i $i
  197.             set i [lindex $s $i]
  198.         }
  199.         if {[string compare $a "bar"] < 0} {
  200.             set i $i
  201.             set i [lindex $s $i]
  202.         }
  203.         if {[string compare $a "bar"] < 0} {
  204.             set i $i
  205.             set i [lindex $s $i]
  206.         }
  207.         set i [expr $i-1]
  208.         }
  209.     }
  210.     set a 2
  211.     while {$a != "xxx"} {
  212.         break;
  213.         while {$i >= 0} {
  214.         if {[string compare $a "bar"] < 0} {
  215.             set i $i
  216.             set i [lindex $s $i]
  217.         }
  218.         if {[string compare $a "bar"] < 0} {
  219.             set i $i
  220.             set i [lindex $s $i]
  221.         }
  222.         if {[string compare $a "bar"] < 0} {
  223.             set i $i
  224.             set i [lindex $s $i]
  225.         }
  226.         if {[string compare $a "bar"] < 0} {
  227.             set i $i
  228.             set i [lindex $s $i]
  229.         }
  230.         set i [expr $i-1]
  231.         }
  232.     }
  233.     set a 3
  234.     } elseif 1<2 then { #; this if arm should be taken
  235.     set a 4
  236.     while {$a != "xxx"} {
  237.         break;
  238.         while {$i >= 0} {
  239.         if {[string compare $a "bar"] < 0} {
  240.             set i $i
  241.             set i [lindex $s $i]
  242.         }
  243.         if {[string compare $a "bar"] < 0} {
  244.             set i $i
  245.             set i [lindex $s $i]
  246.         }
  247.         if {[string compare $a "bar"] < 0} {
  248.             set i $i
  249.             set i [lindex $s $i]
  250.         }
  251.         if {[string compare $a "bar"] < 0} {
  252.             set i $i
  253.             set i [lindex $s $i]
  254.         }
  255.         set i [expr $i-1]
  256.         }
  257.     }
  258.     set a 5
  259.     while {$a != "xxx"} {
  260.         break;
  261.         while {$i >= 0} {
  262.         if {[string compare $a "bar"] < 0} {
  263.             set i $i
  264.             set i [lindex $s $i]
  265.         }
  266.         if {[string compare $a "bar"] < 0} {
  267.             set i $i
  268.             set i [lindex $s $i]
  269.         }
  270.         if {[string compare $a "bar"] < 0} {
  271.             set i $i
  272.             set i [lindex $s $i]
  273.         }
  274.         if {[string compare $a "bar"] < 0} {
  275.             set i $i
  276.             set i [lindex $s $i]
  277.         }
  278.         set i [expr $i-1]
  279.         }
  280.     }
  281.     set a 6
  282.     }
  283.     set a
  284. } 6
  285.  
  286. test if-3.1 {TclCompileIfCmd: "else" clause} {
  287.     set a {}
  288.     if 3>4 {set a 1} elseif {$a == "foo"} {set a 2} else {set a 3}
  289.     set a
  290. } 3
  291. # Since "else" is optional, the "elsex" below is treated as a command.
  292. # But then there shouldn't be any additional argument words for the "if".
  293. test if-3.2 {TclCompileIfCmd: keyword other than "else"} {
  294.     set a {}
  295.     catch {if 1<2 then {set a 1} elsex {set a 2}} msg 
  296.     set msg
  297. } {wrong # args: extra words after "else" clause in "if" command}
  298. test if-3.3 {TclCompileIfCmd: missing body after "else"} {
  299.     set a {}
  300.     catch {if 2<1 {set a 1} else} msg 
  301.     set msg
  302. } {wrong # args: no script following "else" argument}
  303. test if-3.4 {TclCompileIfCmd: error compiling body after "else"} {
  304.     set a {}
  305.     catch {if 2<1 {set a 1} else {set}} msg 
  306.     set errorInfo
  307. } {wrong # args: should be "set varName ?newValue?"
  308.     while compiling
  309. "set"
  310.     ("if" else script line 1)
  311.     while compiling
  312. "if 2<1 {set a 1} else {set}"}
  313. test if-3.5 {TclCompileIfCmd: extra arguments after "else" argument} {
  314.     set a {}
  315.     catch {if 2<1 {set a 1} else {set a 2} or something} msg 
  316.     set msg
  317. } {wrong # args: extra words after "else" clause in "if" command}
  318. # The following test also checks whether contained loops and other
  319. # commands are properly relocated because a short jump must be replaced
  320. # by a "long distance" one.
  321. test if-3.6 {TclCompileIfCmd: test jumpFalse instruction replacement after long "else" clause} {
  322.     catch {unset i}
  323.     set a {}
  324.     if 1>2 {
  325.     set a 1
  326.     while {$a != "xxx"} {
  327.         break;
  328.         while {$i >= 0} {
  329.         if {[string compare $a "bar"] < 0} {
  330.             set i $i
  331.             set i [lindex $s $i]
  332.         }
  333.         if {[string compare $a "bar"] < 0} {
  334.             set i $i
  335.             set i [lindex $s $i]
  336.         }
  337.         if {[string compare $a "bar"] < 0} {
  338.             set i $i
  339.             set i [lindex $s $i]
  340.         }
  341.         if {[string compare $a "bar"] < 0} {
  342.             set i $i
  343.             set i [lindex $s $i]
  344.         }
  345.         set i [expr $i-1]
  346.         }
  347.     }
  348.     set a 2
  349.     while {$a != "xxx"} {
  350.         break;
  351.         while {$i >= 0} {
  352.         if {[string compare $a "bar"] < 0} {
  353.             set i $i
  354.             set i [lindex $s $i]
  355.         }
  356.         if {[string compare $a "bar"] < 0} {
  357.             set i $i
  358.             set i [lindex $s $i]
  359.         }
  360.         if {[string compare $a "bar"] < 0} {
  361.             set i $i
  362.             set i [lindex $s $i]
  363.         }
  364.         if {[string compare $a "bar"] < 0} {
  365.             set i $i
  366.             set i [lindex $s $i]
  367.         }
  368.         set i [expr $i-1]
  369.         }
  370.     }
  371.     set a 3
  372.     } elseif 1==2 then { #; this if arm should be taken
  373.     set a 4
  374.     while {$a != "xxx"} {
  375.         break;
  376.         while {$i >= 0} {
  377.         if {[string compare $a "bar"] < 0} {
  378.             set i $i
  379.             set i [lindex $s $i]
  380.         }
  381.         if {[string compare $a "bar"] < 0} {
  382.             set i $i
  383.             set i [lindex $s $i]
  384.         }
  385.         if {[string compare $a "bar"] < 0} {
  386.             set i $i
  387.             set i [lindex $s $i]
  388.         }
  389.         if {[string compare $a "bar"] < 0} {
  390.             set i $i
  391.             set i [lindex $s $i]
  392.         }
  393.         set i [expr $i-1]
  394.         }
  395.     }
  396.     set a 5
  397.     while {$a != "xxx"} {
  398.         break;
  399.         while {$i >= 0} {
  400.         if {[string compare $a "bar"] < 0} {
  401.             set i $i
  402.             set i [lindex $s $i]
  403.         }
  404.         if {[string compare $a "bar"] < 0} {
  405.             set i $i
  406.             set i [lindex $s $i]
  407.         }
  408.         if {[string compare $a "bar"] < 0} {
  409.             set i $i
  410.             set i [lindex $s $i]
  411.         }
  412.         if {[string compare $a "bar"] < 0} {
  413.             set i $i
  414.             set i [lindex $s $i]
  415.         }
  416.         set i [expr $i-1]
  417.         }
  418.     }
  419.     set a 6
  420.     } else {
  421.     set a 7
  422.     while {$a != "xxx"} {
  423.         break;
  424.         while {$i >= 0} {
  425.         if {[string compare $a "bar"] < 0} {
  426.             set i $i
  427.             set i [lindex $s $i]
  428.         }
  429.         if {[string compare $a "bar"] < 0} {
  430.             set i $i
  431.             set i [lindex $s $i]
  432.         }
  433.         if {[string compare $a "bar"] < 0} {
  434.             set i $i
  435.             set i [lindex $s $i]
  436.         }
  437.         if {[string compare $a "bar"] < 0} {
  438.             set i $i
  439.             set i [lindex $s $i]
  440.         }
  441.         set i [expr $i-1]
  442.         }
  443.     }
  444.     set a 8
  445.     while {$a != "xxx"} {
  446.         break;
  447.         while {$i >= 0} {
  448.         if {[string compare $a "bar"] < 0} {
  449.             set i $i
  450.             set i [lindex $s $i]
  451.         }
  452.         if {[string compare $a "bar"] < 0} {
  453.             set i $i
  454.             set i [lindex $s $i]
  455.         }
  456.         if {[string compare $a "bar"] < 0} {
  457.             set i $i
  458.             set i [lindex $s $i]
  459.         }
  460.         if {[string compare $a "bar"] < 0} {
  461.             set i $i
  462.             set i [lindex $s $i]
  463.         }
  464.         set i [expr $i-1]
  465.         }
  466.     }
  467.     set a 9
  468.     }
  469.     set a
  470. } 9
  471.  
  472. test if-4.1 {TclCompileIfCmd: "if" command result} {
  473.     set a {}
  474.     set a [if 3<4 {set i 27}]
  475.     set a
  476. } 27
  477. test if-4.2 {TclCompileIfCmd: "if" command result} {
  478.     set a {}
  479.     set a [if 3>4 {set i 27}]
  480.     set a
  481. } {}
  482. test if-4.3 {TclCompileIfCmd: "if" command result} {
  483.     set a {}
  484.     set a [if 0 {set i 1} elseif 1 {set i 2}]
  485.     set a
  486. } 2
  487. test if-4.4 {TclCompileIfCmd: "if" command result} {
  488.     set a {}
  489.     set a [if 0 {set i 1} elseif 0 {set i 2} elseif 2>5 {set i 3} else {set i 4}]
  490.     set a
  491. } 4
  492. test if-4.5 {TclCompileIfCmd: return value} {
  493.     if 0 then {set a 22; concat abc} elseif 1 {concat def} {concat ghi}
  494. } def
  495.  
  496. # Check "if" and computed command names.
  497.  
  498. test if-5.1 {if and computed command names} {
  499.     set i 0
  500.     set z if
  501.     $z 1 {
  502.         set i 1
  503.     }
  504.     set i
  505. } 1
  506.