|
| 1 | +# Soap Box Slide is a computational take on soapbox racing. |
| 2 | +# © 2025 Toon Verstraelen |
| 3 | +# |
| 4 | +# This file is part of Soap Box Slide. |
| 5 | +# |
| 6 | +# Soap Box Slide is free software; you can redistribute it and/or |
| 7 | +# modify it under the terms of the GNU General Public License |
| 8 | +# as published by the Free Software Foundation; either version 3 |
| 9 | +# of the License, or (at your option) any later version. |
| 10 | +# |
| 11 | +# Soap Box Slide is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU General Public License |
| 17 | +# along with this program; if not, see <http://www.gnu.org/licenses/> |
| 18 | +# |
| 19 | +# -- |
| 20 | +"""Unit tests for Soap Box Slide.""" |
| 21 | + |
| 22 | +import attrs |
| 23 | +import numpy as np |
| 24 | + |
| 25 | +from soapboxslide import EndState, Trajectory |
| 26 | + |
| 27 | + |
| 28 | +def test_npz_trajectory(tmpdir): |
| 29 | + traj = Trajectory( |
| 30 | + time=[0, 1, 2], |
| 31 | + mass=[1, 1.2], |
| 32 | + gamma=[0.3, 0.0], |
| 33 | + pos=[ |
| 34 | + [[0, 0, 0], [1, 0, 0]], |
| 35 | + [[0, 1, 0], [1, 1, 0]], |
| 36 | + [[0, 2, 0], [1, 2, 0]], |
| 37 | + ], |
| 38 | + vel=[ |
| 39 | + [[0, 0, 0], [0, 0, 0]], |
| 40 | + [[1, 0, 0], [1, 0, 0]], |
| 41 | + [[1, 1, 0], [1, 1, 0]], |
| 42 | + ], |
| 43 | + grad=[ |
| 44 | + [[0.5, 0.1], [1.2, 0.3]], |
| 45 | + [[0.6, 0.2], [1.3, 0.4]], |
| 46 | + [[0.7, 0.3], [1.4, 0.5]], |
| 47 | + ], |
| 48 | + hess=[ |
| 49 | + [[0, 0, 0], [0, 0, 0]], |
| 50 | + [[1, 0, 0], [1, 0, 0]], |
| 51 | + [[1, 1, 0], [1, 1, 0]], |
| 52 | + ], |
| 53 | + spring_idx=[[0, 1]], |
| 54 | + spring_par=[[100, 0.5, 1.2]], |
| 55 | + end_state=EndState.STOP, |
| 56 | + stop_time=30.0, |
| 57 | + stop_pos=[[10, 0, 0], [10, 1, 0]], |
| 58 | + stop_vel=[[0, 0, 0], [0, 0, 0]], |
| 59 | + ) |
| 60 | + path = tmpdir.join("trajectory.npz") |
| 61 | + traj.to_file(path) |
| 62 | + print(path) |
| 63 | + loaded_traj = Trajectory.from_file(path) |
| 64 | + |
| 65 | + for attr in attrs.fields(Trajectory): |
| 66 | + val_orig = getattr(traj, attr.name) |
| 67 | + val_loaded = getattr(loaded_traj, attr.name) |
| 68 | + assert np.array_equal(val_orig, val_loaded) |
0 commit comments