PageRenderTime 138ms CodeModel.GetById 41ms RepoModel.GetById 4ms app.codeStats 0ms

/brlcad/branches/dmtogl/src/other/tcl/tests/while.test

https://bitbucket.org/vrrm/brl-cad-copy-for-fast-history-browsing-in-git
Unknown | 621 lines | 604 code | 17 blank | 0 comment | 0 complexity | b9959c00aacd6749e1cf34bb8362c04f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1, Apache-2.0, AGPL-3.0, LGPL-3.0, GPL-3.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, 0BSD, BSD-3-Clause
  1. # Commands covered: while
  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. # Copyright (c) 1998-1999 by Scriptics Corporation.
  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. # RCS: @(#) $Id$
  14. if {[lsearch [namespace children] ::tcltest] == -1} {
  15. package require tcltest 2
  16. namespace import -force ::tcltest::*
  17. }
  18. # Basic "while" operation.
  19. catch {unset i}
  20. catch {unset a}
  21. test while-1.1 {TclCompileWhileCmd: missing test expression} {
  22. catch {while } msg
  23. set msg
  24. } {wrong # args: should be "while test command"}
  25. test while-1.2 {TclCompileWhileCmd: error in test expression} -body {
  26. set i 0
  27. catch {while {$i<} break} msg
  28. set ::errorInfo
  29. } -match glob -result {*"while {$i<} break"}
  30. test while-1.3 {TclCompileWhileCmd: error in test expression} {
  31. set err [catch {while {"a"+"b"} {error "loop aborted"}} msg]
  32. list $err $msg
  33. } {1 {can't use non-numeric string as operand of "+"}}
  34. test while-1.4 {TclCompileWhileCmd: multiline test expr} {
  35. set value 1
  36. while {($tcl_platform(platform) != "foobar1") && \
  37. ($tcl_platform(platform) != "foobar2")} {
  38. incr value
  39. break
  40. }
  41. set value
  42. } {2}
  43. test while-1.5 {TclCompileWhileCmd: non-numeric boolean test expr} {
  44. set value 1
  45. while {"true"} {
  46. incr value;
  47. if {$value > 5} {
  48. break;
  49. }
  50. }
  51. set value
  52. } 6
  53. test while-1.6 {TclCompileWhileCmd: test expr is enclosed in quotes} {
  54. set i 0
  55. while "$i > 5" {}
  56. } {}
  57. test while-1.7 {TclCompileWhileCmd: missing command body} {
  58. set i 0
  59. catch {while {$i < 5} } msg
  60. set msg
  61. } {wrong # args: should be "while test command"}
  62. test while-1.8 {TclCompileWhileCmd: error compiling command body} -body {
  63. set i 0
  64. catch {while {$i < 5} {set}} msg
  65. set ::errorInfo
  66. } -match glob -result {wrong # args: should be "set varName ?newValue?"
  67. while *ing
  68. "set"*}
  69. test while-1.9 {TclCompileWhileCmd: simple command body} {
  70. set a {}
  71. set i 1
  72. while {$i<6} {
  73. if $i==4 break
  74. set a [concat $a $i]
  75. incr i
  76. }
  77. set a
  78. } {1 2 3}
  79. test while-1.10 {TclCompileWhileCmd: command body in quotes} {
  80. set a {}
  81. set i 1
  82. while {$i<6} "append a x; incr i"
  83. set a
  84. } {xxxxx}
  85. test while-1.11 {TclCompileWhileCmd: computed command body} {
  86. catch {unset x1}
  87. catch {unset bb}
  88. catch {unset x2}
  89. set x1 {append a x1; }
  90. set bb {break}
  91. set x2 {; append a x2; incr i}
  92. set a {}
  93. set i 1
  94. while {$i<6} $x1$bb$x2
  95. set a
  96. } {x1}
  97. test while-1.12 {TclCompileWhileCmd: long command body} {
  98. set a {}
  99. set i 1
  100. while {$i<6} {
  101. if $i==4 break
  102. if $i>5 continue
  103. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  104. catch {set a $a} msg
  105. catch {incr i 5} msg
  106. catch {incr i -5} msg
  107. }
  108. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  109. catch {set a $a} msg
  110. catch {incr i 5} msg
  111. catch {incr i -5} msg
  112. }
  113. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  114. catch {set a $a} msg
  115. catch {incr i 5} msg
  116. catch {incr i -5} msg
  117. }
  118. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  119. catch {set a $a} msg
  120. catch {incr i 5} msg
  121. catch {incr i -5} msg
  122. }
  123. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  124. catch {set a $a} msg
  125. catch {incr i 5} msg
  126. catch {incr i -5} msg
  127. }
  128. set a [concat $a $i]
  129. incr i
  130. }
  131. set a
  132. } {1 2 3}
  133. test while-1.13 {TclCompileWhileCmd: while command result} {
  134. set i 0
  135. set a [while {$i < 5} {incr i}]
  136. set a
  137. } {}
  138. test while-1.14 {TclCompileWhileCmd: while command result} {
  139. set i 0
  140. set a [while {$i < 5} {if $i==3 break; incr i}]
  141. set a
  142. } {}
  143. # Check "while" and "continue".
  144. test while-2.1 {continue tests} {
  145. set a {}
  146. set i 1
  147. while {$i <= 4} {
  148. incr i
  149. if {$i == 3} continue
  150. set a [concat $a $i]
  151. }
  152. set a
  153. } {2 4 5}
  154. test while-2.2 {continue tests} {
  155. set a {}
  156. set i 1
  157. while {$i <= 4} {
  158. incr i
  159. if {$i != 2} continue
  160. set a [concat $a $i]
  161. }
  162. set a
  163. } {2}
  164. test while-2.3 {continue tests, nested loops} {
  165. set msg {}
  166. set i 1
  167. while {$i <= 4} {
  168. incr i
  169. set a 1
  170. while {$a <= 2} {
  171. incr a
  172. if {$i>=3 && $a>=3} continue
  173. set msg [concat $msg "$i.$a"]
  174. }
  175. }
  176. set msg
  177. } {2.2 2.3 3.2 4.2 5.2}
  178. test while-2.4 {continue tests, long command body} {
  179. set a {}
  180. set i 1
  181. while {$i<6} {
  182. if $i==2 {incr i; continue}
  183. if $i==4 break
  184. if $i>5 continue
  185. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  186. catch {set a $a} msg
  187. catch {incr i 5} msg
  188. catch {incr i -5} msg
  189. }
  190. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  191. catch {set a $a} msg
  192. catch {incr i 5} msg
  193. catch {incr i -5} msg
  194. }
  195. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  196. catch {set a $a} msg
  197. catch {incr i 5} msg
  198. catch {incr i -5} msg
  199. }
  200. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  201. catch {set a $a} msg
  202. catch {incr i 5} msg
  203. catch {incr i -5} msg
  204. }
  205. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  206. catch {set a $a} msg
  207. catch {incr i 5} msg
  208. catch {incr i -5} msg
  209. }
  210. set a [concat $a $i]
  211. incr i
  212. }
  213. set a
  214. } {1 3}
  215. # Check "while" and "break".
  216. test while-3.1 {break tests} {
  217. set a {}
  218. set i 1
  219. while {$i <= 4} {
  220. if {$i == 3} break
  221. set a [concat $a $i]
  222. incr i
  223. }
  224. set a
  225. } {1 2}
  226. test while-3.2 {break tests, nested loops} {
  227. set msg {}
  228. set i 1
  229. while {$i <= 4} {
  230. set a 1
  231. while {$a <= 2} {
  232. if {$i>=2 && $a>=2} break
  233. set msg [concat $msg "$i.$a"]
  234. incr a
  235. }
  236. incr i
  237. }
  238. set msg
  239. } {1.1 1.2 2.1 3.1 4.1}
  240. test while-3.3 {break tests, long command body} {
  241. set a {}
  242. set i 1
  243. while {$i<6} {
  244. if $i==2 {incr i; continue}
  245. if $i==5 break
  246. if $i>5 continue
  247. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  248. catch {set a $a} msg
  249. catch {incr i 5} msg
  250. catch {incr i -5} msg
  251. }
  252. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  253. catch {set a $a} msg
  254. catch {incr i 5} msg
  255. catch {incr i -5} msg
  256. }
  257. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  258. catch {set a $a} msg
  259. catch {incr i 5} msg
  260. catch {incr i -5} msg
  261. }
  262. if $i==4 break
  263. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  264. catch {set a $a} msg
  265. catch {incr i 5} msg
  266. catch {incr i -5} msg
  267. }
  268. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  269. catch {set a $a} msg
  270. catch {incr i 5} msg
  271. catch {incr i -5} msg
  272. }
  273. set a [concat $a $i]
  274. incr i
  275. }
  276. set a
  277. } {1 3}
  278. # Check "while" with computed command names.
  279. test while-4.1 {while and computed command names} {
  280. set i 0
  281. set z while
  282. $z {$i < 10} {
  283. incr i
  284. }
  285. set i
  286. } 10
  287. test while-4.2 {while (not compiled): missing test expression} {
  288. set z while
  289. catch {$z } msg
  290. set msg
  291. } {wrong # args: should be "while test command"}
  292. test while-4.3 {while (not compiled): error in test expression} -body {
  293. set i 0
  294. set z while
  295. catch {$z {$i<} {set x 1}} msg
  296. set ::errorInfo
  297. } -match glob -result {*"$z {$i<} {set x 1}"}
  298. test while-4.4 {while (not compiled): error in test expression} {
  299. set z while
  300. set err [catch {$z {"a"+"b"} {error "loop aborted"}} msg]
  301. list $err $msg
  302. } {1 {can't use non-numeric string as operand of "+"}}
  303. test while-4.5 {while (not compiled): multiline test expr} {
  304. set value 1
  305. set z while
  306. $z {($tcl_platform(platform) != "foobar1") && \
  307. ($tcl_platform(platform) != "foobar2")} {
  308. incr value
  309. break
  310. }
  311. set value
  312. } {2}
  313. test while-4.6 {while (not compiled): non-numeric boolean test expr} {
  314. set value 1
  315. set z while
  316. $z {"true"} {
  317. incr value;
  318. if {$value > 5} {
  319. break;
  320. }
  321. }
  322. set value
  323. } 6
  324. test while-4.7 {while (not compiled): test expr is enclosed in quotes} {
  325. set i 0
  326. set z while
  327. $z "$i > 5" {}
  328. } {}
  329. test while-4.8 {while (not compiled): missing command body} {
  330. set i 0
  331. set z while
  332. catch {$z {$i < 5} } msg
  333. set msg
  334. } {wrong # args: should be "while test command"}
  335. test while-4.9 {while (not compiled): error compiling command body} -body {
  336. set i 0
  337. set z while
  338. catch {$z {$i < 5} {set}} msg
  339. set ::errorInfo
  340. } -match glob -result {wrong # args: should be "set varName ?newValue?"
  341. while *ing
  342. "set"
  343. ("while" body line 1)
  344. invoked from within
  345. "$z {$i < 5} {set}"}
  346. test while-4.10 {while (not compiled): simple command body} {
  347. set a {}
  348. set i 1
  349. set z while
  350. $z {$i<6} {
  351. if $i==4 break
  352. set a [concat $a $i]
  353. incr i
  354. }
  355. set a
  356. } {1 2 3}
  357. test while-4.11 {while (not compiled): command body in quotes} {
  358. set a {}
  359. set i 1
  360. set z while
  361. $z {$i<6} "append a x; incr i"
  362. set a
  363. } {xxxxx}
  364. test while-4.12 {while (not compiled): computed command body} {
  365. set z while
  366. catch {unset x1}
  367. catch {unset bb}
  368. catch {unset x2}
  369. set x1 {append a x1; }
  370. set bb {break}
  371. set x2 {; append a x2; incr i}
  372. set a {}
  373. set i 1
  374. $z {$i<6} $x1$bb$x2
  375. set a
  376. } {x1}
  377. test while-4.13 {while (not compiled): long command body} {
  378. set a {}
  379. set z while
  380. set i 1
  381. $z {$i<6} {
  382. if $i==4 break
  383. if $i>5 continue
  384. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  385. catch {set a $a} msg
  386. catch {incr i 5} msg
  387. catch {incr i -5} msg
  388. }
  389. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  390. catch {set a $a} msg
  391. catch {incr i 5} msg
  392. catch {incr i -5} msg
  393. }
  394. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  395. catch {set a $a} msg
  396. catch {incr i 5} msg
  397. catch {incr i -5} msg
  398. }
  399. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  400. catch {set a $a} msg
  401. catch {incr i 5} msg
  402. catch {incr i -5} msg
  403. }
  404. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  405. catch {set a $a} msg
  406. catch {incr i 5} msg
  407. catch {incr i -5} msg
  408. }
  409. set a [concat $a $i]
  410. incr i
  411. }
  412. set a
  413. } {1 2 3}
  414. test while-4.14 {while (not compiled): while command result} {
  415. set i 0
  416. set z while
  417. set a [$z {$i < 5} {incr i}]
  418. set a
  419. } {}
  420. test while-4.15 {while (not compiled): while command result} {
  421. set i 0
  422. set z while
  423. set a [$z {$i < 5} {if $i==3 break; incr i}]
  424. set a
  425. } {}
  426. # Check "break" with computed command names.
  427. test while-5.1 {break and computed command names} {
  428. set i 0
  429. set z break
  430. while 1 {
  431. if {$i > 10} $z
  432. incr i
  433. }
  434. set i
  435. } 11
  436. test while-5.2 {break tests with computed command names} {
  437. set a {}
  438. set i 1
  439. set z break
  440. while {$i <= 4} {
  441. if {$i == 3} $z
  442. set a [concat $a $i]
  443. incr i
  444. }
  445. set a
  446. } {1 2}
  447. test while-5.3 {break tests, nested loops with computed command names} {
  448. set msg {}
  449. set i 1
  450. set z break
  451. while {$i <= 4} {
  452. set a 1
  453. while {$a <= 2} {
  454. if {$i>=2 && $a>=2} $z
  455. set msg [concat $msg "$i.$a"]
  456. incr a
  457. }
  458. incr i
  459. }
  460. set msg
  461. } {1.1 1.2 2.1 3.1 4.1}
  462. test while-5.4 {break tests, long command body with computed command names} {
  463. set a {}
  464. set i 1
  465. set z break
  466. while {$i<6} {
  467. if $i==2 {incr i; continue}
  468. if $i==5 $z
  469. if $i>5 continue
  470. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  471. catch {set a $a} msg
  472. catch {incr i 5} msg
  473. catch {incr i -5} msg
  474. }
  475. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  476. catch {set a $a} msg
  477. catch {incr i 5} msg
  478. catch {incr i -5} msg
  479. }
  480. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  481. catch {set a $a} msg
  482. catch {incr i 5} msg
  483. catch {incr i -5} msg
  484. }
  485. if $i==4 $z
  486. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  487. catch {set a $a} msg
  488. catch {incr i 5} msg
  489. catch {incr i -5} msg
  490. }
  491. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  492. catch {set a $a} msg
  493. catch {incr i 5} msg
  494. catch {incr i -5} msg
  495. }
  496. set a [concat $a $i]
  497. incr i
  498. }
  499. set a
  500. } {1 3}
  501. # Check "continue" with computed command names.
  502. test while-6.1 {continue and computed command names} {
  503. set i 0
  504. set z continue
  505. while 1 {
  506. incr i
  507. if {$i < 10} $z
  508. break
  509. }
  510. set i
  511. } 10
  512. test while-6.2 {continue tests} {
  513. set a {}
  514. set i 1
  515. set z continue
  516. while {$i <= 4} {
  517. incr i
  518. if {$i == 3} $z
  519. set a [concat $a $i]
  520. }
  521. set a
  522. } {2 4 5}
  523. test while-6.3 {continue tests with computed command names} {
  524. set a {}
  525. set i 1
  526. set z continue
  527. while {$i <= 4} {
  528. incr i
  529. if {$i != 2} $z
  530. set a [concat $a $i]
  531. }
  532. set a
  533. } {2}
  534. test while-6.4 {continue tests, nested loops with computed command names} {
  535. set msg {}
  536. set i 1
  537. set z continue
  538. while {$i <= 4} {
  539. incr i
  540. set a 1
  541. while {$a <= 2} {
  542. incr a
  543. if {$i>=3 && $a>=3} $z
  544. set msg [concat $msg "$i.$a"]
  545. }
  546. }
  547. set msg
  548. } {2.2 2.3 3.2 4.2 5.2}
  549. test while-6.5 {continue tests, long command body with computed command names} {
  550. set a {}
  551. set i 1
  552. set z continue
  553. while {$i<6} {
  554. if $i==2 {incr i; continue}
  555. if $i==4 break
  556. if $i>5 $z
  557. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  558. catch {set a $a} msg
  559. catch {incr i 5} msg
  560. catch {incr i -5} msg
  561. }
  562. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  563. catch {set a $a} msg
  564. catch {incr i 5} msg
  565. catch {incr i -5} msg
  566. }
  567. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  568. catch {set a $a} msg
  569. catch {incr i 5} msg
  570. catch {incr i -5} msg
  571. }
  572. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  573. catch {set a $a} msg
  574. catch {incr i 5} msg
  575. catch {incr i -5} msg
  576. }
  577. if {$i>6 && $tcl_platform(machine)=="xxx"} {
  578. catch {set a $a} msg
  579. catch {incr i 5} msg
  580. catch {incr i -5} msg
  581. }
  582. set a [concat $a $i]
  583. incr i
  584. }
  585. set a
  586. } {1 3}
  587. # Test for incorrect "double evaluation" semantics
  588. test while-7.1 {delayed substitution of body} {
  589. set i 0
  590. while {[incr i] < 10} "
  591. set result $i
  592. "
  593. proc p {} {
  594. set i 0
  595. while {[incr i] < 10} "
  596. set result $i
  597. "
  598. set result
  599. }
  600. append result [p]
  601. } {00}
  602. # cleanup
  603. ::tcltest::cleanupTests
  604. return