Пример #1
0
 def test_full_forward_alternative(self):
     doubly_linked_list = DoublyLinkedList()
     doubly_linked_list.insert_last("element 1")
     doubly_linked_list.insert_last("element 2")
     doubly_linked_list.insert_last("element 3")
     it = doubly_linked_list.iterator()
     it.full_forward()
     self.assertEqual(it.get_previous(), "element 3")
Пример #2
0
 def test_has_next_alternative(self):
     doubly_linked_list = DoublyLinkedList()
     it = doubly_linked_list.iterator()
     self.assertFalse(it.has_next())
     doubly_linked_list.insert_last("element 1")
     doubly_linked_list.insert_last("element 2")
     doubly_linked_list.insert_last("element 3")
     it = doubly_linked_list.iterator()
     self.assertTrue(it.has_next())
     it.get_next()
     self.assertTrue(it.has_next())
     it.get_next()
     self.assertTrue(it.has_next())
     it.get_next()
     self.assertFalse(it.has_next())
Пример #3
0
 def setUp(self):
     self.list = DoublyLinkedList()
     self.iterator = self.list.iterator()
Пример #4
0
class TestDoublyLinkedListIterator(unittest.TestCase):
    def setUp(self):
        self.list = DoublyLinkedList()
        self.iterator = self.list.iterator()

    def add_elements(self, quantity, shift=0):
        for i in range(quantity):
            self.list.insert_last(f"element {i+1+shift}")
        self.iterator = self.list.iterator()

    def test_has_next(self):
        # 0 elements
        self.assertFalse(self.iterator.has_next())

        # 1 elements
        self.add_elements(1)
        self.assertTrue(self.iterator.has_next())

        # 2 elements
        self.add_elements(1)
        self.assertTrue(self.iterator.has_next())

        # 5 elements
        self.add_elements(3)
        self.assertTrue(self.iterator.has_next())

        # iterate to the end and test
        for _ in range(5):
            self.assertTrue(self.iterator.has_next())
            self.iterator.get_next()
        self.assertFalse(self.iterator.has_next())

        # clear list
        self.list.make_empty()
        self.iterator = self.list.iterator()
        self.assertFalse(self.iterator.has_next())

    def test_has_next_alternative(self):
        doubly_linked_list = DoublyLinkedList()
        it = doubly_linked_list.iterator()
        self.assertFalse(it.has_next())
        doubly_linked_list.insert_last("element 1")
        doubly_linked_list.insert_last("element 2")
        doubly_linked_list.insert_last("element 3")
        it = doubly_linked_list.iterator()
        self.assertTrue(it.has_next())
        it.get_next()
        self.assertTrue(it.has_next())
        it.get_next()
        self.assertTrue(it.has_next())
        it.get_next()
        self.assertFalse(it.has_next())

    def test_next(self):
        with self.assertRaises(NoSuchElementException):
            self.iterator.get_next()
        self.add_elements(1)
        self.assertEqual(self.iterator.get_next(), "element 1")
        self.add_elements(4, shift=1)
        self.assertEqual(self.iterator.get_next(), "element 1")
        self.assertEqual(self.iterator.get_next(), "element 2")
        self.assertEqual(self.iterator.get_next(), "element 3")
        self.assertEqual(self.iterator.get_next(), "element 4")
        self.assertEqual(self.iterator.get_next(), "element 5")
        with self.assertRaises(NoSuchElementException):
            self.iterator.get_next()

    def test_rewind(self):
        self.iterator.rewind()

        self.add_elements(5)

        for _ in range(3):
            self.iterator.get_next()
        self.iterator.rewind()
        self.assertTrue(self.iterator.has_next())
        self.assertEqual(self.iterator.get_next(), "element 1")

    def test_has_previous_single(self):
        self.add_elements(1)
        self.iterator.get_next()
        self.assertTrue(self.iterator.has_previous())

    def test_has_previous(self):
        self.add_elements(5)
        self.assertTrue(self.iterator.has_next())
        self.assertFalse(self.iterator.has_previous())

        for _ in range(5):
            self.iterator.get_next()
            self.assertTrue(self.iterator.has_previous())

    def test_previous(self):
        with self.assertRaises(NoSuchElementException):
            self.iterator.get_previous()
        self.add_elements(5)
        for i in range(5):
            self.assertEqual(self.iterator.get_next(), f"element {i+1}")
        for i in range(5):
            self.assertEqual(self.iterator.get_previous(), f"element {5-i}")
        with self.assertRaises(NoSuchElementException):
            self.iterator.get_previous()

    def test_previous_single_element(self):
        self.add_elements(1)
        self.iterator.get_next()
        self.iterator.get_previous()

    def test_previous_two_elements(self):
        self.add_elements(2)
        self.iterator.get_next()
        self.iterator.get_previous()

    def test_full_forward(self):
        self.iterator.full_forward()

        self.add_elements(5)
        self.iterator.full_forward()

        # Iterate to middle and rewind
        for _ in range(3):
            self.iterator.get_previous()
        self.iterator.full_forward()
        self.assertTrue(self.iterator.has_previous())
        self.assertEqual(self.iterator.get_previous(), "element 5")

        # Iterate to start and full forward
        while self.iterator.has_previous():
            self.iterator.get_previous()
        self.iterator.full_forward()
        self.assertTrue(self.iterator.has_previous())
        self.assertEqual(self.iterator.get_previous(), "element 5")

    def test_full_forward_alternative(self):
        doubly_linked_list = DoublyLinkedList()
        doubly_linked_list.insert_last("element 1")
        doubly_linked_list.insert_last("element 2")
        doubly_linked_list.insert_last("element 3")
        it = doubly_linked_list.iterator()
        it.full_forward()
        self.assertEqual(it.get_previous(), "element 3")
