Commit | Line | Data |
e5ebe4ce |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
7ff56534 |
6 | use Test::More tests => 33; |
e5ebe4ce |
7 | use Test::Exception; |
8 | |
a15dff8d |
9 | use Scalar::Util 'isweak'; |
10 | |
e5ebe4ce |
11 | { |
12 | package BinaryTree; |
e5ebe4ce |
13 | use Moose; |
14 | |
fa2985bc |
15 | has 'node' => ( is => 'rw', isa => 'Any' ); |
8597d950 |
16 | |
a15dff8d |
17 | has 'parent' => ( |
fa2985bc |
18 | is => 'rw', |
19 | isa => 'BinaryTree', |
29db16a9 |
20 | predicate => 'has_parent', |
fa2985bc |
21 | weak_ref => 1, |
e5ebe4ce |
22 | ); |
23 | |
a15dff8d |
24 | has 'left' => ( |
fa2985bc |
25 | is => 'rw', |
26 | isa => 'BinaryTree', |
27 | predicate => 'has_left', |
7c6cacb4 |
28 | lazy => 1, |
fa2985bc |
29 | default => sub { BinaryTree->new( parent => $_[0] ) }, |
e5ebe4ce |
30 | ); |
31 | |
a15dff8d |
32 | has 'right' => ( |
fa2985bc |
33 | is => 'rw', |
34 | isa => 'BinaryTree', |
35 | predicate => 'has_right', |
36 | lazy => 1, |
37 | default => sub { BinaryTree->new( parent => $_[0] ) }, |
e5ebe4ce |
38 | ); |
39 | |
40 | before 'right', 'left' => sub { |
fa2985bc |
41 | my ( $self, $tree ) = @_; |
42 | $tree->parent($self) if defined $tree; |
43 | }; |
44 | |
45 | __PACKAGE__->meta->make_immutable( debug => 0 ); |
e5ebe4ce |
46 | } |
47 | |
8597d950 |
48 | my $root = BinaryTree->new(node => 'root'); |
e5ebe4ce |
49 | isa_ok($root, 'BinaryTree'); |
50 | |
8597d950 |
51 | is($root->node, 'root', '... got the right node value'); |
52 | |
e5ebe4ce |
53 | ok(!$root->has_left, '... no left node yet'); |
54 | ok(!$root->has_right, '... no right node yet'); |
55 | |
a15dff8d |
56 | ok(!$root->has_parent, '... no parent for root node'); |
57 | |
7c6cacb4 |
58 | # make a left node |
e5ebe4ce |
59 | |
7c6cacb4 |
60 | my $left = $root->left; |
61 | isa_ok($left, 'BinaryTree'); |
e5ebe4ce |
62 | |
7c6cacb4 |
63 | is($root->left, $left, '... got the same node (and it is $left)'); |
e5ebe4ce |
64 | ok($root->has_left, '... we have a left node now'); |
65 | |
66 | ok($left->has_parent, '... lefts has a parent'); |
67 | is($left->parent, $root, '... lefts parent is the root'); |
68 | |
a15dff8d |
69 | ok(isweak($left->{parent}), '... parent is a weakened ref'); |
70 | |
7c6cacb4 |
71 | ok(!$left->has_left, '... $left no left node yet'); |
72 | ok(!$left->has_right, '... $left no right node yet'); |
e5ebe4ce |
73 | |
8597d950 |
74 | is($left->node, undef, '... left has got no node value'); |
75 | |
76 | lives_ok { |
77 | $left->node('left') |
78 | } '... assign to lefts node'; |
79 | |
80 | is($left->node, 'left', '... left now has a node value'); |
81 | |
7c6cacb4 |
82 | # make a right node |
e5ebe4ce |
83 | |
8597d950 |
84 | ok(!$root->has_right, '... still no right node yet'); |
85 | |
86 | is($root->right->node, undef, '... right has got no node value'); |
87 | |
88 | ok($root->has_right, '... now we have a right node'); |
89 | |
7c6cacb4 |
90 | my $right = $root->right; |
91 | isa_ok($right, 'BinaryTree'); |
e5ebe4ce |
92 | |
8597d950 |
93 | lives_ok { |
94 | $right->node('right') |
95 | } '... assign to rights node'; |
96 | |
97 | is($right->node, 'right', '... left now has a node value'); |
98 | |
7c6cacb4 |
99 | is($root->right, $right, '... got the same node (and it is $right)'); |
e5ebe4ce |
100 | ok($root->has_right, '... we have a right node now'); |
101 | |
102 | ok($right->has_parent, '... rights has a parent'); |
103 | is($right->parent, $root, '... rights parent is the root'); |
a15dff8d |
104 | |
105 | ok(isweak($right->{parent}), '... parent is a weakened ref'); |
6ba6d68c |
106 | |
7c6cacb4 |
107 | my $left_left = $left->left; |
6ba6d68c |
108 | isa_ok($left_left, 'BinaryTree'); |
109 | |
110 | ok($left_left->has_parent, '... left does have a parent'); |
111 | |
112 | is($left_left->parent, $left, '... got a parent node (and it is $left)'); |
113 | ok($left->has_left, '... we have a left node now'); |
114 | is($left->left, $left_left, '... got a left node (and it is $left_left)'); |
115 | |
116 | ok(isweak($left_left->{parent}), '... parent is a weakened ref'); |
117 | |