Algorithm-TravelingSalesman-BitonicTour
view release on metacpan or search on metacpan
lib/Algorithm/TravelingSalesman/BitonicTour.pm view on Meta::CPAN
sub new {
my $class = shift;
my $self = bless { _tour => {}, _points => {} }, $class;
return $self;
}
=head2 $ts->add_point($x,$y)
Adds a point at position (C<$x>, C<$y>) to be included in the solution. Method
C<add_point()> checks to make sure that no two points have the same
I<x>-coordinate. This method will C<croak()> with a descriptive error message
if anything goes wrong.
Example:
# add point at position (x=2, y=3) to the problem
$ts->add_point(2,3);
=cut
sub add_point {
t/01-setup.t view on Meta::CPAN
use warnings;
use Test::More 'no_plan';
use Test::Exception;
use Algorithm::TravelingSalesman::BitonicTour;
my $b = Algorithm::TravelingSalesman::BitonicTour->new;
ok($b);
is($b->N, 0);
throws_ok { $b->R } qr/Problem has no rightmost point/, '... with a nice error message';
# add a few points, making sure they're stored and sorted correctly
$b->add_point(0,0);
is($b->N, 1);
is($b->R, 0);
is_deeply( [$b->sorted_points], [[0,0]] );
$b->add_point(3,0);
is($b->N, 2);
t/01-setup.t view on Meta::CPAN
is($b->N, 4);
is($b->R, 3);
is_deeply( [$b->sorted_points], [[0,0], [1,1], [2,1], [3,0]] );
# make sure that attempts to add points with duplicate X-coordinates croak()
{
dies_ok { $b->add_point(2,1) } 'repeated X-coordinate should die';
dies_ok { $b->add_point(2,2) } 'repeated X-coordinate should die';
dies_ok { $b->add_point(2,3) } 'repeated X-coordinate should die';
throws_ok { $b->add_point(2,1) } qr/duplicates previous point/,
'with a nice error message';
}
# make sure we can retrieve coordinates correctly
is_deeply([ $b->coord( 0) ], [ 0, 0 ]);
is_deeply([ $b->coord( 1) ], [ 1, 1 ]);
is_deeply([ $b->coord( 2) ], [ 2, 1 ]);
is_deeply([ $b->coord( 3) ], [ 3, 0 ]);
is_deeply([ $b->coord(-1) ], [ 3, 0 ]); # sweet
# verify that delta() returns the correct distances between points
( run in 0.644 second using v1.01-cache-2.11-cpan-65fba6d93b7 )