File: 513-matcher.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 (127 lines) | stat: -rw-r--r-- 4,602 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
# 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 lib 'buildlib';

package MyMatcher;
use base qw( Lucy::Search::Matcher );

package main;

use Test::More tests => 22;

use LucyX::Search::MockMatcher;
use Lucy::Test;

my $matcher = MyMatcher->new;
for (qw( score get_doc_id next )) {
    eval { $matcher->$_; };
    like( $@, qr/abstract/i, "$_ is abstract" );
}

my $got = test_search( docs => [ 1 .. 10 ] );
is_deeply( $got, [ 1 .. 10 ], "defaults" );

$got = test_search( docs => [ 1 .. 3, 5 .. 10 ], dels => [4] );
is_deeply( $got, [ 1 .. 3, 5 .. 10 ], "deletion between hits" );

$got = test_search( docs => [ 1 .. 3, 5 .. 10 ], dels => [5] );
is_deeply( $got, [ 1 .. 3, 6 .. 10 ], "deletion after gap" );

$got = test_search( docs => [ 1 .. 3, 5 .. 10 ], dels => [1] );
is_deeply( $got, [ 2 .. 3, 5 .. 10 ], "first doc deleted" );

$got = test_search( docs => [ 1 .. 3, 5 .. 10 ], dels => [ 1, 2 ] );
is_deeply( $got, [ 3, 5 .. 10 ], "first two docs deleted" );

$got = test_search( docs => [ 1 .. 3, 5 .. 10 ], dels => [10] );
is_deeply( $got, [ 1 .. 3, 5 .. 9 ], "last doc deleted" );

$got = test_search( docs => [ 1 .. 3, 5 .. 10 ], dels => [ 9, 10 ] );
is_deeply( $got, [ 1 .. 3, 5 .. 8 ], "last two docs deleted" );

$got = test_search( docs => [ 1 .. 3, 5 .. 10 ], dels => [ 3, 4 ] );
is_deeply( $got, [ 1 .. 2, 5 .. 10 ], "deletions continuing into gap" );

$got = test_search( docs => [ 1 .. 3, 5 .. 10 ], dels => [ 4, 5 ] );
is_deeply( $got, [ 1 .. 3, 6 .. 10 ], "deletions continuing from gap" );

$got = test_search( docs => [ 1 .. 3, 5 .. 10 ], dels => [ 3, 4, 5 ] );
is_deeply( $got, [ 1 .. 2, 6 .. 10 ], "deletions spanning gap" );

$got = test_search( docs => [ 1 .. 3, 5 .. 10 ], dels => [ 3, 5 ] );
is_deeply( $got, [ 1 .. 2, 6 .. 10 ], "deletions surrounding gap" );

$got = test_search( docs => [ 1 .. 3, 5, 7 .. 10 ], dels => [5] );
is_deeply( $got, [ 1 .. 3, 7 .. 10 ], "gaps surrounding deletion" );

$got = test_search( docs => [ 1, 3, 5, 7, 9 ], dels => [ 2, 4, 6, 8, 10 ] );
is_deeply( $got, [ 1, 3, 5, 7, 9 ], "synchronized gaps and deletions" );

$got = test_search( docs => [ 1, 3, 5, 7, 9 ], dels => [ 1, 3, 5, 7, 9 ] );
is_deeply( $got, [], "alternating gaps and deletions" );

$got = test_search( docs => [ 1 .. 3, 6 .. 10 ], dels => [ 4, 5 ] );
is_deeply( $got, [ 1 .. 3, 6 .. 10 ], "two deletions between hits" );

$got = test_search( docs => [ 1 .. 3, 6 .. 10 ], dels => [3] );
is_deeply( $got, [ 1 .. 2, 6 .. 10 ], "deletion before double gap" );

$got = test_search( docs => [ 1 .. 3, 6 .. 10 ], dels => [6] );
is_deeply( $got, [ 1 .. 3, 7 .. 10 ], "deletion after double gap" );

$got = test_search( docs => [ 1 .. 3, 6 .. 10 ], dels => [ 3, 4, 5 ] );
is_deeply( $got, [ 1 .. 2, 6 .. 10 ],
    "deletions continuing into double gap" );

$got = test_search( docs => [ 1 .. 3, 6 .. 10 ], dels => [ 4, 5, 6 ] );
is_deeply(
    $got,
    [ 1 .. 3, 7 .. 10 ],
    "deletions continuing out of double gap"
);

sub test_search {
    my %args = @_;
    my $docs = delete $args{docs} || [];
    my $dels = delete $args{dels} || [];
    my $del_enum;

    my $matcher = LucyX::Search::MockMatcher->new(
        doc_ids => $docs,
        scores  => [ (0) x scalar @$docs ],
    );
    if (@$dels) {
        my $bit_vec
            = Lucy::Object::BitVector->new( capacity => $dels->[-1] + 1 );
        $bit_vec->set($_) for @$dels;
        $del_enum
            = Lucy::Search::BitVecMatcher->new( bit_vector => $bit_vec );
    }

    my $collector
        = Lucy::Search::Collector::SortCollector->new( wanted => 100 );
    $matcher->collect(
        %Lucy::Search::Matcher::collect_PARAMS,
        collector => $collector,
        deletions => $del_enum,
        %args,
    );
    my $match_docs = $collector->pop_match_docs;
    my @doc_ids = map { $_->get_doc_id } @$match_docs;
    return \@doc_ids;
}