Algorithm-AM
view release on metacpan or search on metacpan
t/02-DataSet.t view on Meta::CPAN
sub test_constructor {
throws_ok {
Algorithm::AM::DataSet->new();
} qr/Failed to provide 'cardinality' parameter/,
q<dies without 'cardinality' parameter>;
throws_ok {
Algorithm::AM::DataSet->new(
cardinality => 3,
foo => 'bar',
baz => 'buff'
);
} qr/Unknown parameters in DataSet constructor: baz, foo/,
'dies with unknown parameters';
lives_ok {
Algorithm::AM::DataSet->new(
cardinality => 3,
);
} 'constructor lives with normal input';
my $dataset = Algorithm::AM::DataSet->new(cardinality => 3);
is($dataset->cardinality, 3, 'cardinality set by constructor');
return;
}
# test that add_item correctly adds data to the set and validates input
# TODO: rename this something more descriptive
sub test_data {
# first check empty DataSet
my $dataset = Algorithm::AM::DataSet->new(cardinality => 3);
is($dataset->size, 0, 'new data set has 0 items');
is($dataset->num_classes, 0, 'new data set has 0 classes');
$dataset->add_item(
features => ['a','b','c'],
class => 'b',
comment => 'stuff'
);
is($dataset->size, 1,
'add_item adds 1 item to data set');
is($dataset->num_classes, 1, 'data set has 1 class');
$dataset->add_item(
features => ['a','b','d'],
class => 'c',
comment => 'stuff'
);
is($dataset->num_classes, 2, 'data set has 2 classes');
is($dataset->get_item(1)->comment, 'stuff', 'get_item');
throws_ok {
$dataset->add_item(
features => ['3','1'],
class => 'c',
comment => 'comment'
);
} qr/Expected 3 features, but found 2 in 3 1 \(comment\)/,
'add_item fails with wrong number of features';
# The error should be thrown from Tiny.pm, the caller of DataSet,
# not from DataSet (tests that @CARP_NOT is working cardinalityperly).
throws_ok {
$dataset->add_item();
} qr/Must provide 'features' parameter of type array ref.*DataSet.t/,
'add_item fails with missing features parameter';
return;
}
# test the dataset_from_file function
sub test_dataset_from_file {
subtest 'read nocommas data set' => sub {
plan tests => 5;
my $dataset = dataset_from_file(
path => path($data_dir, 'chapter_3_no_commas.txt'),
format => 'nocommas'
);
is($dataset->cardinality, 3, 'cardinality');
is($dataset->size, 5, 'size');
my $item = $dataset->get_item(0);
is($item->class, 'e', 'item class');
is_deeply($item->features, ['3', '1', '0'],
'item features');
is($item->comment, 'myFirstCommentHere');
};
subtest 'read commas data set' => sub {
plan tests => 5;
my $dataset = dataset_from_file(
path => path($data_dir, 'chapter_3_commas.txt'),
format => 'commas'
);
is($dataset->cardinality, 3, 'cardinality');
is($dataset->size, 5, 'size');
my $item = $dataset->get_item(0);
is($item->class, 'e', 'item class');
is_deeply($item->features, ['3', '1', '0'],
'item features');
is($item->comment, 'myFirstCommentHere');
};
throws_ok {
my $dataset = dataset_from_file(
path => path($data_dir, 'chapter_3_commas.txt'),
);
} qr/Failed to provide 'format' parameter/,
'fail with missing format parameter';
throws_ok {
my $dataset = dataset_from_file(
path => path($data_dir, 'chapter_3_commas.txt'),
format => 'buh'
);
} qr/Unknown value buh for format parameter \(should be 'commas' or 'nocommas'\)/,
'fail with incorrect format parameter';
throws_ok {
my $dataset = dataset_from_file(
format => 'commas'
);
} qr/Failed to provide 'path' parameter/,
'fail with missing path parameter';
throws_ok {
( run in 1.244 second using v1.01-cache-2.11-cpan-4991d5b9bd9 )