Exemplo n.º 1
0
    def test_filter(self):
        for k in range(-10, 11):
            cutoff = k * 11

            def _filter(item):
                if item <= cutoff:
                    return True

            items = list(range(-1000, 1000))
            random.shuffle(items)
            q = _NoThreadingMaxPriorityFirstQueue()
            for i in items:
                q.put(i, 1)
            assert_isheap(q._heap)
            correct = []
            removed = []
            for _, _, item in q._heap:
                if item <= cutoff:
                    correct.append(item)
                else:
                    removed.append(item)
            removed_ = q.filter(_filter)
            assert removed_ == removed
            assert_isheap(q._heap)
            check = []
            for _, _, item in q._heap:
                check.append(item)
            assert sorted(correct) == sorted(check)
Exemplo n.º 2
0
 def test_put_get(self):
     q = _NoThreadingMaxPriorityFirstQueue()
     with pytest.raises(AssertionError):
         q.put(None, 0)
     with pytest.raises(AssertionError):
         q.put_get(None, 0)
     assert sorted(q.items()) == []
     assert_isheap(q._heap)
     assert q.put_get(1, 1) == (0, 1)
     assert q.put_get(1, 1) == (1, 1)
     cntr = {}
     cntr[1] = q.put(1, 1)
     assert sorted(q.items()) == [1]
     assert_isheap(q._heap)
     cntr[2] = q.put(2, 2)
     assert sorted(q.items()) == [1, 2]
     assert_isheap(q._heap)
     cntr[3] = q.put(3, 2)
     assert sorted(q.items()) == [1, 2, 3]
     assert_isheap(q._heap)
     cntr[4], item_ = q.put_get(4, -4)
     assert item_ == 2
     assert sorted(q.items()) == [1, 3, 4]
     assert_isheap(q._heap)
     assert q.next() == (cntr[q.next()[1]], 3)
     assert q.get() == 3
     assert sorted(q.items()) == [1, 4]
     assert_isheap(q._heap)
     assert q.next() == (cntr[q.next()[1]], 1)
     assert q.get() == 1
     assert sorted(q.items()) == [4]
     assert_isheap(q._heap)
     assert q.next() == (cntr[q.next()[1]], 4)
     assert q.get() == 4
     assert sorted(q.items()) == []
     assert_isheap(q._heap)
     with pytest.raises(IndexError):
         q.next()
Exemplo n.º 3
0
 def test_size(self):
     q = _NoThreadingMaxPriorityFirstQueue()
     assert_isheap(q._heap)
     assert q.size() == 0
     with pytest.raises(IndexError):
         q.next()
     cntr = {}
     for i in range(10):
         cntr[i] = q.put(i, 0)
         assert q.size() == i + 1
         assert_isheap(q._heap)
     for i in range(10):
         c_, x_ = q.next()
         assert c_ == cntr[x_]
         x = q.get()
         assert x_ is x
         assert x == i
         assert q.size() == 10 - (i + 1)
         assert_isheap(q._heap)
     assert q.size() == 0
     assert q.get() is None
     with pytest.raises(IndexError):
         q.next()
     assert_isheap(q._heap)