1#!/usr/bin/perl 2 3use strict; 4use warnings; 5 6use Test::More tests => 67; 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->getHeight(), '==', 1, '... D has a height 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->getHeight(), '==', 2, '... D has a height of 2'); 39 cmp_ok($E->getHeight(), '==', 1, '... E has a height 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->getHeight(), '==', 3, '... D has a height of 3'); 54 cmp_ok($E->getHeight(), '==', 2, '... E has a height of 2'); 55 cmp_ok($F->getHeight(), '==', 1, '... F has a height 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->getHeight(), '==', 3, '... D has a height of 3'); 70 cmp_ok($E->getHeight(), '==', 2, '... E has a height of 2'); 71 cmp_ok($F->getHeight(), '==', 1, '... F has a height of 1'); 72 cmp_ok($C->getHeight(), '==', 1, '... C has a height of 1'); 73 74 my $B = Tree::Simple->new('B'); 75 isa_ok($B, 'Tree::Simple'); 76 77 $C->addChild($B); 78 79 # | 80 # <D> 81 # / \ 82 # <C> <E> 83 # / \ 84 # <B> <F> 85 86 87 cmp_ok($D->getHeight(), '==', 3, '... D has a height of 3'); 88 cmp_ok($E->getHeight(), '==', 2, '... E has a height of 2'); 89 cmp_ok($F->getHeight(), '==', 1, '... F has a height of 1'); 90 cmp_ok($C->getHeight(), '==', 2, '... C has a height of 2'); 91 cmp_ok($B->getHeight(), '==', 1, '... B has a height of 1'); 92 93 my $A = Tree::Simple->new('A'); 94 isa_ok($A, 'Tree::Simple'); 95 96 $B->addChild($A); 97 98 # | 99 # <D> 100 # / \ 101 # <C> <E> 102 # / \ 103 # <B> <F> 104 # / 105 # <A> 106 107 cmp_ok($D->getHeight(), '==', 4, '... D has a height of 4'); 108 cmp_ok($E->getHeight(), '==', 2, '... E has a height of 2'); 109 cmp_ok($F->getHeight(), '==', 1, '... F has a height of 1'); 110 cmp_ok($C->getHeight(), '==', 3, '... C has a height of 3'); 111 cmp_ok($B->getHeight(), '==', 2, '... B has a height of 2'); 112 cmp_ok($A->getHeight(), '==', 1, '... A has a height of 1'); 113 114 my $G = Tree::Simple->new('G'); 115 isa_ok($G, 'Tree::Simple'); 116 117 $E->insertChild(0, $G); 118 119 # | 120 # <D> 121 # / \ 122 # <C> <E> 123 # / / \ 124 # <B> <G> <F> 125 # / 126 # <A> 127 128 cmp_ok($D->getHeight(), '==', 4, '... D has a height of 4'); 129 cmp_ok($E->getHeight(), '==', 2, '... E has a height of 2'); 130 cmp_ok($F->getHeight(), '==', 1, '... F has a height of 1'); 131 cmp_ok($G->getHeight(), '==', 1, '... G has a height of 1'); 132 cmp_ok($C->getHeight(), '==', 3, '... C has a height of 3'); 133 cmp_ok($B->getHeight(), '==', 2, '... B has a height of 2'); 134 cmp_ok($A->getHeight(), '==', 1, '... A has a height of 1'); 135 136 my $H = Tree::Simple->new('H'); 137 isa_ok($H, 'Tree::Simple'); 138 139 $G->addChild($H); 140 141 # | 142 # <D> 143 # / \ 144 # <C> <E> 145 # / / \ 146 # <B> <G> <F> 147 # / \ 148 # <A> <H> 149 150 cmp_ok($D->getHeight(), '==', 4, '... D has a height of 4'); 151 cmp_ok($E->getHeight(), '==', 3, '... E has a height of 3'); 152 cmp_ok($F->getHeight(), '==', 1, '... F has a height of 1'); 153 cmp_ok($G->getHeight(), '==', 2, '... G has a height of 2'); 154 cmp_ok($H->getHeight(), '==', 1, '... H has a height of 1'); 155 cmp_ok($C->getHeight(), '==', 3, '... C has a height of 3'); 156 cmp_ok($B->getHeight(), '==', 2, '... B has a height of 2'); 157 cmp_ok($A->getHeight(), '==', 1, '... A has a height of 1'); 158 159 ok($B->removeChild($A), '... removed A subtree from B tree'); 160 161 # | 162 # <D> 163 # / \ 164 # <C> <E> 165 # / / \ 166 # <B> <G> <F> 167 # \ 168 # <H> 169 170 cmp_ok($D->getHeight(), '==', 4, '... D has a height of 4'); 171 cmp_ok($E->getHeight(), '==', 3, '... E has a height of 3'); 172 cmp_ok($F->getHeight(), '==', 1, '... F has a height of 1'); 173 cmp_ok($G->getHeight(), '==', 2, '... G has a height of 2'); 174 cmp_ok($H->getHeight(), '==', 1, '... H has a height of 1'); 175 cmp_ok($C->getHeight(), '==', 2, '... C has a height of 2'); 176 cmp_ok($B->getHeight(), '==', 1, '... B has a height of 1'); 177 178 # and the removed tree is ok 179 cmp_ok($A->getHeight(), '==', 1, '... A has a height of 1'); 180 181 ok($D->removeChild($E), '... removed E subtree from D tree'); 182 183 # | 184 # <D> 185 # / 186 # <C> 187 # / 188 # <B> 189 190 cmp_ok($D->getHeight(), '==', 3, '... D has a height of 3'); 191 cmp_ok($C->getHeight(), '==', 2, '... C has a height of 2'); 192 cmp_ok($B->getHeight(), '==', 1, '... B has a height of 1'); 193 194 # and the removed trees are ok 195 cmp_ok($E->getHeight(), '==', 3, '... E has a height of 3'); 196 cmp_ok($F->getHeight(), '==', 1, '... F has a height of 1'); 197 cmp_ok($G->getHeight(), '==', 2, '... G has a height of 2'); 198 cmp_ok($H->getHeight(), '==', 1, '... H has a height of 1'); 199 200 ok($D->removeChild($C), '... removed C subtree from D tree'); 201 202 # | 203 # <D> 204 205 cmp_ok($D->getHeight(), '==', 1, '... D has a height of 1'); 206 207 # and the removed tree is ok 208 cmp_ok($C->getHeight(), '==', 2, '... C has a height of 2'); 209 cmp_ok($B->getHeight(), '==', 1, '... B has a height of 1'); 210} 211