Beispiel #1
0
def test_truncate_equivalent_to_scale(x: Vector, max_length: float):
    """Vector.scale_to and truncate are equivalent when max_length <= x.length"""
    assume(max_length <= x.length)
    note(f"x.length = {x.length}")
    if max_length > 0:
        note(f"x.length = {x.length / max_length} * max_length")

    scale: Union[Vector, Type[Exception]]
    truncate: Union[Vector, Type[Exception]]

    try:
        truncate = x.truncate(max_length)
    except Exception as e:
        truncate = type(e)

    try:
        scale = x.scale_to(max_length)
    except Exception as e:
        event(f"Exception {type(e).__name__} thrown")
        scale = type(e)

    if isinstance(scale, Vector) and x.length == max_length:
        # Permit some edge-case where truncation and scaling aren't equivalent
        assert isinstance(truncate, Vector)
        assert scale.isclose(truncate, abs_tol=0, rel_tol=1e-12)

    else:
        assert scale == truncate
Beispiel #2
0
def test_scale_negative_length(v: Vector, length: float):
    """Test that Vector.scale_to raises ValueError on negative lengths."""
    assume(length < 0)
    with raises(ValueError):
        v.scale_to(length)
Beispiel #3
0
def test_scale_aligned(x: Vector, length: float):
    """Test that x.scale_to(length) is aligned with x."""
    assume(length > 0 and x)
    assert angle_isclose(x.scale_to(length).angle(x), 0)
Beispiel #4
0
def test_scale_to_length(x: Vector, length: float):
    """Test that the length of x.scale_to(length) is length with x non-null."""
    assume(x)
    assert isclose(x.scale_to(length).length, length)