Exemple #1
0
 def test_properties(self):
     self.assertEqual(vec2(1, 2).x, 1)
     self.assertEqual(vec2(1, 2).y, 2)
     a = vec2()
     a.x = 5
     self.assertEqual((5, 0), a)
     a.y = 6
     self.assertEqual((5, 6), a)
Exemple #2
0
 def test_setitem(self):
     a = vec2(0)
     a[0] = 1
     self.assertEqual(vec2(1, 0), a)
     a[1] = 2
     self.assertEqual(vec2(1, 2), a)
     with self.assertRaises(IndexError):
         a[3] = 1
Exemple #3
0
 def test_round(self):
     self.assertEqual((0, 1), vec2(0.5, 0.51).round())
     self.assertEqual((0, -1), vec2(-0.5, -0.51).round())
     self.assertEqual((0.5, 0.5), vec2(0.5, 0.51).round(1))
     self.assertEqual((-0.5, -0.5), vec2(-0.5, -0.51).round(1))
     self.assertEqual((0.12346, 0.12341),
                      vec2(0.123456, 0.1234123456789).round(5))
     self.assertEqual((0, 1), vec2(0.5, 0.51).rounded())
     self.assertEqual((0, -1), vec2(-0.5, -0.51).rounded())
Exemple #4
0
    def test_add(self):
        self.assertEqual(vec2(3), vec2(1) + 2)
        self.assertEqual(vec2(3), vec2(1) + vec2(2))
        self.assertEqual(vec2(3, 5), vec2(1, 2) + vec2(2, 3))
        self.assertEqual(vec2(3), vec2(1) + vec2(2))
        self.assertEqual(vec2(2, 3), vec2(1) + [1, 2])
        self.assertEqual(vec2(2, 3), vec2(1) + ["1", "2"])

        self.assertEqual(vec2(3), 2 + vec2(1))
        self.assertEqual(vec2(3), "2" + vec2(1))
        self.assertEqual(vec2(2, 3), [1, 2] + vec2(1))
        self.assertEqual(vec2(2, 3), ["1", "2"] + vec2(1))

        with self.assertRaises(TypeError):
            vec2() + [1]

        a = vec2(1)
        a += 1
        self.assertEqual(vec2(2), a)
        a += [1, 2]
        self.assertEqual(vec2(3, 4), a)
Exemple #5
0
 def test_floor(self):
     self.assertEqual(vec2(1, 2), vec2(1.4, 2.5).floor())
     self.assertEqual(vec2(-2, -3), vec2(-1.4, -2.5).floor())
     self.assertEqual(vec2(1, 2), vec2(1.4, 2.5).floored())
     self.assertEqual(vec2(-2, -3), vec2(-1.4, -2.5).floored())
Exemple #6
0
 def test_abs(self):
     self.assertEqual(vec2(1, 2), abs(vec2(-1, -2)))
     self.assertEqual(vec2(1, 2), abs(vec2(1, -2)))
Exemple #7
0
 def test_iter(self):
     self.assertEqual([1, 2], [x for x in vec2(1, 2)])
Exemple #8
0
 def test_getitem(self):
     a = vec2(1, 2)
     self.assertEqual(1, a[0])
     self.assertEqual(2, a[1])
     with self.assertRaises(IndexError):
         b = a[3]
Exemple #9
0
 def test_equal(self):
     self.assertTrue(vec2(1) == vec2(1))
     self.assertFalse(vec2(1) == vec2(2))
     self.assertTrue(vec2(1) == (1, 1))
     self.assertFalse(vec2(1) == (1, 1, 1))
Exemple #10
0
 def test_dot(self):
     self.assertEqual(2 * 4 + 3 * 5, vec2(2, 3).dot((4, 5)))
     with self.assertRaises(TypeError):
         vec2().dot((1, 2, 3))
Exemple #11
0
 def test_rotated(self):
     self.assertEqual(vec2(-2, 1), vec2(1, 2).rotated_z(90).rounded())
     self.assertEqual(vec2(3, 2), vec2(2, -3).rotated_z(90).rounded())
Exemple #12
0
 def test_mod(self):
     self.assertEqual(vec2(1, 0), vec2(1, 2) % 2)
     self.assertEqual(vec2(1, .5), vec2(1, 3) % 2.5)
Exemple #13
0
    def test_div(self):
        self.assertEqual(vec2(1.5), vec2(3) / vec2(2))
        self.assertEqual(vec2(1.5), vec2(3) / 2)
        self.assertEqual(vec2(2, 1), vec2(2) / (1, 2))
        self.assertEqual(vec2(2, 1), vec2(2) / vec2((1, 2)))
        self.assertEqual(vec2(1.5), 3 / vec2(2))
        self.assertEqual(vec2(.5, 1), (1, 2) / vec2(2))

        a = vec2(8)
        a /= 2
        self.assertEqual(vec2(4), a)
        a /= [1, 2]
        self.assertEqual(vec2((4, 2)), a)
Exemple #14
0
    def test_mul(self):
        self.assertEqual(vec2(6), vec2(2) * vec2(3))
        self.assertEqual(vec2(6), vec2(2) * 3)
        self.assertEqual(vec2(2, 4), vec2(2) * (1, 2))
        self.assertEqual(vec2(2, 4), vec2(2) * vec2(1, 2))
        self.assertEqual(vec2(6), 2 * vec2(3))
        self.assertEqual(vec2(3, 6), (1, 2) * vec2(3))

        a = vec2(1)
        a *= 2
        self.assertEqual(vec2(2), a)
        a *= [1, 2]
        self.assertEqual(vec2(2, 4), a)
Exemple #15
0
    def test_sub(self):
        self.assertEqual(vec2(1), vec2(3) - vec2(2))
        self.assertEqual(vec2(1), vec2(3) - 2)
        self.assertEqual(vec2(2, 1), vec2(3) - (1, 2))
        self.assertEqual(vec2(1), 3 - vec2(2))
        self.assertEqual(vec2(-1, 0), (1, 2) - vec2(2))

        a = vec2(1)
        a -= 2
        self.assertEqual(vec2(-1), a)
        a -= [1, 2]
        self.assertEqual(vec2((-2, -3)), a)
Exemple #16
0
 def test_assignment(self):
     self.assertEqual(
         "vec2(0, 0)",
         str(vec2()),
     )
     self.assertEqual("vec2(1, 1)", str(vec2(1)))
     self.assertEqual("vec2(5, 5)", str(vec2("5")))
     self.assertEqual("vec2(1, 2)", str(vec2(1, 2)))
     self.assertEqual("vec2(1, 2)", str(vec2("1", "2")))
     self.assertEqual("vec2(1, 0)", str(vec2((1, ))))
     self.assertEqual("vec2(1, 2)", str(vec2((1, 2))))
     self.assertEqual("vec2(1, 2)", str(vec2((1, 2))))
     self.assertEqual("vec2(1, 2)", str(vec2(1, (2, ))))
     self.assertEqual("vec2(1, 2)", str(vec2(("1", "2"))))
     self.assertEqual("vec2(1, 0)", str(vec2([1])))
     self.assertEqual("vec2(1, 2)", str(vec2([1, 2])))
     with self.assertRaises(ValueError):
         vec2(1, 2, 3)
     with self.assertRaises(ValueError):
         vec2((1, 2, 3, 4))
     with self.assertRaises(TypeError):
         vec2("bla")
     with self.assertRaises(TypeError):
         vec2({"x": 23})