use strict; use warnings FATAL => 'all'; use Test::Exception; use Test::More tests => 12; use_ok('Algorithm::Graph'); my %graphs = ( '1,2' => [ [ 1, 2 ] ], 'a|b|c|d' => [ [qw(a a)], [qw(b b)], [qw(c c)], [qw(d d)] ], '1,2,3,4,99|a,b,c,d,e,f,g,z' => [ [qw(a b)], [qw(b c)], [qw(d c)], [qw(d e)], [qw(d f)], [qw(g d)], [qw(z g)], [ 1, 2 ], [ 3, 2 ], [ 4, 3 ], [ 99, 1 ] ], '1,2,3,4,99|apple,kiwi,pear|a,b,c,d,e,f,g,z' => [ [qw(a b)], [qw(b c)], [qw(d c)], [qw(d e)], [qw(d f)], [qw(g d)], [qw(z g)], [ 1, 2 ], [ 3, 2 ], [ 4, 3 ], [ 99, 1 ], [ 'apple', 'pear' ], [ 'apple', 'kiwi' ] ], ); foreach my $result ( keys %graphs ) { my $cc = Algorithm::Graph::connected_components( $graphs{$result} ); my $cc2 = join( "|", map { join( ",", @$_ ) } @$cc ); is( $cc2, $result, $cc2 ); } throws_ok { Algorithm::Graph::connected_components() } qr/arrayref/, 'empty'; throws_ok { Algorithm::Graph::connected_components('fish') } qr/arrayref/, 'fish'; throws_ok { Algorithm::Graph::connected_components( {} ) } qr/arrayref/, 'hash'; throws_ok { Algorithm::Graph::connected_components( [] ) } qr/empty/, '[]'; throws_ok { Algorithm::Graph::connected_components( [ [] ] ) } qr/arrayref/, '[[]]'; throws_ok { Algorithm::Graph::connected_components( [ [ 1, 2, 3 ] ] ) } qr/2-elem/, '[[1,2,3]]'; throws_ok { Algorithm::Graph::connected_components( [ [ 1, 2 ], [] ] ) } qr/2-elem/, '[[1,2],[]]';