AI-Evolve-Befunge

 view release on metacpan or  search on metacpan

t/10migration.t  view on Meta::CPAN

#!/usr/bin/perl
use strict;
use warnings;

use Carp;
use Cwd;
use File::Temp qw(tempfile);
use IO::Select;
use IO::Socket::INET;
use POSIX qw(sysconf _SC_OPEN_MAX);
use Test::More;
use Test::Exception;
use Test::MockRandom {
    rand => [qw(AI::Evolve::Befunge::Population Algorithm::Evolutionary::Wheel)],
    srand => { main => 'seed' },
    oneish => [qw(main)]
};
use Time::HiRes qw(sleep);

my $incoming; # lines of migration data sent by Population.pm
my $serverpid;
my $port = spawn_test_server();
my($temp, $tempfn) = tempfile();
$temp->print(<<"EOF");
migrationd_host: 127.0.0.1
migrationd_port: $port
popsize: 3
EOF

$ENV{AIEVOLVEBEFUNGE} = $tempfn;

require AI::Evolve::Befunge::Population;

AI::Evolve::Befunge::Util::push_quiet(1);

my $num_tests;
BEGIN { $num_tests = 0; };
plan tests => $num_tests;


# constructor
throws_ok(sub { AI::Evolve::Befunge::Migrator->new() }, qr/'Local' parameter/, 'dies without Local');
BEGIN { $num_tests += 1 };


my $quit1    = "q";
my $scorer1 = "[   @]02M^]20M^]11M^" . (' 'x605);
my $scorer2 = "[   @]22M^]21M^]20M^" . (' 'x605);
my $scorer3 = "[@  <]02M^]20M^]11M^" . (' 'x605);

# migrate (input overrun)
my $population = AI::Evolve::Befunge::Population->load('t/savefile');
is(scalar @{$population->blueprints}, 3, "3 critters to start with");
$population->host('whee');
$population->popsize(5);
sleep(0.25);
seed(0.85);
alarm(3);
$population->migrate();
is($incoming->getline, '[I-4 D4 F3 Hnot_test1]'.$scorer3."\n", 'migration exported a critter');
alarm(0);
my $ref = $population->blueprints;
is(scalar @$ref, 8, 'there are now 8 blueprints in list');
BEGIN { $num_tests += 3 };
my @expected_results = (
    {id => -4,  code => $scorer3,  fitness =>  3, host => 'not_test1'},
    {id => -2,  code => $scorer2,  fitness =>  2, host => 'not_test'},
    {id => -10, code => $quit1,    fitness =>  1, host => 'test'},
    {id => 12345, code => 'abcdefgh', fitness => 31,  host => 'test2'},
    {id => 12346, code => 'abcdefgi', fitness => 30,  host => 'test2'},
    {id => 12347, code => 'abcdefgj', fitness => 29,  host => 'test2'},
    {id => 12348, code => 'abcdefgk', fitness => 28,  host => 'test2'},
    {id => 12349, code => 'abcdefgl', fitness => 27,  host => 'test2'},



( run in 0.696 second using v1.01-cache-2.11-cpan-437f7b0c052 )