Ejemplo n.º 1
0
    def test_deserialization(self):
        print("\nVector deserialization", end="")
        vec2d = vec.Vector2d()
        vec3d = vec.Vector3d()
        vec4d = vec.Vector4d()

        self.assertEqual(vec.Vector2d.fromDict(json.loads(repr(vec2d))), vec2d)
        self.assertEqual(vec.Vector3d.fromDict(json.loads(repr(vec3d))), vec3d)
        self.assertEqual(vec.Vector4d.fromDict(json.loads(repr(vec4d))), vec4d)

        vec2d = vec.Vector2d(1)
        vec3d = vec.Vector3d(2)
        vec4d = vec.Vector4d(3)

        self.assertEqual(vec.Vector2d.fromDict(json.loads(repr(vec2d))), vec2d)
        self.assertEqual(vec.Vector3d.fromDict(json.loads(repr(vec3d))), vec3d)
        self.assertEqual(vec.Vector4d.fromDict(json.loads(repr(vec4d))), vec4d)

        vec2d = vec.Vector2d(1.23, 32.132)
        vec3d = vec.Vector3d(2.2, 312.1231, 2431.123)
        vec4d = vec.Vector4d(3.231, 1231.23, 1231.213, 1231.213)

        self.assertEqual(vec.Vector2d.fromDict(json.loads(repr(vec2d))), vec2d)
        self.assertEqual(vec.Vector3d.fromDict(json.loads(repr(vec3d))), vec3d)
        self.assertEqual(vec.Vector4d.fromDict(json.loads(repr(vec4d))), vec4d)

        bad_dict = {"Vect": [0, 0]}
        with self.assertRaises(KeyError):
            vec.Vector2d.fromDict(bad_dict)
Ejemplo n.º 2
0
    def test_vec2_vector_operations(self):
        print("\nVector2d vector operations", end="")
        v1, v2 = vec.Vector2d(1, 2), vec.Vector2d(2, 1)

        actual1 = v1.dot(v2)
        actual2 = v2.dot(v1)
        expected = 4
        self.assertEqual(actual1, actual2)
        self.assertEqual(actual1, expected)

        actual = v1.sqrdistance(v2)
        expected = 2
        self.assertEqual(actual, expected)

        actual = v1.distance(v2)
        expected = math.sqrt(2)
        self.assertEqual(actual, expected)

        v3 = vec.Vector2d(3 * math.cos(10), 3 * math.sin(10))
        actual = v3.normalized()
        self.assertFalse(actual == v3)
        v3.normalize()
        self.assertTrue(actual == v3)
        expected = vec.Vector2d(math.cos(10), math.sin(10))
        self.assertEqual(round(actual.x, 10), round(expected.x, 10))
        self.assertEqual(round(actual.y, 10), round(expected.y, 10))

        v3 = vec.Vector2d(3, 4)
        actual = v3.length()
        expected = 5
        self.assertEqual(actual, expected)

        actual = v3.sqrlength()
        expected = 25
        self.assertEqual(actual, expected)
Ejemplo n.º 3
0
    def test_serialization(self):
        print("\nVector serialization", end="")
        vec2d = vec.Vector2d()
        vec3d = vec.Vector3d()
        vec4d = vec.Vector4d()

        self.assertEqual(json.loads(repr(vec2d)), {"Vector2d": [0, 0]})
        self.assertEqual(json.loads(repr(vec3d)), {"Vector3d": [0, 0, 0]})
        self.assertEqual(json.loads(repr(vec4d)), {"Vector4d": [0, 0, 0, 0]})

        vec2d = vec.Vector2d(1)
        vec3d = vec.Vector3d(2)
        vec4d = vec.Vector4d(3)

        self.assertEqual(json.loads(repr(vec2d)), {"Vector2d": [1, 1]})
        self.assertEqual(json.loads(repr(vec3d)), {"Vector3d": [2, 2, 2]})
        self.assertEqual(json.loads(repr(vec4d)), {"Vector4d": [3, 3, 3, 3]})

        vec2d = vec.Vector2d(1.23, 32.132)
        vec3d = vec.Vector3d(2.2, 312.1231, 2431.123)
        vec4d = vec.Vector4d(3.231, 1231.23, 1231.213, 1231.213)

        self.assertEqual(json.loads(repr(vec2d)), {"Vector2d": [1.23, 32.132]})
        self.assertEqual(json.loads(repr(vec3d)),
                         {"Vector3d": [2.2, 312.1231, 2431.123]})
        self.assertEqual(json.loads(repr(vec4d)),
                         {"Vector4d": [3.231, 1231.23, 1231.213, 1231.213]})
