Ejemplo n.º 1
0
    def test_genAST_sub_any(self):
        string = '''
        MACHINE Test
        VARIABLES xx
        INVARIANT xx:NAT 
        INITIALISATION BEGIN xx:=1;
                        ANY r1, r2 WHERE
                        r1 : NAT &
                        r2 : NAT &
                        r1*r1 + r2*r2 = 25
                        THEN
                        xx := r1 + r2
                        END
                    END
        END'''
        string_to_file(string, file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        env._min_int = -1
        env._max_int = 5
        mch = parse_ast(root, env)
        type_check_bmch(root, env, mch) # also checks all included, seen, used and extend
        arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT
        assert isinstance(root.children[2], AInvariantMachineClause)
        assert interpret(root.children[2], env)
        assert env.get_value("xx")==5 or env.get_value("xx")==7 # 3+4 or 5+0
Ejemplo n.º 2
0
    def test_examples_knights_knaves(self):
        string ='''
        MACHINE KnightsKnaves
        /* Puzzle from Smullyan:
        Knights: always tell the truth
        Knaves: always lie

        1: A says: “B is a knave or C is a knave”
        2: B says “A is a knight”

        What are A & B & C?
        */
        CONSTANTS A,B,C
        PROPERTIES
        A:BOOL & B:BOOL & C:BOOL /* TRUE if they are a Knight */
        &
        (A=TRUE <=> (B=FALSE or C=FALSE)) &
        (B=TRUE <=> A=TRUE)
        END'''
        # Build AST
        string_to_file(string, file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        mch = parse_ast(root, env)
        type_check_bmch(root, env, mch) # also checks all included, seen, used and extend
        arbitrary_init_machine(root, env, mch) # search for CONSTANTS which make PROPERTIES True
        assert env.get_value("A") == True
        assert env.get_value("B") == True
        assert env.get_value("C") == False
Ejemplo n.º 3
0
    def test_genAST_subst_def2(self):
        string='''
        MACHINE Test
        VARIABLES z, b, x
        INVARIANT x:NAT & z:NAT & b:BOOL
        INITIALISATION x:=2 ; Assign(x+1, z) ; Assign(TRUE, b)
        DEFINITIONS Assign(Expr, VarName) == VarName := Expr;
        END'''
        string_to_file(string, file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        #Test
        env = Environment()
        dh = DefinitionHandler(env, str_ast_to_python_ast)
        dh.repl_defs(root)
        mch = parse_ast(root, env)
        type_check_bmch(root, env, mch) # also checks all included, seen, used and extend
        arbitrary_init_machine(root, env, mch)# init VARIABLES and eval INVARIANT
        invariant = root.children[2]
        assert isinstance(invariant, AInvariantMachineClause)
        assert interpret(invariant, env)
        assert env.get_value("z")==3
        assert env.get_value("b")==True
        assert env.get_value("x")==2
Ejemplo n.º 4
0
    def test_simple_model_checking2(self):
        path = "examples/rpython_performance/SigmaLoop.mch"
        if os.name=='nt':
            path="examples/rpython_performance\SigmaLoop"
        ast_string = file_to_AST_str(path)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        env._max_int = 2**31
        mch = parse_ast(root, env)
        type_check_bmch(root, env, mch) # also checks all included, seen, used and extend 
        
        solution_file_read = False
        bstates = set_up_constants(root, env, mch, solution_file_read)
        assert len(bstates)==0 # no setup possible
        bstates = exec_initialisation(root, env, mch, solution_file_read)
        assert len(bstates)==1 # only one possibility (sum:=45)  
        assert len(env.state_space.seen_states)==0        
        assert isinstance(bstates[0], BState)
        env.state_space.set_current_state(bstates[0])
        assert len(env.state_space.seen_states)==1
        invatiant = root.children[2]
        assert isinstance(invatiant, AInvariantMachineClause)
        assert interpret(invatiant, env)
        assert len(env.state_space.stack)==2 
        next_states = calc_next_states(env, mch)
        assert len(next_states)==1
        assert len(env.state_space.stack)==2 # init and empty setup
        assert env.get_value('sum')==55
        env.state_space.set_current_state(next_states[0].bstate) 
        assert env.get_value('sum')==55  
Ejemplo n.º 5
0
    def test_genAST_sub_let(self):
        string = '''
        MACHINE Test
        VARIABLES SumR, DifferenceR, Var1, Var2
        INVARIANT SumR:NAT & DifferenceR:NAT & Var1:NAT & Var2:NAT
        INITIALISATION BEGIN Var1:=2; Var2:=3;
                        LET r1, r2 BE
                        r1 = (Var1 + Var2) / 2 &
                        r2 = (Var1 - Var2) / 2
                        IN
                        SumR := r1 + r2 ||
                        DifferenceR := r1 - r2
                        END

                    END
        END'''
        string_to_file(string, file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        env._min_int = -1
        env._max_int = 5
        mch = parse_ast(root, env)
        type_check_bmch(root, env, mch) # also checks all included, seen, used and extend
        arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT
        assert isinstance(root.children[2], AInvariantMachineClause)
        assert interpret(root.children[2], env)
        assert env.get_value("SumR")==1
        assert env.get_value("DifferenceR")==3
        assert env.get_value("Var1")==2
        assert env.get_value("Var2")==3
Ejemplo n.º 6
0
    def test_simple_model_checking0(self):
        path = "examples/rpython_performance/Lift2.mch"
        if os.name=='nt':
            path="examples\rpython_performance\Lift2"
        ast_string = file_to_AST_str(path)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        mch = parse_ast(root, env)
        type_check_bmch(root, env, mch) # also checks all included, seen, used and extend
        env._max_int = 2**31
        solution_file_read = False
        bstates = set_up_constants(root, env, mch, solution_file_read)
        assert len(bstates)==0 # no setup possible
        bstates = exec_initialisation(root, env, mch, solution_file_read)
        assert len(bstates)==1 # only one possibility (floor:=4)
        assert len(env.state_space.seen_states)==0        
        assert isinstance(bstates[0], BState)
        env.state_space.set_current_state(bstates[0])
        assert len(env.state_space.seen_states)==1
        invatiant = root.children[2]
        assert isinstance(invatiant, AInvariantMachineClause)
        assert interpret(invatiant, env)
        assert len(env.state_space.stack)==2 
        next_states = calc_next_states(env, mch)
        assert len(next_states)==2
        assert len(env.state_space.stack)==2 # init and empty setup
        assert env.get_value('floor')==4
        env.state_space.undo()
        assert len(env.state_space.stack)==1 # setup
        assert len(env.state_space.seen_states)==1
        for n_state in next_states:
            bstate = n_state.bstate
            assert isinstance(bstate, BState)
            if not env.state_space.is_seen_state(bstate):
                env.state_space.set_current_state(bstate)
        assert len(env.state_space.stack)==3 # dec, inc, setup
        assert len(env.state_space.seen_states)==3
        assert env.get_value('floor')==3 or env.get_value('floor')==5
        
        # TODO: Bstate needs refactoring. 
        # - Remove init state
        # - dont map None to values if parsing unit is no machine
        # - check empty on stack length 0
        # model checking loop
        while not env.state_space.empty():
            assert interpret(invatiant, env) 
            next_states = calc_next_states(env, mch)
            env.state_space.undo()
            for n_state in next_states:
                bstate = n_state.bstate
                if not env.state_space.is_seen_state(bstate):
                    env.state_space.set_current_state(bstate)  
        assert len(env.state_space.seen_states)==100
Ejemplo n.º 7
0
    def test_genAST_para_def(self):
        # Build AST
        string ='''
        MACHINE Test
        VARIABLES z
        INVARIANT z:MyType 
        INITIALISATION z:= Expr(2)
        DEFINITIONS
        Expr(X) == 1+X;
        MyType == NAT;
        END'''
        string_to_file(string, file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        dh = DefinitionHandler(env, str_ast_to_python_ast)
        dh.repl_defs(root)
        mch = parse_ast(root, env)
        type_check_bmch(root, env, mch) # also checks all included, seen, used and extend
        arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT
        invariant = root.children[2]
        assert isinstance(invariant, AInvariantMachineClause)
        assert interpret(invariant, env)
        assert env.get_value("z")==3
Ejemplo n.º 8
0
    def test_genAST_sub_let2(self):
        string = '''
        MACHINE Test
        VARIABLES X, Y
        INVARIANT X:NAT & Y:NAT
        INITIALISATION BEGIN X:=10; 
                        LET r1, X BE
                    X  = 6 &
                        r1 = X / 2 
                        IN
                        Y := r1
                        END

                    END
        END'''
        string_to_file(string, file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)
        
        # Test
        env = Environment()
        env._min_int = -1
        env._max_int = 5
        mch = parse_ast(root, env)
        type_check_bmch(root, env, mch) # also checks all included, seen, used and extend
        arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT
        assert isinstance(root.children[2], AInvariantMachineClause)
        assert interpret(root.children[2], env)
        assert env.get_value("X")==10
        assert env.get_value("Y")==3
Ejemplo n.º 9
0
    def test_genAST_sub_var(self):
        # Build AST
        string = '''
        MACHINE Test
        VARIABLES xx
        INVARIANT xx:NAT 
        INITIALISATION BEGIN xx:=1; 
                        VAR varLoc1, varLoc2 IN
                        varLoc1 := xx + 1 ;
                        varLoc2 := 2 * varLoc1 ;
                        xx := varLoc2
                        END
                    END
        END'''
        string_to_file(string, file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        env._min_int = -1
        env._max_int = 5
        mch = parse_ast(root, env)
        type_check_bmch(root, env, mch) # also checks all included, seen, used and extend
        arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT
        assert isinstance(root.children[2], AInvariantMachineClause)
        assert interpret(root.children[2], env)
        assert env.get_value("xx")==4
Ejemplo n.º 10
0
    def test_genAST_sub_case3(self):
        # Build AST
        string = '''
        MACHINE Test
        VARIABLES xx
        INVARIANT xx:NAT 
        INITIALISATION 
            BEGIN xx:=1; 
                CASE 1+1 OF 
                EITHER 4,5,6 THEN xx:=2 
                OR 7,8,9 THEN xx:=3
                ELSE xx:=4 END 
                END
            END
        END'''
        string_to_file(string, file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        mch = parse_ast(root, env)
        type_check_bmch(root, env, mch) # also checks all included, seen, used and extend
        arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT
        assert isinstance(root.children[2], AInvariantMachineClause)
        assert interpret(root.children[2], env)
        assert env.get_value("xx")==4
Ejemplo n.º 11
0
    def test_genAST_pred_rel_expr(self):
        # Build AST:
        string_to_file("#PREDICATE f1 = {(-1|->{0, 2}), (1|->{6, 8}), (3|->{3})} & r= rel(f1)", file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        assert interpret(root,env)
        assert env.get_value("r") == frozenset([(-1,0),(-1,2),(1,6),(1,8),(3,3)])
Ejemplo n.º 12
0
    def test_genAST_pred_fnc_expr(self):
        # Build AST
        string_to_file("#PREDICATE R1 = {(0|->1), (0|->2), (1|->1), (1|->7), (2|->3)} & f= fnc(R1)", file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        
        assert interpret(root,env)
        f = env.get_value("f")
        assert f == frozenset([(0,frozenset([1,2])),(1,frozenset([1,7])),(2,frozenset([3]))])
Ejemplo n.º 13
0
    def test_genAST_set_GEN_INTER2(self):
        string = '''
        MACHINE Test
        VARIABLES xx, E2
        INVARIANT xx:NAT & E2:POW(NAT)
        INITIALISATION E2:={2,4} ; 
            xx:: INTER (x1).(x1 : E2 | {y1 | y1 : NAT & y1 <= x1}) 
        END'''
        string_to_file(string, file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        env._min_int = -1
        env._max_int = 5
        mch = parse_ast(root, env)
        type_check_bmch(root, env, mch) # also checks all included, seen, used and extend
        arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT
        assert isinstance(root.children[2], AInvariantMachineClause)
        assert interpret(root.children[2], env)
        assert env.get_value("xx")==0 or env.get_value("xx")==1 or env.get_value("xx")==2 
Ejemplo n.º 14
0
    def test_genAST_pred_set_compreh3(self):      
        string = '''
        MACHINE Test
        VARIABLES f, T
        SETS      S={a,b,c}
        INVARIANT f<:S & T<:S
        INITIALISATION T:={a,b}; f:={x| x:S & x/:T}
        END'''

        # Build AST
        string_to_file(string, file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        mch = parse_ast(root, env)
        type_check_bmch(root, env, mch) # also checks all included, seen, used and extend
        arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT
        assert "c" in env.get_value("f")
        assert "a" in env.get_value("T")
        assert "b" in env.get_value("T")
Ejemplo n.º 15
0
 def test_genAST_mch_seq_conc(self):
     # Build AST
     string = '''
     MACHINE Test
     VARIABLES s,t,u
     SETS S={a,b}
     INVARIANT s:perm(S) & t:perm(S) & u:seq(S)
     INITIALISATION s:={(2,a),(1,b)}; t:={(1,a),(2,b)}; u:=s^t
     END'''
     string_to_file(string, file_name)
     ast_string = file_to_AST_str(file_name)
     root = str_ast_to_python_ast(ast_string)
     
     # Test
     env = Environment()
     mch = parse_ast(root, env)
     type_check_bmch(root, env, mch) # also checks all included, seen, used and extend
     arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT
     assert env.get_value("S")==frozenset(['a', 'b'])
     assert env.get_value("s")==frozenset([(2,'a'), (1,'b')])
     assert env.get_value("t")==frozenset([(1,'a'), (2,'b')])
     assert env.get_value("u")==frozenset([(1,'b'), (2,'a'), (3,'a'), (4,'b')])
Ejemplo n.º 16
0
 def test_ProB_vs_pyB_num_rounding2(self):
     string = '''
     MACHINE Test
     VARIABLES xx
     INVARIANT xx:NAT
     INITIALISATION xx:=(99) / 100
     END'''
     # Build AST
     string_to_file(string, file_name)
     ast_string = file_to_AST_str(file_name)
     root = str_ast_to_python_ast(ast_string)
     
     # Test
     env = Environment()
     mch = parse_ast(root, env)
     type_check_bmch(root, env, mch)
     arbitrary_init_machine(root, env, mch) # search for CONSTANTS which make PROPERTIES True
     assert env.get_value("xx") == 0    
Ejemplo n.º 17
0
    def test_string_set(self):
        string = '''
        MACHINE Test
        VARIABLES s
        INVARIANT s:STRING
        INITIALISATION s:="Hallo Welt"
        END'''
        # Build AST
        string_to_file(string, file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        mch = parse_ast(root, env)
        type_check_bmch(root, env, mch) # also checks all included, seen, used and extend
        arbitrary_init_machine(root, env, mch) # search for CONSTANTS which make PROPERTIES True
        assert env.get_value("s") == "Hallo Welt"
Ejemplo n.º 18
0
    def test_structs3(self):
        string = '''
        MACHINE Test
        VARIABLES RES, xx
        INVARIANT RES:struct(Mark:NAT, Good_enough:BOOL) & xx:NAT
        INITIALISATION RES := rec(Mark:4, Good_enough:TRUE); xx:=RES'Mark
        END'''
        # Build AST
        string_to_file(string, file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        mch = parse_ast(root, env)
        type_check_bmch(root, env, mch) # also checks all included, seen, used and extend
        arbitrary_init_machine(root, env, mch) # search for CONSTANTS which make PROPERTIES True
        assert env.get_value("xx") == 4
Ejemplo n.º 19
0
    def test_structs4(self):
        string = '''
        MACHINE StructTest
        CONSTANTS record
        PROPERTIES
        record : struct(a: STRING, b : INTEGER) &
        record =  rec(a: "abc", b : 1)

        END'''
        # Build AST
        string_to_file(string, file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        mch = parse_ast(root, env)
        type_check_bmch(root, env, mch) # also checks all included, seen, used and extend
        arbitrary_init_machine(root, env, mch) # search for CONSTANTS which make PROPERTIES True
        assert env.get_value("record")==frozenset([('a', 'abc'), ('b', 1)])
Ejemplo n.º 20
0
    def test_genAST_sub_func_overw(self):
        # Build AST
        string = '''
        MACHINE Test
        VARIABLES f
        INVARIANT f:POW({1,2,3}*{1,2,3})
        INITIALISATION f:={(1,2),(3,4)} ; f(3) := 1+2
        END'''
        string_to_file(string, file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        mch = parse_ast(root, env)
        type_check_bmch(root, env, mch) # also checks all included, seen, used and extend
        arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT
        assert isinstance(root.children[2], AInvariantMachineClause)
        assert interpret(root.children[2], env)
        assert env.get_value("f")==frozenset([(1,2),(3,3)])
Ejemplo n.º 21
0
    def test_genAST_sub_enum_set_as(self):    
        string = '''
        MACHINE Test
        SETS ID={aa,bb}
        VARIABLES xx
        INVARIANT xx:ID
        INITIALISATION xx:=aa
        END'''
        string_to_file(string, file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        mch = parse_ast(root, env)
        type_check_bmch(root, env, mch) # also checks all included, seen, used and extend
        arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT       
        assert isinstance(root.children[3], AInvariantMachineClause)
        assert interpret(root.children[3], env)
        assert env.get_value("xx")=="aa"
        
Ejemplo n.º 22
0
    def test_genAST_sub_simple_asgn(self):
        # Build AST
        string = '''
        MACHINE Test
        VARIABLES xx
        INVARIANT xx:NAT
        INITIALISATION xx:=3
        END'''
        string_to_file(string, file_name)
        ast_string = file_to_AST_str(file_name)
        root = str_ast_to_python_ast(ast_string)

        # Test
        env = Environment()
        mch = parse_ast(root, env)
        type_check_bmch(root, env, mch) # also checks all included, seen, used and extend
        arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT
        assert isinstance(root.children[2], AInvariantMachineClause)
        assert interpret(root.children[2], env)
        assert env.get_value("xx")==3
        assert isinstance(get_type_by_name(env, "xx"), IntegerType)