24f4e76d9ae3ef4a2adffd85e4a11777acf86cf8
[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 tests => 1;
7
8 BEGIN {
9     use_ok('Algorithm::C3');
10 }
11
12 =pod
13
14 This is 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 $foo = {
22   k => [qw(j i)],
23   j => [qw(f)],
24   i => [qw(h f)],
25   h => [qw(g)],
26   g => [qw(d)],
27   f => [qw(e)],
28   e => [qw(f)],
29   d => [qw(a b c)],
30   c => [],
31   b => [],
32   a => [],
33 };
34
35 sub supers {
36   return @{ $foo->{ $_[0] } };
37 }
38
39 eval {
40     local $SIG{ALRM} = sub { die "ALRMTimeout" };
41     alarm(3);
42     Algorithm::C3::merge('k', \&supers);
43 };
44
45 if(my $err = $@) {
46     if($err =~ /ALRMTimeout/) {
47         ok(0, "Loop terminated by SIGALRM");
48     }
49     elsif($err =~ /Infinite loop detected/) {
50         ok(1, "Graceful exception thrown");
51     }
52     else {
53         ok(0, "Unrecognized exception: $err");
54     }
55 }
56 else {
57     ok(0, "Infinite loop apparently succeeded???");
58 }