def test_p2_add():

    assert P2(1, 1) == P2(0, 0) + V2(1, 1)
    assert P2(1, 1) == V2(1, 1) + P2(0, 0)

    with pytest.raises(TypeError):
        P2(0, 0) + P2(1, 1)
예제 #2
0
def test_qv2_div():
    qv2 = make(V2(11, 21), si.meters)

    div_value = qv2 / 2
    assert V2(5.5, 10.5).approx(div_value.get_as(si.meters))

    qv2 /= 2
    assert V2(5.5, 10.5).approx(qv2.get_as(si.meters))
def test_floordiv():
    v1 = V2(1, 2)
    assert V2(0, 1).approx(v1 // 2)

    v1 //= 2
    assert V2(0, 1).approx(v1)

    with pytest.raises(TypeError):
        v1 // None
def test_div():
    v1 = V2(1, 2)
    assert V2(0.5, 1).approx(v1 / 2)

    v1 /= -2
    assert V2(-0.5, -1).approx(v1)

    with pytest.raises(TypeError):
        v1 / None
def test_v2_eq():
    assert V2(1, 1) == V2(1, 1)
    assert not V2(1, 1) == V2(0, 0)
    assert not None == V2(1, 1)

    assert V2(0.9999999, 0.9999999).approx(V2(1, 1))
    assert not V2(0.9, 0.9).approx(V2(1, 1))

    with pytest.raises(TypeError):
        V2(1, 1).approx(None)
def test_v2_mul():
    v1 = V2(1, 2)
    assert V2(2, 4).approx(v1 * 2)
    assert V2(2, 4).approx(2 * v1)

    v1 *= -3
    assert V2(-3, -6).approx(v1)

    with pytest.raises(TypeError):
        v1 * None
예제 #7
0
def test_qv2_unit():

    qv2 = make(V2(100, 0), si.meters)

    assert make(V2(1, 0), si.unity) == qv2.unit()

    qv2 = qv2.cvt_to(si.millimeters)

    assert make(V2(1, 0), si.unity) == qv2.unit()

    assert qv2 == abs(qv2) * qv2.unit()
예제 #8
0
def test_qv2_mul():
    qv2 = make(V2(3, 4), si.meters)

    mul_value = qv2 * 2
    assert V2(6, 8).approx(mul_value.get_as(si.meters))

    mul_value = 2 * qv2
    assert V2(6, 8).approx(mul_value.get_as(si.meters))

    qv2 *= 2
    assert V2(6, 8).approx(qv2.get_as(si.meters))
def test_create():

    xyz = make(V3(1, 2, 3), si.meters)

    assert V3(1, 2, 3) * si.meters == xyz
    assert V3(1, 2, 3) == xyz.get_as(si.meters)
    assert V3(1000, 2000, 3000) == xyz.get_as(si.millimeters)

    assert V2(1, 2) * si.meters == xyz.xy()
    assert V2(2, 3) * si.meters == xyz.yz()
    assert V2(1, 3) * si.meters == xyz.xz()
def test_v2_dot_product():
    v1 = V2(1, 0)
    v2 = V2(0, 1)
    assert v1.dot(v2) == pytest.approx(0)
    assert v1.dot(v1) == pytest.approx(1)
    assert v2.dot(v2) == pytest.approx(1)

    v3 = V2(1, 1)
    assert v3.dot(v3) == pytest.approx(2)

    v4 = V2(-2, -2)
    assert v4.dot(v4) == pytest.approx(8)
예제 #11
0
def test_qv2_rotate():
    vector = V2(1, 0)
    qv2 = make(vector, si.meters)

    qv2 = qv2.rotate(eta * si.radians)
    assert make(V2(0, 1), si.meters).approx(qv2)

    qv2 = qv2.rotate(eta * si.radians)
    assert make(V2(-1, 0), si.meters).approx(qv2)

    qv2 = qv2.rotate(eta * si.radians)
    assert make(V2(0, -1), si.meters).approx(qv2)
def test_v2_sub():
    v1 = V2(100, 100)
    v2 = V2(25, 50)
    v3 = V2(0, 0)

    assert V2(75, 50).approx(v1 - v2)
    assert v2.approx(v2 - v3)

    v3 -= v1
    assert V2(-100, -100).approx(v3)

    with pytest.raises(TypeError):
        v3 - None
def test_cross_product():
    v1 = V2(1, 0)
    v2 = V2(0, 1)
    assert v1.cross(v2) == pytest.approx(1)
    assert v2.cross(v1) == pytest.approx(-1)
    assert v1.cross(v1) == pytest.approx(0)
    assert v2.cross(v2) == pytest.approx(0)

    v3 = V2(1, 1)
    assert v3.cross(v1) == pytest.approx(-1)
    assert v1.cross(v3) == pytest.approx(1)
    assert v3.cross(v2) == pytest.approx(1)
    assert v2.cross(v3) == pytest.approx(-1)
def test_rotate():
    v1 = V2(1, 0)
    v1 = v1.rotate(eta)
    assert V2(0, 1).approx(v1)

    v1 = v1.rotate(eta)
    assert V2(-1, 0).approx(v1)

    v1 = v1.rotate(eta)
    assert V2(0, -1).approx(v1)

    v1 = v1.rotate(eta)
    assert V2(1, 0).approx(v1)
예제 #15
0
def test_qv2_angle():
    qv_x = make(V2(1, 0), si.meters)
    qv_y = make(V2(0, 1), si.meters)
    qv_xy = make(V2(1, 1), si.meters)

    assert pytest.approx(eta) == qv_x.angle(qv_y).get_as(si.radians)
    assert pytest.approx(eta / 2) == qv_x.angle(qv_xy).get_as(si.radians)
    assert pytest.approx(eta / 2) == qv_y.angle(qv_xy).get_as(si.radians)

    assert pytest.approx(eta) == qv_x.angle(V2(0, 1)).get_as(si.radians)

    with pytest.raises(TypeError):
        V2(0, 1).angle(qv_x)
예제 #16
0
def test_qv2_create():
    vector = V2(1, 0)

    vector_q = make(vector, si.meters)

    assert vector == vector_q.get_as(si.meters)
    assert V2(1000, 0) == vector_q.get_as(si.millimeters)

    vector_q = vector * si.meters
    assert vector == vector_q.get_as(si.meters)

    vector_q = si.meters * vector
    assert vector == vector_q.get_as(si.meters)
예제 #17
0
def test_approx_set_contains():

    pset = ApproxSet({P2(0, 0), P2(1, 1), P2(3, 3)})

    assert P2(0, 0) in pset
    assert P2(0.0, 0.0) in pset
    assert P2(0.0000001, 0.0000001) in pset

    assert P2(0.1, 0.1) not in pset

    assert P2(-1, -1) not in pset
    assert P2(10, 10) not in pset

    with pytest.raises(TypeError):
        ApproxSet({V2(0, 0), V2(1, 1)})
def test_v2_iter():
    it = iter(V2(1, 2))
    assert 1 == next(it)
    assert 2 == next(it)

    with pytest.raises(StopIteration):
        next(it)
예제 #19
0
def test_qv2_is_parallel():
    qv2 = make(V2(1, 1), si.meters)

    assert qv2.is_parallel(make(V2(2, 2), si.meters))
    assert qv2.is_parallel(make(V2(-1, -1), si.meters))

    assert not qv2.is_parallel(make(V2(1, 0), si.meters))
    assert not qv2.is_parallel(make(V2(0, 1), si.meters))

    assert qv2.is_parallel(V2(2, 2))
    assert qv2.is_parallel(V2(-1, -1))

    assert not qv2.is_parallel(V2(1, 0))
    assert not qv2.is_parallel(V2(0, 1))

    with pytest.raises(TypeError):
        V2(1, 1).is_parallel(qv2)
예제 #20
0
def test_line_contains_point():

    line = Line(1, 1, 0)

    assert line.contains(P2(0, 0))
    assert line.contains(P2(-1, 1))
    assert line.contains(P2(1, -1))

    assert not line.contains(P2(0, 1))

    assert not line.contains(V2(0, 0))
예제 #21
0
def test_translate():
    line = Line(2, 1, 2)
    assert line.y(0) == pytest.approx(2)
    assert line.x(0) == pytest.approx(1)

    translated = line.translate(V2(1, 1))
    assert translated.y(0) == pytest.approx(3)
    assert translated.x(0) == pytest.approx(2)

    horz = Line.ByPoints(P2(-1, 10), P2(1, 10))
    assert horz.y(0) == pytest.approx(10)

    translated = horz.translate(V2(1, 1))
    assert translated.y(0) == pytest.approx(11)

    vert = Line.ByPoints(P2(10, -1), P2(10, 1))
    assert vert.x(0) == pytest.approx(10)

    translated = vert.translate(V2(1, 1))
    assert translated.x(0) == pytest.approx(11)
def test_p2_ordering():
    assert P2(0, 0) < P2(1, 1)
    assert P2(0, 0) < P2(1, 0)
    assert P2(0, 0) < P2(1, -1)

    assert P2(0, 0) > P2(0, -1)

    with pytest.raises(TypeError):
        P2(0, 0) >= V2(0, 0)

    with pytest.raises(TypeError):
        P2(0, 0) > None
예제 #23
0
def test_contains(circle_00, circle_11):

    assert circle_00.contains(P2(0, 0))
    assert circle_11.contains(P2(0, 0))

    assert circle_00.contains(P2(-100, 0))
    assert circle_00.contains(P2(100, 0))
    assert circle_00.contains(P2(0, 100))
    assert circle_00.contains(P2(0, -100))

    assert not circle_00.contains(None)

    assert not circle_00.contains(P2(150, 0))
    assert not circle_00.contains(P2(0, 150))

    assert not circle_11.contains(V2(0, 0))
예제 #24
0
def test_qv2_is_orthogonal():
    qv2 = make(V2(1, 1), si.meters)

    assert qv2.is_orthogonal(make(V2(-1, 1), si.meters))
    assert qv2.is_orthogonal(V2(-1, 1))

    assert not qv2.is_orthogonal(make(V2(1, 0), si.meters))
    assert not qv2.is_orthogonal(V2(1, 0))

    with pytest.raises(TypeError):
        V2(1, 0).is_orthogonal(qv2)
예제 #25
0
def test_line_segment_contains():
    ls = LineSegment(P2(-10, -10), P2(10, 10))
    assert ls.contains(P2(0, 0))

    assert ls.contains(P2(5, 5))
    assert ls.contains(P2(-10, -10))
    assert ls.contains(P2(10, 10))

    assert not ls.contains(P2(-11, -11))
    assert not ls.contains(P2(11, 11))
    assert not ls.contains(P2(1, 0))

    with pytest.raises(TypeError):
        ls.contains(V2(10, 10))

    ls = LineSegment(P2(0, 0), P2(9999999999.9, 9999999999.9))
    assert ls.contains(P2(0, 0))
    assert ls.contains(P2(9999999999.9, 9999999999.9))
    assert not ls.contains(P2(9999999999.99, 9999999999.99))
def test_add():
    v1 = V2(0, 0)
    v2 = V2(1, 0)
    v3 = V2(0, 1)

    assert V2(1, 1).approx(v1 + v2 + v3)

    v1 += v2
    assert V2(1, 0).approx(v1)

    v1 += v3
    assert V2(1, 1).approx(v1)

    with pytest.raises(TypeError):
        v1 + None
예제 #27
0
def test_approx():

    assert approx(P2(0, 0), P2(0.0, 0.0))
    assert approx(P2(0.0000001, 0.0000001), P2(0, 0))
    assert not approx(P2(0, 0), V2(0, 0))
예제 #28
0
def test_qv2_dot_product():
    qv2 = make(V2(3, 4), si.meters)

    dot_product = qv2.dot(qv2)
    assert not isinstance(dot_product, V2Quantity)
    assert 25 == dot_product.get_as(si.meters**2)
예제 #29
0
def test_qv2_cross_product():
    qv2 = make(V2(1, 1), si.meters)

    cross_product = qv2.cross(qv2)
    assert not isinstance(cross_product, V2Quantity)
    assert 0 == cross_product.get_as(si.meters**2)
예제 #30
0
def test_qv2_magnitude():
    qv2 = make(V2(3, 4), si.meters)

    assert 5 == qv2.magnitude().get_as(si.meters)