#!./perl
# We have the following types of loop:
#
# 1a) while(A) {B}
# 1b) B while A;
#
# 2a) until(A) {B}
# 2b) B until A;
#
# 3a) for(@A) {B}
# 3b) B for A;
#
# 4a) for (A;B;C) {D}
#
# 5a) { A } # a bare block is a loop which runs once
#
# control, so we ignore them here. Type (a) loops can
# all be labelled, so there are ten possibilities (each
# of 5 types, labelled/unlabelled). We therefore need
# thirty tests to try the three control statements against
# the ten types of loop. For the first four types it's useful
# to distinguish the case where next re-iterates from the case
# where it leaves the loop. That makes 38.
# All these tests rely on "last LABEL"
# so if they've *all* failed, maybe you broke that...
#
# These tests are followed by an extra test of nested loops.
# Feel free to add more here.
#
# -- .robin. <robin@kitsite.com> 2001-03-13
print "1..43\n";
my $ok;
## while() loop without a label
$ok = 0;
my $x = 1;
my $first_time = 1;
while($x--) {
$ok = 1;
}
$ok = 0;
$first_time = 0;
redo;
}
continue {
$ok = 0;
}
$ok = 0;
}
$ok = 0;
my $x = 2;
my $first_time = 1;
my $been_in_continue = 0;
while($x--) {
}
$ok = 0;
$first_time = 0;
next;
}
continue {
$been_in_continue = 1;
}
$ok = 0;
}
$ok = 0;
my $x = 1;
my $first_time = 1;
my $been_in_loop = 0;
my $been_in_continue = 0;
while($x--) {
$been_in_loop = 1;
$ok = 0;
}
$ok = 0;
$first_time = 0;
next;
}
continue {
$been_in_continue = 1;
}
}
$ok = 0;
my $x = 1;
my $first_time = 1;
while($x++) {
$ok = 0;
}
$ok = 0;
$first_time = 0;
last;
}
continue {
$ok = 0;
}
$ok = 1;
}
## until() loop without a label
$ok = 0;
my $x = 0;
my $first_time = 1;
until($x++) {
$ok = 1;
}
$ok = 0;
$first_time = 0;
redo;
}
continue {
$ok = 0;
}
$ok = 0;
}
$ok = 0;
my $x = 0;
my $first_time = 1;
my $been_in_continue = 0;
until($x++ >= 2) {
}
$ok = 0;
$first_time = 0;
next;
}
continue {
$been_in_continue = 1;
}
$ok = 0;
}
$ok = 0;
my $x = 0;
my $first_time = 1;
my $been_in_loop = 0;
my $been_in_continue = 0;
until($x++) {
$been_in_loop = 1;
$ok = 0;
}
$ok = 0;
$first_time = 0;
next;
}
continue {
$been_in_continue = 1;
}
}
$ok = 0;
my $x = 0;
my $first_time = 1;
until($x++ == 10) {
$ok = 0;
}
$ok = 0;
$first_time = 0;
last;
}
continue {
$ok = 0;
}
$ok = 1;
}
## for(@array) loop without a label
$ok = 0;
my $first_time = 1;
for(1) {
$ok = 1;
}
$ok = 0;
$first_time = 0;
redo;
}
continue {
$ok = 0;
}
$ok = 0;
}
$ok = 0;
my $first_time = 1;
my $been_in_continue = 0;
for(1,2) {
}
$ok = 0;
$first_time = 0;
next;
}
continue {
$been_in_continue = 1;
}
$ok = 0;
}
$ok = 0;
my $first_time = 1;
my $been_in_loop = 0;
my $been_in_continue = 0;
for(1) {
$been_in_loop = 1;
$ok = 0;
}
$ok = 0;
$first_time = 0;
next;
}
continue {
$been_in_continue = 1;
}
}
$ok = 0;
my $first_time = 1;
for(1..10) {
$ok = 0;
}
$ok = 0;
$first_time = 0;
last;
}
continue {
$ok=0;
}
$ok = 1;
}
## for(;;) loop without a label
$ok = 0;
$ok = 1;
}
$ok = 0;
$first_time=0;
redo;
}
$ok = 0;
}
$ok = 0;
$ok = 1;
}
$ok = 0;
next;
}
$ok = 0;
}
$ok = 0;
my $x=1;
my $been_in_loop = 0;
$been_in_loop = 1;
$ok = 0;
}
$ok = 0;
$first_time = 0;
next;
}
$ok = $been_in_loop;
}
$ok = 0;
$ok = 0;
}
$ok = 0;
$first_time = 0;
last;
}
$ok = 1;
}
## bare block without a label
$ok = 0;
my $first_time = 1;
{
$ok = 1;
}
$ok = 0;
$first_time=0;
redo;
}
continue {
$ok = 0;
}
$ok = 0;
}
$ok = 0;
{
next;
}
continue {
$ok = 1;
}
$ok = 0;
}
$ok = 0;
{
last;
}
continue {
$ok = 0;
}
$ok = 1;
}
### Now do it all again with labels
## while() loop with a label
$ok = 0;
my $x = 1;
my $first_time = 1;
$ok = 1;
}
$ok = 0;
$first_time = 0;
}
continue {
$ok = 0;
}
$ok = 0;
}
$ok = 0;
my $x = 2;
my $first_time = 1;
my $been_in_continue = 0;
}
$ok = 0;
$first_time = 0;
next LABEL21;
}
continue {
$been_in_continue = 1;
}
$ok = 0;
}
$ok = 0;
my $x = 1;
my $first_time = 1;
my $been_in_loop = 0;
my $been_in_continue = 0;
$been_in_loop = 1;
$ok = 0;
}
$ok = 0;
$first_time = 0;
next LABEL22;
}
continue {
$been_in_continue = 1;
}
}
$ok = 0;
my $x = 1;
my $first_time = 1;
$ok = 0;
}
$ok = 0;
$first_time = 0;
}
continue {
$ok = 0;
}
$ok = 1;
}
## until() loop with a label
$ok = 0;
my $x = 0;
my $first_time = 1;
$ok = 1;
}
$ok = 0;
$first_time = 0;
}
continue {
$ok = 0;
}
$ok = 0;
}
$ok = 0;
my $x = 0;
my $first_time = 1;
my $been_in_continue = 0;
}
$ok = 0;
$first_time = 0;
next LABEL25;
}
continue {
$been_in_continue = 1;
}
$ok = 0;
}
$ok = 0;
my $x = 0;
my $first_time = 1;
my $been_in_loop = 0;
my $been_in_continue = 0;
$been_in_loop = 1;
$ok = 0;
}
$ok = 0;
$first_time = 0;
next LABEL26;
}
continue {
$been_in_continue = 1;
}
}
$ok = 0;
my $x = 0;
my $first_time = 1;
$ok = 0;
}
$ok = 0;
$first_time = 0;
}
continue {
$ok = 0;
}
$ok = 1;
}
## for(@array) loop with a label
$ok = 0;
my $first_time = 1;
$ok = 1;
}
$ok = 0;
$first_time = 0;
}
continue {
$ok = 0;
}
$ok = 0;
}
$ok = 0;
my $first_time = 1;
my $been_in_continue = 0;
}
$ok = 0;
$first_time = 0;
next LABEL29;
}
continue {
$been_in_continue = 1;
}
$ok = 0;
}
$ok = 0;
my $first_time = 1;
my $been_in_loop = 0;
my $been_in_continue = 0;
$been_in_loop = 1;
$ok = 0;
}
$ok = 0;
$first_time = 0;
next LABEL30;
}
continue {
$been_in_continue = 1;
}
}
$ok = 0;
my $first_time = 1;
$ok = 0;
}
$ok = 0;
$first_time = 0;
}
continue {
$ok=0;
}
$ok = 1;
}
## for(;;) loop with a label
$ok = 0;
$ok = 1;
}
$ok = 0;
$first_time=0;
}
$ok = 0;
}
$ok = 0;
$ok = 1;
}
$ok = 0;
next LABEL33;
}
$ok = 0;
}
$ok = 0;
my $x=1;
my $been_in_loop = 0;
$been_in_loop = 1;
$ok = 0;
}
$ok = 0;
$first_time = 0;
next LABEL34;
}
$ok = $been_in_loop;
}
$ok = 0;
$ok = 0;
}
$ok = 0;
$first_time = 0;
}
$ok = 1;
}
## bare block with a label
$ok = 0;
my $first_time = 1;
LABEL36: {
$ok = 1;
}
$ok = 0;
$first_time=0;
}
continue {
$ok = 0;
}
$ok = 0;
}
$ok = 0;
LABEL37: {
next LABEL37;
}
continue {
$ok = 1;
}
$ok = 0;
}
$ok = 0;
LABEL38: {
}
continue {
$ok = 0;
}
$ok = 1;
}
### Now test nested constructs
$ok = 0;
my ($x, $y, $z) = (1,1,1);
$ok = 0;
$ok = 0;
next two39;
}
continue {
$ok = 0;
}
}
continue {
$ok = 1;
}
$ok = 0;
}
}
### Test that loop control is dynamicly scoped.
TEST40: {
$ok = 1;
$ok = 0;
}
TEST41: {
$ok = 1;
$ok = 0;
}
# [perl #27206] Memory leak in continue loop
# Ensure that the temporary object is freed each time round the loop,
# rather then all 10 of them all being freed right at the end
{
my $n=10; my $late_free = 0;
sub X::DESTROY { $late_free++ if $n < 0 };
{
}
$n = 10; $late_free = 0;
{
}
continue { }
}