示例#1
0
 def test_base(self):
     v = Vector3()
     self.assertEqual(type(v), Vector3)
     v2 = Vector3(1, -2, 5)
     self.assertEqual(type(v), Vector3)
     self.assertEqual(v2, Vector3(1, -2, 5))
     self.assertNotEqual(v, v2)
示例#2
0
 def test_base(self):
     v = Vector3()
     assert isinstance(v, Vector3)
     v2 = Vector3(1, -2, 5)
     assert isinstance(v2, Vector3)
     assert v2 == Vector3(1, -2, 5)
     assert v != v2
示例#3
0
 def test_base(self):
     v = Plane(Vector3(1, 2, 3), 0.5)
     assert type(v) == Plane
     v2 = Plane(Vector3(1, 2, 3), 1)
     assert type(v) == Plane
     assert v2 == Plane(Vector3(1, 2, 3), 1)
     assert v != v2
示例#4
0
 def test_base(self):
     v = Transform()
     assert type(v) == Transform
     v2 = Transform(Basis(), Vector3(1, 2, 3))
     assert type(v) == Transform
     assert v2 == Transform(Basis(), Vector3(1, 2, 3))
     assert v != v2
 def setup(self):
     self.acls = PoolVector3Array
     random.seed(0)  # Fix seed for reproducibility
     # Use integer instead of float to avoid floating point imprecision in comparisons
     self.vg = lambda c=None: Vector3(random.randint(
         0, 100)) if c is None else [
             Vector3(random.randint(0, 100)) for x in range(c)
         ]
示例#6
0
 def test_properties(self, args):
     v = AABB(Vector3(1, 2, 3), Vector3(4, 5, 6))
     field, ret_type = args
     assert hasattr(v, field)
     field_val = getattr(v, field)
     assert type(field_val) == ret_type
     for val in (Vector3(), Vector3(0.1, -0.1, 2)):
         setattr(v, field, val)
         field_val = getattr(v, field)
         assert field_val == val
示例#7
0
 def test_equal(self):
     basis1 = Basis.build_from_euler(Vector3(1, 2, 3))
     basis2 = Basis.build_from_euler(Vector3(1, 2, 3))
     assert basis1 == basis2
     basis2.x = Vector3(1, 2, 3)
     assert basis1 != basis2
     basis1.x = Vector3(1, 2, 3)
     assert basis1 == basis2
     bad = Basis.build_from_euler(Vector3(1, 2, 4))
     assert not basis1 == bad  # Force use of __eq__
示例#8
0
 def test_build_from_vectors(self):
     # Can build it with int or float or nothing
     msg_tmpl = "%s vs (expected) %s (args=%s)"
     for args, expected_normal, expected_d in (
         [(), (0, 0, 0), 0],
         [
             (Vector3(0, 0, 0), Vector3(4, 5, 6), Vector3(7, 8, 9)),
             (0.40824827551841736, -0.8164965510368347,
              0.40824827551841736),
             0.0,
         ],
     ):
         v = Plane.build_from_vectors(*args)
         normal = (
             pytest.approx(v.normal.x),
             pytest.approx(v.normal.y),
             pytest.approx(v.normal.z),
         )
         assert normal == expected_normal, (
             msg_tmpl % (v.normal, expected_normal, args))
         assert v.d == expected_d, msg_tmpl % (v.d, expected_d, args)
     with pytest.raises(TypeError):
         Plane.build_from_vectors("a", Vector3(4, 5, 6), Vector3(7, 8, 9))
     with pytest.raises(TypeError):
         Plane.build_from_vectors(Vector3(1, 2, 3), "b", Vector3(7, 8, 9))
     with pytest.raises(TypeError):
         Plane.build_from_vectors(Vector3(1, 2, 3), Vector3(4, 5, 6), "c")
示例#9
0
 def test_property_normal(self):
     v = Plane(Vector3(1, 2, 3), 4)
     assert hasattr(v, 'normal')
     field_val = v.normal
     assert isinstance(field_val, Vector3)
     for val in (Vector3(), Vector3(0.1, -0.1, 2)):
         v.normal = val
         field_val = v.normal
         assert field_val == val
     for bad in ('dummy', None, b'b'):
         with pytest.raises(TypeError):
             v.normal = bad
