def test_scale_is_equivalent_to_truncate():
    """ 
    Vector2.scale is equivalent to Vector2.truncate 
    when scalar is less than length
    """
    vector_scale = Vector2(3, 4).scale(4)
    vector_truncate = Vector2(3, 4).truncate(4)
    assert vector_scale == vector_truncate
Beispiel #2
0
def test_reflect_prop(initial: Vector2, normal: Vector2):
    # Exclude cases where the initial vector is very close to the surface
    assume(not angle_isclose(initial.angle(normal) % 180, 90, epsilon=10))

    # Exclude cases where the initial vector is very small
    assume(initial.length > 1e-10)

    reflected = initial.reflect(normal)
    returned = reflected.reflect(normal)
    note(f"|normal|: {normal.length}, |initial|: {initial.length}")
    note(f"angle(normal, initial): {normal.angle(initial)}")
    note(f"angle(normal, reflected): {normal.angle(reflected)}")
    note(f"initial ^ normal: {initial ^ normal}")
    note(f"Reflected: {reflected}")
    assert not any(map(isinf, reflected))
    assert initial.isclose(returned)
    note(f"initial ⋅ normal: {initial * normal}")
    note(f"reflected ⋅ normal: {reflected * normal}")
    assert isclose((initial * normal), -(reflected * normal))
    assert angle_isclose(normal.angle(initial), 180 - normal.angle(reflected))
def test_substraction_vector_list():
    test_vector = Vector2(3, 7)
    test_list = [1, 3]
    result = test_vector - test_list
    assert result == Vector2(2, 4)
def test_for_exception():
    with pytest.raises(TypeError):
        Vector2('gibberish', 1).rotate(180)
Beispiel #5
0
def test_addition_vectors():
    test_vector1 = Vector2(1, 0)
    test_vector2 = Vector2(0, 1)
    result = test_vector1 + test_vector2
    assert result == Vector2(1, 1)
Beispiel #6
0
def test_addition_vector_list():
    test_vector = Vector2(1, 1)
    test_list = [1, 3]
    result = test_vector + test_list
    assert result == Vector2(2, 4)
Beispiel #7
0
def test_addition_vector_list():
    test_vector = Vector2(1, 1)
    test_list = [1, 3]
    result = test_vector + test_list
    assert result == Vector2(2, 4)


def test_addition_vector_dict():
    test_vector = Vector2(1, 1)
    test_dict = {'x': 3, 'y': 5}
    result = test_vector + test_dict
    assert result == Vector2(4, 6)


data = [
    ([Vector2(1, 1), (2, 2)], Vector2(3, 3)),
    ([Vector2(1, 2), [2, 2]], Vector2(3, 4)),
    ([Vector2(1, 2), {
        'x': 2,
        'y': 2
    }], Vector2(3, 4)),
    ([Vector2(10, 16), Vector2(2, 2)], Vector2(12, 18)),
    ([Vector2(25, 22), (12, 92)], Vector2(37, 114)),
    ([Vector2(25, 22), Vector2(22, 61)], Vector2(47, 83)),
    ([Vector2(39, 43), Vector2(92, -12)], Vector2(131, 31)),
    ([Vector2(42, 12), (-5, 23)], Vector2(37, 35)),
    ([Vector2(51, 28), [72, 31]], Vector2(123, 59)),
]


@pytest.mark.parametrize('test_input, expected', data)
Beispiel #8
0
def test_truncate_larger_max_length():
    vector = Vector2(3, 5)
    truncated = vector.truncate(10)
    assert vector == truncated
Beispiel #9
0
def test_is_iterable():
    test_vector = Vector2(3, 4)
    test_tuple = tuple(test_vector)
    assert test_tuple == (3, 4)
Beispiel #10
0
    truncated = vector.truncate(10)
    assert vector == truncated


def test_truncate_equal_max_length():
    vector = Vector2(3, 4)
    truncated = vector.truncate(5)
    assert vector == truncated


def test_truncate_lesser_max_length():
    vector = Vector2(20, 30)
    truncated = vector.truncate(10)
    assert truncated == vector.scale(10)


