1# $Id: /mirror/Senna-Perl/t/01-sanity.t 2830 2006-08-24T02:53:18.040683Z daisuke $ 2# 3# Copyright (c) 2006 Daisuke Maki <dmaki@cpan.org> 4# All rights reserved. 5 6use strict; 7use Test::More (tests => 10); 8 9BEGIN 10{ 11 use_ok("Senna"); 12} 13 14{ 15 # Test API 16 my %api = ( 17 'Senna::Index' => [ 18 qw(create open close remove rename select update path query_exec) ], 19 'Senna::OptArg::Select' => [ 20 qw(new mode similarity_threshold max_interval weight_vector func func_arg) ], 21 'Senna::Query' => [ qw(open rest) ], 22 'Senna::Record' => [ qw(new key score) ], 23 'Senna::Records' => [ 24 qw(next find rewind nhits curr_score curr_key close), 25 qw( union subtract intersect difference) ], 26 'Senna::Snippet' => [ qw(new add_cond exec) ], 27 'Senna::Symbol' => [ qw(create open close get at del size key next pocket_set pocket_get prefix_search suffix_search common_prefix_search) ], 28 'Senna::Values' => [ qw(new open close add) ], 29 ); 30 31 foreach my $package (sort keys %api) { 32 my $api = $api{$package}; 33 can_ok($package, @$api); 34 } 35} 36 37ok(&Senna::Constants::LIBSENNA_VERSION, sprintf("libsenna version = %s", 38 &Senna::Constants::LIBSENNA_VERSION)); 39 401; 41 42__END__ 43use Test::More (tests => 46); 44use File::Spec; 45 46BEGIN 47{ 48 use_ok("Senna::Index", ':all'); 49} 50 51ok(SEN_INDEX_NORMALIZE); 52ok(SEN_INDEX_SPLIT_ALPHA); 53ok(SEN_INDEX_SPLIT_DIGIT); 54ok(SEN_INDEX_SPLIT_SYMBOL); 55ok(SEN_INDEX_NGRAM); 56 57is(SEN_ENC_DEFAULT, 0); 58ok(SEN_ENC_NONE); 59ok(SEN_ENC_EUCJP); 60ok(SEN_ENC_UTF8); 61ok(SEN_ENC_SJIS); 62 63is(SEN_VARCHAR_KEY, 0); 64ok(SEN_INT_KEY); 65 66my $index_name = 'test.db'; 67my $path = File::Spec->catfile('t', $index_name); 68my $index = Senna::Index->create($path); 69my $c; 70 71is($index->key_size, 0); 72is($index->encoding, SEN_ENC_EUCJP); 73 74$index->put("���ܸ�", "���ܸ�Ȥ��ǿ����ޤ���"); 75 76ok($c = $index->search("���ܸ�"), "test search"); 77isa_ok($c, 'Senna::Cursor'); 78is($c->hits, 1, "should hit only 1 result"); 79my @list = $c->as_list; 80is(scalar(@list), 1, "test as_list"); 81 82my $idx = 0; 83while (my $r = $c->next) { 84 isa_ok($r, 'Senna::Result'); 85 is($r->key, $list[$idx++]->key, 86 "make sure next() returns the same thing as as_list()"); 87} 88ok($c->rewind); 89 90# now check when there are no hits 91ok($c = $index->search("�����������ޤ���")); 92isa_ok($c, 'Senna::Cursor'); 93is($c->hits, 0); 94ok(! $c->next); 95ok(! $c->rewind); 96 97ok($index->del("���ܸ�", "���ܸ�Ȥ��ǿ����ޤ���")); 98ok($c = $index->search("���ܸ�")); 99isa_ok($c, 'Senna::Cursor'); 100is($c->hits, 0); 101 102ok($index->remove()); 103 104# Now check for integer keys 105{ 106 ok($index = Senna::Index->create($path, SEN_INT_KEY)); 107 ok($index->put(1, "���ͷ��Υ���")); 108 ok($c = $index->search("���ͷ�")); 109 isa_ok($c, 'Senna::Cursor'); 110 is($c->hits, 1); 111 ok($index->del(1, "���ͷ��Υ���")); 112 ok($c = $index->search("���ͷ�")); 113 is($c->hits, 0); 114 115 # Bad key type 116 ok(!eval { $index->put("ʸ����", "���ͷ��Υ����ΤϤ�") }); 117 118 ok($index->put(2, "���ͷ��Υ���")); 119 ok($index->replace(2, "���ͷ��Υ���", "���ͷ��Υ����������Ƥߤ�")); 120 ok($c = $index->search("���������Ƥߤ�")); 121 is($c->hits, 1); 122 123 ok($index->remove()); 124} 125