File: 011_infinite_loop.t

package info (click to toggle)
libalgorithm-c3-perl 0.11-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 180 kB
  • sloc: perl: 88; makefile: 2
file content (149 lines) | stat: -rw-r--r-- 2,992 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use strict;
use warnings;

use Test::More;
use Algorithm::C3;

plan skip_all => "Your system has no SIGALRM" if !exists $SIG{ALRM};
plan tests => 8;

=pod

These are like the 010_complex_merge_classless test,
but an infinite loop has been made in the heirarchy,
to test that we can fail cleanly instead of going
into an infinite loop

=cut

my @loopies = (
    { #1
        k => [qw(j i)],
        j => [qw(f)],
        i => [qw(h f)],
        h => [qw(g)],
        g => [qw(d)],
        f => [qw(e)],
        e => [qw(f)],
        d => [qw(a b c)],
        c => [],
        b => [],
        a => [],
    },
    { #2
        k => [qw(j i)],
        j => [qw(f)],
        i => [qw(h f)],
        h => [qw(g)],
        g => [qw(d)],
        f => [qw(e)],
        e => [qw(d)],
        d => [qw(a b c)],
        c => [qw(f)],
        b => [],
        a => [],
    },
    { #3
        k => [qw(j i)],
        j => [qw(f)],
        i => [qw(h f)],
        h => [qw(g)],
        g => [qw(d)],
        f => [qw(e)],
        e => [qw(d)],
        d => [qw(a b c)],
        c => [],
        b => [],
        a => [qw(k)],
    },
    { #4
        k => [qw(j i)],
        j => [qw(f k)],
        i => [qw(h f)],
        h => [qw(g)],
        g => [qw(d)],
        f => [qw(e)],
        e => [qw(d)],
        d => [qw(a b c)],
        c => [],
        b => [],
        a => [],
    },
    { #5
        k => [qw(j i)],
        j => [qw(f)],
        i => [qw(h f)],
        h => [qw(k g)],
        g => [qw(d)],
        f => [qw(e)],
        e => [qw(d)],
        d => [qw(a b c)],
        c => [],
        b => [],
        a => [],
    },
    { #6
        k => [qw(j i)],
        j => [qw(f)],
        i => [qw(h f)],
        h => [qw(g)],
        g => [qw(d)],
        f => [qw(e)],
        e => [qw(d)],
        d => [qw(a b c)],
        c => [],
        b => [qw(b)],
        a => [],
    },
    { #7
        k => [qw(k j i)],
        j => [qw(f)],
        i => [qw(h f)],
        h => [qw(g)],
        g => [qw(d)],
        f => [qw(e)],
        e => [qw(d)],
        d => [qw(a b c)],
        c => [],
        b => [],
        a => [],
    },
    { #7
        k => [qw(j i)],
        j => [qw(f)],
        i => [qw(h f)],
        h => [qw(g)],
        g => [qw(d)],
        f => [qw(e)],
        e => [qw(d)],
        d => [qw(a h b c)],
        c => [],
        b => [],
        a => [],
    },
);

foreach my $loopy (@loopies) {
    eval {
        local $SIG{ALRM} = sub { die "ALRMTimeout" };
        alarm(3);
        Algorithm::C3::merge('k', sub {
            return @{ $loopy->{ $_[0] } };
        });
    };

    if(my $err = $@) {
        if($err =~ /ALRMTimeout/) {
            ok(0, "Loop terminated by SIGALRM");
        }
        elsif($err =~ /Infinite loop detected/) {
            ok(1, "Graceful exception thrown");
        }
        else {
            ok(0, "Unrecognized exception: $err");
        }
    }
    else {
        ok(0, "Infinite loop apparently succeeded???");
    }
}