Пример #5
0
 def setUp(self):
     self.list = DoublyLinkedList()
Пример #6
0
class TestDoublyLinkedList(unittest.TestCase):
    def setUp(self):
        self.list = DoublyLinkedList()

    def test_insert_first(self):
        self.list.insert_first("element 1")
        self.assertEqual(self.list.get(0), "element 1")
        self.list.insert_first("element 2")
        self.list.insert_first("element 3")
        self.list.insert_first("element 4")
        self.list.insert_first("element 5")
        self.assertEqual(self.list.get(0), "element 5")

    def test_insert_last(self):
        self.list.insert_last("element 1")
        self.assertEqual(self.list.get(0), "element 1")
        self.list.insert_last("element 2")
        self.list.insert_last("element 3")
        self.list.insert_last("element 4")
        self.list.insert_last("element 5")
        self.assertEqual(self.list.get(4), "element 5")

    def test_insert_first_and_last(self):
        self.list = DoublyLinkedList()
        self.list.insert_first("element 1")
        self.list.insert_last("element 2")
        self.assertEqual(self.list.get(0), "element 1")
        self.assertEqual(self.list.get(1), "element 2")

    # def test_insert(self): pass

    # def test_remove_first(self): pass

    # def test_remove_last(self): pass

    # def test_remove(self): pass

    def test_iterator(self):
        self.assertIsInstance(self.list.iterator(), TwoWayIterator)
Пример #7
0
 def test_insert_first_and_last(self):
     self.list = DoublyLinkedList()
     self.list.insert_first("element 1")
     self.list.insert_last("element 2")
     self.assertEqual(self.list.get(0), "element 1")
     self.assertEqual(self.list.get(1), "element 2")
