Strange behavior in Tree::R CPAN module

View: New views
2 Messages — Rating Filter:   Alert me  

Strange behavior in Tree::R CPAN module

by Brandon Forehand :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

So, I was using the Tree::R module, and I ran into a strange problem, so
I wrote a small unit test to test the problem, and it appears the source
of the bug may be in Tree::R itself.  The underlying cause seems to be
that when you remove everything from a Tree::R object and then insert
something back into the same Tree::R object, the resulting number of
objects in the Tree is no longer correct.  Here's a small unit test that
reproduces the bug:

use strict;
use warnings;

use Test::More tests => 5;

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

my $r_tree = Tree::R->new();

isa_ok($r_tree, 'Tree::R');

my $rects = {
    1 => [0, 0, 10, 20],
    2 => [20, 0, 10, 20],
    3 => [0, 30, 10, 20],
    4 => [20, 30, 10, 20],
};

for my $rect (keys %$rects) {
    $r_tree->insert($rect, @{$rects->{$rect}});
}

{
    my @objects;
    $r_tree->objects(\@objects);

    is(scalar(@objects), 4);
}

for my $rect (keys %$rects) {
    $r_tree->remove($rect);
}

{
    my @objects;
    $r_tree->objects(\@objects);

    is(scalar(@objects), 0);
}

$r_tree->insert($rects->{1}, @{$rects->{1}});

{
    my @objects;
    $r_tree->objects(\@objects);

    is(scalar(@objects), 1);
}

In this unit test, the last test fails for me.  Instead of the value 1,
I got 2.  I don't believe that this is the intended behavior

--
Brandon Forehand

_______________________________________________
Freegis-list mailing list
Freegis-list@...
https://www.intevation.de/mailman/listinfo/freegis-list

Re: Strange behavior in Tree::R CPAN module

by Ari Jolma-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Brandon,

Thanks, I've fixed this by explicitly removing the root node in the case
when last object is removed from the tree. I also now check for empty
trees in methods objects and dump. I added your tests to the test suite.

The new version 0.06 is (or will soon be) in CPAN.

Best regards,

Ari

Brandon Forehand kirjoitti:

> So, I was using the Tree::R module, and I ran into a strange problem, so
> I wrote a small unit test to test the problem, and it appears the source
> of the bug may be in Tree::R itself.  The underlying cause seems to be
> that when you remove everything from a Tree::R object and then insert
> something back into the same Tree::R object, the resulting number of
> objects in the Tree is no longer correct.  Here's a small unit test that
> reproduces the bug:
>
> use strict;
> use warnings;
>
> use Test::More tests => 5;
>
> BEGIN { use_ok('Tree::R'); }
>
> my $r_tree = Tree::R->new();
>
> isa_ok($r_tree, 'Tree::R');
>
> my $rects = {
>     1 => [0, 0, 10, 20],
>     2 => [20, 0, 10, 20],
>     3 => [0, 30, 10, 20],
>     4 => [20, 30, 10, 20],
> };
>
> for my $rect (keys %$rects) {
>     $r_tree->insert($rect, @{$rects->{$rect}});
> }
>
> {
>     my @objects;
>     $r_tree->objects(\@objects);
>
>     is(scalar(@objects), 4);
> }
>
> for my $rect (keys %$rects) {
>     $r_tree->remove($rect);
> }
>
> {
>     my @objects;
>     $r_tree->objects(\@objects);
>
>     is(scalar(@objects), 0);
> }
>
> $r_tree->insert($rects->{1}, @{$rects->{1}});
>
> {
>     my @objects;
>     $r_tree->objects(\@objects);
>
>     is(scalar(@objects), 1);
> }
>
> In this unit test, the last test fails for me.  Instead of the value 1,
> I got 2.  I don't believe that this is the intended behavior
>
>  


--
Prof. Ari Jolma
Environmental Management Information Technology
Teknillinen Korkeakoulu / Helsinki University of Technology
tel: +358 9 4511 address: POBox 5300, 02015 TKK, Finland
Email: ari.jolma at tkk.fi URL: http://geoinformatics.tkk.fi

_______________________________________________
Freegis-list mailing list
Freegis-list@...
https://www.intevation.de/mailman/listinfo/freegis-list