b2910156b1a1e8bf0bb55565b368c4cdf671cfc5
[gitmo/Algorithm-C3.git] / t / 011_infinite_loop.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Algorithm::C3; # we already did use_ok 10 times by now..
8
9 plan skip_all => "Your system has no SIGALRM" if !exists $SIG{ALRM};
10 plan tests => 5;
11
12 =pod
13
14 These are like the 010_complex_merge_classless test,
15 but an infinite loop has been made in the heirarchy,
16 to test that we can fail cleanly instead of going
17 into an infinite loop
18
19 =cut
20
21 my @loopies = (
22     { #1
23         k => [qw(j i)],
24         j => [qw(f)],
25         i => [qw(h f)],
26         h => [qw(g)],
27         g => [qw(d)],
28         f => [qw(e)],
29         e => [qw(f)],
30         d => [qw(a b c)],
31         c => [],
32         b => [],
33         a => [],
34     },
35     { #2
36         k => [qw(j i)],
37         j => [qw(f)],
38         i => [qw(h f)],
39         h => [qw(g)],
40         g => [qw(d)],
41         f => [qw(e)],
42         e => [qw(d)],
43         d => [qw(a b c)],
44         c => [qw(f)],
45         b => [],
46         a => [],
47     },
48     { #3
49         k => [qw(j i)],
50         j => [qw(f)],
51         i => [qw(h f)],
52         h => [qw(g)],
53         g => [qw(d)],
54         f => [qw(e)],
55         e => [qw(d)],
56         d => [qw(a b c)],
57         c => [],
58         b => [],
59         a => [qw(k)],
60     },
61     { #4
62         k => [qw(j i)],
63         j => [qw(f k)],
64         i => [qw(h f)],
65         h => [qw(g)],
66         g => [qw(d)],
67         f => [qw(e)],
68         e => [qw(d)],
69         d => [qw(a b c)],
70         c => [],
71         b => [],
72         a => [],
73     },
74     { #5
75         k => [qw(j i)],
76         j => [qw(f)],
77         i => [qw(h f)],
78         h => [qw(k g)],
79         g => [qw(d)],
80         f => [qw(e)],
81         e => [qw(d)],
82         d => [qw(a b c)],
83         c => [],
84         b => [],
85         a => [],
86     },
87 );
88
89 foreach my $loopy (@loopies) {
90     eval {
91         local $SIG{ALRM} = sub { die "ALRMTimeout" };
92         alarm(3);
93         Algorithm::C3::merge('k', sub {
94             return @{ $loopy->{ $_[0] } };
95         });
96     };
97
98     if(my $err = $@) {
99         if($err =~ /ALRMTimeout/) {
100             ok(0, "Loop terminated by SIGALRM");
101         }
102         elsif($err =~ /Infinite loop detected/) {
103             ok(1, "Graceful exception thrown");
104         }
105         else {
106             ok(0, "Unrecognized exception: $err");
107         }
108     }
109     else {
110         ok(0, "Infinite loop apparently succeeded???");
111     }
112 }