def test_should_apply_all_features():
    new_model = model & (
        PausedBy(partial(eq, 5)),
        BoundedBy(min=10, max=20),
    )
    maxing_step = SimulationStep(x0=flop, inputs=(6, ), duration=1)
    pausing_step = maxing_step._replace(inputs=(5, ))
    assert new_model.min == latest_sample(new_model(pausing_step))
    assert new_model.max == latest_sample(new_model(maxing_step))
def test_should_call_predicate_with_first_input_only():
    def pred(x):
        assert x == 0
        return False
    var = model & ResetBy(pred)
    step = SimulationStep(x0=1, inputs=(0, 1, 2), duration=1)
    var(step)
예제 #3
0
def test_should_pass_first_input_var_to_predicate():
    def pred(x):
        assert x == 0
        return False

    var = model & PausedBy(pred)
    step = SimulationStep(x0=1, inputs=(0, 1, 2), duration=1)
    var(step)
예제 #4
0
def test_pauses():
    """
    Should not flip when paused
    """
    var = model & PausedBy(id)
    step = SimulationStep(x0=1, inputs=(1, ), duration=1)

    assert latest_sample(var(step)) == 1
def test_should_pass_down_input_tail():
    def assert_inputs(sim_step):
        v1, v2 = sim_step.inputs
        assert v1 == 1
        assert v2 == 2
    var = ResetBy(never)(assert_inputs)
    step = SimulationStep(x0=1, inputs=(0, 1, 2), duration=1)
    var(step)
def test_paramterless_feature_creation():
    @feature_type()
    def Inverted(var, sim_step):
        ts, xs = var(sim_step)
        return (ts, tuple(-x for x in xs))

    step = SimulationStep(x0=flip, duration=1, inputs=tuple())
    assert -flop == latest_sample((model & Inverted())(step))
def test_lower_bounds_ensured():
    """
    Long enough simulation should saturate a bounded variable (lower bound)
    """
    example_step = SimulationStep(x0=0, inputs=(-1, 1), duration=100)
    _, samples = bounded_angular_velocity(example_step)
    assert not any(s < bounded_angular_velocity.min for s in samples)
    assert samples[-1] == bounded_angular_velocity.min
def test_upper_bounds_ensured():
    """
    Long enough simulation should saturate a bounded variable
    """
    example_step = SimulationStep(x0=0, inputs=(15000, 1), duration=100)
    _, samples = bounded_angular_velocity(example_step)
    assert not any(s > bounded_angular_velocity.max for s in samples)
    assert samples[-1] == bounded_angular_velocity.max
예제 #9
0
def test_process_interface():
    """
    Should behave like a variable
    """
    var = model & PausedBy(never)
    step = SimulationStep(x0=1, inputs=(0, ), duration=1)

    assert var.name == model.name
    assert var.start == model.start
    assert latest_sample(var(step)) == 0
예제 #10
0
def test_onoff_stays_on_when_signal_within_bounds():
    ctrl = controller(start=0, hyst=1, on=1, off=0)
    step = SimulationStep(x0=ctrl.on, duration=1, inputs=(0.5, 0))
    assert is_on(ctrl, step)
예제 #11
0
def test_onoff_stays_off_when_signal_above_upper_bound():
    ctrl = controller(start=0, hyst=1, on=1, off=0)
    step = SimulationStep(x0=ctrl.off, duration=1, inputs=(2, 0))
    assert is_off(ctrl, step)
예제 #12
0
def test_onoff_stays_on_when_signal_below_lower_bound():
    ctrl = controller(start=0, hyst=1, on=1, off=0)
    step = SimulationStep(x0=ctrl.on, duration=1, inputs=(-2, 0))
    assert is_on(ctrl, step)
예제 #13
0
def test_onoff_off_when_signal_above_set_point():
    step = SimulationStep(x0=0, duration=1, inputs=(1, 0))
    ctrl = controller(start=0, hyst=0, on=1, off=0)
    assert latest_sample(ctrl(step)) == ctrl.off
예제 #14
0
def test_onoff_adheres_to_variable_protocol():
    step = SimulationStep(x0=0, duration=1, inputs=(0, 0))
    ctrl = controller(start=0, hyst=0, on=1, off=0)
    assert ctrl.name == 'abcd'
    assert ctrl.start == 0
    assert latest_sample(ctrl(step)) in (ctrl.on, ctrl.off)
from libsim.models import (
    SimulationStep
)
from libsim.features import (
    ResetBy,
)
from libsim.util import (
    latest_sample,
)
from tests.helpers import (
    create_flip_flop_model,
)
from functools import partial
from operator import eq

sim_step = SimulationStep(x0=0, inputs=(0,), duration=1)
model = create_flip_flop_model(start=20, flip=0, flop=42)


def never(*args):
    return False


def test_reset():
    """
    Should be reset to starting value when condition met
    """
    resettable = model & ResetBy(reset_cond=partial(eq, 0))

    assert latest_sample(resettable(sim_step)) == 20