コード例 #1
0
def test_copy_pickle(py_c_vec) -> None:
    """Test pickling, unpickling and copying Angles."""
    Vec, Angle, Matrix, parse_vec_str = py_c_vec
    vec_mod.Angle = Angle

    test_data = 38.0, 257.125, 0.0

    orig = Angle(test_data)

    cpy_meth = orig.copy()

    assert orig is not cpy_meth  # Must be a new object.
    assert cpy_meth is not orig.copy()  # Cannot be cached
    assert orig == cpy_meth  # Numbers must be exactly identical!

    cpy = copy.copy(orig)

    assert orig is not cpy
    assert cpy_meth is not copy.copy(orig)
    assert orig == cpy

    dcpy = copy.deepcopy(orig)

    assert orig is not dcpy
    assert orig == dcpy

    pick = pickle.dumps(orig)
    thaw = pickle.loads(pick)

    assert orig is not thaw
    assert orig == thaw

    # Ensure both produce the same pickle - so they can be interchanged.
    cy_pick = pickle.dumps(Cy_Angle(test_data))
    py_pick = pickle.dumps(Py_Angle(test_data))

    assert cy_pick == py_pick == pick
コード例 #2
0
def test_construction(py_c_vec):
    """Check various parts of the constructor - Vec(), Vec.from_str()."""
    Vec, Angle, Matrix, parse_vec_str = py_c_vec
    
    for pit, yaw, rol in iter_vec(VALID_ZERONUMS):
        assert_ang(Angle(pit, yaw, rol), pit, yaw, rol)
        assert_ang(Angle(pit, yaw), pit, yaw, 0)
        assert_ang(Angle(pit), pit, 0, 0)
        assert_ang(Angle(), 0, 0, 0)

        assert_ang(Angle([pit, yaw, rol]), pit, yaw, rol)
        assert_ang(Angle([pit, yaw], roll=rol), pit, yaw, rol)
        assert_ang(Angle([pit], yaw=yaw, roll=rol), pit, yaw, rol)
        assert_ang(Angle([pit]), pit, 0, 0)
        assert_ang(Angle([pit, yaw]), pit, yaw, 0)
        assert_ang(Angle([pit, yaw, rol]), pit, yaw, rol)

        # Test this does nothing (except copy).
        ang = Angle(pit, yaw, rol)
        ang2 = Angle(ang)
        assert_ang(ang2, pit, yaw, rol)
        assert ang is not ang2

        ang3 = Angle.copy(ang)
        assert_ang(ang3, pit, yaw, rol)
        assert ang is not ang3

        # Test Angle.from_str()
        assert_ang(Angle.from_str('{} {} {}'.format(pit, yaw, rol)), pit, yaw, rol)
        assert_ang(Angle.from_str('<{} {} {}>'.format(pit, yaw, rol)), pit, yaw, rol)
        # {x y z}
        assert_ang(Angle.from_str('{{{} {} {}}}'.format(pit, yaw, rol)), pit, yaw, rol)
        assert_ang(Angle.from_str('({} {} {})'.format(pit, yaw, rol)), pit, yaw, rol)
        assert_ang(Angle.from_str('[{} {} {}]'.format(pit, yaw, rol)), pit, yaw, rol)

        # Test converting a converted Angle
        orig = Angle(pit, yaw, rol)
        new = Angle.from_str(Angle(pit, yaw, rol))
        assert_ang(new, pit, yaw, rol)
        assert orig is not new  # It must be a copy

        # Check as_tuple() makes an equivalent tuple
        tup = orig.as_tuple()

        # Flip to work arond the coercion.
        pit %= 360.0
        yaw %= 360.0
        rol %= 360.0

        assert isinstance(tup, tuple)
        assert (pit, yaw, rol) == tup
        assert hash((pit, yaw, rol)) == hash(tup)
        # Bypass subclass functions.
        assert tuple.__getitem__(tup, 0) == pit
        assert tuple.__getitem__(tup, 1) == yaw
        assert tuple.__getitem__(tup, 2) == rol

    # Check failures in Angle.from_str()
    # Note - does not pass through unchanged, they're converted to floats!
    for val in VALID_ZERONUMS:
        test_val = val % 360.0
        assert test_val == Angle.from_str('', pitch=val).pitch
        assert test_val == Angle.from_str('blah 4 2', yaw=val).yaw
        assert test_val == Angle.from_str('2 hi 2', pitch=val).pitch
        assert test_val == Angle.from_str('2 6 gh', roll=val).roll
        assert test_val == Angle.from_str('1.2 3.4', pitch=val).pitch
        assert test_val == Angle.from_str('34.5 38.4 -23 -38', roll=val).roll