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

Last change on this file since 1378 was 1248, checked in by wouter, 2 months ago

#386 fixed bug in python PriorityQueue

File size: 1.5 KB
RevLine 
[1230]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
[1248]7class MyComparator(Comparator[Tuple[str, int]]):
8 def compare(self, o1:Tuple[str, int], o2:Tuple[str, int])-> int:
9 return -1 if o1[1] < o2[1] else 1 if o1[1]>o2[1] else 0
[1230]10
11class queueTest(TestCase):
12 def testSimple(self):
13 q=PriorityQueue()
[1231]14 q.append(2)
15 q.append(3)
16 q.append(1)
[1230]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))
[1231]31 q.append(('b',2))
[1230]32 self.assertEqual("[('a', 1), ('b', 2), ('a', 3)]", str(q))
[1248]33
34
35 def testPop(self):
36 q=PriorityQueue([('a',1),('a',3)], MyComparator())
37 val:Tuple[str, int] = q.pop(0)
38 self.assertEqual(('a',1), val)
39 val:Tuple[str, int] = q.pop(0)
40 self.assertEqual(('a',3), val)
41
42 # initial order shouldn't matter
43 q=PriorityQueue([('a',3),('a',1)], MyComparator())
44 val:Tuple[str, int] = q.pop(0)
45 self.assertEqual(('a',1), val)
46
47 q=PriorityQueue([('a',3),('a',1)], MyComparator())
48 val:Tuple[str, int] = q.pop(1)
49 self.assertEqual(('a',3), val)
50 self.assertEqual(1, len(q))
51
Note: See TracBrowser for help on using the repository browser.