Example #1
0
    def test_get_elements(self):
        l = DoublyLinkedList()

        self.assertEqual(l.get_elements(), 0)

        for i in range(100):
            l.add(i)
            self.assertEqual(l.get_elements(), (i + 1))

        for i in range(100):
            l.delete(i)
            self.assertEqual(l.get_elements(), (99 - i))
Example #2
0
    def test_add(self):
        l = DoublyLinkedList()

        self.assertTrue(l.is_empty())

        l.add(0)
        self.assertEqual(l.get_first(), 0)
        self.assertEqual(l.get_last(), 0)
        self.assertEqual(l.get_elements(), 1)

        l.add(1)
        self.assertEqual(l.get_last(), 1)
        self.assertNotEqual(l.get_first(), 1)
        self.assertEqual(l.get_first(), 0)
        self.assertEqual(l.get_elements(), 2)

        l.add(2)
        self.assertEqual(l.get_last(), 2)
        self.assertEqual(l.get_first(), 0)
        self.assertEqual(l.index_of(1), 1)
        self.assertEqual(l.get_elements(), 3)

        self.assertFalse(l.is_empty())
Example #3
0
    def test_delete(self):
        l = DoublyLinkedList()

        self.assertRaises(ValueError, l.delete, 0)

        for i in range(5):
            l.add(i)
            self.assertTrue(l.contains(i))

        l.delete(2)
        self.assertFalse(l.contains(2))
        self.assertEqual(l.index_of(1), 1)
        self.assertEqual(l.index_of(3), 2)
        self.assertEqual(l.get_elements(), 4)

        l.delete(0)
        self.assertFalse(l.contains(0))
        self.assertEqual(l.get_first(), 1)
        self.assertEqual(l.index_of(3), 1)
        self.assertEqual(l.get_elements(), 3)

        l.delete(4)
        self.assertFalse(l.contains(4))
        self.assertEqual(l.get_last(), 3)
        self.assertEqual(l.get_first(), 1)
        self.assertRaises(ValueError, l.index_of, 4)
        self.assertEqual(l.get_elements(), 2)

        l.delete(1)
        self.assertFalse(l.contains(1))
        self.assertEqual(l.get_first(), 3)
        self.assertEqual(l.get_last(), 3)
        self.assertEqual(l.get_elements(), 1)

        l.delete(3)
        self.assertTrue(l.is_empty())
        self.assertEqual(l.get_elements(), 0)