Exemple #1
0
 def test_usage_maximize(self):
     q = CustomPriorityQueue(maximize)
     assert q.size() == 0
     assert q.bound() is None
     assert len(list(q.items())) == 0
     assert q.get() is None
     items = []
     for i in range(1, 11):
         node = Node(size=0)
         node.bound = -i
         node.queue_priority = i
         assert q.put(node._data) == i - 1
         assert node.queue_priority == i
         items.append(node._data)
         assert q.size() == i
         assert q.bound() == -1
         _check_items(q, items)
     assert q.size() == 10
     assert q.bound() == -1
     removed = q.filter(lambda data: \
                        Node._extract_bound(data) <= -5)
     assert q.size() == 6
     assert len(removed) == 4
     for data in removed:
         assert Node._extract_bound(data) > -5
     assert q.bound() == -5
     for i in range(10, 4, -1):
         node = Node(data_=q.get())
         assert node.bound == -i
         assert node.queue_priority == i
         if i != 5:
             assert q.bound() == -5
         else:
             assert q.bound() is None
     assert q.size() == 0
     node = Node(size=0)
     node.bound = 0
     node.queue_priority = 1
     cnt_, data = q.put_get(node._data)
     assert cnt_ == 10
     assert data is node._data
     assert node.queue_priority == 1
     assert q.bound() is None
     node.queue_priority = 2
     cnt_ = q.put(node._data)
     assert node.queue_priority == 2
     assert cnt_ == 11
     assert q.bound() == 0
     node2 = Node(size=0)
     node2.bound = 1
     node2.queue_priority = 3
     cnt_, data = q.put_get(node2._data)
     assert cnt_ == 12
     assert data is node2._data
     assert node2.queue_priority == 3
     assert q.bound() == 0
     node2.queue_priority = 1
     cnt_, data = q.put_get(node2._data)
     assert node2.queue_priority == 1
     assert cnt_ == 13
     assert data is node._data
     assert q.size() == 1
     assert q.bound() == 1
 def test_usage_maximize(self):
     q = CustomPriorityQueue(sense=maximize, track_bound=True)
     assert q.size() == 0
     assert q.bound() is None
     assert len(list(q.items())) == 0
     assert q.get() is None
     items = []
     for i in range(1, 11):
         node = Node()
         node.bound = -i
         node.queue_priority = i
         assert q.put(node) == i - 1
         assert node.queue_priority == i
         items.append(node)
         assert q.size() == i
         assert q.bound() == -1
         _check_items(q, items)
     assert q.size() == 10
     assert q.bound() == -1
     removed = q.filter(lambda n_: n_.bound <= -5)
     assert q.size() == 6
     assert len(removed) == 4
     for node_ in removed:
         assert node_.bound > -5
     assert q.bound() == -5
     for i in range(10, 4, -1):
         node = q.get()
         assert node.bound == -i
         assert node.queue_priority == i
         if i != 5:
             assert q.bound() == -5
         else:
             assert q.bound() is None
     assert q.size() == 0
     node = Node()
     node.bound = 0
     node.queue_priority = 1
     cnt_ = q.put(node)
     node_ = q.get()
     assert cnt_ == 10
     assert node_ is node
     assert node.queue_priority == 1
     assert q.bound() is None
     node.queue_priority = 2
     cnt_ = q.put(node)
     assert node.queue_priority == 2
     assert cnt_ == 11
     assert q.bound() == 0
     node2 = Node()
     node2.bound = 1
     node2.queue_priority = 3
     cnt_ = q.put(node2)
     node_ = q.get()
     assert cnt_ == 12
     assert node_ is node2
     assert node2.queue_priority == 3
     assert q.bound() == 0
     node2.queue_priority = 1
     cnt_ = q.put(node2)
     node_ = q.get()
     assert node2.queue_priority == 1
     assert cnt_ == 13
     assert node_ is node
     assert q.size() == 1
     assert q.bound() == 1
     # no bound tracking
     q = CustomPriorityQueue(sense=maximize, track_bound=False)
     assert q.size() == 0
     assert q.bound() is None
     assert len(list(q.items())) == 0
     assert q.get() is None
     items = []
     for i in range(1, 11):
         node = Node()
         node.bound = -i
         node.queue_priority = i
         assert q.put(node) == i - 1
         assert node.queue_priority == i
         items.append(node)
         assert q.size() == i
         assert q.bound() == inf
         _check_items(q, items)
     assert q.size() == 10
     assert q.bound() == inf
     removed = q.filter(lambda n_: n_.bound <= -5)
     assert q.size() == 6
     assert len(removed) == 4
     for node_ in removed:
         assert node_.bound > -5
     assert q.bound() == inf
     for i in range(10, 4, -1):
         node = q.get()
         assert node.bound == -i
         assert node.queue_priority == i
         if i != 5:
             assert q.bound() == inf
         else:
             assert q.bound() is None
     assert q.size() == 0