Пример #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 = _NoThreadingFIFOQueue()
            for i in items:
                q.put(i)
            correct = []
            removed = []
            for _, item in q._deque:
                if item <= cutoff:
                    correct.append(item)
                else:
                    removed.append(item)
            removed_ = q.filter(_filter)
            assert removed_ == removed
            check = []
            for _, item in q._deque:
                check.append(item)
            assert sorted(correct) == sorted(check)
        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 = _NoThreadingFIFOQueue()
            for i in items:
                q.put(i)
            correct = []
            removed = []
            for cnt, item in q._deque:
                if item <= cutoff:
                    correct.append(item)
                else:
                    removed.append((cnt, item))
            removed_ = q.filter(_filter, include_counters=True)
            assert removed_ == removed
            check = []
            for _, item in q._deque:
                check.append(item)
            assert sorted(correct) == sorted(check)
Пример #2
0
 def test_put_get(self):
     q = _NoThreadingFIFOQueue()
     with pytest.raises(AssertionError):
         q.put(None)
     with pytest.raises(AssertionError):
         q.put_get(None)
     assert list(q.items()) == []
     assert q.put_get(1) == (0, 1)
     assert q.put_get(1) == (1, 1)
     cntr = {}
     cntr[1] = q.put(1)
     assert list(q.items()) == [1]
     cntr[2] = q.put(2)
     assert list(q.items()) == [1, 2]
     cntr[3] = q.put(3)
     assert list(q.items()) == [1, 2, 3]
     cntr[4], item_ = q.put_get(4)
     assert item_ == 1
     assert list(q.items()) == [2, 3, 4]
     assert q.next() == (cntr[q.next()[1]], 2)
     assert q.get() == 2
     assert list(q.items()) == [3, 4]
     assert q.next() == (cntr[q.next()[1]], 3)
     assert q.get() == 3
     assert list(q.items()) == [4]
     assert q.next() == (cntr[q.next()[1]], 4)
     assert q.get() == 4
     assert list(q.items()) == []
     with pytest.raises(IndexError):
         q.next()
Пример #3
0
 def test_size(self):
     q = _NoThreadingFIFOQueue()
     assert q.size() == 0
     with pytest.raises(IndexError):
         q.next()
     cntr = {}
     for i in range(10):
         cntr[i] = q.put(i)
         assert q.size() == i + 1
     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 q.size() == 0
     assert q.get() is None
     with pytest.raises(IndexError):
         q.next()