示例#10
0
 def test_instantiate(self):
     # Can build it with int or float or nothing
     msg_tmpl = "%s vs (expected) %s"
     for expected_normal, expected_d in ([Vector3(0, 0, 0),
                                          0], [Vector3(1, 2, 3), 1]):
         v = Plane(expected_normal, expected_d)
         assert v.normal == expected_normal, msg_tmpl % (v.normal,
                                                         expected_normal)
         assert v.d == expected_d, msg_tmpl % (v.d, expected_d)
     with pytest.raises(TypeError):
         Plane("a", 1)
     with pytest.raises(TypeError):
         Plane(Vector3(), "b")
示例#11
0
 def test_methods(self):
     v = Vector3()
     # Don't test methods' validity but bindings one
     for field, ret_type, params in (['abs', Vector3, ()], [
             'angle_to', float, (v, )
     ], ['ceil', Vector3, ()], ['cross', Vector3, (v, )], [
             'cubic_interpolate', Vector3, (v, v, v, 0.5)
     ], ['distance_squared_to', float,
         (v, )], ['distance_to', float, (v, )], ['dot', float, (v, )], [
             'floor', Vector3,
             ()
         ], ['inverse', Vector3,
             ()], ['length', float,
                   ()], ['length_squared', float,
                         ()], ['linear_interpolate', Vector3,
                               (v, 0.5)], ['max_axis', int,
                                           ()], ['min_axis', int, ()],
                                     ['normalized', Vector3,
                                      ()], ['reflect', Vector3, (v, )],
                                     ['rotated', Vector3,
                                      (v, 0.5)], ['slide', Vector3, (v, )],
                                     ['snapped', Vector3, (v, )]):
         self.assertTrue(hasattr(v, field),
                         msg='`Vector3` has no method `%s`' % field)
         method = getattr(v, field)
         self.assertTrue(callable(method))
         ret = method(*params)
         self.assertEqual(type(ret),
                          ret_type,
                          msg="`Vector3.%s` is expected to return `%s`" %
                          (field, ret_type))
示例#12
0
 def test_methods(self, args):
     v = Vector3()
     # Don't test methods' validity but bindings one
     field, ret_type, params = args
     assert hasattr(v, field)
     method = getattr(v, field)
     assert callable(method)
     ret = method(*params)
     assert isinstance(ret, ret_type)
示例#13
0
 def test_methods(self, args):
     v = Plane(Vector3(1, 1, 1), 1)
     # Don't test methods' validity but bindings one
     field, ret_type, params = args
     assert hasattr(v, field)
     method = getattr(v, field)
     assert callable(method)
     ret = method(*params)
     assert type(ret) == ret_type
示例#14
0
 def test_build_from_reals(self):
     # Can build it with int or float or nothing
     msg_tmpl = "%s vs (expected) %s (args=%s)"
     for args, expected_normal, expected_d in ([(), Vector3(0, 0, 0),
                                                0], [(1, 2, 3, 4),
                                                     Vector3(1, 2, 3), 4]):
         v = Plane.build_from_reals(*args)
         assert v.normal == expected_normal, (
             msg_tmpl % (v.normal, expected_normal, args))
         assert v.d == expected_d, msg_tmpl % (v.d, expected_d, args)
     with pytest.raises(TypeError):
         Plane.build_from_reals("a", 2, 3, 4)
     with pytest.raises(TypeError):
         Plane.build_from_reals(1, "b", 3, 4)
     with pytest.raises(TypeError):
         Plane.build_from_reals(1, 2, "c", 4)
     with pytest.raises(TypeError):
         Plane.build_from_reals(1, 2, 3, "d")
示例#15
0
 def test_properties(self, args):
     v = Vector3()
     field, ret_type = args
     assert hasattr(v, field)
     field_val = getattr(v, field)
     assert isinstance(field_val, ret_type)
     val = 10.
     setattr(v, field, val)
     field_val = getattr(v, field)
     assert field_val == val
示例#16
0
 def test_instantiate(self):
     # Can build it with int or float or nothing
     for args, expected_x, expected_y, expected_z in (
             [(), 0, 0, 0],
             [(0.5, 0.5, 0.5), 0.5, 0.5, 0.5],
             [(1,), 1, 0, 0],
             [(1, 1), 1, 1, 0],
             [(1, 2, 3), 1, 2, 3]):
         v = Vector3(*args)
         assert v.x == expected_x
         assert v.y == expected_y
         assert v.z == expected_z
     with pytest.raises(TypeError):
         Vector3("a", 2, 3)
     with pytest.raises(TypeError):
         Vector3("a", 2)
     with pytest.raises(TypeError):
         Vector3(1, "b", 5)
     with pytest.raises(TypeError):
         Vector3(None, 2, "c")
