Exemplo n.º 1
0
def test_vector_neg():
    a = -Vector(1, 2, 3)
    assert a.x == -1
    assert a.y == -2
    assert a.z == -3

    a = -Vector(0, 0, 0)
    assert a.x == 0
    assert a.y == 0
    assert a.z == 0
Exemplo n.º 2
0
def test_vector_init():
    vec = Vector(1.2, 3.4, 5.6)
    assert vec.x == 1.2
    assert vec.y == 3.4
    assert vec.z == 5.6

    vec = Vector()
    assert vec.x == 0
    assert vec.y == 0
    assert vec.z == 0

    vec = Vector(z=3)
    assert vec.x == 0
    assert vec.y == 0
    assert vec.z == 3
Exemplo n.º 3
0
def test_vector_div():
    a = Vector(2, 4, 6)
    b = a / 2
    assert a.x == 2
    assert b.x == 1
    assert b.y == 2
    assert b.z == 3

    b = a / 2.0
    assert b.x == 1
    assert b.y == 2
    assert b.z == 3

    with pytest.raises(ValueError):
        Vector(1, 2, 3) / "1"
Exemplo n.º 4
0
def test_vector_mul():
    a = Vector(1, 2, 3)
    b = a * 5
    assert a.x == 1
    assert b.x == 5
    assert b.y == 10
    assert b.z == 15

    b = a * 5.0
    assert b.x == 5
    assert b.y == 10
    assert b.z == 15

    with pytest.raises(ValueError):
        Vector(1, 2, 3) * "1"
Exemplo n.º 5
0
def test_vector_sub():
    a = Vector(1, 2, 3)
    b = Vector(3, 4, 5)
    c = a - b

    assert a.x == 1
    assert a.y == 2
    assert a.z == 3
    assert b.x == 3
    assert b.y == 4
    assert b.z == 5
    assert c.x == -2
    assert c.y == -2
    assert c.z == -2

    with pytest.raises(ValueError):
        Vector(1, 2, 3) - 5
Exemplo n.º 6
0
def test_vector_add():
    a = Vector(1, 2, 3)
    b = Vector(3, 4, 5)
    c = a + b

    assert a.x == 1
    assert a.y == 2
    assert a.z == 3
    assert b.x == 3
    assert b.y == 4
    assert b.z == 5
    assert c.x == 4
    assert c.y == 6
    assert c.z == 8

    with pytest.raises(ValueError):
        Vector(1, 2, 3) + 5
Exemplo n.º 7
0
def test_vector_iteration():
    v = Vector(1, 2, 3)
    assert list(v) == [1, 2, 3]
    assert list(v) == [1, 2, 3]

    it = iter(v)
    assert it is not v

    it2 = iter(v)

    assert next(it) == 1
    assert next(it2) == 1
Exemplo n.º 8
0
def test_vector_indexing():
    v = Vector(1, 2, 3)
    assert v[0] == 1
    assert v[2] == 3
    v[1] = 5
    assert v[1] == 5
    assert v.y == 5

    with pytest.raises(IndexError):
        a = v[10]

    with pytest.raises(IndexError):
        v[8] = 5
Exemplo n.º 9
0
def test_vector_str():
    assert str(Vector(0, 0, 0)) == "(0, 0, 0)"
    assert str(Vector(1, 2, 3)) == "(1, 2, 3)"
Exemplo n.º 10
0
def test_vector_eq():
    assert Vector(0, 0, 0) == Vector(0, 0, 0)
    assert Vector(1, 2, 3) == Vector(1, 2, 3)
    assert Vector(1, 2, 3) != Vector(1, 2, 4)
    assert Vector(1, 2, 3) != 5
Exemplo n.º 11
0
def test_vector_length():
    assert Vector(0, 0, 0).length() == 0
    assert Vector(1, 0, 0).length() == 1
    assert "{:.3f}".format(Vector(10, 12, 13).length()) == "20.322"
Exemplo n.º 12
0
def test_vector_unit():
    assert Vector(0, 0, 0).unit == Vector(0, 0, 0)
    assert Vector(0, 8, 0).unit == Vector(0, 1, 0)