1
0
mirror of https://github.com/gsi-upm/soil synced 2024-09-21 07:41:43 +00:00
soil/tests/test_time.py

76 lines
2.0 KiB
Python
Raw Normal View History

2022-10-17 17:29:39 +00:00
from unittest import TestCase
from soil import time, agents, environment
2022-10-20 12:12:10 +00:00
2022-10-17 17:29:39 +00:00
class TestMain(TestCase):
def test_cond(self):
2022-10-20 12:12:10 +00:00
"""
2022-10-17 17:29:39 +00:00
A condition should match a When if the concition is True
2022-10-20 12:12:10 +00:00
"""
2022-10-17 17:29:39 +00:00
t = time.Cond(lambda t: True)
f = time.Cond(lambda t: False)
for i in range(10):
w = time.When(i)
assert w == t
assert w is not f
def test_cond(self):
2022-10-20 12:12:10 +00:00
"""
2022-10-17 17:29:39 +00:00
Comparing a Cond to a Delta should always return False
2022-10-20 12:12:10 +00:00
"""
2022-10-17 17:29:39 +00:00
c = time.Cond(lambda t: False)
d = time.Delta(1)
assert c is not d
def test_cond_env(self):
2022-10-20 12:12:10 +00:00
""" """
2022-10-17 17:29:39 +00:00
times_started = []
times_awakened = []
times_asleep = []
2022-10-17 17:29:39 +00:00
times = []
done = []
2022-10-17 17:29:39 +00:00
class CondAgent(agents.BaseAgent):
def step(self):
nonlocal done
times_started.append(self.now)
while True:
times_asleep.append(self.now)
2022-10-20 12:12:10 +00:00
yield time.Cond(lambda agent: agent.now >= 10, delta=2)
2022-10-17 17:29:39 +00:00
times_awakened.append(self.now)
if self.now >= 10:
break
done.append(self.now)
2022-10-17 17:29:39 +00:00
env = environment.Environment()
env.add_agent(CondAgent)
2022-10-17 17:29:39 +00:00
while env.schedule.time < 11:
times.append(env.now)
env.step()
2022-10-17 17:29:39 +00:00
assert env.schedule.time == 11
assert times_started == [0]
assert times_awakened == [10]
assert done == [10]
2022-10-17 17:29:39 +00:00
# The first time will produce the Cond.
assert env.schedule.steps == 6
assert len(times) == 6
2022-10-17 17:29:39 +00:00
while env.schedule.time < 13:
2022-10-17 17:29:39 +00:00
times.append(env.now)
env.step()
2022-10-17 17:29:39 +00:00
assert times == [0, 2, 4, 6, 8, 10, 11]
assert env.schedule.time == 13
2022-10-17 17:29:39 +00:00
assert times_started == [0, 11]
assert times_awakened == [10]
assert done == [10]
2022-10-17 17:29:39 +00:00
# Once more to yield the cond, another one to continue
assert env.schedule.steps == 7
assert len(times) == 7