def test_segment_tree_creation(self): arr = [2, 4, 3, 6, 8, 9, 3] max_segment_tree = SegmentTree(arr, max) min_segment_tree = SegmentTree(arr, min) sum_segment_tree = SegmentTree(arr, lambda a, b: a + b) gcd_segment_tree = SegmentTree(arr, gcd) self.assertEqual(max_segment_tree.tree, [None, 9, 8, 9, 4, 8, 9, 2, 4, 3, 6, 8, 9, 3]) self.assertEqual(min_segment_tree.tree, [None, 2, 3, 2, 3, 6, 3, 2, 4, 3, 6, 8, 9, 3]) self.assertEqual(sum_segment_tree.tree, [None, 35, 21, 14, 7, 14, 12, 2, 4, 3, 6, 8, 9, 3]) self.assertEqual(gcd_segment_tree.tree, [None, 1, 1, 1, 1, 2, 3, 2, 4, 3, 6, 8, 9, 3])
def __test_all_segments_with_updates(self, arr, fnc, upd): """ Test all possible segments in the tree with updates :param arr: array to test :param fnc: function of the segment tree :param upd: updates to test """ segment_tree = SegmentTree(arr, fnc) for index, value in upd.items(): arr[index] = value segment_tree.update(index, value) self.__test_segments_helper(segment_tree, fnc, arr)
def __test_all_segments(self, arr, fnc): """ Test all possible segments in the tree :param arr: array to test :param fnc: function of the segment tpree """ segment_tree = SegmentTree(arr, fnc) self.__test_segments_helper(segment_tree, fnc, arr)