data = [([Vector2(1, 2), 3], Vector2(1, 2)),
        ([Vector2(5, 12), 6], Vector2(5, 12).scale(6)),
        ([Vector2(92, 19), 61], Vector2(92, 19).scale(61)),
        ([Vector2(22, 5), 41], Vector2(22, 5)),
        ([Vector2(2212481, 189898), 129039], Vector2(2212481,
                                                     189898).scale(129039)),
        ([Vector2(5, 12), 13], Vector2(5, 12)),
        ([Vector2(438, 153), 464], Vector2(438, 153)),
        ([Vector2(155, 155), 155], Vector2(155, 155).scale(155))]


@pytest.mark.parametrize('test_input, expected', data)
def test_multiples_values(test_input, expected):
    assert test_input[0].truncate(test_input[1]) == expected
Beispiel #11
0
def test_convert(value):
    v = Vector2.convert(value)
    assert isinstance(v, Vector2)
    assert v == value
Beispiel #12
0
import pytest  # type: ignore

from ppb_vector import Vector2


def test_is_iterable():
    test_vector = Vector2(3, 4)
    test_tuple = tuple(test_vector)
    assert test_tuple == (3, 4)

negation_data = (
    (Vector2(1, 1), Vector2(-1, -1)),
    (Vector2(2, -3), Vector2(-2, 3)),
    (Vector2(-4, 18), Vector2(4, -18))
)


@pytest.mark.parametrize('test_vector, expected_result', negation_data)
def test_negation(test_vector, expected_result):
    assert -test_vector == expected_result


@pytest.mark.parametrize('value', [
    Vector2(1, 2),
    [3, 4],
    (5, 6),
    {'x': 7, 'y': 8},
])
def test_convert(value):
    v = Vector2.convert(value)
    assert isinstance(v, Vector2)
from ppb_vector import Vector2
from math import isclose
import pytest  # type: ignore
from hypothesis import assume, given, note
from utils import angle_isclose, vectors


@pytest.mark.parametrize("left, right, expected", [
    (Vector2(1, 1), Vector2(0, -1), -135),
    (Vector2(1, 1), Vector2(-1, 0), 135),
    (Vector2(0, 1), Vector2(0, -1), 180),
    (Vector2(-1, -1), Vector2(1, 0), 135),
    (Vector2(-1, -1), Vector2(-1, 0), -45),
    (Vector2(1, 0), Vector2(0, 1), 90),
    (Vector2(1, 0), Vector2(1, 0), 0),
])
def test_angle(left, right, expected):
    lr = left.angle(right)
    rl = right.angle(left)
    assert -180 < lr <= 180
    assert -180 < rl <= 180
    assert isclose(lr, expected)
    assert isclose(rl, 180 if expected == 180 else -expected)


@given(
    left=vectors(),
    right=vectors(),
)
def test_angle_prop(left, right):
    lr = left.angle(right)
def test_substraction_vector_dict():
    test_vector = Vector2(7, 11)
    test_dict = {'x': 3, 'y': 5}
    result = test_vector - test_dict
    assert result == Vector2(4, 6)
Beispiel #15
0
def test_truncate_equal_max_length():
    vector = Vector2(3, 4)
    truncated = vector.truncate(5)
    assert vector == truncated
def test_substraction_vectors():
    test_vector1 = Vector2(0, 1)
    test_vector2 = Vector2(0, 1)
    result = test_vector1 - test_vector2
    assert result == Vector2(0, 0)
Beispiel #17
0
def test_truncate_lesser_max_length():
    vector = Vector2(20, 30)
    truncated = vector.truncate(10)
    assert truncated == vector.scale(10)
def vector():
    return Vector2(10, 20)
Beispiel #19
0
def test_truncate():
    test_vector = Vector2(700, 500)
    test_vector_truncated = test_vector.truncate(5)
    print(test_vector_truncated)
    assert test_vector_truncated == Vector2(4.068667356033675,
                                            2.906190968595482)
Beispiel #20
0
def test_addition_vector_tuple():
    test_vector = Vector2(1, 1)
    test_tuple = (2, 4)
    result = test_vector + test_tuple
    assert result == Vector2(3, 5)
from ppb_vector import Vector2
import pytest  # type: ignore

@pytest.mark.parametrize("left, right, expected", [
    (Vector2(1, 1), Vector2(0, -1), -1),
    (Vector2(1, 1), Vector2(-1, 0), 1),
    (Vector2(0, 1), Vector2(0, -1), 0),
    (Vector2(-1, -1), Vector2(1, 0), 1),
    (Vector2(-1, -1), Vector2(-1, 0), -1)
])
def test_cross(left, right, expected):
    assert left ^ right == expected
    assert right ^ left == -expected
