File: 79_unionfind.t

package info (click to toggle)
libgraph-perl 1%3A0.9726-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 996 kB
  • sloc: perl: 4,083; sh: 8; makefile: 2
file content (50 lines) | stat: -rw-r--r-- 1,041 bytes parent folder | download | duplicates (2)
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
use strict; use warnings;
use Test::More;

use Graph::UnionFind;

my $uf = Graph::UnionFind->new;

is_deeply [$uf->find('a')], [undef];
$uf->add('a');
is_deeply [$uf->find('a')], ['a'];
$uf->add('b');
is_deeply [$uf->find('a')], ['a'];
is_deeply [$uf->find('b')], ['b'];

$uf->union(['a', 'b']); # http://rt.cpan.org/NoAuth/Bug.html?id=2627

is_deeply [$uf->find('a')], ['b'];
is_deeply [$uf->find('b')], ['b'];

$uf->union(['c', 'd']);

is_deeply [$uf->find('c')], ['d'];
is_deeply [$uf->find('d')], ['d'];

is_deeply [$uf->find('e')], [undef];

ok( $uf->same('a', 'b'));
ok( $uf->same('b', 'a'));
ok( $uf->same('c', 'd'));
ok(!$uf->same('a', 'c'));

$uf->union(['a', 'd']);
ok( $uf->same('a', 'c'));

ok(!$uf->same('c', 'e'));

# rt.cpan.org #39805: UnionFind: Repeated adds clobbers graph component information
my $graph = Graph::UnionFind->new;
$graph->add('a');
$graph->union(['a','b']);

ok($graph->same('a', 'b'));
ok($graph->same('b', 'a'));

$graph->add('a');

ok($graph->same('a', 'b'));
ok($graph->same('b', 'a'));

done_testing;