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)
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
def test_check_invariant_different_number_parameters(self): invariant = Function('inv', 2) invariant.parameters = [] invariant.parameters.append(Parameter('t1', 'int')) variables = [] variables.append(Variable('t1','int', 10)) variables.append(Variable('t2','bool', 11)) invariant.returndt = 'bool' checker = ComplianceChecker() with pytest.raises(ComplianceError): checker.check_invariant(invariant, variables)
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
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)
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
def extract_variable(self, spec, position): spec_text = ''.join(spec) var_text = spec_text[spec_text.index('var') + 3:spec_text.index(';')].strip() var_name = var_text[:var_text.index(':')].strip() var_type = var_text[var_text.index(':') + 1:].strip() return Variable(var_name, var_type, position)
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
def test_check_gteq_no_gteq_defined(self): variables=[] variables.append(Variable('four', 'TestType', 13)) gteq = None checker = ComplianceChecker() with pytest.raises(ComplianceError): checker.check_gteq(gteq, variables)
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
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)
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
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
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)
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