3 # We have the following types of loop:
16 # 5a) { A } # a bare block is a loop which runs once
18 # Loops of type (b) don't allow for next/last/redo style
19 # control, so we ignore them here. Type (a) loops can
20 # all be labelled, so there are ten possibilities (each
21 # of 5 types, labelled/unlabelled). We therefore need
22 # thirty tests to try the three control statements against
23 # the ten types of loop. For the first four types it's useful
24 # to distinguish the case where next re-iterates from the case
25 # where it leaves the loop. That makes 38.
26 # All these tests rely on "last LABEL"
27 # so if they've *all* failed, maybe you broke that...
29 # These tests are followed by an extra test of nested loops.
30 # Feel free to add more here.
32 # -- .robin. <robin@kitsite.com> 2001-03-13
38 ## while() loop without a label
62 print ($ok ? "ok 1\n" : "not ok 1\n");
64 TEST2: { # next (succesful)
70 my $been_in_continue = 0;
73 $ok = $been_in_continue;
82 $been_in_continue = 1;
86 print ($ok ? "ok 2\n" : "not ok 2\n");
88 TEST3: { # next (unsuccesful)
95 my $been_in_continue = 0;
108 $been_in_continue = 1;
110 $ok = $been_in_loop && $been_in_continue;
112 print ($ok ? "ok 3\n" : "not ok 3\n");
136 print ($ok ? "ok 4\n" : "not ok 4\n");
139 ## until() loop without a label
163 print ($ok ? "ok 5\n" : "not ok 5\n");
165 TEST6: { # next (succesful)
171 my $been_in_continue = 0;
174 $ok = $been_in_continue;
183 $been_in_continue = 1;
187 print ($ok ? "ok 6\n" : "not ok 6\n");
189 TEST7: { # next (unsuccesful)
195 my $been_in_loop = 0;
196 my $been_in_continue = 0;
209 $been_in_continue = 1;
211 $ok = $been_in_loop && $been_in_continue;
213 print ($ok ? "ok 7\n" : "not ok 7\n");
237 print ($ok ? "ok 8\n" : "not ok 8\n");
239 ## for(@array) loop without a label
262 print ($ok ? "ok 9\n" : "not ok 9\n");
264 TEST10: { # next (succesful)
269 my $been_in_continue = 0;
272 $ok = $been_in_continue;
281 $been_in_continue = 1;
285 print ($ok ? "ok 10\n" : "not ok 10\n");
287 TEST11: { # next (unsuccesful)
292 my $been_in_loop = 0;
293 my $been_in_continue = 0;
306 $been_in_continue = 1;
308 $ok = $been_in_loop && $been_in_continue;
310 print ($ok ? "ok 11\n" : "not ok 11\n");
333 print ($ok ? "ok 12\n" : "not ok 12\n");
335 ## for(;;) loop without a label
341 for(my $first_time = 1; 1;) {
354 print ($ok ? "ok 13\n" : "not ok 13\n");
356 TEST14: { # next (successful)
360 for(my $first_time = 1; 1; $first_time=0) {
371 print ($ok ? "ok 14\n" : "not ok 14\n");
373 TEST15: { # next (unsuccesful)
378 my $been_in_loop = 0;
379 for(my $first_time = 1; $x--;) {
392 print ($ok ? "ok 15\n" : "not ok 15\n");
398 for(my $first_time = 1; 1; last TEST16) {
410 print ($ok ? "ok 16\n" : "not ok 16\n");
412 ## bare block without a label
436 print ($ok ? "ok 17\n" : "not ok 17\n");
451 print ($ok ? "ok 18\n" : "not ok 18\n");
466 print ($ok ? "ok 19\n" : "not ok 19\n");
469 ### Now do it all again with labels
471 ## while() loop with a label
479 LABEL20: while($x--) {
495 print ($ok ? "ok 20\n" : "not ok 20\n");
497 TEST21: { # next (succesful)
503 my $been_in_continue = 0;
504 LABEL21: while($x--) {
506 $ok = $been_in_continue;
515 $been_in_continue = 1;
519 print ($ok ? "ok 21\n" : "not ok 21\n");
521 TEST22: { # next (unsuccesful)
527 my $been_in_loop = 0;
528 my $been_in_continue = 0;
529 LABEL22: while($x--) {
541 $been_in_continue = 1;
543 $ok = $been_in_loop && $been_in_continue;
545 print ($ok ? "ok 22\n" : "not ok 22\n");
553 LABEL23: while($x++) {
569 print ($ok ? "ok 23\n" : "not ok 23\n");
572 ## until() loop with a label
580 LABEL24: until($x++) {
596 print ($ok ? "ok 24\n" : "not ok 24\n");
598 TEST25: { # next (succesful)
604 my $been_in_continue = 0;
605 LABEL25: until($x++ >= 2) {
607 $ok = $been_in_continue;
616 $been_in_continue = 1;
620 print ($ok ? "ok 25\n" : "not ok 25\n");
622 TEST26: { # next (unsuccesful)
628 my $been_in_loop = 0;
629 my $been_in_continue = 0;
630 LABEL26: until($x++) {
642 $been_in_continue = 1;
644 $ok = $been_in_loop && $been_in_continue;
646 print ($ok ? "ok 26\n" : "not ok 26\n");
654 LABEL27: until($x++ == 10) {
670 print ($ok ? "ok 27\n" : "not ok 27\n");
672 ## for(@array) loop with a label
695 print ($ok ? "ok 28\n" : "not ok 28\n");
697 TEST29: { # next (succesful)
702 my $been_in_continue = 0;
705 $ok = $been_in_continue;
714 $been_in_continue = 1;
718 print ($ok ? "ok 29\n" : "not ok 29\n");
720 TEST30: { # next (unsuccesful)
725 my $been_in_loop = 0;
726 my $been_in_continue = 0;
739 $been_in_continue = 1;
741 $ok = $been_in_loop && $been_in_continue;
743 print ($ok ? "ok 30\n" : "not ok 30\n");
750 LABEL31: for(1..10) {
766 print ($ok ? "ok 31\n" : "not ok 31\n");
768 ## for(;;) loop with a label
774 LABEL32: for(my $first_time = 1; 1;) {
787 print ($ok ? "ok 32\n" : "not ok 32\n");
789 TEST33: { # next (successful)
793 LABEL33: for(my $first_time = 1; 1; $first_time=0) {
804 print ($ok ? "ok 33\n" : "not ok 33\n");
806 TEST34: { # next (unsuccesful)
811 my $been_in_loop = 0;
812 LABEL34: for(my $first_time = 1; $x--;) {
825 print ($ok ? "ok 34\n" : "not ok 34\n");
831 LABEL35: for(my $first_time = 1; 1; last TEST16) {
843 print ($ok ? "ok 35\n" : "not ok 35\n");
845 ## bare block with a label
869 print ($ok ? "ok 36\n" : "not ok 36\n");
884 print ($ok ? "ok 37\n" : "not ok 37\n");
899 print ($ok ? "ok 38\n" : "not ok 38\n");
901 ### Now test nested constructs
905 my ($x, $y, $z) = (1,1,1);
906 one39: while ($x--) {
908 two39: while ($y--) {
910 three39: while ($z--) {
925 print ($ok ? "ok 39\n" : "not ok 39\n");
928 ### Test that loop control is dynamicly scoped.
930 sub test_last_label { last TEST40 }
937 print ($ok ? "ok 40\n" : "not ok 40\n");
939 sub test_last { last }
946 print ($ok ? "ok 41\n" : "not ok 41\n");