Пример #1
0
    def test_neg(self):
        self.assertEqual(IntervalLattice().neg(), IntervalLattice())

        self.assertEqual(IntervalLattice(0, 1).neg(), IntervalLattice(-1, 0))

        self.assertEqual(IntervalLattice(upper=2).neg(), IntervalLattice(lower=-2))
        self.assertEqual(IntervalLattice(lower=3).neg(), IntervalLattice(upper=-3))

        self.assertEqual(IntervalLattice(1, 0).neg(), IntervalLattice(1, 0))
Пример #2
0
 def add_variable(self, var: VariableIdentifier):
     if var not in self.store.keys():
         self.variables.add(var)
         self.store[var] = IntervalLattice()  # top
         if isinstance(var.typ, SequenceLyraType):
             length = LengthIdentifier(var)
             self.variables.add(length)
             self.store[length] = IntervalLattice(lower=0)
     else:
         raise ValueError(
             f"Variable can not be added to a store if it is already present"
         )
Пример #3
0
    def test_init(self):
        self.assertFalse(IntervalLattice().is_bottom())
        self.assertTrue(IntervalLattice().is_top())

        self.assertFalse(IntervalLattice(0, 1).is_bottom())
        self.assertFalse(IntervalLattice(0, 1).is_top())

        self.assertEqual(IntervalLattice(upper=2), IntervalLattice(-inf, 2))
        self.assertEqual(IntervalLattice(lower=3), IntervalLattice(3, inf))

        self.assertTrue(IntervalLattice(1, 0).is_bottom())
        self.assertFalse(IntervalLattice(1, 0).is_top())
Пример #4
0
 def add_variable(self, var: VariableIdentifier):
     if var not in self.store.keys():
         self.variables.add(var)
         self.store[var] = IntervalLattice()  # top
     else:
         raise ValueError(
             f"Variable can not be added to a store if it is already present"
         )
Пример #5
0
 def forget_variable(self, var: VariableIdentifier):
     if var in self.store.keys():
         self.store[var].top()
         if isinstance(var.typ, SequenceLyraType):
             length = LengthIdentifier(var)
             self.store[length] = IntervalLattice(lower=0)
     else:
         raise ValueError(
             f"Variable can only be forgotten if it is abstracted in the store"
         )
Пример #6
0
 def __init__(self, type_element=None, range_element=None):
     super().__init__()
     if type_element is None:
         type_element = TypeLattice().top()
     if range_element is None:
         range_element = IntervalLattice().top()
     self._assumptions = {
         AssumptionLattice.Assumption.type_assmp: type_element,
         AssumptionLattice.Assumption.range_assmp: range_element
     }
     self.pp = None
Пример #7
0
    def default(self, obj):
        """ Turns the serialized objects back to assumptions

        :param obj: current serialized object
        :return: assumption representation of the serialized objects
        """
        if not obj:
            return None

        if "0" in obj:
            return obj["0"]

        if "iterations" in obj:
            num_iter = obj["iterations"]
            assmps = obj["assmps"]
            return InputAssumptionLattice(num_iter, assmps)

        type_assumption = TypeLattice()
        range_assumption = IntervalLattice()
        if "type_assmp" in obj:
            type_assmp = obj["type_assmp"]
            if type_assmp == "Int":
                type_assumption = TypeLattice().integer()
            elif type_assmp == "Float":
                type_assumption = TypeLattice().real()
        if "range_assmp" in obj:
            bounds = obj["range_assmp"]
            if bounds == '⊥':
                range_assumption = IntervalLattice().bottom()
            elif bounds == 'T':
                range_assumption = IntervalLattice()
            else:
                bounds = obj["range_assmp"][1:-1].split(',')
                bounds[0] = -inf if bounds[0].strip() == "-inf" else int(bounds[0])
                bounds[1] = inf if bounds[1].strip() == "inf" else int(bounds[1])
                range_assumption = IntervalLattice(bounds[0], bounds[1])

        return AssumptionLattice(type_assumption, range_assumption)
Пример #8
0
    def decomp(self, exclude: 'IntervalKWrapper') \
            -> Set['IntervalKWrapper']:
        left = deepcopy(self)  # left & right non-exclude
        right = deepcopy(self)

        k_state = self.store[self.k_var]
        k_exclude = exclude.store[self.k_var]
        if k_state.is_bottom():
            left.store[self.k_var].bottom()
            right.store[self.k_var].bottom()
        elif k_exclude.is_bottom():
            left.store[self.k_var] = IntervalLattice(k_state.lower,
                                                     k_state.upper)
            right.store[self.k_var].bottom()
        elif k_exclude.is_top():  # exclude everything
            left.store[self.k_var].bottom()
            right.store[self.k_var].bottom()
        else:
            left.store[self.k_var] = IntervalLattice(
                k_state.lower, k_exclude.lower - 1)  # bottom if empty
            right.store[self.k_var] = IntervalLattice(
                k_exclude.upper + 1, k_state.upper)  # bottom if empty

        return {left, right}
Пример #9
0
    def test_add(self):
        self.assertEqual(IntervalLattice().add(IntervalLattice(0, 1)), IntervalLattice())
        self.assertEqual(IntervalLattice().add(IntervalLattice(1, 0)), IntervalLattice(1, 0))

        self.assertEqual(IntervalLattice(0, 1).add(IntervalLattice()), IntervalLattice())
        self.assertEqual(IntervalLattice(0, 1).add(IntervalLattice(1, 0)), IntervalLattice(1, 0))

        self.assertEqual(IntervalLattice(1, 0).add(IntervalLattice()), IntervalLattice(1, 0))
        self.assertEqual(IntervalLattice(1, 0).add(IntervalLattice(0, 1)), IntervalLattice(1, 0))

        self.assertEqual(IntervalLattice(upper=3).add(IntervalLattice(lower=2)), IntervalLattice())
        self.assertEqual(IntervalLattice(2, 4).add(IntervalLattice(1, 3)), IntervalLattice(3, 7))
        self.assertEqual(IntervalLattice(1, 2).add(IntervalLattice(3, 4)), IntervalLattice(4, 6))
