def testReverse(self):
        slist = SinglyLinkedList()
        slist.insert_after(None, 14)
        slist.insert_after(None, 13)
        slist.insert_after(None, 12)
        slist.insert_after(None, 11)

        slist.reverse()

        actual = to_array(slist)
        expected = [14, 13, 12, 11]
        self.assertEqual(expected, actual)
    def testInsertion(self):
        slist = SinglyLinkedList()
        tail = slist.insert_after(None, 10)
        slist.insert_after(None, 11)
        actual = to_array(slist)
        expected = [11, 10]
        self.assertEqual(expected, actual)

        slist.insert_after(tail, 12)
        actual = to_array(slist)
        expected = [11, 10, 12]
        self.assertEqual(expected, actual)
Esempio n. 3
0
    def test_inserts(self):
        """ Test list inserts.
        """
        sll = SinglyLinkedList(Node('first'))

        sll.insert_after(sll.first_node, Node('second'))

        self.assertEquals(sll.first_node.data, 'first')

        self.assertEquals(sll.first_node.next_node.data, 'second')

        sll.insert_beginning(Node('before the first'))

        self.assertEquals(sll.first_node.data, 'before the first')

        self.assertEquals(sll.first_node.next_node.data, 'first')

        self.assertEquals(sll.first_node.next_node.next_node.data, 'second')

        self.assertEquals(str(sll), 'before the first, first, second')
Esempio n. 4
0
    def test_removals(self):
        """ Test list removals.
        """
        node1 = Node('first')
        sll = SinglyLinkedList(node1)

        node2 = Node('second')
        sll.insert_after(node1, node2)

        self.assertEquals(str(sll), 'first, second')

        sll.remove_after(node1)

        self.assertEquals(str(sll), 'first')

        sll.insert_after(node1, node2)

        sll.remove_beginning()

        self.assertEquals(str(sll), 'second')
    def testDeletion(self):
        slist = SinglyLinkedList()
        tail = slist.insert_after(None, 13)
        slist.insert_after(None, 12)

        actual = to_array(slist)
        expected = [12, 13]
        self.assertEqual(expected, actual)

        slist.delete_after(tail)

        actual = to_array(slist)
        expected = [12, 13]
        self.assertEqual(expected, actual)

        slist.delete_after(slist.head)

        actual = to_array(slist)
        expected = [12]
        self.assertEqual(expected, actual)
Esempio n. 6
0
 def test_print(self):
     """ Test the list printing.
     """
     sll = SinglyLinkedList(Node('first'))
     sll.insert_after(sll.first_node, Node('second'))
     self.assertEquals(str(sll), 'first, second')
class TestSinglyLinkedList(unittest.TestCase):

    def setUp(self):
        self.my_list = SinglyLinkedList()

    def test_basic_initialization_and_repr(self):
        self.assertEqual(repr(self.my_list), '[]')

    def test_append(self):
        self.my_list.append(4)
        self.my_list.append(3)
        self.my_list.append(7)
        self.my_list.append(-17)
        self.assertEqual(repr(self.my_list), '[4, 3, 7, -17]')

    def test_prepend(self):
        self.my_list.prepend(4)
        self.my_list.prepend(3)
        self.my_list.prepend(7)
        self.my_list.prepend(-17)
        self.assertEqual(repr(self.my_list), '[-17, 7, 3, 4]')

    def test_insert_after(self):
        self.my_list.insert_after(None, 4)
        self.my_list.insert_after(None, 3)
        self.my_list.insert_after(self.my_list.tail, 7)
        self.my_list.insert_after(self.my_list.head, -17)
        self.assertEqual(repr(self.my_list), '[3, -17, 4, 7]')

    def test_insert_sorted(self):
        self.my_list.insert_after(None, 4)
        self.my_list.insert_after(None, 3)
        self.my_list.insert_after(None, 7)
        self.assertEqual(repr(self.my_list), '[7, 3, 4]')
        self.my_list.insert_sorted(2)
        self.my_list.insert_sorted(8)
        self.assertEqual(repr(self.my_list), '[2, 7, 3, 4, 8]')
        self.my_list.remove_after(None)
        self.my_list.remove_after(None)
        self.my_list.remove_after(None)
        self.my_list.remove_after(None)
        self.my_list.remove_after(None)
        self.my_list.insert_sorted(8)
        self.my_list.insert_sorted(7)
        self.my_list.insert_sorted(6)
        self.my_list.insert_sorted(5)
        self.assertEqual(repr(self.my_list), '[5, 6, 7, 8]')
        self.my_list.reverse()
        self.assertEqual(repr(self.my_list), '[8, 7, 6, 5]')

    def test_remove_after(self):
        self.my_list.append(4)
        self.my_list.append(3)
        self.my_list.append(7)
        self.my_list.append(-17)
        self.assertEqual(repr(self.my_list), '[4, 3, 7, -17]')
        self.my_list.remove_after(None)
        self.assertEqual(repr(self.my_list), '[3, 7, -17]')
        self.my_list.remove_after(self.my_list.head)
        self.assertEqual(repr(self.my_list), '[3, -17]')
        self.my_list.remove_after(self.my_list.tail)
        self.assertEqual(repr(self.my_list), '[3, -17]')
        self.my_list.remove_after(None)
        self.my_list.remove_after(None)
        self.my_list.remove_after(None)
        self.assertEqual(repr(self.my_list), '[]')

    def test_array(self):
        self.my_list.append(4)
        self.my_list.append(3)
        self.my_list.append(7)
        self.my_list.append(-17)
        self.assertEqual(self.my_list.array(), [4, 3, 7, -17])

    def test_search(self):
        self.my_list.append(4)
        self.my_list.append(3)
        self.my_list.append(-17)
        self.my_list.append(7)
        self.assertEqual(self.my_list.search(4).data, 4)
        self.assertEqual(self.my_list.search(3).data, 3)
        self.assertEqual(self.my_list.search(-17).data, -17)
        self.assertEqual(self.my_list.search(17), None)

    def test_reverse(self):
        self.my_list.append(4)
        self.my_list.append(3)
        self.my_list.append(7)
        self.my_list.append(-17)
        self.assertEqual(repr(self.my_list), '[4, 3, 7, -17]')
        self.my_list.reverse()
        self.assertEqual(repr(self.my_list), '[-17, 7, 3, 4]')
        self.my_list.reverse()
        self.assertEqual(repr(self.my_list), '[4, 3, 7, -17]')

    def test_remove_duplicates(self):
        self.my_list.append(4)
        self.my_list.append(3)
        self.my_list.append(3)
        self.my_list.append(3)
        self.my_list.append(7)
        self.my_list.append(-17)
        self.assertEqual(repr(self.my_list), '[4, 3, 3, 3, 7, -17]')
        self.my_list.remove_duplicates()
        self.assertEqual(repr(self.my_list), '[4, 3, 7, -17]')