6
use t::tests qw( %runs );
8
plan tests => 41 + 3 * $runs{stats}{plan};
12
or Test::More->builder->BAILOUT( "Cannot load $CLASS" );
15
# 1) Add two children to a root node to make a 3-level tree.
16
# 2) Verify that all state is correctly reported
17
# 3) Remove the mid-level node
18
# 4) Verify that all state is correctly reported
19
# 5) Re-add the mid-level node
20
# 6) Verify that all state is correctly reported
22
my $root = $CLASS->new;
23
isa_ok( $root, $CLASS );
25
my $child1 = $CLASS->new;
26
isa_ok( $child1, $CLASS );
28
my $child2 = $CLASS->new;
29
isa_ok( $child2, $CLASS );
31
$root->add_child( $child1 );
32
$child1->add_child( $child2 );
34
cmp_ok( $root->children, '==', 1, "The root has one child" );
35
cmp_ok( $child1->children, '==', 1, "The child1 has one child" );
36
cmp_ok( $child2->children, '==', 0, "The child2 has zero children" );
38
$runs{stats}{func}->( $root,
39
height => 3, width => 1, depth => 0, size => 3, is_root => 1, is_leaf => 0,
42
$runs{stats}{func}->( $child1,
43
height => 2, width => 1, depth => 1, size => 2, is_root => 0, is_leaf => 0,
46
$runs{stats}{func}->( $child2,
47
height => 1, width => 1, depth => 2, size => 1, is_root => 0, is_leaf => 1,
50
is( $child1->root, $root, "The child1's root is the root" );
51
is( $child2->root, $root, "The child2's root is the root" );
53
$root->remove_child( $child1 );
55
cmp_ok( $root->height, '==', 1, "The root's height is one after removal." );
56
cmp_ok( $child1->height, '==', 2, "The child1's height is two." );
57
cmp_ok( $child2->height, '==', 1, "The child2's height is one." );
59
cmp_ok( $root->width, '==', 1, "The root's width is one." );
60
cmp_ok( $child1->width, '==', 1, "The child1's width is one." );
61
cmp_ok( $child2->width, '==', 1, "The child2's width is one." );
63
is( $child1->root, $child1, "The child1's root is the child1" );
64
is( $child2->root, $child1, "The child2's root is the child1" );
66
$root->add_child( $child1 );
68
cmp_ok( $root->height, '==', 3, "The root's height is three." );
69
cmp_ok( $child1->height, '==', 2, "The child1's height is two." );
70
cmp_ok( $child2->height, '==', 1, "The child2's height is one." );
72
cmp_ok( $root->width, '==', 1, "The root's width is one." );
73
cmp_ok( $child1->width, '==', 1, "The child1's width is one." );
74
cmp_ok( $child2->width, '==', 1, "The child2's width is one." );
76
is( $child1->root, $root, "The child1's root is the root" );
77
is( $child2->root, $root, "The child2's root is the root" );
79
$child1->remove_child( $child2 );
81
cmp_ok( $root->height, '==', 2, "The root's height is two." );
82
cmp_ok( $child1->height, '==', 1, "The child1's height is one." );
83
cmp_ok( $child2->height, '==', 1, "The child2's height is one." );
85
cmp_ok( $root->width, '==', 1, "The root's width is one." );
86
cmp_ok( $child1->width, '==', 1, "The child1's width is one." );
87
cmp_ok( $child2->width, '==', 1, "The child2's width is one." );
89
is( $child1->root, $root, "The child1's root is the root" );
90
is( $child2->root, $child2, "The child2's root is the root" );
92
# Test 4-level trees and how root works
94
my @nodes = map { $CLASS->new } 1 .. 4;
95
$nodes[2]->add_child( $nodes[3] );
96
$nodes[1]->add_child( $nodes[2] );
97
$nodes[0]->add_child( $nodes[1] );
99
is( $nodes[0]->root, $nodes[0], "The root is correct for level 0" );
100
is( $nodes[1]->root, $nodes[0], "The root is correct for level 1" );
101
is( $nodes[2]->root, $nodes[0], "The root is correct for level 2" );
102
is( $nodes[3]->root, $nodes[0], "The root is correct for level 3" );
104
$nodes[0]->remove_child( 0 );
105
is( $nodes[0]->root, $nodes[0], "The root is correct for level 0" );
106
is( $nodes[1]->root, $nodes[1], "The root is correct for level 1" );
107
is( $nodes[2]->root, $nodes[1], "The root is correct for level 2" );
108
is( $nodes[3]->root, $nodes[1], "The root is correct for level 3" );