Пример #10
0
    def test_widening(self):
        self.assertEqual(IntervalLattice().widening(IntervalLattice(0, 1)), IntervalLattice())
        self.assertEqual(IntervalLattice().widening(IntervalLattice(1, 0)), IntervalLattice())

        self.assertEqual(IntervalLattice(0, 1).widening(IntervalLattice()), IntervalLattice())
        widening1 = IntervalLattice(0, 1).widening(IntervalLattice(1, 0))
        self.assertEqual(widening1, IntervalLattice(0, 1))

        self.assertEqual(IntervalLattice(1, 0).widening(IntervalLattice()), IntervalLattice())
        widening2 = IntervalLattice(1, 0).widening(IntervalLattice(0, 1))
        self.assertEqual(widening2, IntervalLattice(0, 1))

        widening3 = IntervalLattice(upper=3).widening(IntervalLattice(lower=2))
        self.assertEqual(widening3, IntervalLattice())
        widening4 = IntervalLattice(2, 4).widening(IntervalLattice(1, 3))
        self.assertEqual(widening4, IntervalLattice(upper=4))
        widening5 = IntervalLattice(1, 2).widening(IntervalLattice(3, 4))
        self.assertEqual(widening5, IntervalLattice(lower=1))
Пример #11
0
    def test_meet(self):
        self.assertEqual(IntervalLattice().meet(IntervalLattice(0, 1)), IntervalLattice(0, 1))
        self.assertEqual(IntervalLattice().meet(IntervalLattice(1, 0)), IntervalLattice(1, 0))

        self.assertEqual(IntervalLattice(0, 1).meet(IntervalLattice()), IntervalLattice(0, 1))
        self.assertEqual(IntervalLattice(0, 1).meet(IntervalLattice(1, 0)), IntervalLattice(1, 0))

        self.assertEqual(IntervalLattice(1, 0).meet(IntervalLattice()), IntervalLattice(1, 0))
        self.assertEqual(IntervalLattice(1, 0).meet(IntervalLattice(0, 1)), IntervalLattice(1, 0))

        meet = IntervalLattice(upper=3).meet(IntervalLattice(lower=2))
        self.assertEqual(meet, IntervalLattice(2, 3))
        self.assertEqual(IntervalLattice(2, 4).meet(IntervalLattice(1, 3)), IntervalLattice(2, 3))
        self.assertEqual(IntervalLattice(1, 2).meet(IntervalLattice(3, 4)), IntervalLattice(1, 0))
Пример #12
0
    def test_join(self):
        self.assertEqual(IntervalLattice().join(IntervalLattice(0, 1)), IntervalLattice())
        self.assertEqual(IntervalLattice().join(IntervalLattice(1, 0)), IntervalLattice())

        self.assertEqual(IntervalLattice(0, 1).join(IntervalLattice()), IntervalLattice())
        self.assertEqual(IntervalLattice(0, 1).join(IntervalLattice(1, 0)), IntervalLattice(0, 1))

        self.assertEqual(IntervalLattice(1, 0).join(IntervalLattice()), IntervalLattice())
        self.assertEqual(IntervalLattice(1, 0).join(IntervalLattice(0, 1)), IntervalLattice(0, 1))

        join = IntervalLattice(upper=3).join(IntervalLattice(lower=2))
        self.assertEqual(join, IntervalLattice())
        self.assertEqual(IntervalLattice(2, 4).join(IntervalLattice(1, 3)), IntervalLattice(1, 4))
        self.assertEqual(IntervalLattice(1, 2).join(IntervalLattice(3, 4)), IntervalLattice(1, 4))
Пример #13
0
    def test_leq(self):
        self.assertFalse(IntervalLattice().less_equal(IntervalLattice(0, 1)))
        self.assertFalse(IntervalLattice().less_equal(IntervalLattice(1, 0)))

        self.assertTrue(IntervalLattice(0, 1).less_equal(IntervalLattice()))
        self.assertFalse(IntervalLattice(0, 1).less_equal(IntervalLattice(1, 0)))

        self.assertTrue(IntervalLattice(1, 0).less_equal(IntervalLattice()))
        self.assertTrue(IntervalLattice(1, 0).less_equal(IntervalLattice(0, 1)))
Пример #14
0
    def test_sub(self):
        self.assertEqual(IntervalLattice().sub(IntervalLattice(0, 1)), IntervalLattice())
        self.assertEqual(IntervalLattice().sub(IntervalLattice(1, 0)), IntervalLattice(1, 0))

        self.assertEqual(IntervalLattice(0, 1).sub(IntervalLattice()), IntervalLattice())
        self.assertEqual(IntervalLattice(0, 1).sub(IntervalLattice(1, 0)), IntervalLattice(1, 0))

        self.assertEqual(IntervalLattice(1, 0).sub(IntervalLattice()), IntervalLattice(1, 0))
        self.assertEqual(IntervalLattice(1, 0).sub(IntervalLattice(0, 1)), IntervalLattice(1, 0))

        sub = IntervalLattice(upper=3).sub(IntervalLattice(lower=2))
        self.assertEqual(sub, IntervalLattice(upper=1))
        self.assertEqual(IntervalLattice(2, 4).sub(IntervalLattice(1, 3)), IntervalLattice(-1, 3))
        self.assertEqual(IntervalLattice(1, 2).sub(IntervalLattice(3, 4)), IntervalLattice(-3, -1))