summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordoy <doy@tozt.net>2009-02-07 13:01:29 -0500
committerdoy <doy@tozt.net>2009-02-07 13:01:29 -0500
commitf6e75f5852fb8a7cecee8e3a313ea0a5d39700fd (patch)
tree79fd49a0e9d6938c982b539d0a2f5c08e119199a
parentcdf285acc0ca6883e7c189ccd472154f63687647 (diff)
downloadgraph-implicit-f6e75f5852fb8a7cecee8e3a313ea0a5d39700fd.tar.gz
graph-implicit-f6e75f5852fb8a7cecee8e3a313ea0a5d39700fd.zip
huh, didn't i add this before?
-rw-r--r--t/002-traversal.t71
1 files changed, 71 insertions, 0 deletions
diff --git a/t/002-traversal.t b/t/002-traversal.t
new file mode 100644
index 0000000..3bb5d19
--- /dev/null
+++ b/t/002-traversal.t
@@ -0,0 +1,71 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More tests => 48;
+use Test::Deep;
+use Graph::Implicit;
+
+sub is_tree {
+ my ($graph, $start) = @_;
+ # a tree is an acyclic graph with E=V-1
+ my $v = keys %$graph;
+ my $e = (grep { defined } values %$graph);
+
+ VERTEX: for my $vertex (keys %$graph) {
+ next unless defined $graph->{$vertex};
+ my %visited;
+ $visited{$vertex} = 1;
+ my $iter = $vertex;
+ while (1) {
+ $iter = $graph->{$iter};
+ next VERTEX if !defined $iter;
+ return 0 if $visited{$iter};
+ $visited{$iter} = 1;
+ }
+ }
+
+ return $e == $v - 1;
+}
+
+my %graph = (
+ a => [qw/ b c /],
+ b => [qw/ d f h/],
+ c => [qw/a e h/],
+ d => [qw/a b c d e f g h/],
+ e => [qw/ c d /],
+ f => [qw/ /],
+ g => [qw/ g /],
+ h => [qw/ f g /],
+);
+my %reachable = (
+ a => [qw/a b c d e f g h/],
+ b => [qw/a b c d e f g h/],
+ c => [qw/a b c d e f g h/],
+ d => [qw/a b c d e f g h/],
+ e => [qw/a b c d e f g h/],
+ f => [qw/ f /],
+ g => [qw/ g /],
+ h => [qw/ f g h/],
+);
+my $edge_calculator = sub {
+ my $vertex = shift;
+ return map { [$_, 1] } @{ $graph{$vertex} };
+};
+
+my $graph = Graph::Implicit->new($edge_calculator);
+for my $traversal (qw/bfs dfs/) {
+ for my $vertex (qw/a b c d e f g h/) {
+ my @visited;
+ my $tree = $graph->$traversal($vertex, sub { push @visited, $_[1] });
+ cmp_bag(\@visited, $reachable{$vertex},
+ "$traversal visits each node exactly once from $vertex");
+ ok(is_tree($tree),
+ "$traversal creates a tree from $vertex");
+ SKIP: {
+ skip "don't know a good algorithm for this", 1;
+ no strict 'refs';
+ ok(&{ "check_$traversal" }($tree),
+ "$traversal is in the correct order from $vertex");
+ }
+ }
+}