Commit fae09b30 authored by David A.. Werner's avatar David A.. Werner
Browse files

basic py.test for Manducas.py

parent 0a49330e
......@@ -44,6 +44,7 @@ nosetests.xml
coverage.xml
*,cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
......
import pytest
import numpy as np
#import random
from manduca import Manduca, SimpleManduca, ManducaDeformityError
num_legs = range(2,5)
num_time_steps = range(2,10)
@pytest.mark.parametrize("num_time_steps", num_time_steps)
@pytest.mark.parametrize("num_legs", num_legs)
def test_too_many_muscles(num_legs, num_time_steps):
legs = np.zeros((num_time_steps,num_legs))
muscles = np.zeros((num_time_steps,num_legs))
with pytest.raises(ManducaDeformityError):
Manduca(legs, muscles, num_time_steps)
@pytest.mark.parametrize("num_time_steps", num_time_steps)
@pytest.mark.parametrize("num_legs", num_legs)
def test_too_few_muscles(num_legs, num_time_steps):
legs = np.zeros((num_time_steps,num_legs))
muscles = np.zeros((num_time_steps-2,num_legs))
with pytest.raises(ManducaDeformityError):
Manduca(legs, muscles, num_time_steps)
@pytest.mark.parametrize("num_time_steps", num_time_steps)
@pytest.mark.parametrize("num_legs", num_legs)
def test_well_formed(num_legs, num_time_steps):
legs = np.zeros((num_time_steps,num_legs))
muscles = np.zeros((num_time_steps,num_legs-1))
Manduca(legs, muscles, num_time_steps)
@pytest.mark.parametrize("compressed", [True, False])
@pytest.mark.parametrize("num_time_steps", [2,5,10,100])
@pytest.mark.parametrize("num_legs", [2, 5, 10])
@pytest.mark.parametrize("time_step", [0.01, 1.0, 10])
def test_simple_save(tmpdir, num_legs, num_time_steps, time_step, compressed):
file_name = tmpdir.join('manduca.npz').strpath
man = SimpleManduca.random_individual(num_legs, num_time_steps, time_step, muscle_strength=100)
man.save(file_name, compressed=compressed)
new_man = SimpleManduca.load(file_name, muscle_strength=100)
assert new_man == man
@pytest.mark.parametrize("loaded_muscle_strength", [0.1, 1.0, 100])
@pytest.mark.parametrize("muscle_strength", [0.1, 1.0, 100])
@pytest.mark.parametrize("compressed", [True, False])
def test_simple_save_with_different_muscle_strengths(tmpdir, muscle_strength, loaded_muscle_strength, compressed):
time_step, num_time_steps = 1, 1
num_legs = 2
file_name = tmpdir.join('manduca.npz').strpath
man = SimpleManduca.random_individual(num_legs, num_time_steps, time_step,
muscle_strength=muscle_strength)
man.save(file_name, compressed=compressed)
if muscle_strength != loaded_muscle_strength:
with pytest.raises(AssertionError):
new_man = SimpleManduca.load(file_name, muscle_strength=loaded_muscle_strength)
# If all of the muscles are 0, then it will accept any muscle_strength
ok_to_fail = new_man.muscles.max() == 0
# We expect assertion error, so raise one if we expected this kind of failure
test_failed_as_expected = not(ok_to_fail)
assert test_failed_as_expected
# The actual muscle_strength should be auto-detected, UNLESS all values are 0
try:
new_man = SimpleManduca.load(file_name)
assert new_man == man
except AssertionError:
# This should only happen if all values are 0, so ensure that is the case
new_man = SimpleManduca.load(file_name, muscle_strength=muscle_strength)
assert np.count_nonzero(new_man.muscles) == 0
assert new_man == man
else:
new_man = SimpleManduca.load(file_name, muscle_strength=loaded_muscle_strength)
assert new_man == man
@pytest.mark.parametrize("compressed", [True, False])
@pytest.mark.parametrize("fitness", [None, 0.01, 1.0, 10])
def test_simple_save(tmpdir, fitness, compressed):
time_step, num_time_steps, num_legs = 1, 1, 2
file_name = tmpdir.join('manduca.npz').strpath
man = SimpleManduca.random_individual(num_legs, num_time_steps, time_step, muscle_strength=100)
# Manualy set fitness
man._fitness = fitness
man.save(file_name, compressed=compressed)
new_man = SimpleManduca.load(file_name, muscle_strength=100)
assert new_man._fitness == man._fitness
assert new_man._fitness == fitness
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment