Ejemplo n.º 1
0
    def test_add(self):
        """Addition operator."""

        # adding points is undefined
        with self.assertRaises(
                TypeError, msg="Point2D addition did not raise a TypeError."):
            Point2D() + Point2D()
Ejemplo n.º 2
0
    def test_distance_to(self):
        """Testing method distance_to()."""

        a = Point2D(-1, 5)
        b = Point2D(9, 4)
        v = a.distance_to(b)
        r = sqrt((9 + 1)**2 + (4 - 5)**2)
        self.assertEqual(v, r, "Point2D to Point2D distance is incorrect.")
Ejemplo n.º 3
0
    def test_subtract(self):
        """Subtraction operator."""

        # subtracting points is undefined
        with self.assertRaises(
                TypeError,
                msg="Point2D subtraction did not raise a TypeError."):
            Point2D() - Point2D()
Ejemplo n.º 4
0
    def test_initialise_default(self):
        """Default initialisation, point at local origin."""

        v = Point2D()
        self.assertEqual(v.x, 0.0,
                         "Default initialisation is not (0,0,0) [X].")
        self.assertEqual(v.y, 0.0,
                         "Default initialisation is not (0,0,0) [Y].")
Ejemplo n.º 5
0
    def test_iter(self):
        """Obtain values by iteration."""

        p = Point2D(2.5, 6.7)
        l = list(p)
        self.assertEqual(
            len(l), 2,
            "Iteration failed to return the correct number of items.")
        self.assertEqual(l[0], 2.5, "Iteration failed [X].")
        self.assertEqual(l[1], 6.7, "Iteration failed [Y].")
Ejemplo n.º 6
0
    def test_y(self):
        """Get/set y co-ordinate."""

        v = Point2D(2.5, 6.7)

        # get y attribute
        self.assertEqual(v.y, 6.7, "Getting y attribute failed.")

        # set y attribute
        v.y = -7.1
        self.assertEqual(v.y, -7.1, "Setting y attribute failed.")
Ejemplo n.º 7
0
    def test_x(self):
        """Get/set x co-ordinate."""

        v = Point2D(2.5, 6.7)

        # get x attribute
        self.assertEqual(v.x, 2.5, "Getting x attribute failed.")

        # set x attribute
        v.x = 10.0
        self.assertEqual(v.x, 10.0, "Setting x attribute failed.")
Ejemplo n.º 8
0
    def test_copy(self):
        """Testing method copy()."""

        v = Point2D(1.0, 2.0)
        r = v.copy()

        # check a new instance has been created by modifying the original
        v.x = 5.0
        v.y = 6.0

        self.assertEqual(r.x, 1.0, "Copy failed [X].")
        self.assertEqual(r.y, 2.0, "Copy failed [Y].")
Ejemplo n.º 9
0
    def test_not_equal(self):
        """Inequality operator."""

        self.assertFalse(
            Point2D(1, 2) != Point2D(1, 2),
            "Inequality operator returned true for equal points.")
        self.assertTrue(
            Point2D(5, 2) != Point2D(1, 2),
            "Inequality operator returned false for a point with non-equal x components."
        )
        self.assertTrue(
            Point2D(1, 5) != Point2D(1, 2),
            "Inequality operator returned false for a point with non-equal y components."
        )
Ejemplo n.º 10
0
    def test_indexing(self):
        """Getting/setting components by indexing."""

        v = Point2D(2.5, 6.7)

        v[0] = 1.0
        v[1] = 2.0

        # check getting/setting via valid indexes
        self.assertEqual(v[0], 1.0, "Indexing failed [X].")
        self.assertEqual(v[1], 2.0, "Indexing failed [Y].")

        # check invalid indexes
        with self.assertRaises(
                IndexError,
                msg="Invalid positive index did not raise IndexError."):
            r = v[3]

        with self.assertRaises(
                IndexError,
                msg="Invalid negative index did not raise IndexError."):
            r = v[-1]
Ejemplo n.º 11
0
    def test_initialise_invalid(self):
        """Initialisation with invalid types should raise a TypeError."""

        with self.assertRaises(TypeError, msg="Initialised with a string."):
            Point2D("spoon")
Ejemplo n.º 12
0
    def test_initialise_indexable(self):
        """Initialisation with an indexable object."""

        v = Point2D(1.0, 2.0)
        self.assertEqual(v.x, 1.0, "Initialisation with indexable failed [X].")
        self.assertEqual(v.y, 2.0, "Initialisation with indexable failed [Y].")