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

  1. # Commands covered:  regexp, regsub
  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/regexp.test,v 1.13 93/10/14 14:53:21 ouster Exp $ (Berkeley)
  28.  
  29. if {[string compare test [info procs test]] == 1} then {source defs}
  30.  
  31. catch {unset foo}
  32. test regexp-1.1 {basic regexp operation} {
  33.     regexp ab*c abbbc
  34. } 1
  35. test regexp-1.2 {basic regexp operation} {
  36.     regexp ab*c ac
  37. } 1
  38. test regexp-1.3 {basic regexp operation} {
  39.     regexp ab*c ab
  40. } 0
  41. test regexp-1.4 {basic regexp operation} {
  42.     regexp -- -gorp abc-gorpxxx
  43. } 1
  44.  
  45. test regexp-2.1 {getting substrings back from regexp} {
  46.     set foo {}
  47.     list [regexp ab*c abbbbc foo] $foo
  48. } {1 abbbbc}
  49. test regexp-2.2 {getting substrings back from regexp} {
  50.     set foo {}
  51.     set f2 {}
  52.     list [regexp a(b*)c abbbbc foo f2] $foo $f2
  53. } {1 abbbbc bbbb}
  54. test regexp-2.3 {getting substrings back from regexp} {
  55.     set foo {}
  56.     set f2 {}
  57.     list [regexp a(b*)(c) abbbbc foo f2] $foo $f2
  58. } {1 abbbbc bbbb}
  59. test regexp-2.4 {getting substrings back from regexp} {
  60.     set foo {}
  61.     set f2 {}
  62.     set f3 {}
  63.     list [regexp a(b*)(c) abbbbc foo f2 f3] $foo $f2 $f3
  64. } {1 abbbbc bbbb c}
  65. test regexp-2.5 {getting substrings back from regexp} {
  66.     set foo {}; set f1 {}; set f2 {}; set f3 {}; set f4 {}; set f5 {};
  67.     set f6 {}; set f7 {}; set f8 {}; set f9 {}
  68.     list [regexp (1*)(2*)(3*)(4*)(5*)(6*)(7*)(8*)(9*) 12223345556789999 \
  69.         foo f1 f2 f3 f4 f5 f6 f7 f8 f9] $foo $f1 $f2 $f3 $f4 $f5 \
  70.         $f6 $f7 $f8 $f9
  71. } {1 12223345556789999 1 222 33 4 555 6 7 8 9999}
  72. test regexp-2.6 {getting substrings back from regexp} {
  73.     set foo 2; set f2 2; set f3 2; set f4 2
  74.     list [regexp (a)(b)? xay foo f2 f3 f4] $foo $f2 $f3 $f4
  75. } {1 a a {} {}}
  76. test regexp-2.7 {getting substrings back from regexp} {
  77.     set foo 1; set f2 1; set f3 1; set f4 1
  78.     list [regexp (a)(b)?(c) xacy foo f2 f3 f4] $foo $f2 $f3 $f4
  79. } {1 ac a {} c}
  80.  
  81. test regexp-3.1 {-indices option to regexp} {
  82.     set foo {}
  83.     list [regexp -indices ab*c abbbbc foo] $foo
  84. } {1 {0 5}}
  85. test regexp-3.2 {-indices option to regexp} {
  86.     set foo {}
  87.     set f2 {}
  88.     list [regexp -indices a(b*)c abbbbc foo f2] $foo $f2
  89. } {1 {0 5} {1 4}}
  90. test regexp-3.3 {-indices option to regexp} {
  91.     set foo {}
  92.     set f2 {}
  93.     list [regexp -indices a(b*)(c) abbbbc foo f2] $foo $f2
  94. } {1 {0 5} {1 4}}
  95. test regexp-3.4 {-indices option to regexp} {
  96.     set foo {}
  97.     set f2 {}
  98.     set f3 {}
  99.     list [regexp -indices a(b*)(c) abbbbc foo f2 f3] $foo $f2 $f3
  100. } {1 {0 5} {1 4} {5 5}}
  101. test regexp-3.5 {-indices option to regexp} {
  102.     set foo {}; set f1 {}; set f2 {}; set f3 {}; set f4 {}; set f5 {};
  103.     set f6 {}; set f7 {}; set f8 {}; set f9 {}
  104.     list [regexp -indices (1*)(2*)(3*)(4*)(5*)(6*)(7*)(8*)(9*) \
  105.         12223345556789999 \
  106.         foo f1 f2 f3 f4 f5 f6 f7 f8 f9] $foo $f1 $f2 $f3 $f4 $f5 \
  107.         $f6 $f7 $f8 $f9
  108. } {1 {0 16} {0 0} {1 3} {4 5} {6 6} {7 9} {10 10} {11 11} {12 12} {13 16}}
  109. test regexp-3.6 {getting substrings back from regexp} {
  110.     set foo 2; set f2 2; set f3 2; set f4 2
  111.     list [regexp -indices (a)(b)? xay foo f2 f3 f4] $foo $f2 $f3 $f4
  112. } {1 {1 1} {1 1} {-1 -1} {-1 -1}}
  113. test regexp-3.7 {getting substrings back from regexp} {
  114.     set foo 1; set f2 1; set f3 1; set f4 1
  115.     list [regexp -indices (a)(b)?(c) xacy foo f2 f3 f4] $foo $f2 $f3 $f4
  116. } {1 {1 2} {1 1} {-1 -1} {2 2}}
  117.  
  118. test regexp-4.1 {-nocase option to regexp} {
  119.     regexp -nocase foo abcFOo
  120. } 1
  121. test regexp-4.2 {-nocase option to regexp} {
  122.     set f1 22
  123.     set f2 33
  124.     set f3 44
  125.     list [regexp -nocase {a(b*)([xy]*)z} aBbbxYXxxZ22 f1 f2 f3] $f1 $f2 $f3
  126. } {1 aBbbxYXxxZ Bbb xYXxx}
  127. test regexp-4.3 {-nocase option to regexp} {
  128.     regexp -nocase FOo abcFOo
  129. } 1
  130. set x abcdefghijklmnopqrstuvwxyz1234567890
  131. set x $x$x$x$x$x$x$x$x$x$x$x$x
  132. test regexp-4.4 {case conversion in regsub} {
  133.     list [regexp -nocase $x $x foo] $foo
  134. } "1 $x"
  135. unset x
  136.  
  137. test regexp-5.1 {exercise cache of compiled expressions} {
  138.     regexp .*a b
  139.     regexp .*b c
  140.     regexp .*c d
  141.     regexp .*d e
  142.     regexp .*e f
  143.     regexp .*a bbba
  144. } 1
  145. test regexp-5.2 {exercise cache of compiled expressions} {
  146.     regexp .*a b
  147.     regexp .*b c
  148.     regexp .*c d
  149.     regexp .*d e
  150.     regexp .*e f
  151.     regexp .*b xxxb
  152. } 1
  153. test regexp-5.3 {exercise cache of compiled expressions} {
  154.     regexp .*a b
  155.     regexp .*b c
  156.     regexp .*c d
  157.     regexp .*d e
  158.     regexp .*e f
  159.     regexp .*c yyyc
  160. } 1
  161. test regexp-5.4 {exercise cache of compiled expressions} {
  162.     regexp .*a b
  163.     regexp .*b c
  164.     regexp .*c d
  165.     regexp .*d e
  166.     regexp .*e f
  167.     regexp .*d 1d
  168. } 1
  169. test regexp-5.5 {exercise cache of compiled expressions} {
  170.     regexp .*a b
  171.     regexp .*b c
  172.     regexp .*c d
  173.     regexp .*d e
  174.     regexp .*e f
  175.     regexp .*e xe
  176. } 1
  177.  
  178. test regexp-6.1 {regexp errors} {
  179.     list [catch {regexp a} msg] $msg
  180. } {1 {wrong # args: should be "regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?"}}
  181. test regexp-6.2 {regexp errors} {
  182.     list [catch {regexp -nocase a} msg] $msg
  183. } {1 {wrong # args: should be "regexp ?switches? exp string ?matchVar? ?subMatchVar subMatchVar ...?"}}
  184. test regexp-6.3 {regexp errors} {
  185.     list [catch {regexp -gorp a} msg] $msg
  186. } {1 {bad switch "-gorp": must be -indices, -nocase, or --}}
  187. test regexp-6.4 {regexp errors} {
  188.     list [catch {regexp a( b} msg] $msg
  189. } {1 {couldn't compile regular expression pattern: unmatched ()}}
  190. test regexp-6.5 {regexp errors} {
  191.     list [catch {regexp a( b} msg] $msg
  192. } {1 {couldn't compile regular expression pattern: unmatched ()}}
  193. test regexp-6.6 {regexp errors} {
  194.     list [catch {regexp a a f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1} msg] $msg
  195. } {1 {too many substring variables}}
  196. test regexp-6.7 {regexp errors} {
  197.     set f1 44
  198.     list [catch {regexp abc abc f1(f2)} msg] $msg
  199. } {1 {couldn't set variable "f1(f2)"}}
  200.  
  201. test regexp-7.1 {basic regsub operation} {
  202.     list [regsub aa+ xaxaaaxaa 111&222 foo] $foo
  203. } {1 xax111aaa222xaa}
  204. test regexp-7.2 {basic regsub operation} {
  205.     list [regsub aa+ aaaxaa &111 foo] $foo
  206. } {1 aaa111xaa}
  207. test regexp-7.3 {basic regsub operation} {
  208.     list [regsub aa+ xaxaaa 111& foo] $foo
  209. } {1 xax111aaa}
  210. test regexp-7.4 {basic regsub operation} {
  211.     list [regsub aa+ aaa 11&2&333 foo] $foo
  212. } {1 11aaa2aaa333}
  213. test regexp-7.5 {basic regsub operation} {
  214.     list [regsub aa+ xaxaaaxaa &2&333 foo] $foo
  215. } {1 xaxaaa2aaa333xaa}
  216. test regexp-7.6 {basic regsub operation} {
  217.     list [regsub aa+ xaxaaaxaa 1&22& foo] $foo
  218. } {1 xax1aaa22aaaxaa}
  219. test regexp-7.7 {basic regsub operation} {
  220.     list [regsub a(a+) xaxaaaxaa {1\122\1} foo] $foo
  221. } {1 xax1aa22aaxaa}
  222. test regexp-7.8 {basic regsub operation} {
  223.     list [regsub a(a+) xaxaaaxaa {1\\\122\1} foo] $foo
  224. } "1 {xax1\\aa22aaxaa}"
  225. test regexp-7.9 {basic regsub operation} {
  226.     list [regsub a(a+) xaxaaaxaa {1\\122\1} foo] $foo
  227. } "1 {xax1\\122aaxaa}"
  228. test regexp-7.10 {basic regsub operation} {
  229.     list [regsub a(a+) xaxaaaxaa {1\\&\1} foo] $foo
  230. } "1 {xax1\\aaaaaxaa}"
  231. test regexp-7.11 {basic regsub operation} {
  232.     list [regsub a(a+) xaxaaaxaa {1\&\1} foo] $foo
  233. } {1 xax1&aaxaa}
  234. test regexp-7.12 {basic regsub operation} {
  235.     list [regsub a(a+) xaxaaaxaa {\1\1\1\1&&} foo] $foo
  236. } {1 xaxaaaaaaaaaaaaaaxaa}
  237. test regexp-7.13 {basic regsub operation} {
  238.     set foo xxx
  239.     list [regsub abc xyz 111 foo] $foo
  240. } {0 xyz}
  241. test regexp-7.14 {basic regsub operation} {
  242.     set foo xxx
  243.     list [regsub ^ xyz "111 " foo] $foo
  244. } {1 {111 xyz}}
  245. test regexp-7.15 {basic regsub operation} {
  246.     set foo xxx
  247.     list [regsub -- -foo abc-foodef "111 " foo] $foo
  248. } {1 {abc111 def}}
  249. test regexp-7.16 {basic regsub operation} {
  250.     set foo xxx
  251.     list [regsub x "" y foo] $foo
  252. } {0 {}}
  253.  
  254. test regexp-8.1 {case conversion in regsub} {
  255.     list [regsub -nocase a(a+) xaAAaAAay & foo] $foo
  256. } {1 xaAAaAAay}
  257. test regexp-8.2 {case conversion in regsub} {
  258.     list [regsub -nocase a(a+) xaAAaAAay & foo] $foo
  259. } {1 xaAAaAAay}
  260. test regexp-8.3 {case conversion in regsub} {
  261.     set foo 123
  262.     list [regsub a(a+) xaAAaAAay & foo] $foo
  263. } {0 xaAAaAAay}
  264. test regexp-8.4 {case conversion in regsub} {
  265.     set foo 123
  266.     list [regsub -nocase a CaDE b foo] $foo
  267. } {1 CbDE}
  268. test regexp-8.5 {case conversion in regsub} {
  269.     set foo 123
  270.     list [regsub -nocase XYZ CxYzD b foo] $foo
  271. } {1 CbD}
  272. test regexp-8.6 {case conversion in regsub} {
  273.     set x abcdefghijklmnopqrstuvwxyz1234567890
  274.     set x $x$x$x$x$x$x$x$x$x$x$x$x
  275.     set foo 123
  276.     list [regsub -nocase $x $x b foo] $foo
  277. } {1 b}
  278.  
  279. test regexp-9.1 {-all option to regsub} {
  280.     set foo 86
  281.     list [regsub -all x+ axxxbxxcxdx |&| foo] $foo
  282. } {1 a|xxx|b|xx|c|x|d|x|}
  283. test regexp-9.2 {-all option to regsub} {
  284.     set foo 86
  285.     list [regsub -nocase -all x+ aXxXbxxcXdx |&| foo] $foo
  286. } {1 a|XxX|b|xx|c|X|d|x|}
  287. test regexp-9.3 {-all option to regsub} {
  288.     set foo 86
  289.     list [regsub x+ axxxbxxcxdx |&| foo] $foo
  290. } {1 a|xxx|bxxcxdx}
  291. test regexp-9.4 {-all option to regsub} {
  292.     set foo 86
  293.     list [regsub -all bc axxxbxxcxdx |&| foo] $foo
  294. } {0 axxxbxxcxdx}
  295. test regexp-9.5 {-all option to regsub} {
  296.     set foo xxx
  297.     list [regsub -all node "node node more" yy foo] $foo
  298. } {1 {yy yy more}}
  299. test regexp-9.6 {-all option to regsub} {
  300.     set foo xxx
  301.     list [regsub -all ^ xxx 123 foo] $foo
  302. } {1 123xxx}
  303.  
  304. test regexp-10.1 {regsub errors} {
  305.     list [catch {regsub a b c} msg] $msg
  306. } {1 {wrong # args: should be "regsub ?switches? exp string subSpec varName"}}
  307. test regexp-10.2 {regsub errors} {
  308.     list [catch {regsub -nocase a b c} msg] $msg
  309. } {1 {wrong # args: should be "regsub ?switches? exp string subSpec varName"}}
  310. test regexp-10.3 {regsub errors} {
  311.     list [catch {regsub -nocase -all a b c} msg] $msg
  312. } {1 {wrong # args: should be "regsub ?switches? exp string subSpec varName"}}
  313. test regexp-10.4 {regsub errors} {
  314.     list [catch {regsub a b c d e f} msg] $msg
  315. } {1 {wrong # args: should be "regsub ?switches? exp string subSpec varName"}}
  316. test regexp-10.5 {regsub errors} {
  317.     list [catch {regsub -gorp a b c} msg] $msg
  318. } {1 {bad switch "-gorp": must be -all, -nocase, or --}}
  319. test regexp-10.6 {regsub errors} {
  320.     list [catch {regsub -nocase a( b c d} msg] $msg
  321. } {1 {couldn't compile regular expression pattern: unmatched ()}}
  322. test regexp-10.7 {regsub errors} {
  323.     list [catch {regsub -nocase aaa aaa xxx f1(f2)} msg] $msg
  324. } {1 {couldn't set variable "f1(f2)"}}
  325.