def testContains(self): a3 = Vec2dArray([(1, 2), (3, 4), (5, 6)]) self.assertTrue(Vec2d(3, 4) in a3) self.assertTrue(a3.__contains__(Vec2d(3, 4))) self.assertFalse(Vec2d(4, 3) in a3) self.assertFalse(a3.__contains__(Vec2d(4, 3))) self.assertTrue(Vec2d(4, 3) not in a3)
def testCopyByValue(self): v1 = Vec2d(12.5, 42) v2 = Vec2d(v1) self.assertIsNot(v1, v2) self.assertEqual(v1, v2) v2.x = 15 self.assertEqual(v1.x, 12.5) self.assertNotEqual(v1, v2)
def testFromTuple(self): t = (12.5, 42) v = Vec2d(t) self.assertEqual(v.x, 12.5) self.assertEqual(v.y, 42) t2 = (12.5, 42, 12) with self.assertRaises(ValueError): v = Vec2d(t2)
def testBracketOperator(self): v = Vec2d(12.5, 42) self.assertEqual(v[0], 12.5) self.assertEqual(v[1], 42) v[0] = 13.5 v[1] += 1 self.assertEqual(v[0], 13.5) self.assertEqual(v[1], 43)
def testCopyByReference(self): v1 = Vec2d(12.5, 42) v2 = v1 self.assertIs(v1, v2) self.assertEqual(v1, v2) v2.x = 15 self.assertEqual(v1.x, 15) self.assertEqual(v1, v2)
def testXY(self): v = Vec2d(12.5, 42) self.assertEqual(v.x, 12.5) self.assertEqual(v.y, 42) v.x = 13.5 v.y += 1 self.assertEqual(v.x, 13.5) self.assertEqual(v.y, 43)
def testInitializingConstructors(self): n = 3 x0 = Vec2d(0.5, 0.7) a1 = Vec2dArray(n) self.assertEqual(len(a1), n) for x in a1: self.assertEqual(x, Vec2d(0, 0)) a2 = Vec2dArray(n, x0) self.assertEqual(len(a2), n) for x in a2: self.assertEqual(x, x0) a3 = Vec2dArray([(1, 2), (3, 4)]) self.assertEqual(len(a3), 2) self.assertEqual(a3[0], Vec2d(1, 2)) self.assertEqual(a3[1], Vec2d(3, 4))
def testComparisonOperators(self): v1 = Vec2d(1, 2) v2 = Vec2d(3, 4) self.assertTrue(v1 == v1) self.assertTrue(v1 == Vec2d(1, 2)) self.assertTrue(v1 != v2) self.assertTrue(v1 != Vec2d(3, 4)) self.assertTrue(v1 < v2) self.assertTrue(v1 < Vec2d(2, 0)) self.assertTrue(v1 < Vec2d(1, 3)) self.assertTrue(v1 <= v1) self.assertTrue(v1 <= v2) self.assertTrue(v1 <= Vec2d(2, 0)) self.assertTrue(v1 <= Vec2d(1, 3)) self.assertTrue(v2 > v1) self.assertTrue(v2 > Vec2d(2, 100)) self.assertTrue(v2 >= v2) self.assertTrue(v2 >= v1) self.assertTrue(v2 >= Vec2d(2, 100))
def testToString(self): # Setup a French locale (if available on this system) to check that even # when the decimal point is ',' according to the locale, numbers are # still printed with '.' as decimal point. # try: locale.setlocale(locale.LC_ALL, 'fr_FR.UTF8') except: pass v = Vec2d(1, 2.5) self.assertEqual(str(v), "(1, 2.5)")
def testNormalized(self): v1 = Vec2d(3, 4) v2 = v1.normalized() self.assertEqual(v2.length(), 1) self.assertEqual(v1, Vec2d(3, 4)) self.assertEqual(v2, Vec2d(0.6, 0.8))
def testAppend(self): a = Vec2dArray() a.append(Vec2d(1, 2)) a.append(Vec2d(3, 4)) self.assertEqual(a, Vec2dArray([(1, 2), (3, 4)]))
def testToString(self): locale.setlocale(locale.LC_ALL, 'fr_FR.UTF8') v = Vec2d(1, 2.5) self.assertEqual(str(v), "(1, 2.5)")
def testInitializingConstructor(self): v = Vec2d(12.5, 42) self.assertEqual(v.x, 12.5) self.assertEqual(v.y, 42)
def testDefaultConstructor(self): # Note: in Python, Vec2d() does zero-initialization, unlike in C++ v = Vec2d() self.assertEqual(v.x, 0) self.assertEqual(v.y, 0)
def testOrthogonalized(self): v1 = Vec2d(3, 4) v2 = v1.orthogonalized() self.assertEqual(v1, Vec2d(3, 4)) self.assertEqual(v2, Vec2d(-4, 3))
def testNormalize(self): v = Vec2d(3, 4) v.normalize() self.assertEqual(v.length(), 1) self.assertEqual(v, Vec2d(0.6, 0.8))
def testLength(self): for v in [Vec2d(3, 4), Vec2d(-3, 4), Vec2d(3, -4), Vec2d(-3, -4)]: self.assertEqual(v.length(), 5) self.assertEqual(v.squaredLength(), 25)
def testToVec2d(self): v = toVec2d("(1, 2.5)") self.assertTrue(v == Vec2d(1, 2.5))
def testArithmeticOperators(self): v1 = Vec2d(1, 2) v1 += Vec2d(3, 4) self.assertEqual(v1.x, 4) self.assertEqual(v1.y, 6) self.assertEqual(v1, Vec2d(4, 6)) v2 = v1 + Vec2d(1, 1) self.assertEqual(v1, Vec2d(4, 6)) self.assertEqual(v2, Vec2d(5, 7)) v2 -= Vec2d(3, 2) self.assertEqual(v2, Vec2d(2, 5)) v1 = v1 - v2 self.assertEqual(v1, Vec2d(2, 1)) v1 *= 2 self.assertEqual(v1, Vec2d(4, 2)) v3 = 3 * v1 * 2 self.assertEqual(v3, Vec2d(24, 12)) v3 /= 2 self.assertEqual(v3, Vec2d(12, 6)) v4 = v3 / 12 self.assertEqual(v4, Vec2d(1, 0.5))
def testOrthogonalize(self): v = Vec2d(3, 4) v.orthogonalize() self.assertEqual(v, Vec2d(-4, 3))