-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from nbraud/hypothesis/scale_to
Vector2.scale_to: Add Hypothesis tests, fix exposed bugs
- Loading branch information
Showing
3 changed files
with
31 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,28 @@ | ||
import pytest # type: ignore | ||
from math import hypot | ||
from hypothesis import assume, given | ||
from hypothesis.strategies import floats | ||
from math import isclose | ||
from utils import vectors | ||
|
||
from ppb_vector import Vector2 | ||
|
||
|
||
@pytest.fixture() | ||
def vector(): | ||
return Vector2(10, 20) | ||
@given(x=vectors(), l=floats(min_value=-1e150, max_value=1e150)) | ||
def test_scale_to_length(x: Vector2, l: float): | ||
"""Test that the length of x.scale_to(l) is l.""" | ||
try: | ||
assert isclose(x.scale_to(l).length, l) | ||
except ZeroDivisionError: | ||
assert x == (0, 0) | ||
except ValueError: | ||
assert l < 0 | ||
|
||
|
||
def test_calculate_scale(vector): | ||
x, y = 10, 20 | ||
length = hypot(x, y) | ||
scale = 4 | ||
vector_scale_calculated = vector * (scale / length) | ||
assert vector.scale(scale) == vector_scale_calculated | ||
|
||
|
||
def test_scale_is_equivalent_to_truncate(): | ||
""" | ||
Vector2.scale is equivalent to Vector2.truncate | ||
when scalar is less than length | ||
@given(x=vectors(), l=floats(min_value=1e150, max_value=1e150)) | ||
def test_scale_is_equivalent_to_truncate(x: Vector2, l: float): | ||
""" | ||
Vector2.scale_to is equivalent to Vector2.truncate | ||
when the scalar is less than length | ||
""" | ||
vector_scale = Vector2(3, 4).scale(4) | ||
vector_truncate = Vector2(3, 4).truncate(4) | ||
assert vector_scale == vector_truncate | ||
assume(l <= x.length) | ||
assert x.scale_to(l) == x.truncate(l) |