xref: /openbsd/gnu/usr.bin/perl/t/mro/recursion_dfs.t (revision 9f11ffb7)
1850e2753Smillert#!./perl
2850e2753Smillert
3850e2753SmillertBEGIN {
4850e2753Smillert    chdir 't' if -d 't';
5*9f11ffb7Safresh1    require './test.pl';
6*9f11ffb7Safresh1    set_up_inc('../lib');
7850e2753Smillert}
8850e2753Smillert
9b8851fccSafresh1use strict;
10b8851fccSafresh1use warnings;
11b8851fccSafresh1
12850e2753Smillertplan(skip_all => "Your system has no SIGALRM") if !exists $SIG{ALRM};
13850e2753Smillertplan(tests => 8);
14850e2753Smillert
15850e2753Smillert=pod
16850e2753Smillert
17850e2753SmillertThese are like the 010_complex_merge_classless test,
18850e2753Smillertbut an infinite loop has been made in the heirarchy,
19850e2753Smillertto test that we can fail cleanly instead of going
20850e2753Smillertinto an infinite loop
21850e2753Smillert
22850e2753Smillert=cut
23850e2753Smillert
24850e2753Smillert# initial setup, everything sane
25850e2753Smillert{
26850e2753Smillert    package K;
27850e2753Smillert    our @ISA = qw/J I/;
28850e2753Smillert    package J;
29850e2753Smillert    our @ISA = qw/F/;
30850e2753Smillert    package I;
31850e2753Smillert    our @ISA = qw/H F/;
32850e2753Smillert    package H;
33850e2753Smillert    our @ISA = qw/G/;
34850e2753Smillert    package G;
35850e2753Smillert    our @ISA = qw/D/;
36850e2753Smillert    package F;
37850e2753Smillert    our @ISA = qw/E/;
38850e2753Smillert    package E;
39850e2753Smillert    our @ISA = qw/D/;
40850e2753Smillert    package D;
41850e2753Smillert    our @ISA = qw/A B C/;
42850e2753Smillert    package C;
43850e2753Smillert    our @ISA = qw//;
44850e2753Smillert    package B;
45850e2753Smillert    our @ISA = qw//;
46850e2753Smillert    package A;
47850e2753Smillert    our @ISA = qw//;
48850e2753Smillert}
49850e2753Smillert
50898184e3Ssthen# A series of 8 aberations that would cause infinite loops,
51850e2753Smillert#  each one undoing the work of the previous
52850e2753Smillertmy @loopies = (
53850e2753Smillert    sub { @E::ISA = qw/F/ },
54850e2753Smillert    sub { @E::ISA = qw/D/; @C::ISA = qw/F/ },
55850e2753Smillert    sub { @C::ISA = qw//; @A::ISA = qw/K/ },
56850e2753Smillert    sub { @A::ISA = qw//; @J::ISA = qw/F K/ },
57850e2753Smillert    sub { @J::ISA = qw/F/; @H::ISA = qw/K G/ },
58850e2753Smillert    sub { @H::ISA = qw/G/; @B::ISA = qw/B/ },
59850e2753Smillert    sub { @B::ISA = qw//; @K::ISA = qw/K J I/ },
60850e2753Smillert    sub { @K::ISA = qw/J I/; @D::ISA = qw/A H B C/ },
61850e2753Smillert);
62850e2753Smillert
63850e2753Smillertforeach my $loopy (@loopies) {
64850e2753Smillert    eval {
65850e2753Smillert        local $SIG{ALRM} = sub { die "ALRMTimeout" };
66850e2753Smillert        alarm(3);
67850e2753Smillert        $loopy->();
68850e2753Smillert        mro::get_linear_isa('K', 'dfs');
69850e2753Smillert    };
70850e2753Smillert
71850e2753Smillert    if(my $err = $@) {
72850e2753Smillert        if($err =~ /ALRMTimeout/) {
73850e2753Smillert            ok(0, "Loop terminated by SIGALRM");
74850e2753Smillert        }
75850e2753Smillert        elsif($err =~ /Recursive inheritance detected/) {
76850e2753Smillert            ok(1, "Graceful exception thrown");
77850e2753Smillert        }
78850e2753Smillert        else {
79850e2753Smillert            ok(0, "Unrecognized exception: $err");
80850e2753Smillert        }
81850e2753Smillert    }
82850e2753Smillert    else {
83850e2753Smillert        ok(0, "Infinite loop apparently succeeded???");
84850e2753Smillert    }
85850e2753Smillert}
86