Esempio n. 1
0
 def test_heap_pops_always_sorted(self):
     for _ in range(500):
         nums = [
             random.randrange(10) for _ in range(random.randrange(1, 10))
         ]
         heap = Heap(nums)
         assert is_sorted(popper(heap))
Esempio n. 2
0
    def test_heap_post_push_unorderable_not_broken(self):
        heap = Heap([1, 2, 3])
        try:
            heap.push([])
        except ValueError:
            pass
        else:
            pass

        for x in popper(heap):
            pass  # this should run fine and not see that list or Python 2 won't care
Esempio n. 3
0
 def test_heap_pushpops_always_min(self):
     for _ in range(10):
         nums = [
             random.randrange(10) for _ in range(random.randrange(1, 10))
         ]
         heap = Heap(nums)
         for _ in range(500):
             in_ = random.randrange(-5, 15)
             lo = min(min(heap.heap), in_)
             out = heap.pushpop(in_)
             assert out == lo
Esempio n. 4
0
    def test_heap_complain_unorderable(self):
        if sys.version_info < (3, ):
            raise unittest.SkipTest(
                "Python 2 happily compares disparate types.")

        heap = Heap([1, 2, 3])
        try:
            heap.push([])
        except ValueError as e:
            self.assertIn("order", str(e))
        else:
            self.fail("allowed insertion of unorderable type")
Esempio n. 5
0
 def test_heap_replace(self):
     """
     similar to pushpop, but won't return input if that's lower. basically
     a pop-push
     """
     for _ in range(10):
         nums = [
             random.randrange(10) for _ in range(random.randrange(1, 10))
         ]
         heap = Heap(nums)
         for _ in range(500):
             in_ = random.randrange(-5, 15)
             lo = min(heap.heap)
             out = heap.replace(in_)
             assert out == lo