Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vector2.scale_to: Add Hypothesis tests, fix exposed bugs #81

Merged
merged 5 commits into from
Dec 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions ppb_vector/vector2.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,10 @@ def scale_to(self: VectorOrSub, length: Realish) -> VectorOrSub:
"""
Scale the vector to the given length
"""
try:
scale = length / self.length
except ZeroDivisionError:
scale = 1
return self.scale_by(scale)
if length < 0:
raise ValueError("Vector2.scale_to takes non-negative lengths.")

return (length / self.length) * self

scale = scale_to

Expand Down
15 changes: 7 additions & 8 deletions tests/test_vector2_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import ppb_vector


@pytest.mark.parametrize("x, y, expected", [
(3, 4, 1),
(6, 8, 1),
(0, 1, 1),
(1, 0, 1),
(0, 0, 0)
@pytest.mark.parametrize("x, y", [
(3, 4),
(6, 8),
(0, 1),
(1, 0),
])
def test_normalize(x, y, expected):
def test_normalize(x, y):
vector = ppb_vector.Vector2(x, y).normalize()
assert vector.length == expected
assert vector.length == 1
39 changes: 20 additions & 19 deletions tests/test_vector2_scale.py
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)