def test_eq(self): vector_1 = Vector(1, 2) vector_2 = Vector(1, 2) vector_3 = Vector(1, 3) vector_4 = Vector(1, 2, 3) assert (vector_1 == vector_2) == True assert (vector_1 == vector_3) == False assert (vector_1 == vector_4) == False assert (vector_1 == (1, 2)) == False
def test_new_class(self): with pytest.raises(TypeError): Vector([1, 2]) with pytest.raises(TypeError): Vector(1) with pytest.raises(TypeError): Vector(1, 2, 3, 4) assert Vector(1, 2) == Vector2D(1, 2) assert Vector(1, 2, 3) == Vector3D(1, 2, 3) assert Vector(*[1, 2]) == Vector2D(1, 2)
def test_truediv(self): with pytest.raises(TypeError): Vector(1, 2) / Vector(1, 2, 3) with pytest.raises(TypeError): Vector(1, 2) / (1, 2, 3) with pytest.raises(TypeError): 3 / Vector(1, 2) with pytest.raises(TypeError): Vector({}, {}) / 2 assert Vector(3, 6) / 2 == Vector(1.5, 3) assert Vector(3, 6) / 1.5 == Vector(2, 4) assert Vector(3, 6, 9) / 2 == Vector(1.5, 3, 4.5) assert Vector(3, 6, 9) / 1.5 == Vector(2, 4, 6)
def test_rmul(self): assert 3 * Vector(1, 2) == Vector(3, 6) assert 3 * Vector(1, 2, 3) == Vector(3, 6, 9)
def test_mul(self): with pytest.raises(TypeError): Vector(1, 2) * Vector(1, 2, 3) with pytest.raises(TypeError): Vector(1, 2) * (1, 2, 3) with pytest.raises(TypeError): Vector({}, {}) * 2 assert Vector(1, 2) * 3 == Vector(3, 6) assert Vector(1, 2) * 1.5 == Vector(1.5, 3) assert Vector(1, 2, 3) * 3 == Vector(3, 6, 9) assert Vector(1, 2, 3) * 1.5 == Vector(1.5, 3, 4.5)
def test_rsub(self): assert [3, 3] - Vector(1, 1) == Vector(2, 2)
def test_sub(self): with pytest.raises(TypeError): Vector(1, 2) - Vector(1, 2, 3) with pytest.raises(TypeError): Vector(1, 2) - (1, 2, 3) with pytest.raises(TypeError): Vector(1, 2) - 1 assert Vector(3, 3) - Vector(1, 1) == Vector(2, 2) assert Vector(3, 3) - (1, 1) == Vector(2, 2) assert Vector(3, 3, 3) - Vector(1, 1, 1) == Vector(2, 2, 2) assert Vector(3, 3, 3) - (1, 1, 1) == Vector(2, 2, 2)
def test_radd(self): vector = Vector(1, 2) assert [1, 1] + vector == Vector(2, 3)
def test_add(self): vector_2d = Vector(1, 2) vector_3d = Vector(1, 2, 3) with pytest.raises(TypeError): vector_2d + vector_3d with pytest.raises(TypeError): vector_2d + (1, 2, 3) with pytest.raises(TypeError): vector_2d + 1 assert Vector(1, 1) + Vector(1, 1) == Vector(2, 2) assert Vector(1, 1) + (1, 1) == Vector(2, 2) assert Vector(1, 1, 1) + Vector(1, 1, 1) == Vector(2, 2, 2) assert Vector(1, 1, 1) + (1, 1, 1) == Vector(2, 2, 2)
def test_neg(self): vector = Vector(1, 1) assert -vector == Vector2D(-1, -1)
def test_bool(self): vector = Vector(0, 0) assert bool(vector) == False vector = Vector(1, 1) assert bool(vector) == True
def test_dumps(self): vector = Vector(1, 2) assert vector.dumps() == _DATA[0] vector = Vector(1, 2, 3) assert vector.dumps() == _DATA[1]
def test_loads(self): vector = Vector.loads(_DATA[0]) assert vector._data == (1, 2) vector = Vector.loads(_DATA[1]) assert vector._data == (1, 2, 3)
def test_repr_head(self): vector = Vector(1, 2) assert vector._repr_head() == "Vector2D(1, 2)"
def test_abs(self): assert abs(Vector(3, 4)) == 5 assert abs(Vector(1, 1)) == 1.4142135623730951 assert abs(Vector(1, 1, 1)) == 1.7320508075688772
def test_floordiv(self): with pytest.raises(TypeError): Vector(1, 2) // Vector(1, 2, 3) with pytest.raises(TypeError): Vector(1, 2) // (1, 2, 3) with pytest.raises(TypeError): 3 // Vector(1, 2) with pytest.raises(TypeError): Vector({}, {}) // 2 assert Vector(3, 6) // 2 == Vector(1, 3) assert Vector(3, 6) // 2.5 == Vector(1, 2) assert Vector(3, 6, 9) // 2 == Vector(1, 3, 4) assert Vector(3, 6, 9) // 2.5 == Vector(1, 2, 3)