Skip to content
Browse files

implemented both DESTROY and detachParent. Now have tests for both of…

… these function.
  • Loading branch information...
1 parent 8fe0f16 commit a6e44d77951326e3b77bd67296b3accf0765d2a6 @Takadonet committed Dec 12, 2010
Showing with 152 additions and 150 deletions.
  1. +24 −27 lib/Tree/Simple.pm
  2. +128 −123 t/14_Tree_Simple_leak_test.t
View
51 lib/Tree/Simple.pm
@@ -81,12 +81,10 @@ method setParent($parent) {
$.depth = $parent.getDepth() + 1;
}
}
-
-method _detachParent {
- say 'nyi';
+#todo make private
+method !detachParent() {
# return if $USE_WEAK_REFS;
-# my ($self) = @_;
-# $self->{_parent} = undef;
+ self.parent = Mu;
}
method setHeight(Tree::Simple $child) {
@@ -600,28 +598,27 @@ multi sub cloneNode($node,%seen?) {
}
-# ## ----------------------------------------------------------------------------
-# ## Desctructor
-
-method DESTROY {
- say 'nyi';
-# # if we are using weak refs
-# # we dont need to worry about
-# # destruction, it will just happen
-# return if $USE_WEAK_REFS;
-# my ($self) = @_;
-# # we want to detach all our children from
-# # ourselves, this will break most of the
-# # connections and allow for things to get
-# # reaped properly
-# unless (!$self->{_children} && scalar(@{$self->{_children}}) == 0) {
-# foreach my $child (@{$self->{_children}}) {
-# defined $child && $child->_detachParent();
-# }
-# }
-# # we do not need to remove or undef the _children
-# # of the _parent fields, this will cause some
-# # unwanted releasing of connections.
+## ----------------------------------------------------------------------------
+## Desctructor
+
+method DESTROY() {
+ # if we are using weak refs
+ # we dont need to worry about
+ # destruction, it will just happen
+#todo not sure what to do here... need to implement references
+# return if $USE_WEAK_REFS;
+ # we want to detach all our children from
+ # ourselves, this will break most of the
+ # connections and allow for things to get
+ # reaped properly
+ unless (!(@.children) && @.children.elems()==0) {
+ for @.children -> $child {
+ defined $child && $child!detachParent();
+ }
+ }
+ # we do not need to remove or undef the _children
+ # of the _parent fields, this will cause some
+ # unwanted releasing of connections.
}
## ----------------------------------------------------------------------------
View
251 t/14_Tree_Simple_leak_test.t 100644 → 100755
@@ -1,144 +1,149 @@
-# #!/usr/bin/perl
+use v6;
+use Test;
+plan 33;
+BEGIN
+{
+ @*INC.push('lib');
+ @*INC.push('blib');
+}
-# use strict;
-# use warnings;
-# use Test::More;
+eval_lives_ok 'use Tree::Simple', 'Can use Tree::Simple';
+
+use Tree::Simple;
+#since do not have Test::Memory::Cycle module, so just testing the destroy function
# eval "use Test::Memory::Cycle 1.02";
# plan skip_all => "Test::Memory::Cycle required for testing memory leaks" if $@;
+#diag "parental connections must be destroyed manually";
-# plan tests => 51;
-
-# use_ok('Tree::Simple');
-
-# #diag "parental connections must be destroyed manually";
-
-# { #diag "verify the problem exists";
-
-# my $tree2 = Tree::Simple->new("2");
-# ok($tree2->isRoot(), '... tree2 is a ROOT');
-# my $tree1_UID;
-# {
-# my $tree1 = Tree::Simple->new("1");
-# $tree1_UID = $tree1->getUID();
-# $tree1->addChild($tree2);
-# ok(!$tree2->isRoot(), '... now tree2 is not a ROOT');
-
-# memory_cycle_exists($tree2, '... there is a cycle in tree2');
-# }
+#{ #diag "verify the problem exists";
+{
-# memory_cycle_exists($tree2, '... tree1 is still connected with tree2');
-# ok(!$tree2->isRoot(), '... now tree2 is not a ROOT');
-# ok(defined($tree2->getParent()), '... now tree2s parent is still defined');
-# is($tree2->getParent()->getUID(), $tree1_UID, '... and tree2s parent is tree1');
+ my $tree2 = Tree::Simple.new("2");
+ ok($tree2.isRoot(), '... tree2 is a ROOT');
+ my $tree1_UID;
+ {
+ my $tree1 = Tree::Simple.new("1");
+ $tree1_UID = $tree1.getUID();
+ $tree1.addChild($tree2);
+ ok(!$tree2.isRoot(), '... now tree2 is not a ROOT');
+
+ # memory_cycle_exists($tree2, '... there is a cycle in tree2');
+ }
+
+ # memory_cycle_exists($tree2, '... tree1 is still connected with tree2');
+ ok(!$tree2.isRoot(), '... now tree2 is not a ROOT');
+ ok(defined($tree2.getParent()), '... now tree2s parent is still defined');
+ is($tree2.getParent().getUID(), $tree1_UID, '... and tree2s parent is tree1');
-# }
+}
# { #diag "this fixes the problem";
-
-# my $tree2 = Tree::Simple->new("2");
-# ok($tree2->isRoot(), '... tree2 is a ROOT');
+{
+
+ my $tree2 = Tree::Simple.new("2");
+ ok($tree2.isRoot(), '... tree2 is a ROOT');
-# {
-# my $tree1 = Tree::Simple->new("1");
-# $tree1->addChild($tree2);
-# ok(!$tree2->isRoot(), '... now tree2 is not a ROOT');
-
-# memory_cycle_exists($tree2, '... there is a cycle in tree2');
-# $tree1->DESTROY();
-# }
+ {
+ my $tree1 = Tree::Simple.new("1");
+ $tree1.addChild($tree2);
+ ok(!$tree2.isRoot(), '... now tree2 is not a ROOT');
+
+ # memory_cycle_exists($tree2, '... there is a cycle in tree2');
+ $tree1.DESTROY();
+ }
-# memory_cycle_ok($tree2, '... calling DESTORY on tree1 broke the connection with tree2');
-# ok($tree2->isRoot(), '... now tree2 is a ROOT again');
-# ok(!defined($tree2->getParent()), '... now tree2s parent is no longer defined');
-# }
+ # memory_cycle_ok($tree2, '... calling DESTORY on tree1 broke the connection with tree2');
+ ok($tree2.isRoot(), '... now tree2 is a ROOT again');
+ ok(!defined($tree2.getParent()), '... now tree2s parent is no longer defined');
+ }
-# #diag "expand the original problem and see how it effects children";
+#diag "expand the original problem and see how it effects children";
-# {
+{
-# my $tree2 = Tree::Simple->new("2");
-# ok($tree2->isRoot(), '... tree2 is a ROOT');
-# ok($tree2->isLeaf(), '... tree2 is a Leaf');
-# my $tree3 = Tree::Simple->new("3");
-# ok($tree3->isRoot(), '... tree3 is a ROOT');
-# ok($tree3->isLeaf(), '... tree3 is a Leaf');
+ my $tree2 = Tree::Simple.new("2");
+ ok($tree2.isRoot(), '... tree2 is a ROOT');
+ ok($tree2.isLeaf(), '... tree2 is a Leaf');
+ my $tree3 = Tree::Simple.new("3");
+ ok($tree3.isRoot(), '... tree3 is a ROOT');
+ ok($tree3.isLeaf(), '... tree3 is a Leaf');
+
+ {
+ my $tree1 = Tree::Simple.new("1");
+ $tree1.addChild($tree2);
+ ok(!$tree2.isRoot(), '... now tree2 is not a ROOT');
+ $tree2.addChild($tree3);
+ ok(!$tree2.isLeaf(), '... now tree2 is not a Leaf');
+ ok(!$tree3.isRoot(), '... tree3 is no longer a ROOT');
+ ok($tree3.isLeaf(), '... but tree3 is still a Leaf');
+
+ # memory_cycle_exists($tree1, '... there is a cycle in tree1');
+ # memory_cycle_exists($tree2, '... there is a cycle in tree2');
+ # memory_cycle_exists($tree3, '... there is a cycle in tree3');
+ $tree1.DESTROY();
+
+ # memory_cycle_exists($tree1, '... there is still a cycle in tree1 because of the children');
+ }
-# {
-# my $tree1 = Tree::Simple->new("1");
-# $tree1->addChild($tree2);
-# ok(!$tree2->isRoot(), '... now tree2 is not a ROOT');
-# $tree2->addChild($tree3);
-# ok(!$tree2->isLeaf(), '... now tree2 is not a Leaf');
-# ok(!$tree3->isRoot(), '... tree3 is no longer a ROOT');
-# ok($tree3->isLeaf(), '... but tree3 is still a Leaf');
-
-# memory_cycle_exists($tree1, '... there is a cycle in tree1');
-# memory_cycle_exists($tree2, '... there is a cycle in tree2');
-# memory_cycle_exists($tree3, '... there is a cycle in tree3');
-# $tree1->DESTROY();
-
-# memory_cycle_exists($tree1, '... there is still a cycle in tree1 because of the children');
-# }
+ # memory_cycle_exists($tree2, '... calling DESTORY on tree1 broke the connection with tree2');
+ ok($tree2.isRoot(), '... now tree2 is a ROOT again');
+ ok(!$tree2.isLeaf(), '... now tree2 is not a leaf again');
+ ok(!defined($tree2.getParent()), '... now tree2s parent is no longer defined');
+ # cmp_ok($tree2.getChildCount(), '==', 1, '... now tree2 has one child');
+ # memory_cycle_exists($tree3, '... calling DESTORY on tree1 did not break the connection betwee tree2 and tree3');
+ ok(!$tree3.isRoot(), '... now tree3 is not a ROOT');
+ ok($tree3.isLeaf(), '... now tree3 is still a leaf');
+ ok(defined($tree3.getParent()), '... now tree3s parent is still defined');
+ is($tree3.getParent(), $tree2, '... now tree3s parent is still tree2');
+}
+
+#diag "child connections are strong";
+{
+ my $tree1 = Tree::Simple.new("1");
+ my $tree2_UID;
-# memory_cycle_exists($tree2, '... calling DESTORY on tree1 broke the connection with tree2');
-# ok($tree2->isRoot(), '... now tree2 is a ROOT again');
-# ok(!$tree2->isLeaf(), '... now tree2 is not a leaf again');
-# ok(!defined($tree2->getParent()), '... now tree2s parent is no longer defined');
-# cmp_ok($tree2->getChildCount(), '==', 1, '... now tree2 has one child');
-# memory_cycle_exists($tree3, '... calling DESTORY on tree1 did not break the connection betwee tree2 and tree3');
-# ok(!$tree3->isRoot(), '... now tree3 is not a ROOT');
-# ok($tree3->isLeaf(), '... now tree3 is still a leaf');
-# ok(defined($tree3->getParent()), '... now tree3s parent is still defined');
-# is($tree3->getParent(), $tree2, '... now tree3s parent is still tree2');
-# }
-
-# #diag "child connections are strong";
-# {
-# my $tree1 = Tree::Simple->new("1");
-# my $tree2_UID;
-
-# {
-# my $tree2 = Tree::Simple->new("2");
-# $tree1->addChild($tree2);
-# $tree2_UID = $tree2->getUID();
+ {
+ my $tree2 = Tree::Simple.new("2");
+ $tree1.addChild($tree2);
+ $tree2_UID = $tree2.getUID();
-# memory_cycle_exists($tree1, '... tree1 is connected to tree2');
-# memory_cycle_exists($tree2, '... tree2 is connected to tree1');
+ # memory_cycle_exists($tree1, '... tree1 is connected to tree2');
+ # memory_cycle_exists($tree2, '... tree2 is connected to tree1');
-# $tree2->DESTROY(); # this doesn't make sense to do
-# }
-
-# memory_cycle_exists($tree1, '... tree2 is still connected to tree1 because child connections are strong');
-# is($tree1->getChild(0)->getUID(), $tree2_UID, '... tree2 is still connected to tree1');
-# is($tree1->getChild(0)->getParent(), $tree1, '... tree2s parent is tree1');
-# cmp_ok($tree1->getChildCount(), '==', 1, '... tree1 has a child count of 1');
-# }
-
-# #diag "expand upon this issue";
-# {
-# my $tree1 = Tree::Simple->new("1");
-# my $tree2_UID;
-# my $tree3 = Tree::Simple->new("3");
-
-# {
-# my $tree2 = Tree::Simple->new("2");
-# $tree1->addChild($tree2);
-# $tree2_UID = $tree2->getUID();
-# $tree2->addChild($tree3);
+ $tree2.DESTROY(); # this doesn't make sense to do
+ }
+
+ # memory_cycle_exists($tree1, '... tree2 is still connected to tree1 because child connections are strong');
+ is($tree1.getChild(0).getUID(), $tree2_UID, '... tree2 is still connected to tree1');
+ is($tree1.getChild(0).getParent(), $tree1, '... tree2s parent is tree1');
+ is($tree1.getChildCount(), 1, '... tree1 has a child count of 1');
+}
+
+#diag "expand upon this issue";
+{
+ my $tree1 = Tree::Simple.new("1");
+ my $tree2_UID;
+ my $tree3 = Tree::Simple.new("3");
+
+ {
+ my $tree2 = Tree::Simple.new("2");
+ $tree1.addChild($tree2);
+ $tree2_UID = $tree2.getUID();
+ $tree2.addChild($tree3);
-# memory_cycle_exists($tree1, '... tree1 is connected to tree2');
-# memory_cycle_exists($tree2, '... tree2 is connected to tree1');
-# memory_cycle_exists($tree3, '... tree3 is connected to tree2');
+ # memory_cycle_exists($tree1, '... tree1 is connected to tree2');
+ # memory_cycle_exists($tree2, '... tree2 is connected to tree1');
+ # memory_cycle_exists($tree3, '... tree3 is connected to tree2');
-# $tree2->DESTROY(); # this doesn't make sense to do
-# }
-
-# memory_cycle_exists($tree1, '... tree2 is still connected to tree1 because child connections are strong');
-# is($tree1->getChild(0)->getUID(), $tree2_UID, '... tree2 is still connected to tree1');
-# is($tree1->getChild(0)->getParent(), $tree1, '... tree2s parent is tree1');
-# cmp_ok($tree1->getChildCount(), '==', 1, '... tree1 has a child count of 1');
-# cmp_ok($tree1->getChild(0)->getChildCount(), '==', 1, '... tree2 is still connected to tree3');
-# is($tree1->getChild(0)->getChild(0), $tree3, '... tree2 is still connected to tree3');
-# }
+ $tree2.DESTROY(); # this doesn't make sense to do
+ }
+
+ # memory_cycle_exists($tree1, '... tree2 is still connected to tree1 because child connections are strong');
+ is($tree1.getChild(0).getUID(), $tree2_UID, '... tree2 is still connected to tree1');
+ is($tree1.getChild(0).getParent(), $tree1, '... tree2s parent is tree1');
+ is($tree1.getChildCount(), 1, '... tree1 has a child count of 1');
+ is($tree1.getChild(0).getChildCount(), 1, '... tree2 is still connected to tree3');
+ is($tree1.getChild(0).getChild(0), $tree3, '... tree2 is still connected to tree3');
+}

0 comments on commit a6e44d7

Please sign in to comment.
Something went wrong with that request. Please try again.