Skip to content

Commit

Permalink
improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pawa- committed Oct 7, 2012
1 parent 08f6932 commit fd914aa
Show file tree
Hide file tree
Showing 7 changed files with 489 additions and 379 deletions.
220 changes: 137 additions & 83 deletions t/01_basic.t
Expand Up @@ -4,134 +4,188 @@ use Lingua::JA::WebIDF;
use Test::More;
use Test::Warn;
use Test::Fatal;
use Test::Requires qw/TokyoCabinet/;


my $df_file = './df/utf8.tch';
my $IS_TOKYOCABINET_INSTALLED = eval "use TokyoCabinet; 1";
my @DF_FILE_LIST = qw|./df/utf8.st ./df/utf8.tch|;

can_ok('Lingua::JA::WebIDF', qw/new idf df db_open db_close purge/);

subtest 'new method' => sub {

my $exception = exception { Lingua::JA::WebIDF->new(appid => 'test'); };
like($exception, qr/df_file is not found/, 'not set df_file');
for my $df_file (@DF_FILE_LIST)
{
subtest $df_file => sub {

my $webidf;
$exception = exception { $webidf = Lingua::JA::WebIDF->new(df_file => $df_file); };
is($exception, undef, 'set df_file');
isa_ok($webidf, 'Lingua::JA::WebIDF');
plan skip_all => "TokyoCabinet is not installed."
if (!$IS_TOKYOCABINET_INSTALLED && $df_file =~ /\.tch$/);

$exception = exception { $webidf = Lingua::JA::WebIDF->new({ df_file => $df_file }); };
is($exception, undef, 'set a hash');
isa_ok($webidf, 'Lingua::JA::WebIDF');
my $driver = ($df_file =~ /\.tch$/) ? 'TokyoCabinet' : 'Storable';

$exception = exception { Lingua::JA::WebIDF->new(df_file => $df_file, document => 250_0000_0000); };
like($exception, qr/Unknown option: document/, 'set an unknown option');
my $exception = exception { Lingua::JA::WebIDF->new(driver => $driver, appid => 'test'); };
like($exception, qr/df_file is not found/, 'not set df_file');

$exception = exception { Lingua::JA::WebIDF->new(df_file => $df_file, appid => 'test', fetch_df => 0); };
is($exception, undef, "appid => 'test', fetch_df => 0");
my $webidf;
$exception = exception { $webidf = Lingua::JA::WebIDF->new(driver => $driver, df_file => $df_file); };
is($exception, undef, 'set df_file');
isa_ok($webidf, 'Lingua::JA::WebIDF');

$exception = exception { Lingua::JA::WebIDF->new(df_file => $df_file, appid => 'test', fetch_df => 1); };
is($exception, undef, "appid => 'test', fetch_df => 1");
$exception = exception { $webidf = Lingua::JA::WebIDF->new({driver => $driver, df_file => $df_file }); };
is($exception, undef, 'set a hash');
isa_ok($webidf, 'Lingua::JA::WebIDF');

$exception = exception { Lingua::JA::WebIDF->new(df_file => $df_file, appid => undef, fetch_df => 0); };
is($exception, undef, "appid => undef, fetch_df => 0");
$exception = exception { Lingua::JA::WebIDF->new(driver => $driver, df_file => $df_file, document => 250_0000_0000); };
like($exception, qr/Unknown option: document/, 'set an unknown option');

$exception = exception { Lingua::JA::WebIDF->new(df_file => $df_file, appid => undef, fetch_df => 1); };
like($exception, qr/appid is required/, "appid => undef, fetch_df => 1");
$exception = exception { Lingua::JA::WebIDF->new(driver => $driver, df_file => $df_file, appid => 'test', fetch_df => 0); };
is($exception, undef, "appid => 'test', fetch_df => 0");

$exception = exception { Lingua::JA::WebIDF->new(df_file => $df_file, appid => 'test', fetch_df => 1, api => 'Wahoo'); };
unlike($exception, qr/^$/, "appid => test, fetch_df => 1, api => 'Wahoo'");
$exception = exception { Lingua::JA::WebIDF->new(driver => $driver, df_file => $df_file, appid => 'test', fetch_df => 1); };
is($exception, undef, "appid => 'test', fetch_df => 1");

for my $idf_type (0 .. 4)
{
$exception = exception { Lingua::JA::WebIDF->new(df_file => $df_file, idf_type => $idf_type); };

if ($idf_type == 0 || $idf_type == 4)
{
like($exception, qr/Unknown idf type/, 'set a unknow idf_type');
}
else
{
is($exception, undef, 'set a correct idf_type');
}
$exception = exception { Lingua::JA::WebIDF->new(driver => $driver, df_file => $df_file, appid => undef, fetch_df => 0); };
is($exception, undef, "appid => undef, fetch_df => 0");

$exception = exception { Lingua::JA::WebIDF->new(driver => $driver, df_file => $df_file, appid => undef, fetch_df => 1); };
like($exception, qr/appid is required/, "appid => undef, fetch_df => 1");

$exception = exception { Lingua::JA::WebIDF->new(driver => $driver, df_file => $df_file, appid => 'test', fetch_df => 1, api => 'Wahoo'); };
unlike($exception, qr/^$/, "appid => test, fetch_df => 1, api => 'Wahoo'");

for my $idf_type (0 .. 4)
{
$exception = exception { Lingua::JA::WebIDF->new(driver => $driver, df_file => $df_file, idf_type => $idf_type); };

if ($idf_type == 0 || $idf_type == 4)
{
like($exception, qr/Unknown idf type/, 'set a unknow idf_type');
}
else
{
is($exception, undef, 'set a correct idf_type');
}
}
};
}
};

subtest 'df method' => sub {

my $webidf = Lingua::JA::WebIDF->new(
df_file => $df_file,
fetch_df => 0,
);
for my $df_file (@DF_FILE_LIST)
{
subtest $df_file => sub {

plan skip_all => "TokyoCabinet is not installed."
if (!$IS_TOKYOCABINET_INSTALLED && $df_file =~ /\.tch$/);

my $driver = ($df_file =~ /\.tch$/) ? 'TokyoCabinet' : 'Storable';

my $exception = exception { $webidf->df('ほげ'); };
like($exception, qr/not opened/, 'fetch df without opening df_file');
my $webidf = Lingua::JA::WebIDF->new(
driver => $driver,
df_file => $df_file,
fetch_df => 0,
verbose => 0,
);

$webidf->db_open;
my $exception = exception { $webidf->df('ほげ'); };
like($exception, qr/not opened/, 'fetch df without opening df_file') if $driver eq 'TokyoCabinet';
is($exception, undef, 'fetch df without opening df_file') if $driver eq 'Storable';

my $weight;
$webidf = Lingua::JA::WebIDF->new(
driver => $driver,
df_file => $df_file,
fetch_df => 0,
verbose => 1,
);

warning_like { $weight = $webidf->df; }
qr/Undefined or empty word/, 'set an undefined word';
is($weight, undef);
$webidf->db_open;

warning_like { $weight = $webidf->df(''); }
qr/Undefined or empty word/, 'set an empty word';
is($weight, undef);
my $weight;

isnt($webidf->df(''), undef, "fetch df of '川' from df file");
warning_like { $weight = $webidf->df; }
qr/Undefined or empty word/, 'set an undefined word';
is($weight, undef);

warning_like { $weight = $webidf->idf('' x 100); }
qr/use fetch_df/, "calculate idf of '川' x 100 via df file";
is($weight, undef, "calculate idf of '川' x 100 via df file");
warning_like { $weight = $webidf->df(''); }
qr/Undefined or empty word/, 'set an empty word';
is($weight, undef);

$webidf->db_close;
isnt($webidf->df(''), undef, "fetch df of '川' from df file");

warning_like { $weight = $webidf->idf('' x 100); }
qr/use fetch_df/, "calculate idf of '川' x 100 via df file";
is($weight, undef, "calculate idf of '川' x 100 via df file");

$webidf->db_close;
};
}
};

subtest 'idf method' => sub {

my $num_of_documents = 250_0000_0000;
for my $df_file (@DF_FILE_LIST)
{
subtest $df_file => sub {

plan skip_all => "TokyoCabinet is not installed."
if (!$IS_TOKYOCABINET_INSTALLED && $df_file =~ /\.tch$/);

my $webidf = Lingua::JA::WebIDF->new(
df_file => $df_file,
fetch_df => 0,
documents => $num_of_documents,
);

my $exception = exception { $webidf->idf('ほげ'); };
like($exception, qr/not opened/, 'fetch idf without opening df_file');
my $driver = ($df_file =~ /\.tch$/) ? 'TokyoCabinet' : 'Storable';
my $num_of_documents = 250_0000_0000;

$webidf->db_open;
my $webidf = Lingua::JA::WebIDF->new(
driver => $driver,
df_file => $df_file,
fetch_df => 0,
documents => $num_of_documents,
verbose => 0,
);

my $weight;
my $exception = exception { $webidf->idf('ほげ'); };
like($exception, qr/not opened/, 'fetch df without opening df_file') if $driver eq 'TokyoCabinet';
is($exception, undef, 'fetch df without opening df_file') if $driver eq 'Storable';

warning_like { $weight = $webidf->idf; }
qr/Undefined or empty word/, 'set an undefined word';
is($weight, undef);
$webidf = Lingua::JA::WebIDF->new(
driver => $driver,
df_file => $df_file,
fetch_df => 0,
documents => $num_of_documents,
verbose => 1,
);

warning_like { $weight = $webidf->idf(''); }
qr/Undefined or empty word/, 'set an empty word';
is($weight, undef);
$webidf->db_open;

warning_like { $weight = $webidf->idf(undef, 'df'); }
qr/Undefined or empty df/, 'set an undefined df';
is($weight, undef);
my $weight;

warning_like { $weight = $webidf->idf('', 'df'); }
qr/Undefined or empty df/, 'set an empty df';
is($weight, undef);
warning_like { $weight = $webidf->idf; }
qr/Undefined or empty word/, 'set an undefined word';
is($weight, undef);

isnt($webidf->idf(''), undef, "calculate idf of '川' via df file");
warning_like { $weight = $webidf->idf(''); }
qr/Undefined or empty word/, 'set an empty word';
is($weight, undef);

warning_like { $weight = $webidf->idf('' x 100); }
qr/use fetch_df/, "calculate idf of '川' x 100 via df file";
is($weight, undef, "calculate idf of '川' x 100 via df file");
warning_like { $weight = $webidf->idf(undef, 'df'); }
qr/Undefined or empty df/, 'set an undefined df';
is($weight, undef);

my $df = 100;
is($webidf->idf($df, 'df'), log($num_of_documents / $df), 'calculate idf with the given df');
warning_like { $weight = $webidf->idf('', 'df'); }
qr/Undefined or empty df/, 'set an empty df';
is($weight, undef);

$webidf->db_close;
isnt($webidf->idf(''), undef, "calculate idf of '川' via df file");

warning_like { $weight = $webidf->idf('' x 100); }
qr/use fetch_df/, "calculate idf of '川' x 100 via df file";
is($weight, undef, "calculate idf of '川' x 100 via df file");

my $df = 100;
is($webidf->idf($df, 'df'), log($num_of_documents / $df), 'calculate idf with the given df');

$webidf->db_close;
};
}
};

done_testing;
69 changes: 41 additions & 28 deletions t/02_idf_type.t
Expand Up @@ -2,41 +2,54 @@ use strict;
use warnings;
use Lingua::JA::WebIDF;
use Test::More;
use Test::Requires qw/TokyoCabinet/;


binmode Test::More->builder->$_ => ':utf8'
for qw/output failure_output todo_output/;


my $df_file = './df/utf8.tch';
my $word = '';
my $IS_TOKYOCABINET_INSTALLED = eval 'use TokyoCabinet; 1;';
my @df_file_list = qw|./df/utf8.st ./df/utf8.tch|;
my $word = '';
my $num_of_documents = 250_0000_0000;

my %config = (
idf_type => 1,
df_file => $df_file,
fetch_df => 0,
);

my $webidf = Lingua::JA::WebIDF->new(%config);
$webidf->db_open;
my $df = $webidf->df($word);
is( $webidf->idf($word), log($num_of_documents / $df) );
$webidf->db_close;

$config{idf_type} = 2;
$webidf = Lingua::JA::WebIDF->new(%config);
$webidf->db_open;
$df = $webidf->df($word);
is( $webidf->idf($word), log( ($num_of_documents - $df + 0.5) / ($df + 0.5) ) );
$webidf->db_close;

$config{idf_type} = 3;
$webidf = Lingua::JA::WebIDF->new(%config);
$webidf->db_open;
$df = $webidf->df($word);
is( $webidf->idf($word), log( ($num_of_documents + 0.5) / ($df + 0.5) ) );
$webidf->db_close;
for my $df_file (@df_file_list)
{
subtest $df_file => sub {

plan skip_all => "TokyoCabinet is not installed."
if (!$IS_TOKYOCABINET_INSTALLED && $df_file =~ /\.tch$/);

my $driver = ($df_file =~ /\.tch$/) ? 'TokyoCabinet' : 'Storable';

my %config = (
driver => $driver,
idf_type => 1,
df_file => $df_file,
fetch_df => 0,
verbose => 0,
);

my $webidf = Lingua::JA::WebIDF->new(%config);
$webidf->db_open;
my $df = $webidf->df($word);
is( $webidf->idf($word), log($num_of_documents / $df) );
$webidf->db_close;

$config{idf_type} = 2;
$webidf = Lingua::JA::WebIDF->new(%config);
$webidf->db_open;
$df = $webidf->df($word);
is( $webidf->idf($word), log( ($num_of_documents - $df + 0.5) / ($df + 0.5) ) );
$webidf->db_close;

$config{idf_type} = 3;
$webidf = Lingua::JA::WebIDF->new(%config);
$webidf->db_open;
$df = $webidf->df($word);
is( $webidf->idf($word), log( ($num_of_documents + 0.5) / ($df + 0.5) ) );
$webidf->db_close;
};
}

done_testing;

0 comments on commit fd914aa

Please sign in to comment.