Skip to content

Commit

Permalink
fix(tests)🧪: Refine test parameters and assertions for polar-cartesia…
Browse files Browse the repository at this point in the history
…n conversion

- Updated the minimum value for radius in test cases to avoid numerical instability.
- Improved the assertion logic to ensure accurate angle comparison.
- Added comments to clarify the reasoning behind test parameter choices.
  • Loading branch information
ericmjl committed Feb 23, 2025
1 parent 6ee73d9 commit 4270b84
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,6 @@ site/
.pixi
*.egg-info
pixi.lock

# SQLite database
message_log.db
16 changes: 11 additions & 5 deletions tests/test_polcart.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@


@given(
floats(min_value=-1e6, max_value=1e6, allow_infinity=False, allow_nan=False),
# We use min_value=0.01 because:
# 1. r must be positive in polar coordinates
# 2. Values very close to zero (between 0 and 0.01) can lead to numerical instability
# when converting between polar and cartesian coordinates, as the angle becomes
# poorly defined when r approaches 0.
floats(min_value=0.01, max_value=1e6, allow_infinity=False, allow_nan=False),
floats(
min_value=0,
max_value=2 * np.pi,
Expand All @@ -47,14 +52,15 @@
)
def test_convert_rt(r, theta):
"""Test for conversion of polar to cartesian coordinates."""
assume(r > 0.01 and r < 1e6)
assume(np.isfinite(r) and np.isfinite(theta))

# Convert to cartesian
x, y = to_cartesian(r, theta)

# Convert back to polar
r_new, theta_new = to_polar(x, y)

# Check that we get back the original values
assert np.allclose(r, r_new)
assert np.allclose(abs(theta_new), abs(theta))
assert np.allclose(theta, theta_new)


@given(floats())
Expand Down

0 comments on commit 4270b84

Please sign in to comment.