コード例 #1
0
 def get_spec(self):
     spec = Specification('sample')
     spec.add_variable(Variable('set', '[int]bool', 1))
     procedure = Procedure('merge_6', 15)
     procedure.add_parameter(Parameter('set1', '[int]bool'))
     procedure.add_modifies('set')
     procedure.add_ensures(
         '(forall i:int :: set[i] == (old(set)[i] || set1[i]))')
     procedure.set_implementation('assume false;')
     spec.set_merge(procedure)
     gteq = Function('gteq', 5)
     gteq.add_param(Parameter('set1', '[int]bool'))
     gteq.add_param(Parameter('set2', '[int]bool'))
     gteq.set_return('bool')
     spec.set_gteq(gteq)
     spec.set_preface(
         'var set:[int]bool;\n//@gteq\nfunction gteq(set1:[int]bool, set2:[int]bool) returns(bool)\n{(forall i:int :: set2[i] ==> set1[i])}'
     )
     procedure = Procedure('add', 15)
     procedure.add_parameter(Parameter('value', 'int'))
     procedure.add_modifies('set')
     procedure.add_ensures(
         '(forall i:int :: (i == value ==> set[i] == true) && (i != value ==> set[i] == old(set)[i]))'
     )
     procedure.set_implementation('set[value] := true;')
     spec.add_procedure(procedure)
     return spec
コード例 #2
0
 def test_check_invariant_compliant_invariant(self):
     invariant = Function('inv', 2)
     invariant.parameters = []
     invariant.parameters.append(Parameter('t1', 'int'))
     invariant.parameters.append(Parameter('t2', 'bool'))
     variables = []
     variables.append(Variable('t1','int', 10))
     variables.append(Variable('t2','bool', 11))
     invariant.returndt = 'bool'
     checker = ComplianceChecker()
     assert checker.check_invariant(invariant, variables) == True
コード例 #3
0
 def test_check_merge_modifies_number_less(self):
     variables = []
     variables.append(Variable('one', 'int', 4))
     variables.append(Variable('two', 'bool', 4))
     merge = Procedure('merge_proc', 10)
     merge.add_parameter(Parameter('one1', 'int'))
     merge.add_parameter(Parameter('two1', 'bool'))
     merge.add_modifies('two')
     checker = ComplianceChecker()
     with pytest.raises(ComplianceError):
         checker.check_merge(merge, variables)
コード例 #4
0
 def test_check_merge_compliant(self):
     variables = []
     variables.append(Variable('one', 'int', 4))
     variables.append(Variable('two', 'bool', 4))
     merge = Procedure('merge_proc', 10)
     merge.add_parameter(Parameter('one1', 'int'))
     merge.add_parameter(Parameter('two1', 'bool'))
     merge.add_modifies('one')
     merge.add_modifies('two')
     checker = ComplianceChecker()
     assert checker.check_merge(merge, variables) == True
コード例 #5
0
 def test_check_gteq_diff_parameter_number_less(self):
     variables=[]
     variables.append(Variable('one', 'int', 10))
     variables.append(Variable('two', 'TestType', 13))
     gteq = Function('gteq', 2)
     gteq.parameters.append(Parameter('one1', 'int'))
     gteq.parameters.append(Parameter('one2', 'int'))
     gteq.returndt = 'bool'
     checker = ComplianceChecker()
     with pytest.raises(ComplianceError):
         checker.check_gteq(gteq, variables)
コード例 #6
0
 def test_check_gteq_compliant_gteq(self):
     variables=[]
     variables.append(Variable('one', 'int', 10))
     variables.append(Variable('two', 'TestType', 13))
     gteq = Function('gteq', 2)
     gteq.parameters.append(Parameter('one1', 'int'))
     gteq.parameters.append(Parameter('one2', 'int'))
     gteq.parameters.append(Parameter('two1', 'TestType'))
     gteq.parameters.append(Parameter('two2', 'TestType'))
     gteq.returndt = 'bool'
     checker = ComplianceChecker()
     assert checker.check_gteq(gteq, variables) == True
コード例 #7
0
 def test_check_invariant_return_datatype_bool(self):
     invariant = Function('inv', 2)
     invariant.parameters = []
     invariant.parameters.append(Parameter('t1', 'int'))
     invariant.parameters.append(Parameter('t2', 'bool'))
     variables = []
     variables.append(Variable('t1','int', 10))
     variables.append(Variable('t2','bool', 11))
     invariant.returndt = 'int'
     checker = ComplianceChecker()
     with pytest.raises(ComplianceError):
         checker.check_invariant(invariant, variables)
コード例 #8
0
 def get_compliant_spec(self):
     spec = Specification('name')
     spec.add_variable(Variable('one', 'int', 4))
     gteq = Function('gteq', 2)
     gteq.parameters.append(Parameter('one1', 'int'))
     gteq.parameters.append(Parameter('one2', 'int'))
     gteq.returndt = 'bool'
     spec.set_gteq(gteq)
     inv = Function('inv', 2)
     inv.returndt = 'bool'
     inv.parameters.append(Parameter('one', 'int'))
     spec.set_invariant(inv)
     merge = Procedure('merge_proc', 10)
     merge.add_parameter(Parameter('one1', 'int'))
     merge.add_modifies('one')
     spec.set_merge(merge)
     return spec
コード例 #9
0
 def __get_params(self, param_spec):
     if not param_spec.strip():
         return []
     params = []
     for each in param_spec.split(','):
         p_name = each[:each.index(':')].strip()
         p_type = each[each.index(':') + 1:].strip()
         param = Parameter(p_name, p_type)
         params.append(param)
     return params
