# Stephanie Taylor # CS152 Fall 2017 # Project 5 # This module tests the cullElephants function in elephant.py # Call it like this: # python test_cullElephants.py # Because I have seeded the random number generator, the output will be identical every time you run it. You should see: # Test 1 # We needed to cull 1000 elephants and 1000 elephants were culled # Out of 1000 calves before culling, 933 remain after culling. # # Test 2 # We needed to cull 0 elephants and 0 elephants were culled # Out of 0 calves before culling, 0 remain after culling. # # Test 3 # We needed to cull 10000 elephants and 10000 elephants were culled # Out of 10000 calves before culling, 5019 remain after culling. # # Test 4 # We didn't need to cull any elephants because the carrying capacity is 10000 and there were only 9900 elephants. There are now 9900 elephants # If you would like it to change from run to run, then comment out the call to random.seed. import elephant import random def testCulling( population_overrun ): calving_interval = 3.1 max_juvenile_age = 12 max_adult_age = 60 p_calf_survival = 0.85 p_adult_survival = 0.996 p_senior_survival = 0.2 carrying_capacity = 10000 pcnt_darted = 0.0 params = [calving_interval, pcnt_darted, max_juvenile_age, max_adult_age, p_calf_survival, p_adult_survival, p_senior_survival, carrying_capacity] # Create a new, undarted population pop = [] for i in range( carrying_capacity ): # Create a new adult elephant. pop.append( [random.choice(['m','f']), random.randint(max_juvenile_age+1, max_adult_age ), 0, 0] ) if population_overrun >= 0: for i in range( population_overrun ): # Create a new calf. pop.append( [random.choice(['m','f']), 1, 0, 0] ) (pop,numCulled) = elephant.cullElephants( params, pop ) print "We needed to cull %d elephants and %d elephants were culled" % (population_overrun, numCulled) # If the population has been properly culled, there will still be calves. numCalves = 0 for e in pop: if e[1] == 1: numCalves += 1 print "Out of %d calves before culling, %d remain after culling." % (population_overrun, numCalves) elif population_overrun < 0: for i in range( -population_overrun ): pop.pop() (newpop,numCulled) = elephant.cullElephants( params, pop ) print "We didn't need to cull any elephants because the carrying capacity is %d and there were only %d elephants. There are now %d elephants" % (carrying_capacity, len(pop), len(newpop)) def main(): random.seed( 0 ) # Test the function with a population overrun of 1000 print "Test 1" testCulling( 1000 ) # Test the function with a population overrun of 0 print "\nTest 2" testCulling( 0 ) # Test the function with a population overrun of 10000 print "\nTest 3" testCulling( 10000 ) print "\nTest 4" testCulling( -100 ) if __name__ == '__main__': main()