Coverage for tests / tests_simulation / test_time_manager.py: 99%
99 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-09 16:40 +0100
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-09 16:40 +0100
1# SPDX-FileCopyrightText: Copyright INRIA
2#
3# SPDX-License-Identifier: LGPL-3.0-only
4#
5# Copyright INRIA
6#
7# This file is part of PhysioBlocks, a library mostly developed by the
8# [Ananke project-team](https://team.inria.fr/ananke) at INRIA.
9#
10# Authors:
11# - Colin Drieu
12# - Dominique Chapelle
13# - François Kimmig
14# - Philippe Moireau
15#
16# PhysioBlocks is free software: you can redistribute it and/or modify it under the
17# terms of the GNU Lesser General Public License as published by the Free Software
18# Foundation, version 3 of the License.
19#
20# PhysioBlocks is distributed in the hope that it will be useful, but WITHOUT ANY
21# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
22# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
23#
24# You should have received a copy of the GNU Lesser General Public License along with
25# PhysioBlocks. If not, see <https://www.gnu.org/licenses/>.
27import pytest
29from physioblocks.simulation.time_manager import Time, TimeManager
32@pytest.fixture
33def zero():
34 return 0.0
37@pytest.fixture
38def step():
39 return 0.1
42@pytest.fixture
43def end():
44 return 2.0
47@pytest.fixture
48def min_step():
49 return 0.001
52class TestTime:
53 def test_constructor(self, zero):
54 time = Time(zero)
55 assert time.current == pytest.approx(zero)
56 assert time.new == pytest.approx(zero)
57 assert time.dt == pytest.approx(zero)
58 assert time.inv_dt == pytest.approx(zero)
60 def test_initialize(self, zero, step):
61 time = Time(zero)
62 time.initialize(step)
64 assert time.current == pytest.approx(step)
65 assert time.new == pytest.approx(step)
66 assert time.dt == pytest.approx(zero)
67 assert time.inv_dt == pytest.approx(zero)
69 def test_update(self, zero, step):
70 time = Time(zero)
71 time.update(step)
73 assert time.current == pytest.approx(zero)
74 assert time.new == pytest.approx(step)
75 assert time.dt == pytest.approx(step)
76 assert time.inv_dt == pytest.approx(1.0 / step)
78 def test_set(self, zero, step):
79 time = Time(zero)
81 with pytest.raises(AttributeError):
82 time.current = step
84 with pytest.raises(AttributeError):
85 time.new = step
87 with pytest.raises(AttributeError):
88 time.dt = step
90 with pytest.raises(AttributeError):
91 time.inv_dt = step
94class TestTimeManager:
95 def test_constructor(self, zero, step, end):
96 time_manager = TimeManager(zero, end, step)
97 assert time_manager.ended is False
98 assert time_manager.time.current == zero
99 assert time_manager.time.new == zero
101 with pytest.raises(ValueError):
102 time_manager = TimeManager(end, zero, step)
104 with pytest.raises(ValueError):
105 time_manager = TimeManager(zero, end, -step)
107 with pytest.raises(AttributeError):
108 time_manager.time = Time(zero)
110 with pytest.raises(ValueError):
111 TimeManager(zero, end, step, 0.0)
113 with pytest.raises(ValueError):
114 TimeManager(zero, end, step, step + 0.1)
116 def test_set(self, zero, step, end):
117 time_manager = TimeManager(
118 zero,
119 end,
120 step,
121 )
123 # start
124 assert time_manager.start == zero
125 time_manager.start = step
126 assert time_manager.end == time_manager.duration + step
127 assert time_manager.start == step
129 # step
130 assert time_manager.step_size == step
131 with pytest.raises(ValueError):
132 time_manager.step_size = -step
134 # duration
135 time_manager.duration = end + step
136 assert time_manager.end == end + 2 * step
137 with pytest.raises(ValueError):
138 time_manager.duration = zero - step
140 def test_update(self, zero, step, end):
141 time_manager = TimeManager(zero, end, step)
143 time_manager.update_time()
144 assert time_manager.time.current == zero
145 assert time_manager.time.new == step
147 time_manager.update_time()
148 assert time_manager.time.current == step
149 assert time_manager.time.new == (step + step)
151 def test_ended(self, zero, step):
152 time_manager = TimeManager(zero, step, step)
153 time_manager.update_time()
154 assert time_manager.ended is True
155 assert time_manager.time.current == zero
156 assert time_manager.time.new == step
158 time_manager.update_time()
159 assert time_manager.ended is True
160 assert time_manager.time.current == step
161 assert time_manager.time.new == step
163 def test_initialize(self, zero, step, end):
164 time_manager = TimeManager(zero, step, end)
165 time_manager.update_time()
166 time_manager.update_time()
167 time_manager.initialize()
168 assert time_manager.time.current == zero
169 assert time_manager.time.new == zero