def test_interval_search(self): tree, nodes, keys = get_random_interval_tree() low_endpoint = random.randint(0, 949) high_endpoint = low_endpoint + random.randint(0, 50) interval = Interval(low_endpoint, high_endpoint) actual_found = interval_search(tree, interval) if actual_found is not tree.nil: assert_that(overlap(actual_found.int, interval)) else: for node in nodes: assert_that(not_(overlap(node.int, interval)))
def interval_search_all(T, x, i): if x.left is not T.nil and x.left.max >= i.low: interval_search_all(T, x.left, i) if x is not T.nil and overlap(i, x.int): print(x.int) if x.right is not T.nil and x.right.max >= i.low and x.int.low <= i.high: interval_search_all(T, x.right, i)
def test_rectangles_overlap(self): n = random.randint(1, 30) rectangles = [] for _ in range(n): low_endpoint_x = random.randint(0, 899) low_endpoint_y = random.randint(0, 899) high_endpoint_x = low_endpoint_x + random.randint(1, 100) high_endpoint_y = low_endpoint_y + random.randint(1, 100) rectangles.append((Interval(low_endpoint_x, high_endpoint_x), Interval(low_endpoint_y, high_endpoint_y))) rectangles_array = Array(rectangles) actual_overlap = rectangles_overlap(rectangles_array) expected_overlap = False for i in range(n): for j in range(i + 1, n): if overlap(rectangles[i][0], rectangles[j][0]) and overlap(rectangles[i][1], rectangles[j][1]): expected_overlap = True assert_that(actual_overlap, is_(expected_overlap))
def test_rectangles_overlap(self): n = random.randint(1, 30) rectangles = [] for _ in range(n): low_endpoint_x = random.randint(0, 899) low_endpoint_y = random.randint(0, 899) high_endpoint_x = low_endpoint_x + random.randint(1, 100) high_endpoint_y = low_endpoint_y + random.randint(1, 100) rectangles.append((Interval(low_endpoint_x, high_endpoint_x), Interval(low_endpoint_y, high_endpoint_y))) rectangles_array = Array(rectangles) actual_overlap = rectangles_overlap(rectangles_array) expected_overlap = False for i in range(n): for j in range(i + 1, n): if overlap(rectangles[i][0], rectangles[j][0]) and overlap( rectangles[i][1], rectangles[j][1]): expected_overlap = True assert_that(actual_overlap, is_(expected_overlap))
def min_interval_search(T, i): x = interval_search(T, i) if x is not T.nil: y = x.left while y is not T.nil: if overlap(i, y.int): x = y y = x.left else: if y.left is not T.nil and y.left.max >= i.low: y = y.left else: y = y.right return x
def get_expected_poms(intervals): poms = set() max_overlaps = 0 for i in intervals: overlaps = 0 low_endpoint_interval = Interval(i.low, i.low) for j in intervals: if overlap(j, low_endpoint_interval): overlaps += 1 if overlaps > max_overlaps: max_overlaps = overlaps poms = {i.low} elif overlaps == max_overlaps: poms.add(i.low) return poms
def test_interval_search_all(self): tree, nodes, keys = get_random_interval_tree() low_endpoint = random.randint(0, 899) high_endpoint = low_endpoint + random.randint(0, 100) interval = Interval(low_endpoint, high_endpoint) captured_output = io.StringIO() with redirect_stdout(captured_output): interval_search_all(tree, tree.root, interval) actual_output = captured_output.getvalue().splitlines() actual_intervals = [] p = re.compile('\[(\d+), (\d+)\]') for line in actual_output: m = p.match(line) i = Interval(int(m.group(1)), int(m.group(2))) actual_intervals.append(i) for actual_interval in actual_intervals: assert_that(overlap(actual_interval, interval))