def test_find_pom_tree(self): n = random.randint(1, 30) tree = RedBlackTree(sentinel=IntervalPomNode(None)) intervals = [] node_pairs = [] for _ in range(n): low_endpoint = random.randint(0, 899) high_endpoint = low_endpoint + random.randint(0, 100) interval = Interval(low_endpoint, high_endpoint) intervals.append(interval) node_pair = interval_pom_insert(tree, interval) node_pairs.append(node_pair) assert_interval_pom_tree(tree) while node_pairs: actual_pom = find_pom(tree) expected_poms = get_expected_poms(intervals) assert_that(actual_pom, is_in(expected_poms)) node_pair = random.choice(node_pairs) node_pairs.remove(node_pair) interval_to_delete = Interval(node_pair[0].key, node_pair[1].key) intervals.remove(interval_to_delete) interval_pom_delete(tree, *node_pair) assert_interval_pom_tree(tree)
def points_cover(points): points.elements.sort() p = -math.inf I = set() for i in between(1, points.length): if points[i] - p > 1.0: p = points[i] I.add(Interval(p, p + 1)) return I
def test_fuzzy_sort(self): n = random.randint(1, 20) endpoints_list = [sorted([random.randint(0, 20), random.randint(0, 20)]) for _ in range(n)] elements = [Interval(*endpoints) for endpoints in endpoints_list] array = Array(elements) fuzzy_sort(array, 1, array.length) for i in range(2, n + 1): if array[i].low < array[i - 1].low: assert_that(array[i].high, is_(greater_than_or_equal_to(array[i - 1].low)))
def fill_subtree_with_intervals(node, keys, max_value, sentinel): if node is sentinel: return left_subtree_size = get_subtree_size(node.left, sentinel) node.key = keys[left_subtree_size] high_endpoint = random.randint(node.key, node.key + int(.1 * max_value)) node.int = Interval(node.key, high_endpoint) fill_subtree_with_intervals(node.left, keys[:left_subtree_size], max_value, sentinel) fill_subtree_with_intervals(node.right, keys[left_subtree_size + 1:], max_value, sentinel)
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))
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_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 test_interval_search_exactly_random(self): _, keys = get_random_array(max_size=100, max_value=89) tree = RedBlackTree(sentinel=IntervalNode(None, None)) intervals = [] for key in keys: i = Interval(key, key + random.randint(0, 10)) intervals.append(i) interval_insert_exactly(tree, IntervalNode(key, i)) assert_interval_tree(tree) low_endpoint = random.randint(0, 89) high_endpoint = low_endpoint + random.randint(0, 10) interval = Interval(low_endpoint, high_endpoint) actual_found = interval_search_exactly(tree, interval) if actual_found is not tree.nil: assert_that(actual_found.int, is_(equal_to(interval))) else: for i in intervals: assert_that(interval, is_not(equal_to(i)))
def test_interval_insert(self): keys = [random.randrange(949) for _ in range(20)] tree = RedBlackTree(sentinel=IntervalNode(None, None)) for key in keys: interval_insert( tree, IntervalNode(key, Interval(key, key + random.randint(0, 50)))) assert_interval_tree(tree) assert_parent_pointers_consistent(tree, sentinel=tree.nil) actual_keys = get_binary_tree_keys(tree, sentinel=tree.nil) assert_that(actual_keys, contains_inanyorder(*keys))
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_exactly_positive(self): _, keys = get_random_array(max_size=100, max_value=89) tree = RedBlackTree(sentinel=IntervalNode(None, None)) intervals = [] for key in keys: i = Interval(key, key + random.randint(0, 10)) intervals.append(i) interval_insert_exactly(tree, IntervalNode(key, i)) assert_interval_tree(tree) interval = random.choice(intervals) actual_found = interval_search_exactly(tree, interval) assert_that(actual_found.int, is_(equal_to(interval)))