Coverage for tests / helpers / assertion_helpers.py: 76%

41 statements  

« 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/>. 

26 

27from typing import Any 

28 

29import pytest 

30 

31from physioblocks.computing.quantities import Quantity 

32from physioblocks.description.nets import Net 

33from physioblocks.simulation.solvers import AbstractSolver 

34from physioblocks.simulation.state import State 

35from physioblocks.simulation.time_manager import TimeManager 

36 

37 

38def assert_net_equals(net_a: Net, net_b: Net) -> None: 

39 assert isinstance(net_a, Net) 

40 assert len(net_a.blocks) == len(net_b.blocks) 

41 for block_id, block in net_a.blocks.items(): 

42 assert block_id in net_b.blocks 

43 assert type(block) is type(net_b.blocks[block_id]) 

44 

45 assert len(net_a.nodes) == len(net_b.nodes) 

46 for node_id, node in net_a.nodes.items(): 

47 assert node_id in net_b.nodes 

48 assert node.dofs == net_b.nodes[node_id].dofs 

49 assert node.local_nodes == net_b.nodes[node_id].local_nodes 

50 

51 

52def assert_parameters_equals( 

53 parameters_a: dict[str, Quantity[Any]], parameters_b: dict[str, Quantity[Any]] 

54) -> None: 

55 assert len(parameters_a) == len(parameters_b) 

56 for ref_id, ref_qty in parameters_b.items(): 

57 assert ref_id in parameters_a 

58 qty = parameters_a[ref_id] 

59 assert qty.size == ref_qty.size 

60 if qty.size == 1: 

61 assert ref_qty.current == pytest.approx(qty.current) 

62 assert ref_qty.new == pytest.approx(qty.new) 

63 else: 

64 assert qty.current == pytest.approx(ref_qty.current, abs=1e-16) 

65 assert qty.new == pytest.approx(ref_qty.new, abs=1e-16) 

66 

67 

68def assert_solvers_equals(solver_a: AbstractSolver, solver_b: AbstractSolver): 

69 assert solver_a.tolerance == solver_b.tolerance 

70 assert solver_a.iteration_max == solver_b.iteration_max 

71 

72 

73def assert_states_equals(state_a: State, state_b: State) -> None: 

74 assert state_a.variables == state_b.variables 

75 assert state_a.indexes == state_b.indexes 

76 

77 

78def assert_time_manager_equals( 

79 time_manager_a: TimeManager, time_manager_b: TimeManager 

80) -> None: 

81 assert time_manager_a.step_size == pytest.approx(time_manager_b.step_size) 

82 assert time_manager_a.end == pytest.approx(time_manager_b.end) 

83 assert time_manager_a.start == pytest.approx(time_manager_b.start) 

84 assert time_manager_a.min_step == pytest.approx(time_manager_b.min_step) 

85 assert time_manager_a.step_size == pytest.approx(time_manager_b.step_size)