class SinglyLinkedListTest(unittest.TestCase): def setUp(self): self.list = SinglyLinkedList() def test_node(self): a = Node(2) b = Node(3) a.next = b self.assertEqual(2, a.data) self.assertEqual(3, a.next.data) self.assertEqual(3, b.data) self.assertIsNone(b.next) def test_addFirst(self): self.list.addFirst(1) self.list.addFirst(2) self.list.addFirst(3) self.list.addFirst(4) self.assertEqual('4,3,2,1', self.list.toString()) def test_addLast(self): self.list.addLast(1) self.list.addLast(2) self.list.addLast(3) self.list.addLast(4) self.assertEqual('1,2,3,4', self.list.toString()) def test_addFront(self): self.list.addFront(1, 0) self.list.addFront(2, 1) self.list.addFront(3, 1) self.list.addFront(4, 2) self.assertEqual('1,3,4,2', self.list.toString()) def test_addBack(self): self.list.addBack(1, 0) self.list.addBack(2, 1) self.list.addBack(3, 1) self.list.addBack(4, 1) self.assertEqual('1,2,4,3', self.list.toString()) def _addNodes(self): self.list.addLast(1) self.list.addLast(2) self.list.addLast(3) self.list.addLast(4) self.list.addLast(5) def test_removeFirst(self): self._addNodes() self.assertEqual(1, self.list.removeFirst()) self.assertEqual(2, self.list.removeFirst()) self.assertEqual(3, self.list.removeFirst()) self.assertEqual(4, self.list.removeFirst()) self.assertEqual(5, self.list.removeFirst()) self.assertIsNone(self.list.removeFirst()) def test_remove(self): self._addNodes() self.assertIsNone(self.list.remove(5)) self.assertEqual(5, self.list.remove(4)) self.assertEqual(1, self.list.remove(0)) self.assertEqual(3, self.list.remove(1)) self.assertEqual(4, self.list.remove(1)) self.assertEqual(2, self.list.remove(0)) def test_removeLast(self): self._addNodes() self.assertEqual(5, self.list.removeLast()) self.assertEqual(4, self.list.removeLast()) self.assertEqual(3, self.list.removeLast()) self.assertEqual(2, self.list.removeLast()) self.assertEqual(1, self.list.removeLast()) self.assertIsNone(self.list.removeLast()) def test_size(self): self.assertEqual(0, self.list.size()) self._addNodes() self.assertEqual(5, self.list.size()) def test_empty(self): self.assertEqual(True, self.list.empty()) self._addNodes() self.assertEqual(False, self.list.empty()) def test_get(self): self._addNodes() self.assertIsNone(self.list.get(-1)) self.assertEqual(1, self.list.get(0)) self.assertEqual(2, self.list.get(1)) self.assertEqual(3, self.list.get(2)) self.assertEqual(4, self.list.get(3)) self.assertEqual(5, self.list.get(4)) self.assertIsNone(self.list.get(5)) def test_indexOf(self): self._addNodes() self.assertEqual(0, self.list.indexOf(1)) self.assertEqual(1, self.list.indexOf(2)) self.assertEqual(2, self.list.indexOf(3)) self.assertEqual(3, self.list.indexOf(4)) self.assertEqual(4, self.list.indexOf(5)) self.assertEqual(-1, self.list.indexOf(6))
class SinglyLinkedListTest(unittest.TestCase): def setUp(self): self.linkedList = SinglyLinkedList() def test_addFirst(self): self.linkedList.addFirst('first') self.linkedList.addFirst('second') self.assertEqual(self.linkedList.toString(), 'second,first') def test_addLast(self): self.linkedList.addLast('first') self.linkedList.addLast('second') self.assertEqual(self.linkedList.toString(), 'first,second') def test_addFront(self): self.linkedList.addFront(2, 'end') self.linkedList.addFront(0, 'first') self.linkedList.addFront(2, 'end2') self.linkedList.addFront(1, 'second') self.linkedList.addFront(2, 'third') self.linkedList.addFront(1, 'second1') self.assertEqual(self.linkedList.toString(), 'first,second1,second,third,end,end2') def test_addBack(self): self.linkedList.addBack(2, 'end') self.linkedList.addBack(0, 'first') self.linkedList.addBack(2, 'end2') self.linkedList.addBack(1, 'second') self.linkedList.addBack(2, 'third') self.linkedList.addBack(1, 'second1') self.assertEqual(self.linkedList.toString(), 'end,first,second1,second,third,end2') def test_removeFirst(self): self.linkedList.addFirst('first') self.linkedList.addFirst('second') self.assertEqual(self.linkedList.removeFirst(), 'second') self.assertEqual(self.linkedList.toString(), 'first') self.assertEqual(self.linkedList.removeFirst(), 'first') self.assertIsNone(self.linkedList.removeFirst()) def test_remove(self): self.linkedList.addFirst('third') self.linkedList.addFirst('second') self.linkedList.addFirst('first') self.assertIsNone(self.linkedList.remove(3)) self.assertEqual(self.linkedList.remove(0), 'first') self.assertEqual(self.linkedList.remove(1), 'third') self.assertEqual(self.linkedList.toString(), 'second') def test_removeLast(self): self.assertIsNone(self.linkedList.removeLast()) self.linkedList.addFirst('third') self.linkedList.addFirst('second') self.linkedList.addFirst('first') self.assertEqual(self.linkedList.removeLast(), 'third') self.assertEqual(self.linkedList.removeLast(), 'second') self.assertEqual(self.linkedList.removeLast(), 'first') self.assertIsNone(self.linkedList.removeLast()) def test_size(self): self.assertEqual(self.linkedList.size(), 0) self.linkedList.addFirst('third') self.linkedList.addFirst('second') self.linkedList.addFirst('first') self.assertEqual(self.linkedList.size(), 3) def test_empty(self): self.assertTrue(self.linkedList.empty()) self.linkedList.addFirst('third') self.linkedList.addFirst('second') self.linkedList.addFirst('first') self.assertFalse(self.linkedList.empty()) def test_get(self): self.assertIsNone(self.linkedList.get(1)) self.linkedList.addFirst('third') self.linkedList.addFirst('second') self.linkedList.addFirst('first') self.assertEqual(self.linkedList.get(1), 'second') self.assertIsNone(self.linkedList.get(3)) def test_indexOf(self): self.assertEqual(self.linkedList.indexOf('test'), -1) self.linkedList.addFirst('third') self.linkedList.addFirst('second') self.linkedList.addFirst('first') self.assertEqual(self.linkedList.indexOf('first'), 0) self.assertEqual(self.linkedList.indexOf('second'), 1) self.assertEqual(self.linkedList.indexOf('third'), 2) self.assertEqual(self.linkedList.indexOf('thirdt'), -1)