home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / tcl / tclsrc / tests / test / parse_test < prev    next >
Encoding:
Text File  |  1996-01-16  |  11.4 KB  |  425 lines

  1. # Commands covered:  set (plus basic command syntax)
  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. # Copyright (c) 1994 Sun Microsystems, Inc.
  9. #
  10. # See the file "license.terms" for information on usage and redistribution
  11. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12. #
  13. # @(#) parse.test 1.29 95/06/29 13:39:15
  14.  
  15. if {[string compare test [info procs test]] == 1} then {source defs}
  16.  
  17. #unset argv for RISCOS
  18. puts stdout "unset argv as its defined for RISCOS"
  19. unset argv
  20.  
  21. proc fourArgs {a b c d} {
  22.     global arg1 arg2 arg3 arg4
  23.     set arg1 $a
  24.     set arg2 $b
  25.     set arg3 $c
  26.     set arg4 $d
  27. }
  28.  
  29. proc getArgs args {
  30.     global argv
  31.     set argv $args
  32. }
  33.  
  34. # Basic argument parsing.
  35.  
  36. test parse-1.1 {basic argument parsing} {
  37.     set arg1 {}
  38.     fourArgs a b    c          d
  39.     list $arg1 $arg2 $arg3 $arg4
  40. } {a b c d}
  41. test parse-1.2 {basic argument parsing} {
  42.     set arg1 {}
  43.     eval "fourArgs 123\v4\f56\r7890"
  44.     list $arg1 $arg2 $arg3 $arg4
  45. } {123 4 56 7890}
  46.  
  47. # Quotes.
  48.  
  49. test parse-2.1 {quotes and variable-substitution} {
  50.     getArgs "a b c" d
  51.     set argv
  52. } {{a b c} d}
  53. test parse-2.2 {quotes and variable-substitution} {
  54.     set a 101
  55.     getArgs "a$a b c"
  56.     set argv
  57. } {{a101 b c}}
  58. test parse-2.3 {quotes and variable-substitution} {
  59.     set argv "xy[format xabc]"
  60.     set argv
  61. } {xyxabc}
  62. test parse-2.4 {quotes and variable-substitution} {
  63.     set argv "xy\t"
  64.     set argv
  65. } xy\t
  66. test parse-2.5 {quotes and variable-substitution} {
  67.     set argv "a b    c
  68. d e f"
  69.     set argv
  70. } a\ b\tc\nd\ e\ f
  71. test parse-2.6 {quotes and variable-substitution} {
  72.     set argv a"bcd"e
  73.     set argv
  74. } {a"bcd"e}
  75.  
  76. # Braces.
  77.  
  78. test parse-3.1 {braces} {
  79.     getArgs {a b c} d
  80.     set argv
  81. } "{a b c} d"
  82. test parse-3.2 {braces} {
  83.     set a 101
  84.     set argv {a$a b c}
  85.     set b [string index $argv 1]
  86.     set b
  87. } {$}
  88. test parse-3.3 {braces} {
  89.     set argv {a[format xyz] b}
  90.     string length $argv
  91. } 15
  92. test parse-3.4 {braces} {
  93.     set argv {a\nb\}}
  94.     string length $argv
  95. } 6
  96. test parse-3.5 {braces} {
  97.     set argv {{{{}}}}
  98.     set argv
  99. } "{{{}}}"
  100. test parse-3.6 {braces} {
  101.     set argv a{{}}b
  102.     set argv
  103. } "a{{}}b"
  104. test parse-3.7 {braces} {
  105.     set a [format "last]"]
  106.     set a
  107. } {last]}
  108.  
  109. # Command substitution.
  110.  
  111. test parse-4.1 {command substitution} {
  112.     set a [format xyz]
  113.     set a
  114. } xyz
  115. test parse-4.2 {command substitution} {
  116.     set a a[format xyz]b[format q]
  117.     set a
  118. } axyzbq
  119. test parse-4.3 {command substitution} {
  120.     set a a[
  121. set b 22;
  122. format %s $b
  123.  
  124. ]b
  125.     set a
  126. } a22b
  127.  
  128. # Variable substitution.
  129.  
  130. test parse-5.1 {variable substitution} {
  131.     set a 123
  132.     set b $a
  133.     set b
  134. } 123
  135. test parse-5.2 {variable substitution} {
  136.     set a 345
  137.     set b x$a.b
  138.     set b
  139. } x345.b
  140. test parse-5.3 {variable substitution} {
  141.     set _123z xx
  142.     set b $_123z^
  143.     set b
  144. } xx^
  145. test parse-5.4 {variable substitution} {
  146.     set a 78
  147.     set b a${a}b
  148.     set b
  149. } a78b
  150. test parse-5.5 {variable substitution} {catch {$_non_existent_} msg} 1
  151. test parse-5.6 {variable substitution} {
  152.     catch {$_non_existent_} msg
  153.     set msg
  154. } {can't read "_non_existent_": no such variable}
  155. test parse-5.7 {array variable substitution} {
  156.     catch {unset a}
  157.     set a(xyz) 123
  158.     set b $a(xyz)foo
  159.     set b
  160. } 123foo
  161. test parse-5.8 {array variable substitution} {
  162.     catch {unset a}
  163.     set "a(x y z)" 123
  164.     set b $a(x y z)foo
  165.     set b
  166. } 123foo
  167. test parse-5.9 {array variable substitution} {
  168.     catch {unset a}; catch {unset qqq}
  169.     set "a(x y z)" qqq
  170.     set $a([format x]\ y [format z]) foo
  171.     set qqq
  172. } foo
  173. test parse-5.10 {array variable substitution} {
  174.     catch {unset a}
  175.     list [catch {set b $a(22)} msg] $msg
  176. } {1 {can't read "a(22)": no such variable}}
  177. test parse-5.11 {array variable substitution} {
  178.     set b a$!
  179.     set b
  180. } {a$!}
  181. test parse-5.12 {array variable substitution} {
  182.     set b a$()
  183.     set b
  184. } {a$()}
  185. catch {unset a}
  186. test parse-5.13 {array variable substitution} {
  187.     catch {unset a}
  188.     set long {This is a very long variable, long enough to cause storage \
  189.     allocation to occur in Tcl_ParseVar.  If that storage isn't getting \
  190.     freed up correctly, then a core leak will occur when this test is \
  191.     run.  This text is probably beginning to sound like drivel, but I've \
  192.     run out of things to say and I need more characters still.}
  193.     set a($long) 777
  194.     set b $a($long)
  195.     list $b [array names a]
  196. } {777 {{This is a very long variable, long enough to cause storage \
  197.     allocation to occur in Tcl_ParseVar.  If that storage isn't getting \
  198.     freed up correctly, then a core leak will occur when this test is \
  199.     run.  This text is probably beginning to sound like drivel, but I've \
  200.     run out of things to say and I need more characters still.}}}
  201. test parse-5.14 {array variable substitution} {
  202.     catch {unset a}; catch {unset b}; catch {unset a1}
  203.     set a1(22) foo
  204.     set a(foo) bar
  205.     set b $a($a1(22))
  206.     set b
  207. } bar
  208. catch {unset a}; catch {unset a1}
  209.  
  210. # Backslash substitution.
  211.  
  212. set errNum 1
  213. proc bsCheck {char num} {
  214.     global errNum
  215.     test parse-6.$errNum {backslash substitution} {
  216.     scan $char %c value
  217.     set value
  218.     } $num
  219.     set errNum [expr $errNum+1]
  220. }
  221.  
  222. bsCheck \b    8
  223. bsCheck \e    101
  224. bsCheck \f    12
  225. bsCheck \n    10
  226. bsCheck \r    13
  227. bsCheck \t    9
  228. bsCheck \v    11
  229. bsCheck \{    123
  230. bsCheck \}    125
  231. bsCheck \[    91
  232. bsCheck \]    93
  233. bsCheck \$    36
  234. bsCheck \     32
  235. bsCheck \;    59
  236. bsCheck \\    92
  237. bsCheck \Ca    67
  238. bsCheck \Ma    77
  239. bsCheck \CMa    67
  240. bsCheck \8a    8
  241. bsCheck \14    12
  242. bsCheck \141    97
  243. bsCheck \340    224
  244. bsCheck b\0    98
  245. bsCheck \x    120
  246. bsCheck \xa    10
  247. bsCheck \x41    65
  248. bsCheck \x541    65
  249.  
  250. test parse-6.1 {backslash substitution} {
  251.     set a "\a\c\n\]\}"
  252.     string length $a
  253. } 5
  254. test parse-6.2 {backslash substitution} {
  255.     set a {\a\c\n\]\}}
  256.     string length $a
  257. } 10
  258. test parse-6.3 {backslash substitution} {
  259.     set a "abc\
  260. def"
  261.     set a
  262. } {abc def}
  263. test parse-6.4 {backslash substitution} {
  264.     set a {abc\
  265. def}
  266.     set a
  267. } {abc def}
  268. test parse-6.5 {backslash substitution} {
  269.     set msg {}
  270.     set a xxx
  271.     set error [catch {if {24 < \
  272.     35} {set a 22} {set \
  273.         a 33}} msg]
  274.     list $error $msg $a
  275. } {0 22 22}
  276. test parse-6.6 {backslash substitution} {
  277.     eval "concat abc\\"
  278. } "abc\\"
  279. test parse-6.7 {backslash substitution} {
  280.     eval "concat \\\na"
  281. } "a"
  282. test parse-6.8 {backslash substitution} {
  283.     eval "concat x\\\n       \na"
  284. } "x a"
  285. test parse-6.9 {backslash substitution} {
  286.     eval "concat \\x"
  287. } "x"
  288. test parse-6.10 {backslash substitution} {
  289.     eval "list a b\\\nc d"
  290. } {a b c d}
  291. test parse-6.11 {backslash substitution} {
  292.     eval "list a \"b c\"\\\nd e"
  293. } {a {b c} d e}
  294.  
  295. # Semi-colon.
  296.  
  297. test parse-7.1 {semi-colons} {
  298.     set b 0
  299.     getArgs a;set b 2
  300.     set argv
  301. } a
  302. test parse-7.2 {semi-colons} {
  303.     set b 0
  304.     getArgs a;set b 2
  305.     set b
  306. } 2
  307. test parse-7.3 {semi-colons} {
  308.     getArgs a b ; set b 1
  309.     set argv
  310. } {a b}
  311. test parse-7.4 {semi-colons} {
  312.     getArgs a b ; set b 1
  313.     set b
  314. } 1
  315.  
  316. # The following checks are to ensure that the interpreter's result
  317. # gets re-initialized by Tcl_Eval in all the right places.
  318.  
  319. test parse-8.1 {result initialization} {concat abc} abc
  320. test parse-8.2 {result initialization} {concat abc; proc foo {} {}} {}
  321. test parse-8.3 {result initialization} {concat abc; proc foo {} $a} {}
  322. test parse-8.4 {result initialization} {proc foo {} [concat abc]} {}
  323. test parse-8.5 {result initialization} {concat abc; } abc
  324. test parse-8.6 {result initialization} {
  325.     eval {
  326.     concat abc
  327. }} abc
  328. test parse-8.7 {result initialization} {} {}
  329. test parse-8.8 {result initialization} {concat abc; ; ;} abc
  330.  
  331. # Syntax errors.
  332.  
  333. test parse-9.1 {syntax errors} {catch "set a \{bcd" msg} 1
  334. test parse-9.2 {syntax errors} {
  335.     catch "set a \{bcd" msg
  336.     set msg
  337. } {missing close-brace}
  338. test parse-9.3 {syntax errors} {catch {set a "bcd} msg} 1
  339. test parse-9.4 {syntax errors} {
  340.     catch {set a "bcd} msg
  341.     set msg
  342. } {missing "}
  343. test parse-9.5 {syntax errors} {catch {set a "bcd"xy} msg} 1
  344. test parse-9.6 {syntax errors} {
  345.     catch {set a "bcd"xy} msg
  346.     set msg
  347. } {extra characters after close-quote}
  348. test parse-9.7 {syntax errors} {catch "set a {bcd}xy" msg} 1
  349. test parse-9.8 {syntax errors} {
  350.     catch "set a {bcd}xy" msg
  351.     set msg
  352. } {extra characters after close-brace}
  353. test parse-9.9 {syntax errors} {catch {set a [format abc} msg} 1
  354. test parse-9.10 {syntax errors} {
  355.     catch {set a [format abc} msg
  356.     set msg
  357. } {missing close-bracket}
  358. test parse-9.11 {syntax errors} {catch gorp-a-lot msg} 1
  359. test parse-9.12 {syntax errors} {
  360.     catch gorp-a-lot msg
  361.     set msg
  362. } {invalid command name "gorp-a-lot"}
  363. test parse-9.13 {syntax errors} {
  364.     set a [concat {a}\
  365.  {b}]
  366.     set a
  367. } {a b}
  368.  
  369. # Long values (stressing storage management)
  370.  
  371. set a {1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH}
  372.  
  373. test parse-10.1 {long values} {
  374.     string length $a
  375. } 214
  376. test parse-10.2 {long values} {
  377.     llength $a
  378. } 43
  379. test parse-1a1.3 {long values} {
  380.     set b "1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH"
  381.     set b
  382. } $a
  383. test parse-10.3 {long values} {
  384.     set b "$a"
  385.     set b
  386. } $a
  387. test parse-10.4 {long values} {
  388.     set b [set a]
  389.     set b
  390. } $a
  391. test parse-10.5 {long values} {
  392.     set b [concat 1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH]
  393.     string length $b
  394. } 214
  395. test parse-10.6 {long values} {
  396.     set b [concat 1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH]
  397.     llength $b
  398. } 43
  399. test parse-10.7 {long values} {
  400.     set b
  401. } $a
  402. test parse-10.8 {long values} {
  403.     set a [concat 0000 1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH IIII JJJJ KKKK LLLL MMMM NNNN OOOO PPPP QQQQ RRRR SSSS TTTT UUUU VVVV WWWW XXXX YYYY ZZZZ]
  404.     llength $a
  405. } 62
  406. set i 0
  407. foreach j [concat 0000 1111 2222 3333 4444 5555 6666 7777 8888 9999 aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn oooo pppp qqqq rrrr ssss tttt uuuu vvvv wwww xxxx yyyy zzzz AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH IIII JJJJ KKKK LLLL MMMM NNNN OOOO PPPP QQQQ RRRR SSSS TTTT UUUU VVVV WWWW XXXX YYYY ZZZZ] {
  408.     set test [string index 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ $i]
  409.     set test $test$test$test$test
  410.     set i [expr $i+1]
  411.     test parse-10.9 {long values} {
  412.     set j
  413.     } $test
  414. }
  415. test parse-10.10 {test buffer overflow in backslashes in braces} {
  416.     expr {"a" == {xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101\101}}
  417. } 0
  418.  
  419. test parse-11.1 {comments at the end of a bracketed script} {
  420.     set x "[
  421. expr 1+1
  422. # skip this!
  423. ]"
  424. } {2}
  425.