def test_breed_low_weight():
    """Test that a herbivore with a low weight cannot breed"""
    herbivore = ani.Herbivores()
    herbivore.weight = 1
    cell = topo.Jungle()
    cell.add_animal(herbivore)
    herbivore.breed(cell, 100)
    assert len(cell.herbivore_list) == 1
def test_breed_certain_probability_all_in_cell():
    """Tests that all animals breed, when the breeding-probability = 1"""
    cell = topo.Jungle()
    for _ in range(100):
        cell.add_animal(animals.Herbivores(age=10, weight=100))
        cell.add_animal(animals.Carnivores(age=10, weight=100))
    cell.breed_all_animals_in_cell()
    assert len(cell.herbivore_list) == 200
    assert len(cell.carnivore_list) == 200
def test_animal_with_fitness_level_0_dies():
    """Test that an animal with a very low fitness dies"""
    animal = animals.Herbivores(age=200, weight=1)
    cell = topo.Jungle()
    cell.add_animal(animal)
    animals.Herbivores.parameters["omega"] = 1
    cell.natural_death_all_animals_in_cell()
    animals.Herbivores.parameters["omega"] = 0.4
    assert len(cell.herbivore_list) == 0
def test_breed_certain_prob_overweight_newborn():
    """Tests that a herbivore cannot give birth to a child with
    greater weight """
    herbivore = ani.Herbivores(weight=50)
    cell = topo.Jungle()
    cell.add_animal(herbivore)
    herbivore.parameters["xi"] = 100
    herbivore.breed(cell, 1000)
    herbivore.parameters["xi"] = 1.2
    assert len(cell.herbivore_list) == 1
def test_feeding_carnivores_in_a_cell():
    """Tests if the most fit herbivore kills the least fit herbivore etc"""
    animals.Carnivores.set_parameters({"DeltaPhiMax": 0.1})
    jungle_cell = topo.Jungle()
    [jungle_cell.add_animal(animals.Herbivores()) for _ in range(10)]
    [jungle_cell.add_animal(animals.Carnivores(weight=80)) for _ in range(10)]
    assert jungle_cell.biomass_carnivores() == 800
    pre_feeding_herbi_biomass = jungle_cell.biomass_herbivores()
    jungle_cell.feed_carnivores_in_cell()
    assert pre_feeding_herbi_biomass > jungle_cell.biomass_herbivores()
def test_breed_certain_probability():
    """Test that a fit herbivore will give birth to another herbivore when the
    breeding probability = 1"""
    cell = topo.Jungle()
    herbivore = ani.Herbivores()
    herbivore.weight, herbivore.age = 80, 30
    cell.add_animal(herbivore)
    herbivore.breed(cell, 100)
    assert len(cell.herbivore_list) == 2
    assert isinstance(cell.herbivore_list[0], ani.Herbivores)
    assert isinstance(cell.herbivore_list[1], ani.Herbivores)
def test_remove_carnivore():
    """Test that a carnivore can be removed"""
    jungle_cell = topo.Jungle()
    test_carnivore = animals.Carnivores()
    jungle_cell.add_animal(test_carnivore)
    assert test_carnivore in animals.Animals.instances
    assert test_carnivore in jungle_cell.carnivore_list
    jungle_cell.remove_animal(test_carnivore)
    assert test_carnivore in animals.Animals.instances
    assert test_carnivore not in jungle_cell.carnivore_list
    animals.Animals.instances.remove(test_carnivore)
    assert test_carnivore not in animals.Animals.instances
def low_fitness_animals():
    """Creates some test instances with very low fitness"""
    jungle_cell = topo.Jungle()
    herbivore = animals.Herbivores()
    carnivore = animals.Carnivores()
    carnivore.weight, carnivore.age = 1, 1000
    herbivore.weight, herbivore.age = 1, 1000
    herbivore.parameters["omega"] = 1
    carnivore.parameters["omega"] = 1
    jungle_cell.add_animal(herbivore)
    jungle_cell.add_animal(carnivore)
    return jungle_cell
def test_breed_uncertain_probability():
    """Test a herbivore will give birth to around 400 kids, when it trys 1000
    times, when the probability for birth is around 0.4"""
    born = 0
    for _ in range(1000):
        cell = topo.Jungle()
        herbivore = ani.Herbivores()
        herbivore.weight, herbivore.age = 400, 10
        cell.add_animal(herbivore)
        herbivore.breed(cell, 3)
        if len(cell.herbivore_list) == 2:
            born += 1
    assert born > 350
    assert born < 450
def test_herbivore_grazing():
    """Test that the herbivore cannot gain weight when grazing in a desert,
    but gains weight when grazing in the jungle and savanna"""
    herbivore = ani.Herbivores()
    pre_eating_weight = herbivore.weight
    desert_cell = topo.Desert()
    herbivore.graze(desert_cell)
    assert herbivore.weight == pre_eating_weight
    jungle_cell = topo.Jungle()
    herbivore.graze(jungle_cell)
    assert herbivore.weight == pre_eating_weight + (
        herbivore.parameters["beta"] * herbivore.parameters["F"])
    after_jungle_graze = herbivore.weight
    savanna_cell = topo.Savanna()
    herbivore.graze(savanna_cell)
    assert herbivore.weight == after_jungle_graze + (
        herbivore.parameters["beta"] * herbivore.parameters["F"])
def test_feeding_herbivores_in_a_cell():
    """ Tests the method "test_feeding_herbivores_in_a_cell" where the
    least fittest animal shall in not be able to eat, due to overgrazing,
    and therefore keep the same weight after the graze commando"""
    jungle_cell = topo.Jungle()
    animals.Herbivores.parameters["sigma_birth"] = 1.5
    [jungle_cell.add_animal(animals.Herbivores()) for _ in range(81)]
    herbivore_fitness_sort = sorted(jungle_cell.herbivore_list,
                                    key=lambda herbi: herbi.fitness)
    assert herbivore_fitness_sort[0].fitness < \
        herbivore_fitness_sort[30].fitness
    least_fittest_herb = herbivore_fitness_sort[0]
    second_least_fittest_herb = herbivore_fitness_sort[1]
    least_fittest_weight = least_fittest_herb.weight
    second_least_fittest_weight = second_least_fittest_herb.weight
    jungle_cell.feed_herbivores_in_cell()
    assert least_fittest_weight == least_fittest_herb.weight
    assert second_least_fittest_weight != second_least_fittest_herb.weight
    # Test that the available fodder now are = 0, and that the increase_fodder
    # works, so the food level again = f_max
    assert jungle_cell.fodder == 0
    jungle_cell.increase_fodder()
    assert jungle_cell.fodder == 800
def basic_jungle():
    """Creates an non-populated test cell"""
    return topo.Jungle()