File: chain.t

package info (click to toggle)
libgraph-easy-perl 0.71-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,284 kB
  • sloc: perl: 24,909; makefile: 2
file content (114 lines) | stat: -rw-r--r-- 3,339 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl -w

use Test::More;
use strict;

BEGIN
   {
   plan tests => 44;
   chdir 't' if -d 't';
   use lib '../lib';
   use_ok ("Graph::Easy::Layout::Chain") or die($@);
   use_ok ("Graph::Easy") or die($@);
   };

can_ok ("Graph::Easy::Layout::Chain", qw/
  new error
  length nodes add_node layout
  /);

#############################################################################
# chain tests

my $c = 'Graph::Easy::Layout::Chain';

my $graph = Graph::Easy->new();
is (ref($graph), 'Graph::Easy');
is ($graph->error(), '', 'no error yet');

my ($node, $node2) = $graph->add_edge('A','B');

my $chain = Graph::Easy::Layout::Chain->new( 
  start => $node, graph => $graph );

is (ref($chain), $c, 'new() seemed to work');
is ($chain->error(), '', 'no error'); 
is ($chain->start(), $node, 'start node is $node');
is ($chain->end(), $node, 'end node is $node');

is ($node->{_chain}, $chain, 'chain stored at node');
is ($chain->length(), 1, 'length() is 1');
is ($chain->length($node), 1, 'length($node) is 1');

$chain->add_node($node2);

is ($node->{_chain}, $chain, 'chain stored at node');
is ($node2->{_chain}, $chain, 'chain stored at node2');
is ($chain->length(), 2, 'length() is now 2');
is ($chain->start(), $node, 'start node is $node');
is ($chain->end(), $node2, 'end node is $node2');
is ($chain->length($node), 2, 'length($node) is 2');
is ($chain->length($node2), 1, 'length($node2) is 1');


#############################################################################
# merging two chains

my ($node3, $node4) = $graph->add_edge('C','D');

my $other = $c->new ( start => $node3, graph => $graph );

is (ref($other), $c, 'new() seemed to work');
is ($other->error(), '', 'no error'); 
is ($other->length(), 1, 'length() is 1');
is ($other->start(), $node3, 'start node is $node3');
is ($other->end(), $node3, 'end node is $node3');

$other->add_node($node4);
is ($other->length(), 2, 'length() is now 2');
is ($other->start(), $node3, 'start node is $node3');
is ($other->end(), $node4, 'end node is $node4');

#diag ("merging chains\n");

$chain->merge($other);

is ($other->error(), '', 'no error'); 
is ($other->length(), 0, 'other length() is still 0');
is ($other->start(), undef, 'start node is $node3');
is ($other->end(), undef, 'end node is $node4');

is ($chain->error(), '', 'no error'); 
is ($chain->length(), 4, 'chain length() is now 4');
is ($chain->start(), $node, 'start node is $node3');
is ($chain->end(), $node4, 'end node is $node4');

my @nodes = $chain->nodes();

is_deeply (\@nodes, [ $node, $node2, $node3, $node4 ], 'nodes got merged');

#############################################################################
# merging two chains, with offset

my ($node5, $node6) = $graph->add_edge('E','F');

$other = $c->new ( start => $node5, graph => $graph );
$other->add_node($node6);

# merge $chain into $other, but keep the first 3 nodes of $chain

$other->merge($chain, $node3);

is ($chain->length(), 4, 'left all four nodes');
is ($other->length(), 4, 'consumed two nodes');

@nodes = $chain->nodes();
is_deeply (\@nodes, [ $node, $node2, $node3, $node4 ], 'nodes got merged');
@nodes = $other->nodes();
is_deeply (\@nodes, [ $node5, $node6, $node3, $node4 ], 'other got two nodes');

for my $node ( @nodes )
  {
  is ($node->{_chain}, $other, 'node got set to new chain');
  }