File: 310-heat_map.t

package info (click to toggle)
liblucy-perl 0.3.3-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 9,328 kB
  • ctags: 8,492
  • sloc: ansic: 80,468; perl: 7,080; yacc: 681; java: 174; lex: 96; makefile: 20
file content (143 lines) | stat: -rw-r--r-- 4,518 bytes parent folder | download | duplicates (2)
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
135
136
137
138
139
140
141
142
143
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

use strict;
use warnings;

use Test::More tests => 13;
use Lucy::Test;

my $heat_map = Lucy::Highlight::HeatMap->new( spans => [], );

my $big_boost = $heat_map->calc_proximity_boost(
    span1 => make_span( 0,  10, 1.0 ),
    span2 => make_span( 10, 10, 1.0 )
);
my $equally_big_boost = $heat_map->calc_proximity_boost(
    span1 => make_span( 0, 10, 1.0 ),
    span2 => make_span( 5, 4,  1.0 )
);
my $smaller_boost = $heat_map->calc_proximity_boost(
    span1 => make_span( 0,   10, 1.0 ),
    span2 => make_span( 100, 10, 1.0 )
);
my $zero_boost = $heat_map->calc_proximity_boost(
    span1 => make_span( 0,   10, 1.0 ),
    span2 => make_span( 150, 10, 1.0 )
);
is( $big_boost, $equally_big_boost,
    "overlapping and abutting produce the same proximity boost" );
cmp_ok( $big_boost, '>', $smaller_boost, "closer is better" );
is( $zero_boost, 0, "distance outside of window yields no prox boost" );

my $spans = make_spans( [ 10, 10, 1.0 ], [ 16, 14, 2.0 ] );
my $flattened = $heat_map->flatten_spans($spans);
is_deeply(
    spans_to_arg_array($flattened),
    [ [ 10, 6, 1.0 ], [ 16, 4, 3.0 ], [ 20, 10, 2.0 ] ],
    "flatten two overlapping spans"
);
my $boosts = $heat_map->generate_proximity_boosts($spans);
is_deeply(
    spans_to_arg_array($boosts),
    [ [ 10, 20, 3.0 ] ],
    "prox boosts for overlap"
);

$spans = make_spans( [ 10, 10, 1.0 ], [ 16, 14, 2.0 ], [ 50, 1, 1.0 ] );
$flattened = $heat_map->flatten_spans($spans);
is_deeply(
    spans_to_arg_array($flattened),
    [ [ 10, 6, 1.0 ], [ 16, 4, 3.0 ], [ 20, 10, 2.0 ], [ 50, 1, 1.0 ] ],
    "flatten two overlapping spans, leave hole, then third span"
);
$boosts = $heat_map->generate_proximity_boosts($spans);
is( scalar @$boosts,
    2 + 1, "boosts generated for each unique pair, since all were in range" );

$spans = make_spans( [ 10, 10, 1.0 ], [ 14, 4, 4.0 ], [ 16, 14, 2.0 ] );
$flattened = $heat_map->flatten_spans($spans);
is_deeply(
    spans_to_arg_array($flattened),
    [   [ 10, 4,  1.0 ],
        [ 14, 2,  5.0 ],
        [ 16, 2,  7.0 ],
        [ 18, 2,  3.0 ],
        [ 20, 10, 2.0 ]
    ],
    "flatten three overlapping spans"
);
$boosts = $heat_map->generate_proximity_boosts($spans);
is( scalar @$boosts,
    2 + 1, "boosts generated for each unique pair, since all were in range" );

$spans = make_spans(
    [ 10, 10, 1.0 ],
    [ 16, 14, 4.0 ],
    [ 16, 14, 2.0 ],
    [ 30, 10, 10.0 ]
);
$flattened = $heat_map->flatten_spans($spans);
is_deeply(
    spans_to_arg_array($flattened),
    [ [ 10, 6, 1.0 ], [ 16, 4, 7.0 ], [ 20, 10, 6.0 ], [ 30, 10, 10.0 ] ],
    "flatten 4 spans, middle two have identical range"
);
$boosts = $heat_map->generate_proximity_boosts($spans);
is( scalar @$boosts,
    3 + 2 + 1,
    "boosts generated for each unique pair, since all were in range"
);

$spans = make_spans(
    [ 10,  10, 1.0 ],
    [ 16,  4,  4.0 ],
    [ 16,  14, 2.0 ],
    [ 230, 10, 10.0 ]
);
$flattened = $heat_map->flatten_spans($spans);
is_deeply(
    spans_to_arg_array($flattened),
    [ [ 10, 6, 1.0 ], [ 16, 4, 7.0 ], [ 20, 10, 2.0 ], [ 230, 10, 10.0 ] ],
    "flatten 4 spans, middle two have identical starts but different ends"
);
$boosts = $heat_map->generate_proximity_boosts($spans);
is( scalar @$boosts, 2 + 1, "boosts not generated for out of range span" );

sub make_span {
    return Lucy::Search::Span->new(
        offset => $_[0],
        length => $_[1],
        weight => $_[2],
    );
}

sub make_spans {
    my @spans;
    for my $arg_ref (@_) {
        push @spans, make_span( @{$arg_ref}[ 0 .. 2 ] );
    }
    return \@spans;
}

sub spans_to_arg_array {
    my $spans = shift;
    my @out;
    for (@$spans) {
        push @out, [ $_->get_offset, $_->get_length, $_->get_weight ];
    }
    return \@out;
}