Exemplo n.º 1
0
class TestPoint(unittest.TestCase):
    def setUp(self):
        self.sample_point = Point(2, 4)

    def test_str(self):
        self.assertEqual(str(self.sample_point), "(2, 4)")
        self.assertEqual(repr(self.sample_point), "Point(2, 4)")

    def test_eq(self):
        self.assertTrue(self.sample_point == self.sample_point)
        self.assertTrue(self.sample_point == Point(2, 4))
        self.assertFalse(self.sample_point == Point(1, 4))
        self.assertFalse(self.sample_point == Point(2, 5))

    def test_ne(self):
        self.assertTrue(self.sample_point != Point(1, 4))
        self.assertTrue(self.sample_point != Point(2, 5))
        self.assertFalse(self.sample_point != self.sample_point)
        self.assertFalse(self.sample_point != Point(2, 4))

    def test_add_(self):
        self.assertEqual(self.sample_point + self.sample_point, Point(4, 8))
        self.assertEqual(Point(1, 2) + Point(3, 3), Point(4, 5))

    def test_sub(self):
        self.assertEqual(Point(6, 3) - Point(2, 7), Point(4, -4))
        self.assertEqual(Point(6, 3) - Point(2, 0), Point(4, 3))

    def test_mul(self):
        self.assertEqual(self.sample_point * Point(2, 0), 4)
        self.assertEqual(self.sample_point * Point(2, 3), 16)
        self.assertEqual(self.sample_point * Point(2, -3), -8)

    def test_cross(self):
        self.assertEqual(self.sample_point.cross(Point(2, 5)), 2)

    def test_length(self):
        from math import sqrt
        self.assertEqual(self.sample_point.length(), sqrt(20))
Exemplo n.º 2
0
 def test_cross(self):
     self.assertEqual(Point.cross(Point(1, 2), Point(3, 4)), -2)
Exemplo n.º 3
0
from points import Point

point1 = Point(3, 7)
point2 = Point(-2, 1)
point1.__str__()
point2.__repr__()
print point1.eq(point2)
print point1.ne(point2)
print point1.add(point2)
print point1.sub(point2)
print point1.mul(point2)
print point2.mul(point1)
print point1.cross(point2)
print point2.cross(point1)
print point1.length()
print point2.length()