Skip to content

Commit

Permalink
tests: Clarify scalar names
Browse files Browse the repository at this point in the history
  • Loading branch information
nbraud committed Jan 1, 2019
1 parent 41bfd8c commit c71ce1a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
12 changes: 6 additions & 6 deletions tests/test_vector2_angle.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ def test_angle_additive(left, middle, right):
lr = left.angle(right)
assert angle_isclose(lm + mr, lr)

@given(x=vectors(), l=floats())
def test_angle_aligned(x: Vector2, l: float):
"""x.angle(l * x) is 0 or 180, depending on whether l > 0"""
assume(l != 0)
y = l * x
assert angle_isclose(x.angle(y), 0 if l > 0 else 180)
@given(x=vectors(), scalar=floats())
def test_angle_aligned(x: Vector2, scalar: float):
"""x.angle(scalar * x) is 0 or 180, depending on whether scalar > 0"""
assume(scalar != 0)
y = scalar * x
assert angle_isclose(x.angle(y), 0 if scalar > 0 else 180)
35 changes: 16 additions & 19 deletions tests/test_vector2_scalar_multiplication.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,25 @@ def test_scalar_multiplication(x, y, expected):
assert x * y == expected


@given(
l=floats(),
m=floats(),
x=vectors()
)
def test_scalar_associative(l: float, m: float, x: Vector2):
"""(l * m) * x == l * (m * x)"""
left = (l * m) * x
right = l * (m * x)
@given(scalar1=floats(), scalar2=floats(), x=vectors())
def test_scalar_associative(scalar1: float, scalar2: float, x: Vector2):
"""(scalar1 * scalar2) * x == scalar1 * (scalar2 * x)"""
left = (scalar1 * scalar2) * x
right = scalar1 * (scalar2 * x)
assert left.isclose(right)

@given(l=floats(), x=vectors(), y=vectors())
def test_scalar_linear(l: float, x: Vector2, y: Vector2):
assert (l * (x + y)).isclose(l*x + l*y, rel_to=[x, y, l*x, l*y])
@given(scalar=floats(), x=vectors(), y=vectors())
def test_scalar_linear(scalar: float, x: Vector2, y: Vector2):
assert (scalar * (x + y)).isclose(scalar*x + scalar*y,
rel_to=[x, y, scalar*x, scalar*y])

@given(l=floats(), x=vectors())
def test_scalar_length(l: float, x: Vector2):
assert isclose((l * x).length, abs(l) * x.length)
@given(scalar=floats(), x=vectors())
def test_scalar_length(scalar: float, x: Vector2):
assert isclose((scalar * x).length, abs(scalar) * x.length)


@given(x=vectors(), l=floats())
def test_scalar_division(x: Vector2, l: float):
@given(x=vectors(), scalar=floats())
def test_scalar_division(x: Vector2, scalar: float):
"""Test that (x / λ) = (1 / λ) * x"""
assume(abs(l) > 1e-100)
assert (x / l).isclose((1/l) * x)
assume(abs(scalar) > 1e-100)
assert (x / scalar).isclose((1/scalar) * x)
10 changes: 5 additions & 5 deletions tests/test_vector2_scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
from ppb_vector import Vector2


@given(x=vectors(), l=floats())
def test_scale_to_length(x: Vector2, l: float):
"""Test that the length of x.scale_to(l) is l.
@given(x=vectors(), length=floats())
def test_scale_to_length(x: Vector2, length: float):
"""Test that the length of x.scale_to(length) is length.
Additionally, Vector2.scale_to may raise:
- ZeroDivisionError if the vector is null;
- ValueError if the desired length is negative.
"""
try:
assert isclose(x.scale_to(l).length, l)
assert isclose(x.scale_to(length).length, length)
except ZeroDivisionError:
assert x == (0, 0)
except ValueError:
assert l < 0
assert length < 0

0 comments on commit c71ce1a

Please sign in to comment.