示例#17
0
 def test_property_d(self):
     v = Plane(Vector3(1, 2, 3), 4)
     assert hasattr(v, 'd')
     field_val = v.d
     assert isinstance(field_val, (float, int))
     for val in (0.5, -1, 2):
         v.d = val
         field_val = v.d
         assert field_val == val
     for bad in ('dummy', None, b'b'):
         with pytest.raises(TypeError):
             v.d = bad
示例#18
0
 def test_base(self):
     v = AABB(Vector3(1, 2, 3), Vector3(4, 5, 6))
     assert type(v) == AABB
     v2 = AABB(Vector3(1, 2, 3), Vector3(4, 5, 7))
     assert type(v) == AABB
     assert v2 == AABB(Vector3(1, 2, 3), Vector3(4, 5, 7))
     assert v != v2
示例#19
0
 def test_base(self):
     v = Rect3(Vector3(1, 2, 3), Vector3(4, 5, 6))
     assert type(v) == Rect3
     v2 = Rect3(Vector3(1, 2, 3), Vector3(4, 5, 7))
     assert type(v) == Rect3
     assert v2 == Rect3(Vector3(1, 2, 3), Vector3(4, 5, 7))
     assert v != v2
示例#20
0
 def test_build_from_rows(self):
     args = (Vector3(1, 2, 3), Vector3(4, 5, 6), Vector3(7, 8, 9))
     v = Basis.build_from_rows(*args)
     assert isinstance(v, Basis)
     assert (v.x, v.y, v.z) == (Vector3(1, 4,
                                        7), Vector3(2, 5,
                                                    8), Vector3(3, 6, 9))
示例#21
0
 def test_instantiate(self):
     # Can build it with int or float or nothing
     msg_tmpl = "%s vs (expected) %s (args=%s)"
     for args, expected_pos, expected_size in (
         [(), Vector3(0, 0, 0), Vector3(0, 0, 0)],
         [(Vector3(0, 1, 0), Vector3(0, 0, 1)),
          Vector3(0, 1, 0),
          Vector3(0, 0, 1)],
     ):
         v = AABB(*args)
         assert v.position == expected_pos, msg_tmpl % (v.position,
                                                        expected_pos, args)
         assert v.size == expected_size, msg_tmpl % (v.size, expected_size,
                                                     args)
     with pytest.raises(TypeError):
         AABB("a", Vector3())
     with pytest.raises(TypeError):
         AABB(Vector3(), "b")
示例#22
0
 def test_properties(self):
     v = Vector3()
     for field, ret_type in (('x', float), ('y', float), ('z', float)):
         self.assertTrue(hasattr(v, field),
                         msg='`Vector3` has no property `%s`' % field)
         field_val = getattr(v, field)
         self.assertEqual(type(field_val),
                          ret_type,
                          msg="`Vector3.%s` is expected to be a `%s`" %
                          (field, ret_type))
         val = 10.
         setattr(v, field, val)
         field_val = getattr(v, field)
         self.assertEqual(
             field_val,
             val,
             msg="`Vector3.%s` is expected to be equal to `%d`" %
             (field_val, val))
示例#23
0
 def test_instanciate(self):
     # Can build it with int or float or nothing
     msg_tmpl = "%s vs (expected) %s (args=%s)"
     for args, expected_x, expected_y, expected_z in ([(), 0, 0, 0], [
         (0.5, 0.5, 0.5), 0.5, 0.5, 0.5
     ], [(1, ), 1, 0, 0], [(1, 1), 1, 1, 0], [(1, 2, 3), 1, 2, 3]):
         v = Vector3(*args)
         self.assertEqual(v.x,
                          expected_x,
                          msg=msg_tmpl % (v.x, expected_x, args))
         self.assertEqual(v.y,
                          expected_y,
                          msg=msg_tmpl % (v.y, expected_y, args))
         self.assertEqual(v.z,
                          expected_z,
                          msg=msg_tmpl % (v.z, expected_z, args))
     self.assertRaises(TypeError, Vector3, "a", 2, 3)
     self.assertRaises(TypeError, Vector3, "a", 2)
     self.assertRaises(TypeError, Vector3, 1, "b", 5)
     self.assertRaises(TypeError, Vector3, None, 2, "c")
