Line | |
---|
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[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 |
|
---|
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 | |
---|
Note:
See
TracBrowser
for help on using the repository browser.