File: 010_Tree.t

package info (click to toggle)
libforest-perl 0.10-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 344 kB
  • sloc: perl: 3,257; makefile: 2
file content (164 lines) | stat: -rw-r--r-- 5,707 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 89;
use Test::Exception;

BEGIN {
    use_ok('Forest::Tree');
};

my $t = Forest::Tree->new();
isa_ok($t, 'Forest::Tree');

ok($t->is_root, '... this is the tree root');
ok($t->is_leaf, '... this is the leaf');

ok(!defined $t->parent, '... no parent');
ok(!$t->has_parent, '... no parent');
ok(!defined $t->node, '... no node value');
is_deeply($t->children, [], '... no children');
is($t->depth, -1, '... the root has a depth of -1');
is($t->height, 0, '... the root has a height of 0');
is($t->size,   1, '... the root has a size of 1');

my $child_1 = Forest::Tree->new(node => '1.0');
isa_ok($child_1, 'Forest::Tree');

ok(!defined $child_1->parent, '... no parent');
ok(!$child_1->has_parent, '... no parent');
ok($child_1->is_leaf, '... this is a leaf');
ok($child_1->is_root, '... this is a root');
is($child_1->node, '1.0', '... got the right node value');
is($child_1->depth, -1, '... the child has a depth of -1');
is_deeply($child_1->children, [], '... no children');

$t->add_child($child_1);

ok(!$t->is_leaf, '... this is no longer leaf');
is_deeply($t->children, [ $child_1 ], '... 1 child');
is($t->depth, -1, '... the root still has a depth of -1');
is($t->height, 1, '... the root now has a height of 1');
is($t->size,   2, '... the root now has a size of 2');
is($t->get_child_at(0), $child_1, '... got the right child');

ok(!$child_1->is_root, '... this is no longer a root');
ok($child_1->is_leaf, '... but this is still a leaf');
ok(defined $child_1->parent, '... has parent now');
ok($child_1->has_parent, '... has parent now');
isa_ok($child_1->parent, 'Forest::Tree');
is($child_1->parent, $t, '... its parent is tree');
is($child_1->depth, 0, '... the child now has a depth of 0');
is_deeply($child_1->siblings, [], '... There are no siblings');

my $child_1_1 = Forest::Tree->new(node => '1.1');
isa_ok($child_1_1, 'Forest::Tree');

ok(!defined $child_1_1->parent, '... no parent');
ok(!$child_1_1->has_parent, '... no parent');
ok($child_1_1->is_leaf, '... this is a leaf');
ok($child_1_1->is_root, '... this is a root');
is($child_1_1->node, '1.1', '... got the right node value');
is($child_1_1->depth, -1, '... the child has a depth of -1');
is_deeply($child_1_1->children, [], '... no children');

$t->get_child_at(0)->add_child($child_1_1);

is_deeply($child_1->children, [ $child_1_1 ], '... one child');

ok(!$child_1->is_leaf, '... this is no longer a leaf');
is($child_1->depth, 0, '... the child still has a depth of 0');

ok(!$child_1_1->is_root, '... this is no longer a root');
ok($child_1_1->is_leaf, '... but this is still a leaf');
ok(defined $child_1_1->parent, '... has parent now');
ok($child_1_1->has_parent, '... has parent now');
isa_ok($child_1_1->parent, 'Forest::Tree');
is($child_1_1->parent, $child_1, '... its parent is tree');
is($child_1_1->depth, 1, '... the child now has a depth of 1');
is($t->height, 2, '... the root now has a height of 2');
is($t->size,   3, '... the root now has a size of 3');

my $child_2 = Forest::Tree->new(node => '2.0');
isa_ok($child_2, 'Forest::Tree');

my $child_3 = Forest::Tree->new(node => '3.0');
isa_ok($child_3, 'Forest::Tree');

my $child_4 = Forest::Tree->new(node => '4.0');
isa_ok($child_4, 'Forest::Tree');

$child_1->add_sibling($child_4);

is_deeply($child_1->siblings, [ $child_4 ], '... There are no siblings');

is_deeply($t->children, [ $child_1, $child_4 ], '... 2 children');

ok(!$child_4->is_root, '... this is no longer a root');
ok($child_4->is_leaf, '... but this is still a leaf');
is($child_4->parent, $t, '... its parent is tree');
is($child_4->depth, 0, '... the child now has a depth of 1');

$t->insert_child_at(1, $child_2);

is_deeply($t->children, [ $child_1, $child_2, $child_4 ], '... 3 children');

ok(!$child_2->is_root, '... this is no longer a root');
ok($child_2->is_leaf, '... but this is still a leaf');
is($child_2->parent, $t, '... its parent is tree');
is($child_2->depth, 0, '... the child now has a depth of 1');

$child_2->insert_sibling_at(2, $child_3);

is_deeply($t->children, [ $child_1, $child_2, $child_3, $child_4 ], '... 4 children');

ok(!$child_3->is_root, '... this is no longer a root');
ok($child_3->is_leaf, '... but this is still a leaf');
is($child_3->parent, $t, '... its parent is tree');
is($child_3->depth, 0, '... the child now has a depth of 1');
is($t->height, 2, '... the root now has a height of 2');
is($t->size,   6, '... the root now has a size of 6');

ok($t->remove_child_at(0), '... removing child 1');
is($t->height, 1, '... the root now has a height of 1');
is($t->size,   4, '... the root now has a size of 4');

# clear them ...
$t->clear_size;
$t->clear_height;

# regenerate ...

ok($t->remove_child_at(0), '... removing child 1');

is($t->height, 1, '... the root now has a height of 1');
is($t->size,   3, '... the root now has a size of 3');



my $child_5 = Forest::Tree->new(node => '5.0');
my $child_6 = Forest::Tree->new(node => '6.0');
my $child_7 = Forest::Tree->new(node => '7.0');

$t->transform( [ 1 ], insert_child_at => 0, $child_5 );

is($t->height, 2, '... the root now has a height of 1');
is($t->size,   4, '... the root now has a size of 3');

is_deeply( $t->locate(1, 0), $child_5, "locate new child" );

$t->transform( [ 1, 0 ], add_child => $child_6 );

is($t->height, 3, '... the root now has a height of 1');
is($t->size,   5, '... the root now has a size of 3');

is( $t->locate(1, 0, 0)->node, '6.0', "correct node" );

$t->transform( [ 1, 0 ], replace => $child_7 );

is($t->height, 2, '... the root now has a height of 1');
is($t->size,   4, '... the root now has a size of 3');

is( $t->locate(1, 0)->node, "7.0", "correct node" );