def test_creatures_starting_hunger(self): test_creature = Creature((0, 0), 0) test_world = World(5, 5) sim = Simulation(world_dim=5, num_foods=5, creature_location=(0, 0), creature_hunger_level=0) sim.world = test_world sim.creature = test_creature prev_hunger = test_creature.get_hunger_level() for i in range(5): self.assertTrue(prev_hunger <= test_creature.get_hunger_level()) prev_hunger = test_creature.get_hunger_level() self.assertEqual(test_creature.get_hunger_level(), sim.creature.get_hunger_level())
def test_simulation_with_new_world_instance(self): ''' This test is mostly to make sure hunger is growing properly in the simulation ''' test_creature = Creature([0, 0], 0) test_world = World(5, 0) sim = Simulation(world_dim=5, num_foods=0, creature_location=(0, 0), creature_hunger_level=0) sim.world = test_world sim.creature = test_creature hunger = test_creature.get_hunger_level() for steps in range(100): sim.step() self.assertEqual(test_creature.get_hunger_level(), hunger + HUNGER_GROWTH) hunger += HUNGER_GROWTH
def test_move_and_eat_random_locations_hungry(self): for test_num in range(0, 100): world = [[CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], ] food_level = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world test_world.food_level = food_level creature_row = randrange(0, WORLD_DIM - 1) creature_col = randrange(0, WORLD_DIM - 1) test_creature = Creature((creature_row, creature_col), 1) food_row = randrange(0, WORLD_DIM - 1) food_col = randrange(0, WORLD_DIM - 1) food_location = [food_row, food_col] test_world.set_cell(food_location[0], food_location[1], CELL_FOOD) food_level[food_row][food_col] = FOOD_GROWTH test_creature.move_and_eat(test_world, food_location) self.assertEqual(test_creature.get_hunger_level(), 1 - FOOD_GROWTH,"A creature (hunger level:1) at a random position moves and eat a food (level:0.025). Then the creature's hunger level becomes 1-0.025") self.assertEqual(test_world.get_food_level(food_row, food_col), FOOD_DEFAULT, "After being eaten, the food level goes back to the FOOD_DEFAULT value.")
def test_move_and_eat_random_locations_hungry(self): for test_num in range(0, 100): world = [[CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], ] food_level = [['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', '']] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world test_world.food_level = food_level creature_row = randrange(0, WORLD_DIM - 1) creature_col = randrange(0, WORLD_DIM - 1) test_creature = Creature((creature_row, creature_col), 1) food_row = randrange(0, WORLD_DIM - 1) food_col = randrange(0, WORLD_DIM - 1) food_location = [food_row, food_col] test_world.set_cell(food_location[0], food_location[1], CELL_FOOD) food_level[food_row][food_col] = FOOD_GROWTH test_creature.move_and_eat(test_world, food_location) self.assertEqual(test_creature.get_hunger_level(), 1 - FOOD_GROWTH,"A creature (hunger level:1) at a random position moves and eat a food (level:0.025). Then the creature's hunger level becomes 1-0.025") self.assertEqual(test_world.get_food_level(food_row, food_col), FOOD_DEFAULT, "After being eaten, the food level goes back to the FOOD_DEFAULT value.")
def test_move_and_eat_random_locations_not_hungry(self): for test_num in range(0, 100): world = [[CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], ] food_level = [['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', '']] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world test_world.food_level = food_level creature_row = randrange(0, WORLD_DIM - 1) creature_col = randrange(0, WORLD_DIM - 1) test_creature = Creature((creature_row, creature_col), 0) food_row = randrange(0, WORLD_DIM - 1) food_col = randrange(0, WORLD_DIM - 1) food_location = [food_row, food_col] test_world.set_cell(food_location[0], food_location[1], CELL_FOOD) food_level[food_row][food_col] = FOOD_GROWTH test_creature.move_and_eat(test_world, food_location) self.assertEqual(test_creature.get_hunger_level(), 0, "If the creature is not hungry, move_and_eat method does not change its hunger level.") self.assertEqual(test_world.get_food_level(food_row, food_col), FOOD_GROWTH, "If the creature is not hungry, move_and_eat method does not change the level of the food.")
def test_move_and_eat_random_locations_not_hungry(self): for test_num in range(0, 100): world = [[CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], ] food_level = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] test_world = World(WORLD_DIM, NUM_FOODS) test_world.grid = world test_world.food_level = food_level creature_row = randrange(0, WORLD_DIM - 1) creature_col = randrange(0, WORLD_DIM - 1) test_creature = Creature((creature_row, creature_col), 0) food_row = randrange(0, WORLD_DIM - 1) food_col = randrange(0, WORLD_DIM - 1) food_location = [food_row, food_col] test_world.set_cell(food_location[0], food_location[1], CELL_FOOD) food_level[food_row][food_col] = FOOD_GROWTH test_creature.move_and_eat(test_world, food_location) self.assertEqual(test_creature.get_hunger_level(), 0, "If the creature is not hungry, move_and_eat method does not change its hunger level.") self.assertEqual(test_world.get_food_level(food_row, food_col), FOOD_GROWTH, "If the creature is not hungry, move_and_eat method does not change the level of the food.")
def test_move_and_eat_no_food(self): world = [[CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], ] food_level = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] test_world = World(6, 0) test_world.grid = world test_world.food_level = food_level test_creature = Creature([0, 1], 0) test_creature.set_hunger_level(1) test_creature.move_and_eat(test_world, [0, 0]) creature_hunger = test_creature.get_hunger_level() creature_location = test_creature.get_location() food_level = test_world.get_food_level(0, 0) self.assertEqual(1, creature_hunger, "If there's no food at the destination, move_and_eat method does not change the hunger level of the creature.") self.assertEqual([0, 1], creature_location, "If there's no food at the destination, move_and_eat method does not change the location of the creature.") self.assertEqual(0, food_level, "If there's not food at the destination, get_food_level(destination) should return 0.")
def test_move_and_eat_not_hungry(self): world = [[CELL_FOOD, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], ] food_level = [[1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] test_world = World(6, 1) test_world.grid = world test_world.food_level = food_level test_creature = Creature([0, 1], 0) test_creature.set_hunger_level(0) test_creature.move_and_eat(test_world, [0, 0]) creature_hunger = test_creature.get_hunger_level() creature_location = test_creature.get_location() food_level = test_world.get_food_level(0, 0) self.assertEqual(0, creature_hunger, "The creature (hunger level:0) moves and eats a food. Then its hunger level stays 0.") self.assertEqual([0, 1], creature_location, "If not hungry, after move_and_eat(world,[0,0]) executed, the creature should stay at [0,1].") self.assertEqual(1, food_level, "If not hungry, the creature does not move and eat the food. Thus the food level stays same (1).")
def test_move_and_eat_hungry(self): world = [[CELL_FOOD, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], [CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY], ] food_level = [[1, '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', '']] test_world = World(6, 1) test_world.grid = world test_world.food_level = food_level test_creature = Creature([0, 1], 0) test_creature.set_hunger_level(1) test_creature.move_and_eat(test_world, [0, 0]) creature_hunger = test_creature.get_hunger_level() creature_location = test_creature.get_location() food_level = test_world.get_food_level(0, 0) self.assertEqual(0, creature_hunger, "The creature (hunger level:1) moves and eats a food. Then its hunger level becomes 0.") self.assertEqual([0, 0], creature_location, "After move_and_eat(world,[0,0]) executed, the creature should be at [0,0].") self.assertEqual(FOOD_DEFAULT, food_level, "After move_and_eat(world,[0,0]) executed, the food at [0,0]'s level becomes the default value.")
def test_move_and_eat_not_hungry(self): world = [ [ CELL_FOOD, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], ] food_level = [[1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] test_world = World(6, 1) test_world.grid = world test_world.food_level = food_level test_creature = Creature([0, 1], 0) test_creature.set_hunger_level(0) test_creature.move_and_eat(test_world, [0, 0]) creature_hunger = test_creature.get_hunger_level() creature_location = test_creature.get_location() food_level = test_world.get_food_level(0, 0) self.assertEqual( 0, creature_hunger, "The creature (hunger level:0) moves and eats a food. Then its hunger level stays 0." ) self.assertEqual( [0, 1], creature_location, "If not hungry, after move_and_eat(world,[0,0]) executed, the creature should stay at [0,1]." ) self.assertEqual( 1, food_level, "If not hungry, the creature does not move and eat the food. Thus the food level stays same (1)." )
def test_move_and_eat_no_food(self): world = [ [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], ] food_level = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] test_world = World(6, 0) test_world.grid = world test_world.food_level = food_level test_creature = Creature([0, 1], 0) test_creature.set_hunger_level(1) test_creature.move_and_eat(test_world, [0, 0]) creature_hunger = test_creature.get_hunger_level() creature_location = test_creature.get_location() food_level = test_world.get_food_level(0, 0) self.assertEqual( 1, creature_hunger, "If there's no food at the destination, move_and_eat method does not change the hunger level of the creature." ) self.assertEqual( [0, 1], creature_location, "If there's no food at the destination, move_and_eat method does not change the location of the creature." ) self.assertEqual( 0, food_level, "If there's not food at the destination, get_food_level(destination) should return 0." )
def test_move_and_eat_hungry(self): world = [ [ CELL_FOOD, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], [ CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY, CELL_EMPTY ], ] food_level = [[1, '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', ''], ['', '', '', '', '', '']] test_world = World(6, 1) test_world.grid = world test_world.food_level = food_level test_creature = Creature([0, 1], 0) test_creature.set_hunger_level(1) test_creature.move_and_eat(test_world, [0, 0]) creature_hunger = test_creature.get_hunger_level() creature_location = test_creature.get_location() food_level = test_world.get_food_level(0, 0) self.assertEqual( 0, creature_hunger, "The creature (hunger level:1) moves and eats a food. Then its hunger level becomes 0." ) self.assertEqual( [0, 0], creature_location, "After move_and_eat(world,[0,0]) executed, the creature should be at [0,0]." ) self.assertEqual( FOOD_DEFAULT, food_level, "After move_and_eat(world,[0,0]) executed, the food at [0,0]'s level becomes the default value." )
def test_get_hunger_level_one(self): test_creature = Creature([0, 0], 1) self.assertEqual(test_creature.get_hunger_level(), 1, "Testing Creature([0,0],1) initialization.")
def test_get_hunger_level_zero(self): test_creature = Creature([0, 0], 0) self.assertEqual(test_creature.get_hunger_level(), 0, "Testing Creature([0,0],0) initialization.")