~noskcaj/ubuntu/vivid/libtree-perl/1.05

« back to all changes in this revision

Viewing changes to t/Tree/005_multilevel_tree.t

  • Committer: Bazaar Package Importer
  • Author(s): Javier Uruen Val
  • Date: 2008-01-17 12:56:58 UTC
  • Revision ID: james.westby@ubuntu.com-20080117125658-w87i4xot9a4azgbd
Tags: upstream-1.01
ImportĀ upstreamĀ versionĀ 1.01

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
use strict;
 
2
use warnings;
 
3
 
 
4
use Test::More;
 
5
 
 
6
use t::tests qw( %runs );
 
7
 
 
8
plan tests => 41 + 3 * $runs{stats}{plan};
 
9
 
 
10
my $CLASS = 'Tree';
 
11
use_ok( $CLASS )
 
12
    or Test::More->builder->BAILOUT( "Cannot load $CLASS" );
 
13
 
 
14
# Test Plan:
 
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
 
21
 
 
22
my $root = $CLASS->new;
 
23
isa_ok( $root, $CLASS );
 
24
 
 
25
my $child1 = $CLASS->new;
 
26
isa_ok( $child1, $CLASS );
 
27
 
 
28
my $child2 = $CLASS->new;
 
29
isa_ok( $child2, $CLASS );
 
30
 
 
31
$root->add_child( $child1 );
 
32
$child1->add_child( $child2 );
 
33
 
 
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" );
 
37
 
 
38
$runs{stats}{func}->( $root,
 
39
    height => 3, width => 1, depth => 0, size => 3, is_root => 1, is_leaf => 0,
 
40
);
 
41
 
 
42
$runs{stats}{func}->( $child1,
 
43
    height => 2, width => 1, depth => 1, size => 2, is_root => 0, is_leaf => 0,
 
44
);
 
45
 
 
46
$runs{stats}{func}->( $child2,
 
47
    height => 1, width => 1, depth => 2, size => 1, is_root => 0, is_leaf => 1,
 
48
);
 
49
 
 
50
is( $child1->root, $root, "The child1's root is the root" );
 
51
is( $child2->root, $root, "The child2's root is the root" );
 
52
 
 
53
$root->remove_child( $child1 );
 
54
 
 
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." );
 
58
 
 
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." );
 
62
 
 
63
is( $child1->root, $child1, "The child1's root is the child1" );
 
64
is( $child2->root, $child1, "The child2's root is the child1" );
 
65
 
 
66
$root->add_child( $child1 );
 
67
 
 
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." );
 
71
 
 
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." );
 
75
 
 
76
is( $child1->root, $root, "The child1's root is the root" );
 
77
is( $child2->root, $root, "The child2's root is the root" );
 
78
 
 
79
$child1->remove_child( $child2 );
 
80
 
 
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." );
 
84
 
 
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." );
 
88
 
 
89
is( $child1->root, $root, "The child1's root is the root" );
 
90
is( $child2->root, $child2, "The child2's root is the root" );
 
91
 
 
92
# Test 4-level trees and how root works
 
93
 
 
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] );
 
98
 
 
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" );
 
103
 
 
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" );