1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 77;
7
8BEGIN { 
9	use_ok('Tree::Simple'); 
10};
11
12
13{ # test height (with pictures)
14    
15    my $tree = Tree::Simple->new();
16    isa_ok($tree, 'Tree::Simple');
17    
18    my $D = Tree::Simple->new('D');
19    isa_ok($D, 'Tree::Simple');
20    
21    $tree->addChild($D);
22    
23    #   |
24    #  <D>
25    
26    cmp_ok($D->getWidth(), '==', 1, '... D has a width of 1');
27    
28    my $E = Tree::Simple->new('E');
29    isa_ok($E, 'Tree::Simple');
30    
31    $D->addChild($E);
32    
33    #   |
34    #  <D>
35    #    \
36    #    <E>
37    
38    cmp_ok($D->getWidth(), '==', 1, '... D has a width of 1');
39    cmp_ok($E->getWidth(), '==', 1, '... E has a width of 1');
40    
41    my $F = Tree::Simple->new('F');
42    isa_ok($F, 'Tree::Simple');
43    
44    $E->addChild($F);
45    
46    #   |
47    #  <D>
48    #    \
49    #    <E>
50    #      \
51    #      <F>
52    
53    cmp_ok($D->getWidth(), '==', 1, '... D has a width of 1');
54    cmp_ok($E->getWidth(), '==', 1, '... E has a width of 1');
55    cmp_ok($F->getWidth(), '==', 1, '... F has a width of 1');
56    
57    my $C = Tree::Simple->new('C');
58    isa_ok($C, 'Tree::Simple');
59    
60    $D->addChild($C);
61    
62    #    |
63    #   <D>
64    #   / \
65    # <C> <E>
66    #       \
67    #       <F>
68    
69    cmp_ok($D->getWidth(), '==', 2, '... D has a width of 2');
70    cmp_ok($E->getWidth(), '==', 1, '... E has a width of 1');
71    cmp_ok($F->getWidth(), '==', 1, '... F has a width of 1');
72    cmp_ok($C->getWidth(), '==', 1, '... C has a width of 1');
73    
74    my $B = Tree::Simple->new('B');
75    isa_ok($B, 'Tree::Simple');
76    
77    $D->addChild($B);
78    
79    #        |
80    #       <D>
81    #      / | \
82    #   <B> <C> <E>
83    #             \
84    #             <F>
85    
86    
87    cmp_ok($D->getWidth(), '==', 3, '... D has a width of 3');
88    cmp_ok($E->getWidth(), '==', 1, '... E has a width of 1');
89    cmp_ok($F->getWidth(), '==', 1, '... F has a width of 1');
90    cmp_ok($C->getWidth(), '==', 1, '... C has a width of 1');
91    cmp_ok($B->getWidth(), '==', 1, '... B has a width of 1');
92        
93    
94    my $A = Tree::Simple->new('A');
95    isa_ok($A, 'Tree::Simple');
96    
97    $E->addChild($A);
98    
99    #        |
100    #       <D>
101    #      / | \
102    #   <B> <C> <E>
103    #           / \
104    #         <A> <F>       
105    
106    cmp_ok($D->getWidth(), '==', 4, '... D has a width of 4');
107    cmp_ok($E->getWidth(), '==', 2, '... E has a width of 2');
108    cmp_ok($F->getWidth(), '==', 1, '... F has a width of 1');
109    cmp_ok($C->getWidth(), '==', 1, '... C has a width of 1');
110    cmp_ok($B->getWidth(), '==', 1, '... B has a width of 1');
111    cmp_ok($A->getWidth(), '==', 1, '... A has a width of 1');
112    
113    my $G = Tree::Simple->new('G');
114    isa_ok($G, 'Tree::Simple');
115    
116    $E->insertChild(1, $G);
117    
118    #        |
119    #       <D>
120    #      / | \
121    #   <B> <C> <E>
122    #          / | \
123    #       <A> <G> <F>         
124    
125    cmp_ok($D->getWidth(), '==', 5, '... D has a width of 5');
126    cmp_ok($E->getWidth(), '==', 3, '... E has a width of 3');
127    cmp_ok($F->getWidth(), '==', 1, '... F has a width of 1');
128    cmp_ok($G->getWidth(), '==', 1, '... G has a width of 1');
129    cmp_ok($C->getWidth(), '==', 1, '... C has a width of 1');
130    cmp_ok($B->getWidth(), '==', 1, '... B has a width of 1');
131    cmp_ok($A->getWidth(), '==', 1, '... A has a width of 1');
132    
133    my $H = Tree::Simple->new('H');
134    isa_ok($H, 'Tree::Simple');
135    
136    $G->addChild($H);
137    
138    #        |
139    #       <D>
140    #      / | \
141    #   <B> <C> <E>
142    #          / | \
143    #       <A> <G> <F> 
144    #            |
145    #           <H>    
146    
147    cmp_ok($D->getWidth(), '==', 5, '... D has a width of 5');
148    cmp_ok($E->getWidth(), '==', 3, '... E has a width of 3');
149    cmp_ok($F->getWidth(), '==', 1, '... F has a width of 1');
150    cmp_ok($G->getWidth(), '==', 1, '... G has a width of 1');
151    cmp_ok($H->getWidth(), '==', 1, '... H has a width of 1');
152    cmp_ok($C->getWidth(), '==', 1, '... C has a width of 1');
153    cmp_ok($B->getWidth(), '==', 1, '... B has a width of 1');
154    cmp_ok($A->getWidth(), '==', 1, '... A has a width of 1');
155    
156    my $I = Tree::Simple->new('I');
157    isa_ok($I, 'Tree::Simple');
158    
159    $G->addChild($I);
160    
161    #        |
162    #       <D>
163    #      / | \
164    #   <B> <C> <E>
165    #          / | \
166    #       <A> <G> <F> 
167    #            | \
168    #           <H> <I>   
169    
170    cmp_ok($D->getWidth(), '==', 6, '... D has a width of 6');
171    cmp_ok($E->getWidth(), '==', 4, '... E has a width of 4');
172    cmp_ok($F->getWidth(), '==', 1, '... F has a width of 1');
173    cmp_ok($G->getWidth(), '==', 2, '... G has a width of 2');
174    cmp_ok($H->getWidth(), '==', 1, '... H has a width of 1');
175    cmp_ok($I->getWidth(), '==', 1, '... I has a width of 1');    
176    cmp_ok($C->getWidth(), '==', 1, '... C has a width of 1');
177    cmp_ok($B->getWidth(), '==', 1, '... B has a width of 1');
178    cmp_ok($A->getWidth(), '==', 1, '... A has a width of 1');      
179
180    ok($E->removeChild($A), '... removed A subtree from B tree');
181
182    #        |
183    #       <D>
184    #      / | \
185    #   <B> <C> <E>
186    #            | \
187    #           <G> <F> 
188    #            | \
189    #           <H> <I>  
190
191    cmp_ok($D->getWidth(), '==', 5, '... D has a width of 5');
192    cmp_ok($E->getWidth(), '==', 3, '... E has a width of 3');
193    cmp_ok($F->getWidth(), '==', 1, '... F has a width of 1');
194    cmp_ok($G->getWidth(), '==', 2, '... G has a width of 2');
195    cmp_ok($H->getWidth(), '==', 1, '... H has a width of 1');
196    cmp_ok($C->getWidth(), '==', 1, '... C has a width of 2');
197    cmp_ok($B->getWidth(), '==', 1, '... B has a width of 1');
198    
199    # and the removed tree is ok
200    cmp_ok($A->getWidth(), '==', 1, '... A has a width of 1');
201    
202    ok($D->removeChild($E), '... removed E subtree from D tree');
203
204    #        |
205    #       <D>
206    #      / | 
207    #   <B> <C>
208
209    cmp_ok($D->getWidth(), '==', 2, '... D has a width of 2');
210    cmp_ok($C->getWidth(), '==', 1, '... C has a width of 1');
211    cmp_ok($B->getWidth(), '==', 1, '... B has a width of 1');
212    
213    # and the removed trees are ok
214    cmp_ok($E->getWidth(), '==', 3, '... E has a width of 3');
215    cmp_ok($F->getWidth(), '==', 1, '... F has a width of 1');
216    cmp_ok($G->getWidth(), '==', 2, '... G has a width of 2');
217    cmp_ok($H->getWidth(), '==', 1, '... H has a width of 1');    
218    
219    ok($D->removeChild($C), '... removed C subtree from D tree');
220
221    #        |
222    #       <D>
223    #      /  
224    #   <B> 
225
226    cmp_ok($D->getWidth(), '==', 1, '... D has a width of 1');
227    cmp_ok($B->getWidth(), '==', 1, '... B has a width of 1');
228    
229    # and the removed tree is ok
230    cmp_ok($C->getWidth(), '==', 1, '... C has a width of 1');
231      
232}
233