File: Tree-R.t

package info (click to toggle)
libtree-r-perl 0.072-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 88 kB
  • sloc: perl: 324; makefile: 2
file content (134 lines) | stat: -rw-r--r-- 2,795 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl Tree-R.t'

#########################

# change 'tests => 1' to 'tests => last_test_to_print';

use Test::More tests => 17;
use vars qw/$warning/;
BEGIN { 
    use_ok('Tree::R');
    $SIG{__WARN__} = sub {  $warning = "@_"; }
};

#########################

# Insert your test code below, the Test::More module is use()ed here so read
# its man page ( perldoc Test::More ) for help writing this test script.

{
    my $rtree = Tree::R->new;
    eval {
        $rtree->query_point();
    };
    ok($warning eq '', "Empty tree should not issue any warnings (rt.cpan.org 57055).");
}

my %objects = (
	       1 => [2,4,4,7],
	       2 => [3,2,7,6],
	       3 => [6,3,9,5],
	       4 => [6,8,9,10],
	       5 => [10,7,13,9]
	       );

my $rtree = new Tree::R m=>2,M=>3;

for my $object (keys %objects) {
    my @bbox = @{$objects{$object}}; # (minx,miny,maxx,maxy)
    $rtree->insert($object,@bbox);
}

for (1..2) {
    my @point = (6.5,4); # (x,y)
    my @results;
    $rtree->query_point(@point,\@results);
    
    my @test = sort @results;
    ok("@test" eq "2 3", "query_point $_");
    
    my @rect = (5,0,11,11); # (minx,miny,maxx,maxy)
    @results = ();
    $rtree->query_completely_within_rect(@rect,\@results);
    
    @test = sort @results;
    ok("@test" eq "3 4", "query_completely_within_rect $_");
    
    @results = ();
    $rtree->query_partly_within_rect(@rect,\@results);
    
    @test = sort @results;
    ok("@test" eq "2 3 4 5", "query_partly_within_rect $_");
    
    $rtree->remove(3);
    $rtree->insert(3,@{$objects{3}});   
}

$rtree = new Tree::R m=>2,M=>3;

for my $object (keys %objects) {
    my @bbox = @{$objects{$object}}; # (minx,miny,maxx,maxy)
    $rtree->insert($object,@bbox);
}

for my $object (keys %objects) {

    my @o1;
    $rtree->objects(\@o1);
    @o1 = sort @o1;

    $rtree->remove($object);

    $rtree->insert($object,@{$objects{$object}});

    my @o2;
    $rtree->objects(\@o2);
    @o2 = sort @o2;

    is_deeply(\@o1, \@o2, 'remove and insert');

}

# Tests	from Brandon Forehand:

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);
}