# tgol2.t - Simple blinker test of Conway Game of Life Organism class # Generate file blinker.txt with: # perl createblinker.pl 3 100 10 >xtest.tmp 2>ytest.tmp # then run this test with: # prove -v tgol2.t use strict; use warnings; use Organism; use Test::More; my $nblinks = 5; my $ntests = ( $nblinks + 1 ) * 3; plan tests => $ntests; # XXX: To read Life 1.06 file, ignore leading line containing "#Life 1.06" sub read_cells { my $fname = shift; open( my $fh, '<', $fname ) or die "error: open '$fname': $!"; map { [ split ' ' ] } <$fh>; } sub test_one { my $org = shift; my $desc = shift; my $expected = shift; my $nexpected = @{$expected}; my $ncells = $org->count(); my @cells = $org->get_live_cells(); cmp_ok( $ncells, '==', $nexpected, "$desc cell count ($ncells)" ); cmp_ok( scalar(@cells), '==', $nexpected, "$desc cell array count" ); is_deeply( \@cells, $expected, "$desc cell array" ); } # Blinker pattern my @blinker1 = read_cells('xtest.tmp'); my @blinker2 = read_cells('ytest.tmp'); my @sblinker1 = sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } @blinker1; my @sblinker2 = sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } @blinker2; # Initialization my $org = Organism->new(); $org->insert_cells(@sblinker1); test_one( $org, "initial", \@sblinker1 ); # Pattern should just blink back and forth for my $i ( 1 .. $nblinks ) { $org->tick(); test_one( $org, "blinker $i", $i % 2 ? \@sblinker2 : \@sblinker1 ); }