Beispiel #22
0
def test_addition_vector_dict():
    test_vector = Vector2(1, 1)
    test_dict = {'x': 3, 'y': 5}
    result = test_vector + test_dict
    assert result == Vector2(4, 6)
def test_substraction_vector_list():
    test_vector = Vector2(3, 7)
    test_list = [1, 3]
    result = test_vector - test_list
    assert result == Vector2(2, 4)


def test_substraction_vector_dict():
    test_vector = Vector2(7, 11)
    test_dict = {'x': 3, 'y': 5}
    result = test_vector - test_dict
    assert result == Vector2(4, 6)


data = [
    ([Vector2(10, 16), Vector2(2, 2)], Vector2(8, 14)),
    ([Vector2(25, 22), Vector2(22, 61)], Vector2(3, -39)),
    ([Vector2(39, 43), Vector2(92, -12)], Vector2(-53, 55)),
    ([Vector2(1, 1), (2, 2)], Vector2(-1, -1)),
    ([Vector2(25, 22), (12, 92)], Vector2(13, -70)),
    ([Vector2(42, 12), (-5, 23)], Vector2(47, -11)),
    ([Vector2(51, 28), [72, 31]], Vector2(-21, -3)),
    ([Vector2(1, 2), [2, 2]], Vector2(-1, 0)),
    ([Vector2(1, 2), {
        'x': 2,
        'y': 2
    }], Vector2(-1, 0)),
]


@pytest.mark.parametrize('test_input, expected', data)
Beispiel #24
0
from ppb_vector import Vector2
import pytest  # type: ignore
from hypothesis import given, assume, note
from math import isclose, isinf
from utils import angle_isclose, units, vectors

reflect_data = ((Vector2(1, 1), Vector2(0, -1),
                 Vector2(1, -1)), (Vector2(1, 1), Vector2(-1,
                                                          0), Vector2(-1, 1)),
                (Vector2(0, 1), Vector2(0, -1),
                 Vector2(0, -1)), (Vector2(-1,
                                           -1), Vector2(1, 0), Vector2(1, -1)),
                (Vector2(-1, -1), Vector2(-1, 0), Vector2(1, -1)))


@pytest.mark.parametrize("initial_vector, surface_normal, expected_vector",
                         reflect_data)
def test_reflect(initial_vector, surface_normal, expected_vector):
    assert initial_vector.reflect(surface_normal).isclose(expected_vector)


@given(initial=vectors(), normal=units())
def test_reflect_prop(initial: Vector2, normal: Vector2):
    # Exclude cases where the initial vector is very close to the surface
    assume(not angle_isclose(initial.angle(normal) % 180, 90, epsilon=10))

    # Exclude cases where the initial vector is very small
    assume(initial.length > 1e-10)

    reflected = initial.reflect(normal)
    returned = reflected.reflect(normal)
def test_substraction_vector_tuple():
    test_vector = Vector2(4, 6)
    test_tuple = (1, 1)
    result = test_vector - test_tuple
    assert result == Vector2(3, 5)
Beispiel #26
0
def units(draw, elements=st.floats(min_value=0, max_value=360)):
    angle = draw(elements)
    return Vector2(1, 0).rotate(angle)
from ppb_vector import Vector2
from utils import angle_isclose, vectors
import pytest  # type: ignore
import math
from hypothesis import assume, given, note
import hypothesis.strategies as st

data = [(Vector2(1, 1), 90, Vector2(-1, 1)),
        (Vector2(1, 1), 180, Vector2(-1, -1)),
        (Vector2(1, 1), 360, Vector2(1, 1)),
        (Vector2(3, -20), 53, Vector2(17.77816, -9.64039)),
        (Vector2(math.pi, -1 * math.e), 30, Vector2(4.07984, -0.7833)),
        (Vector2(math.pi, math.e), 67, Vector2(-1.27467, 3.95397))]


@pytest.mark.parametrize('input, degrees, expected', data)
def test_multiple_rotations(input, degrees, expected):
    assert input.rotate(degrees).isclose(expected)
    assert angle_isclose(input.angle(expected), degrees)


def test_for_exception():
    with pytest.raises(TypeError):
        Vector2('gibberish', 1).rotate(180)


@given(degree=st.floats(min_value=-360, max_value=360))
def test_trig_stability(degree):
    r = math.radians(degree)
    r_cos = math.cos(r)
    r_sin = math.sin(r)