1 | from unittest.case import TestCase
|
---|
2 | from tudelft.utilities.tools.queue import PriorityQueue
|
---|
3 | from tudelft.utilities.tools.comparator import Comparator
|
---|
4 | from typing import Tuple
|
---|
5 |
|
---|
6 |
|
---|
7 | class 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
|
---|
10 |
|
---|
11 | class queueTest(TestCase):
|
---|
12 | def testSimple(self):
|
---|
13 | q=PriorityQueue()
|
---|
14 | q.append(2)
|
---|
15 | q.append(3)
|
---|
16 | q.append(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.append(('b',2))
|
---|
32 | self.assertEqual("[('a', 1), ('b', 2), ('a', 3)]", str(q))
|
---|
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 |
|
---|