|
| 1 | +import unittest |
| 2 | + |
| 3 | +from pygorithm.data_structures import ( |
| 4 | + stack, |
| 5 | + queue, |
| 6 | + linked_list) |
| 7 | + |
| 8 | +class TestStack(unittest.TestCase): |
| 9 | + def test_stack(self): |
| 10 | + myStack = stack.Stack() # create a stack with default stack size 10 |
| 11 | + myStack.push(2) |
| 12 | + myStack.push(10) |
| 13 | + myStack.push(12) |
| 14 | + myStack.push(3) |
| 15 | + |
| 16 | + self.assertEqual(myStack.pop(), 3) |
| 17 | + self.assertEqual(myStack.peek(), 12) |
| 18 | + self.assertFalse(myStack.isEmpty()) |
| 19 | + |
| 20 | +class TestInfixToPostfix(unittest.TestCase): |
| 21 | + def test_infix_to_postfix(self): |
| 22 | + myExp = 'a+b*(c^d-e)^(f+g*h)-i' |
| 23 | + myExp = [i for i in myExp] |
| 24 | + myStack = stack.Stack(len(myExp)) # create a stack |
| 25 | + |
| 26 | + result = stack.InfixToPostfix(myExp, myStack) |
| 27 | + resultString = result.infix_to_postfix() |
| 28 | + expectedResult = 'a b c d ^ e - f g h * + ^ * + i -' |
| 29 | + self.assertTrue(resultString, expectedResult) |
| 30 | + |
| 31 | +class TestQueue(unittest.TestCase): |
| 32 | + def test_queue(self): |
| 33 | + myQueue = queue.Queue() # create a queue with default queue size 10 |
| 34 | + myQueue.enqueue(2) |
| 35 | + myQueue.enqueue(10) |
| 36 | + myQueue.enqueue(12) |
| 37 | + myQueue.enqueue(3) |
| 38 | + |
| 39 | + self.assertEqual(myQueue.dequeue(), 2) |
| 40 | + self.assertEqual(myQueue.dequeue(), 10) |
| 41 | + self.assertFalse(myQueue.isEmpty()) |
| 42 | + self.assertEqual(myQueue.dequeue(), 12) |
| 43 | + self.assertEqual(myQueue.dequeue(), 3) |
| 44 | + self.assertTrue(myQueue.isEmpty()) |
| 45 | + |
| 46 | +class TestLinkedList(unittest.TestCase): |
| 47 | + def test_singly_linked_list(self): |
| 48 | + List = linked_list.SinglyLinkedList() |
| 49 | + List.insert_at_start(3) |
| 50 | + List.insert_at_start(5) |
| 51 | + List.insert_at_start(2) |
| 52 | + List.insert_at_start(1) |
| 53 | + List.insert_at_start(4) |
| 54 | + List.insert_at_end(6) |
| 55 | + |
| 56 | + expectedResult = [4, 1, 2, 5, 3, 6] |
| 57 | + self.assertEqual(List.get_data(), expectedResult) |
| 58 | + |
| 59 | + def test_doubly_linked_list(self): |
| 60 | + dll = linked_list.DoublyLinkedList() |
| 61 | + dll.insert_at_start(1) |
| 62 | + dll.insert_at_start(2) |
| 63 | + dll.insert_at_end(3) |
| 64 | + dll.insert_at_start(4) |
| 65 | + |
| 66 | + expectedResult = [4, 2, 1, 3] |
| 67 | + self.assertEqual(dll.get_data(), expectedResult) |
| 68 | + |
| 69 | + dll.delete(2) |
| 70 | + |
| 71 | + expectedResult = [4, 1, 3] |
| 72 | + self.assertEqual(dll.get_data(), expectedResult) |
| 73 | + |
| 74 | +if __name__ == '__main__': |
| 75 | + unittest.main() |
0 commit comments