コード例 #10
0
 def test_check_not_monotonicity(self):
     spec = Specification('sample')
     spec.add_variable(Variable('set', '[int]bool', 1))
     procedure = Procedure('remove', 15)
     procedure.add_parameter(Parameter('value', 'int'))
     procedure.add_modifies('set')
     procedure.set_implementation('set[value] := false;')
     spec.add_procedure(procedure)
     gteq = Function('gteq', 5)
     gteq.add_param(Parameter('one', '[int]bool'))
     gteq.add_param(Parameter('two', '[int]bool'))
     gteq.set_return('bool')
     spec.set_gteq(gteq)
     spec.set_preface(
         'var set:[int]bool;\n//@gteq\nfunction gteq(set1:[int]bool, set2:[int]bool) returns(bool)\n{(forall i:int :: set2[i] ==> set1[i])}'
     )
     checker = ConvergenceChecker()
     with pytest.raises(ConvergenceError):
         checker.check_monotonicity(spec, procedure)
コード例 #11
0
 def test_check_lub(self):
     spec = Specification('sample')
     spec.add_variable(Variable('set', '[int]bool', 1))
     procedure = Procedure('merge_6', 15)
     procedure.add_parameter(Parameter('set1', '[int]bool'))
     procedure.add_modifies('set')
     procedure.add_ensures(
         '(forall i:int :: set[i] == (old(set)[i] || set1[i]))')
     procedure.set_implementation('assume false;')
     spec.set_merge(procedure)
     gteq = Function('gteq', 5)
     gteq.add_param(Parameter('set1', '[int]bool'))
     gteq.add_param(Parameter('set2', '[int]bool'))
     gteq.set_return('bool')
     spec.set_gteq(gteq)
     spec.set_preface(
         'var set:[int]bool;\n//@gteq\nfunction gteq(set1:[int]bool, set2:[int]bool) returns(bool)\n{(forall i:int :: set2[i] ==> set1[i])}'
     )
     checker = ConvergenceChecker()
     assert checker.check_lub(spec) == True
コード例 #12
0
 def test_stable_pair(self):
     spec = Specification('sample')
     spec.add_variable(Variable('counter', 'int', 1))
     procedure = Procedure('inc', 15)
     procedure.add_parameter(Parameter('value', 'int'))
     procedure.add_modifies('counter')
     procedure.add_requires('value > 0')
     procedure.set_implementation('counter := counter + value;')
     spec.add_procedure(procedure)
     merge = Procedure('merge', 15)
     merge.add_parameter(Parameter('counter1', 'int'))
     merge.add_modifies('counter')
     merge.set_implementation('counter := (if counter1 > counter then counter1 else counter);')
     spec.set_merge(merge)
     invariant = Function('inv', 10)
     invariant.add_param(Parameter('counter', 'int'))
     invariant.set_return('bool')
     spec.set_invariant(invariant)
     spec.set_preface('var counter :int;\n//@invariant\nfunction inv(counter:int) returns(bool)\n{\n  counter >= 0\n}')
     checker = SafetyChecker()
     assert checker.check_stability(spec, procedure) == True
コード例 #13
0
 def test_unsafe_proc(self):
     spec = Specification('sample')
     spec.add_variable(Variable('counter', 'int', 1))
     procedure = Procedure('dec', 15)
     procedure.add_parameter(Parameter('value', 'int'))
     procedure.add_modifies('counter')
     procedure.set_implementation('counter := counter - value;')
     spec.add_procedure(procedure)
     merge = Procedure('merge', 15)
     merge.add_parameter(Parameter('counter1', 'int'))
     merge.add_modifies('counter')
     merge.set_implementation('counter := (if counter1 > counter then counter1 else counter);')
     spec.set_merge(merge)
     invariant = Function('inv', 10)
     invariant.add_param(Parameter('counter', 'int'))
     invariant.set_return('bool')
     spec.set_invariant(invariant)
     spec.set_preface('var counter :int;\n//@invariant\nfunction inv(counter:int) returns(bool)\n{\n  counter >= 0\n}')
     checker = SafetyChecker()
     with pytest.raises(SafetyError):
         checker.check_safety(spec, procedure)
コード例 #14
0
 def test_check_monotonicity(self):
     spec = Specification('sample')
     spec.add_variable(Variable('set', '[int]bool', 1))
     procedure = Procedure('add', 15)
     procedure.add_parameter(Parameter('value', 'int'))
     procedure.add_modifies('set')
     procedure.add_ensures(
         '(forall i:int :: (i == value ==> set[i] == true) && (i != value ==> set[i] == old(set)[i]))'
     )
     procedure.set_implementation('set[value] := true;')
     spec.add_procedure(procedure)
     gteq = Function('gteq', 5)
     gteq.add_param(Parameter('set1', '[int]bool'))
     gteq.add_param(Parameter('set2', '[int]bool'))
     gteq.set_return('bool')
     spec.set_gteq(gteq)
     spec.set_preface(
         'var set:[int]bool;\n//@gteq\nfunction gteq(set1:[int]bool, set2:[int]bool) returns(bool)\n{(forall i:int :: set2[i] ==> set1[i])}'
     )
     checker = ConvergenceChecker()
     assert checker.check_monotonicity(spec, procedure) == True
コード例 #15
0
 def get_constants(self, parameters, prefix='', suffix=''):
     constants = []
     for each in parameters:
         constants.append(
             Parameter(prefix + each.name + suffix, each.datatype))
     return constants