def test_solver_constrained_unsatisfiable(): domain = IntervalDomain(["a", "b", "c"]) state = IntervalAbstractState({ "a": Interval(0, 100), "b": Interval(float("inf"), float("-inf")), "c": Interval(1, 11), }) solution = domain.model(domain.gamma_hat(state)) assert solution is None
def test_single_interval_comparisons(): random_intervals = [] for _ in range(100): # These bounds are chosen arbitrarily. lower = random.randint(-100000, +100000) upper = random.randint(lower, +100000) random_intervals.append(Interval(lower, upper)) random_intervals.append(Interval(lower, float("inf"))) random_intervals.append(Interval(float("-inf"), upper)) # First, we test that Top is greater than everything else and Bottom is # less than everything else. top = Interval(float("-inf"), float("inf")) bottom = Interval(float("inf"), float("-inf")) assert bottom <= top for interval in random_intervals: assert bottom <= interval <= top # Next, we test that nothing else is greater than Top or less than Bottom for interval in random_intervals: assert not interval >= top assert not interval <= bottom # Non-containing intervals should be incomparable. assert not (Interval(5, 100) <= Interval(6, 101)) assert not (Interval(5, 100) >= Interval(6, 101))
def test_abstract_consequence_low_best(): domain = IntervalDomain(["a", "b", "c"]) lower = IntervalAbstractState({ "a": Interval(0, float("inf")), "b": Interval(float("-inf"), float("inf")), "c": Interval(float("-inf"), float("inf")), }) upper = lower.copy() consequence = domain.abstract_consequence(lower, upper) assert consequence == lower
def test_solver_constrained_satisfiable(): domain = IntervalDomain(["a", "b", "c"]) state = IntervalAbstractState({ "a": Interval(0, 100), "b": Interval(-50, -50), "c": Interval(1, 11), }) solution = domain.model(domain.gamma_hat(state)) assert solution is not None assert 0 <= solution.value_of("a") <= 100 assert solution.value_of("b") == -50 assert 1 <= solution.value_of("c") <= 11
def test_solver_one_unconstrained_satisfiable(): domain = IntervalDomain(["a", "b", "c"]) state = IntervalAbstractState({ "a": Interval(0, 100), "b": Interval(-50, -50), "c": Interval(float("-inf"), float("inf")) }) solution = domain.model(domain.gamma_hat(state)) assert solution is not None assert 0 <= solution.value_of("a") <= 100 assert solution.value_of("b") == -50 assert isinstance(solution.value_of("c"), int)
def test_interval_state_creation_change_query(): state1 = IntervalAbstractState({ "a": Interval(-100, 50), "b": Interval(float("-inf"), 5), "c": Interval(100, 200), "d": Interval(6, float("inf")), }) state1.set_interval("a", Interval(-99, 50)) assert state1.interval_of("a") == Interval(-99, 50) assert state1.interval_of("b") == Interval(float("-inf"), 5) assert state1.interval_of("c") == Interval(100, 200) assert state1.interval_of("d") == Interval(6, float("inf"))
def test_interval_state_equality(): state1 = IntervalAbstractState({ "a": Interval(-100, 50), "b": Interval(float("-inf"), 5), "c": Interval(100, 200), "d": Interval(6, float("inf")), }) state2 = IntervalAbstractState({ "a": Interval(-100, 50), "b": Interval(float("-inf"), 5), "c": Interval(100, 200), "d": Interval(6, float("inf")), }) assert state1 == state2 state2.set_interval("a", Interval(-99, 50)) assert state1 != state2
def test_reduce_sign_interval(): domain_A = SignDomain(["x", "y", "z"]) input_state_A = SignAbstractState({ "x": Sign.Negative, "y": Sign.Positive, "z": Sign.Top, }) domain_B = IntervalDomain(["x", "y", "z"]) input_state_B = IntervalAbstractState({ "x": Interval(-2, 3), "y": Interval(-5, 5), "z": Interval(1, 15), }) domain = ReducedProductDomain(["x", "y", "z"], domain_A, domain_B) input_state = ReducedProductAbstractState(input_state_A, input_state_B) reduced = domain.reduce(input_state) assert reduced.state_A.sign_of("x") == Sign.Negative assert reduced.state_A.sign_of("y") == Sign.Positive assert reduced.state_A.sign_of("z") == Sign.Positive assert reduced.state_B.interval_of("x") == Interval(-2, -1) assert reduced.state_B.interval_of("y") == Interval(1, 5) assert reduced.state_B.interval_of("z") == Interval(1, 15)
def test_bilateral_alpha_hat_add_subtract(): """Attempts to analyze computation of the form: x' := x - 5 x'' := x' + 5 """ domain = IntervalDomain(["x", "x'", "x''"]) x = domain.z3_variable("x") xp = domain.z3_variable("x'") xpp = domain.z3_variable("x''") # Bounds needed to avoid infinite ascending chains (in practice we should # use, eg., widening). phi = z3.And(xp == x - 5, xpp == xp + 5, x <= 5, x >= -5) # Just from the statements themselves, we can't say anything about the sign # of x/x'/x'' alpha_hat = bilateral(domain, phi) assert alpha_hat == RSY(domain, phi) assert alpha_hat.interval_of("x") == Interval(-5, 5) assert alpha_hat.interval_of("x'") == Interval(-10, 0) assert alpha_hat.interval_of("x''") == Interval(-5, 5)
def test_join_meet_three_states(): domain = IntervalDomain(["a", "b", "c"]) state1 = IntervalAbstractState({ "a": Interval(-5, 5), "b": Interval(4, 6), "c": Interval(float("inf"), float("-inf")), }) state2 = IntervalAbstractState({ "a": Interval(-4, 6), "b": Interval(float("-inf"), -2), "c": Interval(5, 5), }) state3 = IntervalAbstractState({ "a": Interval(-5, 5), "b": Interval(-4, float("inf")), "c": Interval(5, 5), }) joined = domain.join([state1, state2, state3]) assert joined.interval_of("a") == Interval(-5, 6) assert joined.interval_of("b") == Interval(float("-inf"), float("inf")) assert joined.interval_of("c") == Interval(5, 5) met = domain.meet([state1, state2, state3]) assert met.interval_of("a") == Interval(-4, 5) assert met.interval_of("b") == Interval(float("inf"), float("-inf")) assert met.interval_of("c") == Interval(float("inf"), float("-inf"))
def test_interval_state_ineq(): state1 = IntervalAbstractState({ "a": Interval(-100, 50), "b": Interval(float("-inf"), 5), "c": Interval(100, 200), "d": Interval(float("inf"), float("-inf")), }) state2 = IntervalAbstractState({ "a": Interval(-100, 50), "b": Interval(float("-inf"), float("inf")), "c": Interval(100, 201), "d": Interval(6, float("inf")), }) state3 = IntervalAbstractState({ "a": Interval(-100, 50), "b": Interval(float("-inf"), 4), "c": Interval(100, 201), "d": Interval(7, float("inf")), }) assert state1 <= state2 assert not (state2 <= state1) assert not (state1 <= state3) assert not (state3 <= state1) assert not (state2 <= state3) assert state3 <= state2 assert state2 >= state1 assert not (state1 >= state2) assert not (state3 >= state1) assert not (state1 >= state3) assert not (state3 >= state2) assert state2 >= state3