diff --git a/src/common/io.py b/src/common/io.py index f5b1d79f..78fac0c6 100644 --- a/src/common/io.py +++ b/src/common/io.py @@ -1354,6 +1354,9 @@ def _get_trajectory(self, data_index, start_index = 0, stop_index = None): start_index = max(0, start_index) stop_index = max(0, nbr_points if stop_index is None else min(nbr_points, stop_index)) new_file_position = file_position + start_index*sizeof_type*nbr_variables + # Finally when stop_index = None, we can end up with start > stop, + # therefore we need to use min(start, stop) + start_index = min(start_index, stop_index) new_nbr_points = stop_index - start_index self._data_2[data_index] = fmi_util.read_trajectory( @@ -1602,7 +1605,6 @@ def get_variables_data(self, raise ValueError(f"Invalid values for {start_index=} and {stop_index=}, " + \ "'start_index' needs to be less than or equal to 'stop_index'.") - trajectories = {} # Get the corresponding time trajectory diff --git a/tests/test_io.py b/tests/test_io.py index 52963010..c2befc7b 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -1954,6 +1954,12 @@ def test_trajectory_lengths(self): assert len(rdb.get_variables_data(['h'], 0, 550)[0]['h'].x) == 501 assert len(rdb.get_variables_data(['h'], 0, 10000)[0]['h'].x) == 501 + # test different scenarios of start_index out of bounds + assert len(rdb.get_variables_data(['h'], 501, 502)[0]['h'].x) == 0 + assert len(rdb.get_variables_data(['h'], 501, None)[0]['h'].x) == 0 + assert len(rdb.get_variables_data(['h'], 501)[0]['h'].x) == 0 + assert len(rdb.get_variables_data(['h'], 1234567)[0]['h'].x) == 0 + if assimulo_installed: class TestFileSizeLimit: