Skip to content

Commit

Permalink
Test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprey committed Mar 15, 2019
1 parent dc3b3df commit 8ca87f5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
19 changes: 13 additions & 6 deletions pysd/py_backend/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,14 +519,21 @@ def __str__(self):


class Time(object):
def __init__(self):
self._t = None
def __init__(self, t=None, dt=None):
self._t = t
self._step = dt
self.stage = None

def __call__(self):
return self._t

def step(self):
return self._step

def update(self, value):
if self._t is not None:
self._step = value - self._t

self._t = value


Expand Down Expand Up @@ -889,13 +896,13 @@ def pulse_magnitude(time, magnitude, start, repeat_time=0):
t = time()
small = 1e-6 # What is considered zero according to Vensim Help
if repeat_time <= small:
if abs(t - start) < time_step:
return magnitude * time_step
if abs(t - start) < time.step():
return magnitude * time.step()
else:
return 0
else:
if abs((t - start) % repeat_time) < time_step:
return magnitude * time_step
if abs((t - start) % repeat_time) < time.step():
return magnitude * time.step()
else:
return 0

Expand Down
1 change: 0 additions & 1 deletion tests/.coverage

This file was deleted.

31 changes: 31 additions & 0 deletions tests/unit_test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,37 @@ def test_pulse_chain(self):
# after train
self.assertEqual(functions.pulse_train(lambda: 15, 1, 3, 5, 13), 0)

def test_pulse_magnitude(self):
from pysd import functions

# Pulse function with repeat time
# before first impulse
self.assertEqual(functions.pulse_magnitude(functions.Time(0, 1), 10, 2, 5), 0)
# first impulse
self.assertEqual(functions.pulse_magnitude(functions.Time(2, 1), 10, 2, 5), 10)
# after first impulse and before second
self.assertEqual(functions.pulse_magnitude(functions.Time(4, 1), 10, 2, 5), 0)
# second impulse
self.assertEqual(functions.pulse_magnitude(functions.Time(7, 1), 10, 2, 5), 10)
# after second and before third impulse
self.assertEqual(functions.pulse_magnitude(functions.Time(9, 1), 10, 2, 5), 0)
# third impulse
self.assertEqual(functions.pulse_magnitude(functions.Time(12, 1), 10, 2, 5), 10)
# after third impulse
self.assertEqual(functions.pulse_magnitude(functions.Time(14, 1), 10, 2, 5), 0)

# Pulse function without repeat time
# before first impulse
self.assertEqual(functions.pulse_magnitude(functions.Time(0, 1), 10, 2), 0)
# first impulse
self.assertEqual(functions.pulse_magnitude(functions.Time(2, 1), 10, 2), 10)
# after first impulse and before second
self.assertEqual(functions.pulse_magnitude(functions.Time(4, 1), 10, 2), 0)
# second impulse
self.assertEqual(functions.pulse_magnitude(functions.Time(7, 1), 10, 2), 0)
# after second and before third impulse
self.assertEqual(functions.pulse_magnitude(functions.Time(9, 1), 10, 2), 0)

def test_xidz(self):
from pysd import functions
self.assertEqual(functions.xidz(1, -0.00000001, 5), 5)
Expand Down

0 comments on commit 8ca87f5

Please sign in to comment.