2 # Before 'make install' is performed this script should be runnable with
3 # 'make test'. After 'make install' it should work as 'perl test.pl'
5 #########################
7 # change 'tests => 1' to 'tests => last_test_to_print';
11 BEGIN {$SIG{__WARN__} = sub { die "Terminating test due to warning: $_[0]" } };
14 BEGIN { plan tests => 116 };
15 use Xapian qw(:standard);
17 #########################
19 # Insert your test code below, the Test module is use()ed here so read
20 # its man page ( perldoc Test ) for help writing this test script.
22 foreach my $backend ("inmemory", "auto") {
24 if ($backend eq "inmemory") {
25 ok( $database = Xapian::WritableDatabase->new() );
27 ok( $database = Xapian::WritableDatabase->new( 'testdb', Xapian::DB_CREATE_OR_OVERWRITE ) );
30 ok( $database->get_description() );
33 ok( $stemmer = Xapian::Stem->new( 'english' ) );
34 ok( $stemmer->get_description() );
39 ok( $term = $stemmer->stem_word( $term ) );
40 if ($backend ne "inmemory") {
41 # inmemory doesn't implement spelling correction support.
42 $database->add_spelling( $term, 1 );
46 for my $num (qw( one two three )) {
47 ok( $docs{$num} = Xapian::Document->new() );
48 ok( $docs{$num}->get_description() );
50 $docs{$num}->set_data( "$term $num" );
52 $docs{$num}->add_posting( $term, 0 );
53 $docs{$num}->add_posting( $num, 1 );
55 $docs{$num}->add_value(0, $num);
57 if ($backend ne "inmemory") {
58 # inmemory doesn't implement spelling correction support.
59 $database->add_spelling( "x" . $term, 1 );
60 $database->add_spelling( $term, 1 );
61 $database->remove_spelling( "x" . $term, 1 );
63 ok( $docid = $database->add_document( $docs{$num} ) );
65 $database->delete_document( $docid );
66 is( $database->get_doccount(), 2 );
67 is( $database->get_lastdocid(), 3 );
69 is( $database->get_document(1)->get_docid(), 1 );
70 is( $database->get_document(2)->get_docid(), 2 );
72 # regression test - add_posting with 2 parameters set wdfinc 0 in <=0.8.3.0
73 ok( $database->get_doclength(1) == 2 );
75 is( $database->get_document(1)->get_value(0), "one" );
76 is( $database->get_document(2)->get_value(0), "two" );
78 my $posit = $database->positionlist_begin(1, $term);
79 ok( $posit ne $database->positionlist_end(1, $term) );
80 ok( $posit->get_termpos() == 0 );
82 ok( $posit eq $database->positionlist_end(1, $term) );
84 my $postit = $database->postlist_begin('one');
85 ok( $postit ne $database->postlist_end('one') );
86 ok( $postit != $database->postlist_end('one') );
87 is( $postit->get_docid(), 1 );
89 ok( $postit eq $database->postlist_end('one') );
90 ok( $postit == $database->postlist_end('one') );
92 my $termit = $database->termlist_begin(1);
93 ok( $termit != $database->termlist_end(1) );
94 ok( $termit->get_termname() eq 'one' );
96 ok( $termit ne $database->termlist_end(1) );
97 is( $termit->get_termname(), 'test' );
99 ok( $termit eq $database->termlist_end(1) );
100 ok( $termit == $database->termlist_end(1) );
102 my $alltermit = $database->allterms_begin();
103 ok( $alltermit != $database->allterms_end() );
104 ok( $alltermit->get_termname() eq 'one' );
105 ok( $alltermit->get_termname() eq 'one' );
106 ok( ++$alltermit != $database->allterms_end() );
107 ok( $alltermit->get_termname() eq 'test' );
108 ok( $alltermit->get_termname() eq 'test' );
109 ok( ++$alltermit != $database->allterms_end() );
110 ok( $alltermit->get_termname() eq 'two' );
111 ok( $alltermit->get_termname() eq 'two' );
112 ok( ++$alltermit == $database->allterms_end() );
114 $alltermit = $database->allterms_begin('t');
115 ok( $alltermit != $database->allterms_end('t') );
116 ok( $alltermit->get_termname() eq 'test' );
117 ok( $alltermit->get_termname() eq 'test' );
118 ok( ++$alltermit != $database->allterms_end('t') );
119 ok( $alltermit->get_termname() eq 'two' );
120 ok( $alltermit->get_termname() eq 'two' );
121 ok( ++$alltermit == $database->allterms_end('t') );
123 # Feature test for metadata support.
124 is( $database->get_metadata( "nothing" ), "" );
125 is( $database->get_metadata( "foo" ), "" );
126 $database->set_metadata( "foo", "bar" );
127 is( $database->get_metadata( "nothing" ), "" );
128 is( $database->get_metadata( "foo" ), "bar" );
131 # Check that trying to create an invalid stemmer gives an exception, not an
134 my $badstem = Xapian::Stem->new( 'gibberish' );
137 ok(ref($@), "Xapian::InvalidArgumentError");
138 ok($@->isa('Xapian::Error'));
139 ok($@->get_msg, "Language code gibberish unknown");