Created
July 24, 2008 09:38
-
-
Save larrytheliquid/2099 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module BugWalkSimulation | |
describe Bug, "#dead?" do | |
it "should be false when the number of moves is less than the moves lifetime" do | |
bug = new_bug(:moves_lifetime => 3) | |
bug.expects(:number_of_moves).returns(2) | |
bug.should_not be_dead | |
end | |
it "should be true when the number of moves is greater than the moves lifetime" do | |
bug = new_bug(:moves_lifetime => 2) | |
bug.expects(:number_of_moves).returns(3) | |
bug.should be_dead | |
end | |
it "should be true when the number of moves is equal to the moves lifetime" do | |
bug = new_bug(:moves_lifetime => 2) | |
bug.expects(:number_of_moves).returns(2) | |
bug.should be_dead | |
end | |
end | |
describe Bug, "#has_achieved_goal?" do | |
it "should be false when no goal function exists" do | |
new_bug(:goal => nil).should_not have_achieved_goal | |
end | |
it "should be false when the goal function is false" do | |
new_bug(:goal => lambda {|bug| false } ).should_not have_achieved_goal | |
end | |
it "should be true whent the goal function is true" do | |
new_bug(:goal => lambda {|bug| true } ).should have_achieved_goal | |
end | |
it "should yield the bug to the goal function" do | |
bug = new_bug(:goal => lambda {|yielded_bug| yielded_bug.should == bug } ) | |
bug.has_achieved_goal? | |
end | |
end | |
describe Bug, "#move" do | |
describe "when dead" do | |
before(:each) do | |
@bug = new_bug(:moves_lifetime => 1) | |
@bug.stubs(:number_of_moves).returns(1) | |
end | |
it "should be nil" do | |
@bug.move.should be_nil | |
end | |
it "should not increment the number of moves" do | |
lambda { @bug.move }.should_not change(@bug, :number_of_moves) | |
end | |
it "should not step on a tile" do | |
@bug.floor.expects(:step_on_tile).never | |
@bug.move | |
end | |
end | |
describe "when goal has been achieved" do | |
before(:each) do | |
@bug = new_bug(:goal => lambda {|bug| true }) | |
end | |
it "should be nil" do | |
@bug.move.should be_nil | |
end | |
it "should not increment the number of moves" do | |
lambda { @bug.move }.should_not change(@bug, :number_of_moves) | |
end | |
it "should not step on a tile" do | |
@bug.floor.expects(:step_on_tile).never | |
@bug.move | |
end | |
end | |
describe "when the move thought of is not possible" do | |
before(:each) do | |
@bug = new_bug(:floor => new_floor(:height => 2, :width => 2), :location => [0, 0]) | |
@bug.stubs(:think_of_move).returns([0, -1], [-1, 0], [0, 1]) | |
end | |
it "should continue thinking of new moves until a possible one is thought of" do | |
@bug.expects(:think_of_move).returns([0, -1], [-1, 0], [0, 1]) | |
@bug.move | |
end | |
it "should return the location of the first valid move thought of" do | |
@bug.move.should == [0, 1] | |
end | |
it "should change location to the first valid move thought of" do | |
@bug.move | |
@bug.location.should == [0, 1] | |
end | |
end | |
describe "when the move thought of is possible" do | |
before(:each) do | |
@bug = new_bug(:floor => new_floor(:height => 2, :width => 2), :location => [0, 0]) | |
@bug.stubs(:think_of_move).returns([0, 1]) | |
end | |
it "should think of a move" do | |
@bug.expects(:think_of_move).returns([0, 1]) | |
@bug.move | |
end | |
it "should increment the number of moves" do | |
lambda { @bug.move }.should change(@bug, :number_of_moves).by(1) | |
end | |
it "should step on a tile" do | |
@bug.floor.expects(:step_on_tile).returns([0, 1]) | |
@bug.move | |
end | |
it "should return the location of the tile stepped on" do | |
@bug.move.should == [0, 1] | |
end | |
it "should change location to the tile stepped on" do | |
@bug.move | |
@bug.location.should == [0, 1] | |
end | |
end | |
end | |
describe Bug, ".new" do | |
it "should have a settable floor" do | |
floor = new_floor | |
new_bug(:floor => floor).floor.should == floor | |
end | |
it "should have a settable location" do | |
new_bug(:location => [3, 4]).location.should == [3, 4] | |
end | |
it "should have settable possible moves" do | |
new_bug(:possible_moves => [[-1, -2]]).possible_moves.should == [[-1, -2]] | |
end | |
it "should have settable a moves lifetime limit" do | |
moves_lifetime = 1337 | |
new_bug(:moves_lifetime => moves_lifetime).moves_lifetime.should == moves_lifetime | |
end | |
it "should have a settable goal function" do | |
goal_achieved = lambda{|bug| true } | |
new_bug(:goal => goal_achieved).goal.should == goal_achieved | |
goal_not_achieved = lambda{|bug| false } | |
new_bug(:goal => goal_not_achieved).goal.should == goal_not_achieved | |
end | |
it "should have 0 number of moves by default" do | |
new_bug.number_of_moves.should == 0 | |
end | |
end | |
describe Bug, "#think_of_move, with moves north, south, east, west, northeast, northwest, southeast, and southwest" do | |
before(:each) do | |
@bug = new_bug(:possible_moves => [[-1, 0], [1, 0], [0, -1], [0, 1], [-1, -1], [-1, 1], [1, -1], [1, 1]]) | |
@timeout = 3 | |
srand(1337) | |
end | |
it "should be able to think of move north" do | |
lambda do | |
Timeout.timeout(@timeout) { think_of_move = @bug.think_of_move until think_of_move == [-1, 0] } | |
end.should_not raise_error | |
end | |
it "should be able to think of move south" do | |
lambda do | |
Timeout.timeout(@timeout) { think_of_move = @bug.think_of_move until think_of_move == [1, 0] } | |
end.should_not raise_error | |
end | |
it "should be able to think of move west" do | |
lambda do | |
Timeout.timeout(@timeout) { think_of_move = @bug.think_of_move until think_of_move == [0, -1] } | |
end.should_not raise_error | |
end | |
it "should be able to think of move east" do | |
lambda do | |
Timeout.timeout(@timeout) { think_of_move = @bug.think_of_move until think_of_move == [0, 1] } | |
end.should_not raise_error | |
end | |
it "should be able to think of move northwest" do | |
lambda do | |
Timeout.timeout(@timeout) { think_of_move = @bug.think_of_move until think_of_move == [-1, -1] } | |
end.should_not raise_error | |
end | |
it "should be able to think of move northeast" do | |
lambda do | |
Timeout.timeout(@timeout) { think_of_move = @bug.think_of_move until think_of_move == [-1, 1] } | |
end.should_not raise_error | |
end | |
it "should be able to think of move southwest" do | |
lambda do | |
Timeout.timeout(@timeout) { think_of_move = @bug.think_of_move until think_of_move == [1, -1] } | |
end.should_not raise_error | |
end | |
it "should be able to think of move southeast" do | |
lambda do | |
Timeout.timeout(@timeout) { think_of_move = @bug.think_of_move until think_of_move == [1, 1] } | |
end.should_not raise_error | |
end | |
end | |
describe Bug, "#walk" do | |
it "should move until it cannot" do | |
bug = new_bug(:floor => new_floor(:height => 2, :width => 2), :location => [0, 0]) | |
bug.expects(:move).returns(nil) | |
bug.walk | |
end | |
it "should return the number of moves made" do | |
bug = new_bug(:floor => new_floor(:height => 2, :width => 2), :location => [0, 0], :moves_lifetime => 3) | |
srand(1337) | |
number_of_moves = Timeout.timeout(3) { bug.walk } | |
number_of_moves.should >= 0 | |
number_of_moves.should <= 3 | |
number_of_moves.should == bug.number_of_moves | |
end | |
end | |
describe Floor, "#explored?" do | |
before(:each) do | |
@floor = new_floor(:height => 2, :width => 2) | |
end | |
it "should be false by default" do | |
@floor.should_not be_explored | |
end | |
it "should be false with partial exploration" do | |
@floor.tiles[0][0] = 5 | |
@floor.tiles[0][1] = 47 | |
@floor.tiles[1][1] = 18 | |
@floor.should_not be_explored | |
end | |
it "should be true with complete exploration" do | |
@floor.tiles[0][0] = 5 | |
@floor.tiles[0][1] = 47 | |
@floor.tiles[1][0] = 22 | |
@floor.tiles[1][1] = 18 | |
@floor.should be_explored | |
end | |
end | |
describe Floor, "#height" do | |
before(:each) do | |
@floor = new_floor(:height => 7) | |
end | |
it "should be equal to the initialized height of the floor" do | |
@floor.height.should == 7 | |
end | |
end | |
describe Floor, ".new" do | |
before(:each) do | |
@floor = new_floor | |
end | |
describe "#tiles" do | |
it "should be a kind of Array" do | |
@floor.tiles.should be_kind_of(Array) | |
end | |
it "should contain kinds of sub-arrays" do | |
@floor.tiles.first.should be_kind_of(Array) | |
end | |
it "should have all 0s by default" do | |
@floor.tiles.each do |column| | |
column.each {|tile| tile.should == 0} | |
end | |
end | |
end | |
end | |
describe Floor, "#random_tile" do | |
before(:each) do | |
@floor = new_floor | |
end | |
it "should be a kind of Array" do | |
@floor.random_tile.should be_kind_of(Array) | |
end | |
it "should have two elements" do | |
@floor.random_tile.size.should == 2 | |
end | |
it "should contain integers" do | |
@floor.random_tile.each {|element| element.should be_kind_of(Integer) } | |
end | |
it "should be an existing tile" do | |
valid_tile = @floor.random_tile | |
(@floor.tiles[valid_tile.first][valid_tile.last] rescue nil).should_not be_nil | |
end | |
end | |
describe Floor, "#step_on_tile" do | |
before(:each) do | |
@floor = new_floor(:height => 1, :width => 1) | |
end | |
describe "when stepping outside the bounds of the floor" do | |
it "should be nil when negatively out of height bounds" do | |
@floor.step_on_tile([-1, 0]).should be_nil | |
end | |
it "should be nil when negatively out of width bounds" do | |
@floor.step_on_tile([0, -1]).should be_nil | |
end | |
it "should be nil when negatively out of height and width bounds" do | |
@floor.step_on_tile([-1, -1]).should be_nil | |
end | |
it "should be nil when positively out of height bounds" do | |
@floor.step_on_tile([1, 0]).should be_nil | |
end | |
it "should be nil when positively out of width bounds" do | |
@floor.step_on_tile([0, 1]).should be_nil | |
end | |
it "should be nil when positively out of height and width bounds" do | |
@floor.step_on_tile([1, 1]).should be_nil | |
end | |
end | |
describe "when stepping inside the bounds of the floor" do | |
it "should be the incremented number for that tile" do | |
@floor.step_on_tile([0, 0]).should == 1 | |
end | |
it "should increment the tile" do | |
@floor.step_on_tile([0, 0]) | |
@floor.tiles[0][0].should == 1 | |
@floor.step_on_tile([0, 0]) | |
@floor.tiles[0][0].should == 2 | |
end | |
end | |
end | |
describe Floor, "#steps" do | |
before(:each) do | |
@floor = new_floor(:height => 2, :width => 2) | |
end | |
describe "by default" do | |
it "should be 0" do | |
@floor.steps.should == 0 | |
end | |
end | |
describe "with traveled floors" do | |
it "should be the total number of steps on each tile" do | |
@floor.tiles[0][0] = 3 | |
@floor.tiles[1][0] = 32 | |
@floor.tiles[1][1] = 9 | |
@floor.steps.should == 44 | |
end | |
end | |
end | |
describe Floor, "#width" do | |
before(:each) do | |
@floor = new_floor(:width => 7) | |
end | |
it "should be equal to the initialized width of the floor" do | |
@floor.width.should == 7 | |
end | |
end | |
describe Simulator, ".new" do | |
it "should have a Floor" do | |
new_simulator.floor.should be_kind_of(Floor) | |
end | |
it "should have a Bug" do | |
new_simulator.bug.should be_kind_of(Bug) | |
end | |
it "should assign a bug to a Floor" do | |
new_simulator.bug.floor.should be_kind_of(Floor) | |
end | |
describe "with a randomly set Bug" do | |
it "should set its bug location to a random Floor tile" do | |
Floor.any_instance.expects(:random_tile).returns([0, 0]) | |
new_simulator.bug.location.should == [0, 0] | |
end | |
it "should make the initial location stepped on" do | |
Floor.any_instance.expects(:random_tile).returns([0, 0]) | |
simulator = new_simulator | |
simulator.floor.tiles[simulator.bug.location.first][simulator.bug.location.last].should == 1 | |
end | |
end | |
describe "with an explicitly set Bug" do | |
it "should set its bug location to the specified Floor tile" do | |
new_simulator(:location => [0, 0]).bug.location.should == [0, 0] | |
end | |
it "should make the initial location stepped on" do | |
simulator = new_simulator(:location => [0, 0]) | |
simulator.floor.tiles[simulator.bug.location.first][simulator.bug.location.last].should == 1 | |
end | |
end | |
end | |
describe Simulator, "#simulate" do | |
before(:each) do | |
srand(1337) | |
end | |
describe "when the bug dies" do | |
before(:each) do | |
@simulator = new_simulator(:height => 40, :width => 20, :moves_lifetime => 3, :possible_moves => [[-1, 0], [1, 0], [0, -1], [0, 1]]) | |
end | |
it "should complete" do | |
lambda { Timeout.timeout(3) { @simulator.simulate } }.should_not raise_error | |
end | |
end | |
describe "when the bug reaches its goal" do | |
before(:each) do | |
@simulator = new_simulator(:height => 2, :width => 2, :goal => lambda{|bug| bug.floor.explored? }, :moves_lifetime => 1.0/0, :possible_moves => [[-1, 0], [1, 0], [0, -1], [0, 1]]) | |
end | |
it "should complete" do | |
lambda { Timeout.timeout(3) { @simulator.simulate } }.should_not raise_error | |
end | |
end | |
end | |
describe Simulator, "#steps" do | |
before(:each) do | |
@simulator = new_simulator | |
end | |
it "should be 1 by default" do | |
@simulator.steps.should == 1 | |
end | |
it "should delegate to its Floors steps" do | |
@simulator.floor.expects(:steps).returns(1337) | |
@simulator.steps.should == 1337 | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment