source: geniuswebcore/test/PriorityQueueTest.py@ 88

Last change on this file since 88 was 88, checked in by Bart Vastenhouw, 3 years ago

Added python SimpleRunner GUI

File size: 2.0 KB
Line 
1from queue import Empty
2import unittest
3
4from geniusweb.PriorityQueue import PriorityQueue
5
6
7class A:
8 def __init__(self, x):
9 self.x=x
10 def get(self):
11 return self.x
12 def __repr__(self):
13 return str(self.x)
14 def __hash__(self):
15 return hash(self.x)
16 def __eq__(self,other):
17 return self.x==other.x
18
19
20class PriorityQueueTest(unittest.TestCase):
21
22 def setUp(self):
23 self.q=PriorityQueue[int]( lambda x,y: x.get() < y.get())
24
25 def testSmoke(self):
26 PriorityQueue(lambda:False)
27
28 def testPush1(self):
29 self.assertEqual(0, self.q.qsize())
30 self.assertTrue(self.q.empty())
31 self.q.put(A(1))
32 self.assertEqual(1, self.q.qsize())
33 self.assertFalse(self.q.empty())
34
35 self.assertEqual(A(1), self.q.get( block=False))
36 self.assertEqual(0, self.q.qsize())
37 self.assertTrue(self.q.empty())
38
39 self.assertRaises(Empty, lambda:self.q.get(block=False))
40
41 def testPushDuplicates(self):
42 self.q.put(A(1))
43 self.q.put(A(1))
44 self.assertEqual(2, self.q.qsize())
45
46 self.assertEqual(A(1), self.q.get(block=False))
47 self.assertEqual(A(1), self.q.get(block=False))
48 self.assertTrue( self.q.empty())
49
50 def testProperSorted(self):
51 self.q.put(A(2))
52 self.q.put(A(3))
53 self.q.put(A(1))
54 self.assertEqual(A(1), self.q.get(block=False))
55 self.assertEqual(A(2), self.q.get(block=False))
56 self.assertEqual(A(3), self.q.get(block=False))
57
58 def testReverseSort(self):
59 q1=PriorityQueue( lambda x,y: x.get() > y.get())
60 q1.put(A(2))
61 q1.put(A(3))
62 q1.put(A(1))
63 self.assertEqual(A(3), q1.get(block=False))
64 self.assertEqual(A(2), q1.get(block=False))
65 self.assertEqual(A(1), q1.get(block=False))
66
67 def testContains(self):
68 self.q.put(A(2))
69 self.q.put(A(3))
70 self.q.put(A(1))
71 self.assertTrue(A(1) in self.q)
Note: See TracBrowser for help on using the repository browser.