Пример #8
0
class TestDoublyLinkedList(unittest.TestCase):
    def setUp(self):
        self.list = DoublyLinkedList()

    def add_elements(self, quantity, shift=0):
        for i in range(quantity):
            self.list.insert_last(f"element {i+1+shift}")
    def remove_elements(self, quantity):
        for _ in range(quantity):
            self.list.remove_last()

    def test_is_empty(self):
        self.assertTrue(self.list.is_empty())
        self.add_elements(1)
        self.assertFalse(self.list.is_empty())

    def test_size(self):
        self.assertEqual(self.list.size(), 0)
        self.add_elements(4)
        self.assertEqual(self.list.size(), 4)
        self.remove_elements(4)
        self.assertEqual(self.list.size(), 0)

    def test_get_first(self):
        with self.assertRaises(EmptyListException):
            self.list.get_first()
        self.add_elements(3)
        self.assertEqual(self.list.get_first(), "element 1")

    def test_get_last(self):
        with self.assertRaises(EmptyListException):
            self.list.get_last()
        self.add_elements(3)
        self.assertEqual(self.list.get_last(), "element 3")

    def test_get(self):
        with self.assertRaises(EmptyListException):
            self.list.get(0)
        self.add_elements(10)
        self.assertEqual(self.list.get(9), "element 10")
        self.assertEqual(self.list.get(5), "element 6")
        self.assertEqual(self.list.get(3), "element 4")
        self.assertEqual(self.list.get(2), "element 3")

    def test_find(self):
        self.assertEqual(self.list.find("empty list"), -1)
        self.add_elements(5)
        self.assertEqual(self.list.find("element 3"), 2)
        self.assertEqual(self.list.find("missing element"), -1)

    def test_insert_first(self):
        self.list.insert_first("element")
        self.assertEqual(self.list.get_first(), "element")
        self.list.make_empty()
        self.add_elements(5)
        self.assertNotEqual(self.list.get_first(), "element")
        self.list.insert_first("element")
        self.assertEqual(self.list.get_first(), "element")

    def test_insert_last(self):
        self.list.insert_first("element")
        self.assertEqual(self.list.get_last(), "element")
        self.list.make_empty()
        self.add_elements(5)
        self.assertNotEqual(self.list.get_last(), "element")
        self.list.insert_last("element")
        self.assertEqual(self.list.get_last(), "element")

    def test_insert(self):
        with self.assertRaises(InvalidPositionException):
            self.list.insert("element X", 42)
        self.list.insert("element 1", 0)
        self.assertEqual(self.list.get_first(), "element 1")
        self.add_elements(4, shift=1)
        self.list.insert("element X", 2)
        self.assertEqual(self.list.get(2), "element X")
        self.list.insert("last element", self.list.size())
        self.assertEqual(self.list.get_last(), "last element")
        with self.assertRaises(InvalidPositionException):
            self.list.insert("element bean", 42)

    def test_remove_first(self):
        with self.assertRaises(EmptyListException):
            self.list.remove_first()
        self.add_elements(5)
        self.list.remove_first()
        self.assertEqual(self.list.get_first(), "element 2")
        
    def test_remove_first_single_element(self):
        self.list.make_empty()
        self.add_elements(1)
        self.assertEqual(self.list.remove_first(), "element 1")

        with self.assertRaises(EmptyListException):
            self.list.remove_first()

        with self.assertRaises(EmptyListException):
            self.list.get(0)
        
        with self.assertRaises(EmptyListException):
            self.list.get_last()
        
        with self.assertRaises(EmptyListException):
            self.list.get_first()
        
        self.assertTrue(self.list.is_empty())
        
        self.assertEqual(self.list.find("element 1"), -1)
        
        self.add_elements(1)
        self.assertEqual(self.list.get_first(), "element 1")
        self.assertEqual(self.list.get_last(), "element 1")
        self.assertEqual(self.list.remove_first(), "element 1")    

    def test_remove_last(self):
        with self.assertRaises(EmptyListException):
            self.list.remove_last()
        self.add_elements(5)
        self.list.remove_last()
        self.assertEqual(self.list.get_last(), "element 4")

    def test_remove_last_single_element(self):
        self.list.make_empty()
        self.add_elements(1)
        self.assertEqual(self.list.remove_last(), "element 1")

        with self.assertRaises(EmptyListException):
            self.list.remove_last()

        with self.assertRaises(EmptyListException):
            self.list.get(0)
        
        with self.assertRaises(EmptyListException):
            self.list.get_last()
        
        with self.assertRaises(EmptyListException):
            self.list.get_first()
        
        self.assertTrue(self.list.is_empty())
        
        self.assertEqual(self.list.find("element 1"), -1)
        
        self.add_elements(1)
        self.assertEqual(self.list.get_first(), "element 1")
        self.assertEqual(self.list.get_last(), "element 1")
        self.assertEqual(self.list.remove_last(), "element 1")    

    def test_remove(self):
        with self.assertRaises(InvalidPositionException):
            self.list.remove(1)
        self.add_elements(5)
        with self.assertRaises(InvalidPositionException):
            self.list.remove(6)
        self.assertEqual(self.list.remove(0), "element 1")
        self.assertEqual(self.list.remove(2), "element 4")
        self.assertEqual(self.list.remove(2), "element 5")

    def test_remove_single_element(self):
        self.list.make_empty()
        self.add_elements(1)
        self.assertEqual(self.list.remove(0), "element 1")
        
        with self.assertRaises(InvalidPositionException):
            self.list.remove(0)

        with self.assertRaises(EmptyListException):
            self.list.get(0)
        
        with self.assertRaises(EmptyListException):
            self.list.get_last()
        
        with self.assertRaises(EmptyListException):
            self.list.get_first()
        
        self.assertTrue(self.list.is_empty())
        
        self.assertEqual(self.list.find("element 1"), -1)
        
        self.add_elements(1)
        self.assertEqual(self.list.get_first(), "element 1")
        self.assertEqual(self.list.get_last(), "element 1")
        self.assertEqual(self.list.remove(0), "element 1")    

    def test_make_empty(self):
        self.assertTrue(self.list.is_empty())
        self.add_elements(5)
        self.assertFalse(self.list.is_empty())
        self.list.make_empty()
        self.assertTrue(self.list.is_empty())

    def test_iterator(self):
        self.assertIsInstance(self.list.iterator(), Iterator) # Already tested with TwoWayIterator
        self.assertIsInstance(self.list.iterator(), TwoWayIterator)
        self.assertIsInstance(self.list.iterator(), DoublyLinkedListIterator)