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
def test_truncate_length(x: Vector, max_length: float): assert x.truncate(max_length).length <= (1 + 1e-14) * max_length
def test_truncate_invariant(x: Vector, max_length: float): assume(x.length <= max_length) assert x.truncate(max_length) == x