示例#24
0
 def test_zero_div(self, arg):
     with pytest.raises(ZeroDivisionError):
         Vector3(2, 3, 4) / arg
示例#25
0
 def test_bad_properties(self, args):
     v = Vector3()
     field, bad_value = args
     with pytest.raises(TypeError):
         setattr(v, field, bad_value)
示例#26
0
 def test_bad_div(self, arg):
     with pytest.raises(TypeError):
         Vector3(2, 3, 4) / arg
示例#27
0
class TestVector3:
    def test_base(self):
        v = Vector3()
        assert isinstance(v, Vector3)
        v2 = Vector3(1, -2, 5)
        assert isinstance(v2, Vector3)
        assert v2 == Vector3(1, -2, 5)
        assert v != v2

    def test_repr(self):
        v = Vector3(1, 2, 3)
        assert repr(v) == "<Vector3(x=1.0, y=2.0, z=3.0)>"

    def test_instantiate(self):
        # Can build it with int or float or nothing
        for args, expected_x, expected_y, expected_z in (
            [(), 0, 0, 0],
            [(0.5, 0.5, 0.5), 0.5, 0.5, 0.5],
            [(1, ), 1, 0, 0],
            [(1, 1), 1, 1, 0],
            [(1, 2, 3), 1, 2, 3],
        ):
            v = Vector3(*args)
            assert v.x == expected_x
            assert v.y == expected_y
            assert v.z == expected_z
        with pytest.raises(TypeError):
            Vector3("a", 2, 3)
        with pytest.raises(TypeError):
            Vector3("a", 2)
        with pytest.raises(TypeError):
            Vector3(1, "b", 5)
        with pytest.raises(TypeError):
            Vector3(None, 2, "c")

    @pytest.mark.parametrize(
        "args",
        [
            ["abs", Vector3, ()],
            ["angle_to", float, (Vector3(), )],
            ["ceil", Vector3, ()],
            ["cross", Vector3, (Vector3(), )],
            [
                "cubic_interpolate", Vector3,
                (Vector3(), Vector3(), Vector3(), 0.5)
            ],
            ["distance_squared_to", float, (Vector3(), )],
            ["distance_to", float, (Vector3(), )],
            ["dot", float, (Vector3(), )],
            ["floor", Vector3, ()],
            ["inverse", Vector3, ()],
            ["length", float, ()],
            ["length_squared", float, ()],
            ["linear_interpolate", Vector3, (Vector3(), 0.5)],
            ["max_axis", int, ()],
            ["min_axis", int, ()],
            ["normalized", Vector3, ()],
            ["reflect", Vector3, (Vector3(), )],
            ["rotated", Vector3, (Vector3(), 0.5)],
            ["slide", Vector3, (Vector3(), )],
            ["snapped", Vector3, (Vector3(), )],
        ],
        ids=lambda x: x[0],
    )
    def test_methods(self, args):
        v = Vector3()
        # Don't test methods' validity but bindings one
        field, ret_type, params = args
        assert hasattr(v, field)
        method = getattr(v, field)
        assert callable(method)
        ret = method(*params)
        assert isinstance(ret, ret_type)

    @pytest.mark.parametrize("args", [("x", float), ("y", float),
                                      ("z", float)],
                             ids=lambda x: x[0])
    def test_properties(self, args):
        v = Vector3()
        field, ret_type = args
        assert hasattr(v, field)
        field_val = getattr(v, field)
        assert isinstance(field_val, ret_type)
        val = 10.
        setattr(v, field, val)
        field_val = getattr(v, field)
        assert field_val == val

    @pytest.mark.parametrize("args", [("x", "NaN"), ("y", "NaN"),
                                      ("z", "NaN")],
                             ids=lambda x: x[0])
    def test_bad_properties(self, args):
        v = Vector3()
        field, bad_value = args
        with pytest.raises(TypeError):
            setattr(v, field, bad_value)

    @pytest.mark.parametrize("args", [("AXIS_X", int), ("AXIS_Y", int),
                                      ("AXIS_Z", int)],
                             ids=lambda x: x[0])
    def test_contants(self, args):
        v = Vector3()
        field, ret_type = args
        assert hasattr(v, field)
        field_val = getattr(v, field)
        assert isinstance(field_val, ret_type)

    @pytest.mark.parametrize(
        "args",
        [
            (0, Vector3(0, 0, 0)),
            (1, Vector3(2, 3, 4)),
            (2.5, Vector3(5, 7.5, 10)),
            (Vector3(1, 1, 1), Vector3(2, 3, 4)),
            (Vector3(2, 3, 4), Vector3(4, 9, 16)),
        ],
        ids=lambda x: x[0],
    )
    def test_mult(self, args):
        param, result = args
        calc = Vector3(2, 3, 4) * param
        assert calc == result

    @pytest.mark.parametrize(
        "args",
        [
            (1, Vector3(2, 3, 4)),
            (.5, Vector3(4, 6, 8)),
            (2, Vector3(1, 1.5, 2)),
            (Vector3(1, 1, 1), Vector3(2, 3, 4)),
            (Vector3(2, 3, 4), Vector3(1, 1, 1)),
        ],
        ids=lambda x: x[0],
    )
    def test_div(self, args):
        param, result = args
        calc = Vector3(2, 3, 4) / param
        assert calc == result

    @pytest.mark.parametrize(
        "args",
        [
            (Vector3(0, 0, 0), Vector3(2, 3, 4)),
            (Vector3(3, 2, 1), Vector3(5, 5, 5)),
            (Vector3(-1, -4, -2), Vector3(1, -1, 2)),
        ],
        ids=lambda x: x[0],
    )
    def test_add(self, args):
        param, result = args
        calc = Vector3(2, 3, 4) + param
        assert calc == result

    @pytest.mark.parametrize(
        "args",
        [
            (Vector3(0, 0, 0), Vector3(2, 3, 4)),
            (Vector3(3, 2, 1), Vector3(-1, 1, 3)),
            (Vector3(-1, -1, -1), Vector3(3, 4, 5)),
        ],
        ids=lambda x: x[0],
    )
    def test_sub(self, args):
        param, result = args
        calc = Vector3(2, 3, 4) - param
        assert calc == result

    @pytest.mark.parametrize("arg", [None, 1, "dummy"], ids=lambda x: x[0])
    def test_bad_add(self, arg):
        with pytest.raises(TypeError):
            Vector3(2, 3, 4) + arg

    @pytest.mark.parametrize("arg", [None, 1, "dummy"], ids=lambda x: x[0])
    def test_bad_sub(self, arg):
        with pytest.raises(TypeError):
            Vector3(2, 3, 4) - arg

    @pytest.mark.parametrize("arg", [None, "dummy"], ids=lambda x: x[0])
    def test_bad_div(self, arg):
        with pytest.raises(TypeError):
            Vector3(2, 3, 4) / arg

    @pytest.mark.parametrize(
        "arg",
        [
            0,
            Vector3(0, 1, 1),
            Vector3(1, 0, 1),
            Vector3(1, 1, 0),
            Vector3(0, 0, 0)
        ],
        ids=lambda x: x[0],
    )
    def test_zero_div(self, arg):
        with pytest.raises(ZeroDivisionError):
            Vector3(2, 3, 4) / arg

    @pytest.mark.parametrize("arg", [None, "dummy"], ids=lambda x: x[0])
    def test_bad_mult(self, arg):
        with pytest.raises(TypeError):
            Vector3(2, 3, 4) * arg

    def test_equal(self):
        arr = Vector3(1, 2, 3)
        other = Vector3(1, 2, 3)
        assert arr == other
        bad = Vector3(1, 2, 4)
        assert not arr == bad  # Force use of __eq__

    @pytest.mark.parametrize("arg", [None, 0, "foo", Vector3(1, 2, 4)])
    def test_bad_equal(self, arg):
        arr = Vector3(1, 2, 3)
        assert arr != arg
示例#28
0
 def test_bad_equal(self, arg):
     arr = Vector3(1, 2, 3)
     assert arr != arg
示例#29
0
 def test_equal(self):
     arr = Vector3(1, 2, 3)
     other = Vector3(1, 2, 3)
     assert arr == other
     bad = Vector3(1, 2, 4)
     assert not arr == bad  # Force use of __eq__
示例#30
0
 def test_bad_mult(self, arg):
     with pytest.raises(TypeError):
         Vector3(2, 3, 4) * arg