source: utilitiespy/test/tools/queueTest.py@ 1230

Last change on this file since 1230 was 1230, checked in by wouter, 6 weeks ago

#388 added PriorityQueue and Comparator to utilities package, simulating java classes with same name

File size: 957 bytes
Line 
1from unittest.case import TestCase
2from tudelft.utilities.tools.queue import PriorityQueue
3from tudelft.utilities.tools.comparator import Comparator
4from typing import Tuple
5
6
7class MyComparator(Comparator[Tuple[int, str]]):
8 def compare(self, o1:Tuple[int, str], o2:Tuple[int, str])-> int:
9 return -1 if o1[1] < o2[1] else 1 if o1[1]>o2[1] else 0
10
11class queueTest(TestCase):
12 def testSimple(self):
13 q=PriorityQueue()
14 q.add(2)
15 q.add(3)
16 q.add(1)
17
18 print(str(q))
19
20 self.assertEqual(1,q[0])
21 self.assertEqual(2,q[1])
22 self.assertEqual(3,q[2])
23 self.assertEqual("[1, 2, 3]", str(q))
24
25
26
27
28 def testCustomSort(self):
29 q=PriorityQueue([('a',1),('a',3)], MyComparator())
30 self.assertEqual("[('a', 1), ('a', 3)]", str(q))
31 q.add(('b',2))
32 self.assertEqual("[('a', 1), ('b', 2), ('a', 3)]", str(q))
33
Note: See TracBrowser for help on using the repository browser.