|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | +from hypothesis import given |
| 4 | +from hypothesis import strategies as st |
| 5 | + |
| 6 | +from pyforc.core import Coordinates, CoordinatesHcHb, CoordinatesHHr |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.parametrize( |
| 10 | + ("coord_str", "expected"), |
| 11 | + [ |
| 12 | + ("hchb", CoordinatesHcHb), |
| 13 | + ("hhr", CoordinatesHHr), |
| 14 | + ], |
| 15 | +) |
| 16 | +def test_from_str(coord_str, expected): |
| 17 | + """Test that the right Coordinates subclass is returned for each string.""" |
| 18 | + assert isinstance(Coordinates.from_str(coord_str), expected) |
| 19 | + |
| 20 | + |
| 21 | +def test_from_str_invalid(): |
| 22 | + """Test that an invalid name raises an error for Coordinates.from_str().""" |
| 23 | + name = "foo" |
| 24 | + with pytest.raises(ValueError, match=f"Invalid coordinate type {name}"): |
| 25 | + Coordinates.from_str(name) |
| 26 | + |
| 27 | + |
| 28 | +@given( |
| 29 | + st.floats(min_value=-2 * 53 - 1, max_value=2 * 53 - 1), |
| 30 | + st.floats(min_value=-2 * 53 - 1, max_value=2 * 53 - 1), |
| 31 | +) |
| 32 | +def test_hchb_matrix(h, hr): |
| 33 | + """Test that the hchb coordinate matrix transforms coordinates as expected.""" |
| 34 | + hhr_vec = np.array([h, hr, 0]) |
| 35 | + |
| 36 | + coords = CoordinatesHcHb() |
| 37 | + assert coords.has_inverse |
| 38 | + |
| 39 | + arr = np.array(coords.get_matrix()) |
| 40 | + |
| 41 | + [hc, hb, _] = arr @ hhr_vec |
| 42 | + |
| 43 | + assert np.isclose(h, hb + hc) |
| 44 | + assert np.isclose(hr, hb - hc) |
| 45 | + assert np.isclose(hc, (h - hr) / 2) |
| 46 | + assert np.isclose(hb, (h + hr) / 2) |
| 47 | + |
| 48 | + |
| 49 | +@given( |
| 50 | + st.floats(min_value=-2 * 53 - 1, max_value=2 * 53 - 1), |
| 51 | + st.floats(min_value=-2 * 53 - 1, max_value=2 * 53 - 1), |
| 52 | +) |
| 53 | +def test_hhr_matrix(h, hr): |
| 54 | + """Test that the hhr coordinate matrix transforms coordinates as expected.""" |
| 55 | + hhr_vec = np.array([h, hr, 0]) |
| 56 | + |
| 57 | + coords = CoordinatesHHr() |
| 58 | + assert coords.has_inverse |
| 59 | + |
| 60 | + arr = np.array(coords.get_matrix()) |
| 61 | + |
| 62 | + [h_calc, hr_calc, _] = arr @ hhr_vec |
| 63 | + |
| 64 | + assert h_calc == h |
| 65 | + assert hr_calc == hr |
0 commit comments