File: layout.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 (190 lines) | stat: -rw-r--r-- 5,144 bytes parent folder | download | duplicates (4)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/perl -w

use Test::More;
use strict;

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

can_ok ("Graph::Easy", qw/
  _trace_path
  _find_path
  _create_cell
  _path_is_clear
  _clear_tries
  _find_path_astar
  _find_path_loop

  _find_chains
  _assign_ranks
  /);

can_ok ("Graph::Easy::Node", qw/
  _shuffle_dir
  /);

isnt ($Graph::Easy::VERSION, undef, 'VERSION in Layout');

use Graph::Easy;

Graph::Easy::Edge::Cell->import (qw/
  EDGE_HOR EDGE_VER EDGE_LABEL_CELL
  EDGE_SHORT_S
  EDGE_END_S
  EDGE_START_N
/);

#############################################################################
# layout tests

my $graph = Graph::Easy->new();

is (ref($graph), 'Graph::Easy');

is ($graph->error(), '', 'no error yet');

my ($src, $dst, $edge) = $graph->add_edge('Bonn','Berlin');

my $e = 3;				# elements per path cell (x,y,type)

#############################################################################
# _shuffle_dir()

my $array = [0,1,2,3];

is (join (",",@{ $src->_shuffle_dir($array,0)   }), '3,0,2,1', 'shuffle 0'  );
is (join (",",@{ $src->_shuffle_dir($array,90)  }), '0,1,2,3', 'shuffle 90' );
is (join (",",@{ $src->_shuffle_dir($array)     }), '0,1,2,3', 'shuffle '   );
is (join (",",@{ $src->_shuffle_dir($array,270) }), '2,3,1,0', 'shuffle 270');
is (join (",",@{ $src->_shuffle_dir($array,180) }), '1,2,0,3', 'shuffle 180');

#############################################################################
# _near_places()

$src->{x} = 1; $src->{y} = 1;

my $cells = {};
my @places = $src->_near_places($cells);
is (scalar @places, 4 * 2, '4 places');

@places = $src->_near_places($cells,2);		# $d == 2
is (scalar @places, 4 * 2, '4 places');

@places = $src->_near_places($cells,3);		# $d == 3
is (scalar @places, 4 * 2, '4 places');

@places = $src->_near_places($cells,3,0);	# $d == 3, type is 0
is (scalar @places, 4 * $e, '4 places');

#                        #1+3,1+0,1 ...
is (join (',', @places), '4,1,16,1,4,32,-2,1,64,1,-2,128', 'places');

#############################################################################
# _find_path()

$src->{x} = 1; $src->{y} = 1;
$dst->{x} = 1; $dst->{y} = 1;

my $coords = $graph->_find_path( $src, $dst, $edge);

is (scalar @$coords, 1*$e, 'same cell => short edge path');

$src->{x} = 1; $src->{y} = 1;
$dst->{x} = 2; $dst->{y} = 2;

$coords = $graph->_find_path( $src, $dst, $edge);

#print STDERR "# " . Dumper($coords) . "\n";
#print STDERR "# " . Dumper($graph->{cells}) . "\n";

is (scalar @$coords, 1*$e, 'path with a bend');

# mark one cell as already occupied
$graph->{cells}->{"1,2"} = $src;

$src->{x} = 1; $src->{y} = 1;
$dst->{x} = 1; $dst->{y} = 3;

$coords = $graph->_find_path( $src, $dst, $edge);

#print STDERR "# " . Dumper($coords) . "\n";
#print STDERR "# " . Dumper($graph->{cells}) . "\n";

is (scalar @$coords, 3*$e, 'u shaped path (|---^)');

# block src over/under to avoid an U-shaped path
$graph->{cells}->{"2,1"} = $src;
$graph->{cells}->{"0,1"} = $src;

$graph->{cache} = {};
$coords = $graph->_find_path( $src, $dst, $edge);

#print STDERR "# " . Dumper($coords) . "\n";

# XXX TODO: check what path is actually generated here
is (scalar @$coords, 7*$e, 'cell already blocked');

delete $graph->{cells}->{"1,2"};

$coords = $graph->_find_path( $src, $dst, $edge);

is (scalar @$coords, 1*$e, 'straight path down');
is (join (":", @$coords), '1:2:' . (EDGE_SHORT_S() + EDGE_LABEL_CELL()), 'path 1,1 => 1,3');

$src->{x} = 1; $src->{y} = 0;
$dst->{x} = 1; $dst->{y} = 5;

$coords = $graph->_find_path( $src, $dst, $edge);

is (scalar @$coords, 4*$e, 'straight path down');
my $type = EDGE_VER();
my $type_label = EDGE_VER() + EDGE_LABEL_CELL() + EDGE_START_N();
my $type_end = EDGE_VER() + EDGE_END_S();
is (join (":", @$coords), "1:1:$type_label:1:2:$type:1:3:$type:1:4:$type_end", 'path 1,0 => 1,5');

#############################################################################
#############################################################################

# as_ascii() will load Graph::Easy::Layout::Grid, this provides some
# additional methods:

my $ascii = $graph->as_ascii();

can_ok ("Graph::Easy", qw/
  _balance_sizes
  _prepare_layout
  / );

#############################################################################
# _balance_sizes

my $sizes = [ 3, 4, 5 ];

$graph->_balance_sizes( $sizes, 3+4+5);

is_deeply ( $sizes, [ 3,4,5 ], 'constraint already met');

$graph->_balance_sizes( $sizes = [ 3, 4, 5 ], 3+4+5-1);
is_deeply ( $sizes, [ 3,4,5 ], 'constraint already met');

$graph->_balance_sizes( $sizes = [ 3, 4, 5 ], 3+4+5+1);
is_deeply ( $sizes, [ 4,4,5 ], 'smallest gets bigger');

$graph->_balance_sizes( $sizes = [ 3, 3, 3 ], 3*3 + 2);
is_deeply ( $sizes, [ 4,4,3 ], 'first two smallest get bigger');

$graph->_balance_sizes( $sizes = [ 3, 3, 3 ], 3*3 + 3);
is_deeply ( $sizes, [ 4,4,4 ], 'all got bigger');

$graph->_balance_sizes( $sizes = [ 3, 3, 3 ], 3*3 + 4);
is_deeply ( $sizes, [ 5,4,4 ], 'all got bigger');

$graph->_balance_sizes( $sizes = [ 10, 10, 3 ], 20+7);
is_deeply ( $sizes, [ 10,10,7 ], 'last got bigger');