Sokoban Solver / modules / Shlomif-Sokoban-Solver / t / 01-loading.t

use strict;
use warnings;

use Test::More tests => 26;

use Shlomif::Sokoban::Solver::Board;

my $board_contents = <<'EOF';
  ####
  #  #
  #  ####
###$.$  #
#  .@.  #
#  $.$###
####  #
   #  #
   ####
EOF

{
    my $board = Shlomif::Sokoban::Solver::Board->load($board_contents);

    # TEST
    is ($board->width(), 9, "Testing the board width");
    # TEST
    is ($board->height(), 9, "Testing the board height");

    # TEST
    ok ($board->is_wall(2, 0), "Testing if 2,0 is a wall");
    # TEST
    ok ($board->is_wall(3, 0), "3,0 is a wall");
    # TEST
    ok ($board->is_wall(5, 0), "5,0 is a wall");
    # TEST
    ok (!$board->is_wall(6, 0), "6,0 is not a wall");
    # TEST
    ok (!$board->is_wall(3, 1), "3,1 is not a wall");
    # TEST
    ok ($board->is_wall(2, 3), "2,3 is a wall");
    # TEST
    ok ($board->is_wall(7, 5), "7,5 is a wall");
    # TEST
    ok (!$board->is_wall(4, 7), "4,7 is not a wall");
    # TEST
    ok ($board->is_dest(4, 3), "4,7 is a dest");
    # TEST
    ok (!$board->is_wall(4, 3), "4,3 (which is a dest) is not a wall");
    # TEST
    is_deeply($board->_dests(),
        [[4,3], [3,4], [5,4], [4,5]],
    );

    my $ok_box = sub {
        local $Test::Builder::Level = $Test::Builder::Level + 1;
        my ($x, $y, $msg) = @_;

        ok ($board->is_box($board->_init_state(), $x, $y), $msg);
    };

    # TEST
    $ok_box->(3, 3, "3,3 is a box");
    # TEST
    $ok_box->(3, 5, "3,5 is a box");
    # TEST
    $ok_box->(5, 3, "5,3 is a box");
    # TEST
    $ok_box->(5, 5, "5,5 is a box");

    my $not_box = sub {
        local $Test::Builder::Level = $Test::Builder::Level + 1;
        my ($x, $y, $msg) = @_;

        ok (!$board->is_box($board->_init_state(), $x, $y), $msg);
    };
    
    # TEST
    $not_box->(3, 4, "3,4 is not a box");
    # TEST
    $not_box->(4, 3, "4,3 is not a box");
    # TEST
    $not_box->(2, 4, "2,4 is not a box");

    my $ok_reach = sub {
        local $Test::Builder::Level = $Test::Builder::Level + 1;
        my ($x, $y, $msg) = @_;

        ok ($board->is_reachable($board->_init_state(), $x, $y), $msg);
    };

    # TEST
    $ok_reach->(3, 4, "3,4 is reachable");
    # TEST
    $ok_reach->(2, 4, "2,4 is reachable");

    my $not_reach = sub {
        local $Test::Builder::Level = $Test::Builder::Level + 1;
        my ($x, $y, $msg) = @_;

        ok (!$board->is_reachable($board->_init_state(), $x, $y), $msg);
    };

    
    # TEST
    $not_reach->(1, 2, "1,2 is not reachable since it's outside the walls");
    # TEST
    $not_reach->(3, 3, "3,3 is not reachable since it's a box");
    # TEST
    $not_reach->(3, 0, "3,0 is not reachable since it's a wall");

    my $rotated = ${$board->_rotate($board->_init_state())};
    my $init = ${$board->_init_state()};

    # Trim the trailing zeros.
    foreach ($rotated, $init)
    {
        s{\0+\z}{};
    }

    # TEST
    is ($rotated, $init, 
        "Rotation is OK",
    );
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.