from unittest.case import TestCase from tudelft.utilities.tools.queue import PriorityQueue from tudelft.utilities.tools.comparator import Comparator from typing import Tuple class MyComparator(Comparator[Tuple[str, int]]): def compare(self, o1:Tuple[str, int], o2:Tuple[str, int])-> int: return -1 if o1[1] < o2[1] else 1 if o1[1]>o2[1] else 0 class queueTest(TestCase): def testSimple(self): q=PriorityQueue() q.append(2) q.append(3) q.append(1) print(str(q)) self.assertEqual(1,q[0]) self.assertEqual(2,q[1]) self.assertEqual(3,q[2]) self.assertEqual("[1, 2, 3]", str(q)) def testCustomSort(self): q=PriorityQueue([('a',1),('a',3)], MyComparator()) self.assertEqual("[('a', 1), ('a', 3)]", str(q)) q.append(('b',2)) self.assertEqual("[('a', 1), ('b', 2), ('a', 3)]", str(q)) def testPop(self): q=PriorityQueue([('a',1),('a',3)], MyComparator()) val:Tuple[str, int] = q.pop(0) self.assertEqual(('a',1), val) val:Tuple[str, int] = q.pop(0) self.assertEqual(('a',3), val) # initial order shouldn't matter q=PriorityQueue([('a',3),('a',1)], MyComparator()) val:Tuple[str, int] = q.pop(0) self.assertEqual(('a',1), val) q=PriorityQueue([('a',3),('a',1)], MyComparator()) val:Tuple[str, int] = q.pop(1) self.assertEqual(('a',3), val) self.assertEqual(1, len(q))