Ejemplo n.º 4
0
    def test_vec2_constructors(self):
        print("\nVector2d constructor", end="")
        vec2d = vec.Vector2d()
        self.assertEqual(vec2d.x, 0.0)
        self.assertEqual(vec2d.y, 0.0)

        vec2d = vec.Vector2d(1, 2)
        self.assertEqual(vec2d.x, 1.0)
        self.assertEqual(vec2d.y, 2.0)

        vec2d = vec.Vector2d(1)
        self.assertEqual(vec2d.x, 1.0)
        self.assertEqual(vec2d.y, 1.0)
Ejemplo n.º 5
0
    def test_vec2_bool_operations(self):
        print("\nVector2d bool operations", end="")
        v1, v2 = vec.Vector2d(0.1), vec.Vector2d(0.1)

        self.assertTrue(v1 == v2)
        self.assertTrue(v1 <= v2)
        self.assertTrue(v1 >= v2)

        v2 = vec.Vector2d(0.101)

        self.assertTrue(v1 != v2)

        self.assertTrue(v1 < v2)
        self.assertTrue(v1 <= v2)

        self.assertTrue(v2 > v1)
        self.assertTrue(v2 >= v1)
Ejemplo n.º 6
0
    def test_str(self):
        print("\nVector as str", end="")
        vec2d = vec.Vector2d()
        vec3d = vec.Vector3d()
        vec4d = vec.Vector4d()

        self.assertEqual(str(vec2d), "Vector2d")
        self.assertEqual(str(vec3d), "Vector3d")
        self.assertEqual(str(vec4d), "Vector4d")
Ejemplo n.º 7
0
    def test_vec2_properties(self):
        print("\nVector2d properties", end="")
        vec2 = vec.Vector2d(1)

        self.assertEqual(vec2.x, 1)
        vec2.x = 2
        self.assertEqual(vec2.x, 2)

        self.assertEqual(vec2.y, 1)
        vec2.y = 3
        self.assertEqual(vec2.y, 3)
Ejemplo n.º 8
0
    def test_vec2_simple_operations(self):
        print("\nVector2d simple operations", end="")
        v1, v2 = vec.Vector2d(1, 2), vec.Vector2d(2, 1)

        actual = v1 + v2
        expected = vec.Vector2d(3)
        self.assertEqual(actual, expected)

        v1 += v2
        expected = vec.Vector2d(3)
        self.assertEqual(v1, expected)

        v1 -= v2
        expected = vec.Vector2d(1, 2)
        self.assertEqual(v1, expected)

        actual = v1 - v2
        expected = vec.Vector2d(-1, 1)
        self.assertEqual(actual, expected)

        actual = v1 * 10
        expected = vec.Vector2d(10, 20)
        self.assertEqual(actual, expected)

        v1 *= 10
        expected = vec.Vector2d(10, 20)
        self.assertEqual(actual, expected)

        v1 /= 10
        expected = vec.Vector2d(1, 2)
        self.assertEqual(v1, expected)

        actual = v1 / 10
        expected = vec.Vector2d(0.1, 0.2)
        self.assertEqual(actual, expected)

        actual = -v1
        expected = vec.Vector2d(-1, -2)
        self.assertEqual(actual, expected)