# tgol4.t - Simple standalone test of Organism.pm st_ functions use strict; use warnings; use Organism; use Test::More; my $ntests = 9; plan tests => $ntests * 7; # Test cells sub test_cells { my $desc = shift; # Test description my $row = shift; # Row of cells in square tile my $cells_ref = shift; # Expected cells my $nexpected = Organism::st_count($row); my @cells = Organism::st_getlivecells($row); my $ncells = @cells; cmp_ok( $ncells, '==', $nexpected, "$desc cell count ($ncells)" ); is_deeply( \@cells, $cells_ref, "$desc cell array" ); } # Test one tick of a set of cells sub test_one { my $desc = shift; # Test description my $one_two = shift; # 1 to test one tick, 2 to test two ticks my $incells_ref = shift; # Initial input cells my $outcells_ref = shift; # Expected output cells my $expected_update = shift; # 1 if cells should change $one_two == 1 || $one_two == 2 or die "oops: invalid one_two ($one_two)"; my @incells = @{$incells_ref}; my $TILE_SIZE_FULL = $Organism::TILE_SIZE_FULL; my $TILE_SIZE_FULL_M1 = $TILE_SIZE_FULL - 1; my @rowbuf = (0) x $TILE_SIZE_FULL; my $row = \@rowbuf; # Insert initial cells Organism::st_insertcells($row, @incells); # Sanity check of initial state test_cells($desc, $row, $incells_ref); # Find top and bottom of tile (first and last non-zero row) my $top = my $bottom = $TILE_SIZE_FULL; $row->[$_] and $top = $_, last for 0..$TILE_SIZE_FULL_M1; if ($top != $TILE_SIZE_FULL) { 1 while --$bottom && !$row->[$bottom]; } $top != $TILE_SIZE_FULL or die "oops, wrong top"; ok( 1, "top & bottom sane (TILE_SIZE_FULL=$TILE_SIZE_FULL top=$top bottom=$bottom)" ); # Test st_ tile tick. my ($update_flag, $neigh) = ($one_two == 1) ? Organism::st_tiletick($row, $top, $bottom) : Organism::st_tiletwoticks($row, $top, $bottom); my $update_flag_bits = sprintf '%b', $update_flag; $update_flag = !!$update_flag; # convert to boolean cmp_ok( $update_flag, '==', $expected_update, "$desc update flag ($update_flag_bits)" ); cmp_ok( $neigh, '==', 0, "$desc neigh flag ($neigh)" ); # Check cells after tick/s test_cells($desc, $row, $outcells_ref); } { my @incells = ( [ 8, 5 ], [ 8, 6 ], [ 8, 7 ], ); my @outcells = ( [ 7, 6 ], [ 8, 6 ], [ 9, 6 ], ); @incells = sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } @incells; @outcells = sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } @outcells; test_one("Vertical Blinker one", 1, \@incells, \@outcells, 1); test_one("Vertical Blinker two", 2, \@incells, \@incells, 0); } { my @incells = ( [ 7, 6 ], [ 8, 6 ], [ 9, 6 ], ); my @outcells = ( [ 8, 5 ], [ 8, 6 ], [ 8, 7 ], ); @incells = sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } @incells; @outcells = sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } @outcells; test_one("Horizontal Blinker one", 1, \@incells, \@outcells, 1); test_one("Horizontal Blinker two", 2, \@incells, \@incells, 0); } { my @incells = ( [ 6, 6 ], [ 7, 6 ], [ 6, 7 ], [ 7, 7 ], ); @incells = sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } @incells; test_one("Simple block one", 1, \@incells, \@incells, 0); test_one("Simple block two", 2, \@incells, \@incells, 0); } { my @incells = ( [ 6, 6 ], [ 6, 7 ], [ 6, 8 ], [ 7, 6 ], [ 7, 7 ], [ 7, 8 ], [ 8, 6 ], [ 8, 7 ], [ 8, 8 ], [ 9, 6 ], ); my @outcells = ( [ 5, 7 ], [ 6, 6 ], [ 6, 8 ], [ 7, 5 ], [ 7, 9 ], [ 8, 5 ], [ 8, 8 ], [ 9, 6 ], ); my @outcells2 = ( [ 5, 7 ], [ 6, 6 ], [ 6, 7 ], [ 6, 8 ], [ 7, 5 ], [ 7, 6 ], [ 7, 7 ], [ 7, 8 ], [ 7, 9 ], [ 8, 5 ], [ 8, 6 ], ); @incells = sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } @incells; @outcells = sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } @outcells; @outcells2 = sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } @outcells2; test_one("Weird block one-a", 1, \@incells, \@outcells, 1); test_one("Weird block one-b", 1, \@outcells, \@outcells2, 1); test_one("Weird block two", 2, \@incells, \@outcells2, 1); }