1use strict;
2use warnings;
3
4use lib 'buildlib';
5use Test::More tests => 12;
6
7BEGIN { use_ok('KinoSearch1::Index::DelDocs') }
8use KinoSearch1::Test::TestUtils qw( create_index );
9
10my $invindex = create_index( 'a' .. 'e' );
11
12my $deldocs = KinoSearch1::Index::DelDocs->new();
13
14$deldocs->read_deldocs( $invindex, "_1.del" );
15$deldocs->set(3);
16$deldocs->set(3);
17
18my @deleted_or_not = map { $deldocs->get($_) } 0 .. 4;
19is_deeply( \@deleted_or_not, [ '', '', '', 1, '' ], "set works" );
20is( $deldocs->get_num_deletions, 1, "set increments num_deletions, once" );
21
22my $doc_map = $deldocs->generate_doc_map( 5, 0 );
23my $correct_doc_map = pack( 'i*', 0, 1, 2, -1, 3 );
24is( $$doc_map, $correct_doc_map, "doc map maps around deleted docs" );
25$doc_map = $deldocs->generate_doc_map( 5, 100 );
26is( $doc_map->get(4), 103,   "doc map handles offset correctly" );
27is( $doc_map->get(3), undef, "doc_map handled deletions correctly" );
28is( $doc_map->get(6), undef, "doc_map returns undef for out of range" );
29
30$deldocs->clear(3);
31$deldocs->clear(3);
32$deldocs->clear(3);
33is( $deldocs->get_num_deletions, 0, "clear decrements num_deletions, once" );
34
35$deldocs->set(2);
36$deldocs->set(1);
37$deldocs->write_deldocs( $invindex, "_1.del", 8 );
38$deldocs = KinoSearch1::Index::DelDocs->new();
39$deldocs->read_deldocs( $invindex, "_1.del" );
40
41@deleted_or_not = map { $deldocs->get($_) } 0 .. 7;
42is_deeply(
43    \@deleted_or_not,
44    [ '', 1, 1, '', '', '', '', '' ],
45    "write_deldocs and read_deldocs save/recover deletions correctly"
46);
47
48is( $deldocs->get_num_deletions, 2,
49    "write_deldocs and read_deldocs save/recover num_deletions correctly" );
50is( $deldocs->get_capacity, 8,
51    "write_deldocs wrote correct number of bytes" );
52
53$deldocs->write_deldocs( $invindex, "_1.del", 8 );
54ok( $invindex->file_exists("_1.del"), "overwrite existing deletions file" );
55
56