def test_has_flat(self): u = Vec2(1, 2) v = Vec2(1, 2.0) assert u.flat[0] == 1 assert u.flat[:] == [1, 2] assert v.flat[-1] == 2.0 assert v.flat[:] == [1.0, 2.0]
def test_vec_type_promotion_on_arithmetic_operations(self): u = Vec2(1, 2) v = Vec2(0.0, 0.0) assert isinstance(u + v, Vec2) assert isinstance(u - v, Vec2) assert isinstance(u * 1.0, Vec2) assert isinstance(u / 1.0, Vec2)
class TestDirection2D(LinearFixtures, VectorInterface, VectorInvalidOperations, SequenceInterface, ElementwiseAddition, ScalarMultiplication, Normed): base_cls = Direction2 size = LinearFixtures.size null = pytest.fixture(lambda self: Vec2(0, 0)) u = pytest.fixture(lambda self: Direction2(1, 0)) v = pytest.fixture(lambda self: Direction2(0, 1)) add_uv = pytest.fixture(lambda self: Vec2(1, 1)) sub_uv = pytest.fixture(lambda self: Vec2(1, -1)) double = pytest.fixture(lambda self: Vec2(2, 0)) half = pytest.fixture(lambda self: Vec2(0.5, 0)) # Disabled tests test_vector_norm_defaults_to_euclidean = None test_l1_norm = None def test_triangular_identity_2D(self, norm): self.assert_triangular_identity(Direction2(1, 2), Direction2(3, 4), norm) self.assert_triangular_identity(Direction2(1, 1), Direction2(1, 1), norm) self.assert_triangular_identity(Direction2(1, 2), Direction2(0, 1), norm) def test_direction_is_always_unitary(self): u = Direction2(1, 1) assert simeq(u.x, 1 / sqrt(2)) assert simeq(u.y, 1 / sqrt(2)) def test_sum_of_two_directions_is_a_vector(self): u = Direction2(1, 0) v = Direction2(0, 1) assert isinstance(u + v, Vec2) assert u + v == Vec2(1, 1) def test_floordiv(self): d = Direction2(1, 1) x, y = d // 2 assert x == y == 0
class TestPoint2D(LinearFixtures, VectorInvalidOperations, SequenceInterface): base_cls = Point2 add_uv = ElementwiseAddition.add_uv null = pytest.fixture(lambda self: Vec2(0, 0)) # Disabled tests test_vec_almost_equal = None def test_addition(self, u, v, add_uv): assert u + Vec2(*v) == add_uv assert Vec2(*v) + u == add_uv
def test_cross(self): assert Vec2(1, 0).cross(Vec2(0, 1)) == 1
def test_reverse_vec_equality_with_tuples_and_lists(self): assert [1, 2] == Vec2(1, 2) assert (1, 2) == Vec2(1, 2) assert [1.0, 2.0] == Vec2(1, 2) assert (1.0, 2.0) == Vec2(1, 2)
def test_vec_equality_with_tuples_and_lists(self): assert Vec2(1, 2) == [1, 2] assert Vec2(1, 2) == (1, 2) assert Vec2(1, 2) == [1.0, 2.0] assert Vec2(1, 2) == (1.0, 2.0)
def test_vec_equality(self): assert Vec2(1, 2) == Vec2(1, 2) assert Vec2(1, 2) == Vec2(1.0, 2.0)
def test_correct_type_promotion_on_vec_creation(self): assert isinstance(Vec2(1.0, 2.0), Vec2) assert isinstance(Vec2(1, 2.0), Vec2) assert isinstance(Vec2(1.0, 2), Vec2)
def test_triangular_identity_2D(self, norm): self.assert_triangular_identity(Vec2(1, 2), Vec2(3, 4), norm) self.assert_triangular_identity(Vec2(1, 1), Vec2(1, 1), norm) self.assert_triangular_identity(Vec2(1, 2), Vec2(0, 0), norm)
def test_sum_of_two_directions_is_a_vector(self): u = Direction2(1, 0) v = Direction2(0, 1) assert isinstance(u + v, Vec2) assert u + v == Vec2(1, 1)
def cols(self): yield Vec2(self._a, self._c) yield Vec2(self._b, self._d)
def test_clamp(sef): u = Vec2(3, 4) assert u.clamped(1, 10) == u assert u.clamped(10) == 2 * u assert u.clamped(2, 4) == u.normalized() * 4
def test_rotations(self): v = Vec2(1, 0) assert simeq(v.rotated_by(pi / 2), Vec2(0, 1)) assert simeq(v.rotated_axis(pi / 2, Vec2(1, 0)), v)
def test_polar_coordinates(self): assert Vec2.from_polar(1, 0) == Vec2(1, 0)
def test_vector_repr(self): assert repr(Vec2(1, 2)) == 'Vec2(1, 2)' assert repr(Vec2(0.5, 0.5)) == 'Vec2(0.5, 0.5)'
def test_addition(self, u, v, add_uv): assert u + Vec2(*v) == add_uv assert Vec2(*v) + u == add_uv
def test_identity_solve(self, I, N): b = Vec2(*range(1, N + 1)) assert I.solve(b) == b assert I.solve_jacobi(b) == b assert I.solve_gauss(b) == b assert I.solve_triangular(b) == b
def __iter__(self): yield Vec2(self._a, self._b) yield Vec2(self._c, self._d)
def test_polar(self): r, t = Vec2(1, 1).polar() assert simeq(r, sqrt(2)) assert simeq(t, pi / 4)
def diag(self): return Vec2(self._a, self._d)
def test_perp(self): assert Vec2(1, 0).perpendicular() == Vec2(0, 1) assert Vec2(1, 0).perpendicular(ccw=False) == Vec2(0, -1)