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_env(self):
|
2022-10-20 12:12:10 +00:00
|
|
|
""" """
|
2022-10-17 17:29:39 +00:00
|
|
|
|
|
|
|
times_started = []
|
|
|
|
times_awakened = []
|
2022-10-20 07:14:50 +00:00
|
|
|
times_asleep = []
|
2022-10-17 17:29:39 +00:00
|
|
|
times = []
|
2022-10-20 07:14:50 +00:00
|
|
|
done = []
|
2022-10-17 17:29:39 +00:00
|
|
|
|
|
|
|
class CondAgent(agents.BaseAgent):
|
|
|
|
def step(self):
|
2023-05-03 10:14:49 +00:00
|
|
|
nonlocal done, times_started, times_asleep, times_awakened
|
2022-10-17 17:29:39 +00:00
|
|
|
times_started.append(self.now)
|
|
|
|
while True:
|
2022-10-20 07:14:50 +00:00
|
|
|
times_asleep.append(self.now)
|
2023-05-03 10:14:49 +00:00
|
|
|
while self.now < 10:
|
|
|
|
yield self.delay(2)
|
2022-10-17 17:29:39 +00:00
|
|
|
times_awakened.append(self.now)
|
|
|
|
if self.now >= 10:
|
|
|
|
break
|
2022-10-20 07:14:50 +00:00
|
|
|
done.append(self.now)
|
2022-10-17 17:29:39 +00:00
|
|
|
|
2023-04-09 02:19:24 +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)
|
2022-10-20 07:14:50 +00:00
|
|
|
env.step()
|
|
|
|
|
2022-10-17 17:29:39 +00:00
|
|
|
assert env.schedule.time == 11
|
|
|
|
assert times_started == [0]
|
|
|
|
assert times_awakened == [10]
|
2022-10-20 07:14:50 +00:00
|
|
|
assert done == [10]
|
|
|
|
assert env.schedule.steps == 6
|
|
|
|
assert len(times) == 6
|
2022-10-17 17:29:39 +00:00
|
|
|
|
2022-10-20 07:14:50 +00:00
|
|
|
while env.schedule.time < 13:
|
2022-10-17 17:29:39 +00:00
|
|
|
times.append(env.now)
|
2022-10-20 07:14:50 +00:00
|
|
|
env.step()
|
2022-10-17 17:29:39 +00:00
|
|
|
|
2023-05-03 10:14:49 +00:00
|
|
|
assert times == [0, 2, 4, 6, 8, 10, 11, 12]
|
2022-10-20 07:14:50 +00:00
|
|
|
assert env.schedule.time == 13
|
2023-05-03 10:14:49 +00:00
|
|
|
assert times_started == [0, 11, 12]
|
|
|
|
assert times_awakened == [10, 11, 12]
|
|
|
|
assert done == [10, 11, 12]
|
|
|
|
assert env.schedule.steps == 8
|
|
|
|
assert len(times) == 8
|