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