class TestHeap(unittest.TestCase): def setUp(self) -> None: self.heap = Heap() self.heap.heap = [1, 3, 2] def test_peek(self) -> None: self.assertEqual(self.heap.peek(), 1) def test_append(self) -> None: self.heap.append(2) self.assertEqual(self.heap.heap, [1, 2, 2, 3]) self.heap.append(1) self.assertEqual(self.heap.heap, [1, 1, 2, 3, 2]) self.heap.append(3) self.assertEqual(self.heap.heap, [1, 1, 2, 3, 2, 3]) def test_pop(self) -> None: self.assertEqual(self.heap.pop(), 1) self.assertEqual(self.heap.heap, [2, 3]) self.assertEqual(self.heap.pop(), 2) self.assertEqual(self.heap.heap, [3]) self.assertEqual(self.heap.pop(), 3) self.assertEqual(self.heap.heap, []) def test_is_empty(self) -> None: self.assertFalse(self.heap.is_empty()) self.heap.heap = [] self.assertTrue(self.heap.is_empty()) def test_clear(self) -> None: self.assertNotEqual(self.heap.heap, []) self.heap.clear() self.assertEqual(self.heap.heap, []) def test_big_test(self) -> None: self.heap.clear() test_case = 10_000 for i in reversed(range(test_case)): self.heap.append(i) for i in range(test_case): self.assertEqual(self.heap.pop(), i) def test_repr(self) -> None: self.heap.append(4) self.heap.append(5) self.heap.append(6) self.assertEqual(self.heap.__repr__(), "Heap [1, 3, 2, 4, 5, 6]") self.heap.append(7) self.assertEqual(self.heap.__repr__(), "Heap [1, 3, 2, 4, 5, 6, ...]")
def test_heap_pop_big_list(): from src.heap import Heap big_list = [ 1, 9, 6, 2, 3, 7, 4, 5, 9, 1, 2, 5, 4, 6, 8, 7, 2, 3, 4, 9, 8, 7, 6, 1, 3, 4, 6, 7, 9, 0, 1 ] test_heap = Heap(big_list) test_value = test_heap.pop() assert test_value == max(big_list)
def test_pop_1(self): h = Heap() h.append(42) t = h.pop() assert t == 42 assert not h
def test_pop_3(self, N=10): h = Heap() for i in range(N): h.append(i) assert len(h) == N for i in reversed(range(N)): assert i == N - 1 - h.pop() assert not h
def test_pop_2(self, N=10): h = Heap() for i in range(N): h.append(i) assert len(h) == N for i in range(N): assert i == h.pop() assert not h
def find_rectangle_candidates( cutout: np.array, points: np.array, candidate_limit: int = 20) -> List[Tuple[np.array]]: """Returns a list of rectangles with maximum space overlap with original figure.""" heap = Heap() for quad in tqdm(combinations(points, 4), total=num_combinations(len(points), 4)): zeros = np.zeros_like(cutout) for p in quad: zeros[p[0], p[1]] = 1 chull = convex_hull_image(zeros) space_part = np.sum(chull * cutout) heap.insert((space_part, quad)) best_space, _ = heap.peek() retlist = [] total = 0 while total < candidate_limit and heap.peek()[0] >= 0.9 * best_space: space, cur_points = heap.pop() retlist.append(cur_points) return retlist
def test_heap_pop_didnt_kill_list(): from src.heap import Heap test_heap = Heap([2, 4, 6]) test_value = test_heap.pop() assert len(test_heap.heapList) == 3
def test_heap_pop_removed(): from src.heap import Heap test_heap = Heap([2, 4, 6]) test_value = test_heap.pop() assert test_value not in test_heap.heapList
def test_heap_pop_root_is_maxval(): from src.heap import Heap test_heap = Heap([2, 4, 6]) test_value = test_heap.pop() assert test_value >= max(test_heap.heapList[1:])
def test_heap_pop_simple(): from src.heap import Heap test_heap = Heap([2, 4, 6]) test_value = test_heap.root assert test_heap.pop() == test_value
def test_heap_pop_big_list(): from src.heap import Heap big_list = [1,9,6,2,3,7,4,5,9,1,2,5,4,6,8,7,2,3,4,9,8,7,6,1,3,4,6,7,9,0,1] test_heap = Heap(big_list) test_value = test_heap.pop() assert test_value == max(big_list)
def test_heap_pop_didnt_kill_list(): from src.heap import Heap test_heap = Heap([2,4,6]) test_value = test_heap.pop() assert len(test_heap.heapList) == 3
def test_heap_pop_removed(): from src.heap import Heap test_heap = Heap([2,4,6]) test_value = test_heap.pop() assert test_value not in test_heap.heapList
def test_heap_pop_root_is_maxval(): from src.heap import Heap test_heap = Heap([2,4,6]) test_value = test_heap.pop() assert test_value >= max(test_heap.heapList[1:])
def test_heap_pop_simple(): from src.heap import Heap test_heap = Heap([2,4,6]) test_value = test_heap.root assert test_heap.pop() == test_value