Пример #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
Пример #2
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
Пример #3
0
    def test_CartesianProductOverride(self):
        string = '''
        MACHINE CartesianProductOverride
        SETS
         S;T
        CONSTANTS a,b,c
        PROPERTIES
         /* Rule Hypotheses */
         a :  S <-> T &
         dom(a) = b &
         c <: T & 
        
         /* Rule Conclusion */
         not( a <+ b * c = b * c )
        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) # eval CONSTANTS and PROPERTIES
        assert isinstance(root.children[3], APropertiesMachineClause)
        assert interpret(root.children[3], env)
Пример #4
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
Пример #5
0
    def test_examples_no_query_op(self):
        string = '''
        MACHINE Query

        VARIABLES xx

        INVARIANT  xx:NAT

        INITIALISATION xx:=1
        
        OPERATIONS
        
        no_query = xx:=2

        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 isinstance(root.children[2], AInvariantMachineClause)
        assert interpret(root.children[2], env)
Пример #6
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
Пример #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
Пример #8
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
Пример #9
0
    def test_library_length(self):
        string = '''
        MACHINE LibraryStrings
        CONSTANTS length
        PROPERTIES
          /* compute the length of a string */
          length: STRING --> INTEGER &
          length = %x.(x:STRING|STRING_LENGTH(x)) 
        DEFINITIONS
          STRING_LENGTH(x) == length(x);
          EXTERNAL_FUNCTION_STRING_LENGTH == STRING --> INTEGER;
        ASSERTIONS
          length("abc") = 3;
          length("") = 0;
          length("hello") = 5
        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()
        dh = DefinitionHandler(env, str_ast_to_python_ast)                                   
        dh.repl_defs(root)
        mch = parse_ast(root, env)
        type_check_bmch(root, env, mch)
        assert isinstance(get_type_by_name(env, "length"), PowerSetType)
        assert isinstance(get_type_by_name(env, "length").data, CartType)
        assert isinstance(get_type_by_name(env, "length").data.left.data, StringType)
        assert isinstance(get_type_by_name(env, "length").data.right.data, IntegerType)
        arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT
        assert isinstance(root.children[4], AAssertionsMachineClause)
        interpret(root.children[4], env)
Пример #10
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
Пример #11
0
    def test_examples_simple_testset(self):
        string = '''
        MACHINE TestSet
        SETS
        ID={aa, bb, cc}

        CONSTANTS iv
        PROPERTIES
        iv:ID
        VARIABLES xx
        INVARIANT
        xx:ID
        INITIALISATION xx:=iv
        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 isinstance(root.children[5], AInvariantMachineClause)
        assert interpret(root.children[5], env)
Пример #12
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
Пример #13
0
    def test_examples_simple_bakery1(self):
        string ='''
        MACHINE Bakery1

        ABSTRACT_VARIABLES  p1, p2, y1, y2

        INVARIANT  
                p1:0..2 & p2:0..2 & y1:NATURAL & y2:NATURAL &
                (p1=2 => p2<2) &
                (p2=2 => p1<2) 

        INITIALISATION  p1,p2,y1,y2 := 0,0,0,0

        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 isinstance(root.children[2], AInvariantMachineClause)
        assert interpret(root.children[2], env)
Пример #14
0
    def test_abrial_book_page83_fail(self):
        # added some 42 in the assertions clause
        string = """
        MACHINE BBook_Page83
        /* Translation of example from page 83 of Abrial's B-Book */
        CONSTANTS p,w,q,f,g,s,t,h,k
        PROPERTIES
        p = {3|->5, 3|->9, 6|->3, 9|->2} &
        w = {1, 2, 3} &
        p[w] = {5,9} &
        q = {2|->7, 3|->4, 5|->1, 9|->5} &
        q <+ p = {3|->5, 3|->9, 6|->3, 9|->2, 2|->7, 5|->1} &
        f = {8|->10, 7|->11, 2|->11, 6|->12} &
        g = {1|->20, 7|->20, 2|->21, 1|->22} &
        f >< g = {(7|->(11|->20)), (2|->(11|->21))} &
        s = {1,4} &
        t = {2,3} &
        prj1(s,t) = {((1|->2)|->1),((1|->3)|->1),((4|->2)|->4),((4|->3)|->4)} &
        prj2(s,t) = {((1|->2)|->2),((1|->3)|->3),((4|->2)|->2),((4|->3)|->3)} &
        h = {1|->11, 4|->12} &
        k = {2|->21, 7|->22} &
        (h||k) = { (1,2) |-> (11,21), (1,7) |-> (11,22),
                    (4,2) |-> (12,21), (4,7) |-> (12,22) }
        ASSERTIONS
        p[w] = {5,42};
        q <+ p = {(3|->5),(3|->9),(6|->3),(9|->2),(2|->7),(5|->42)};
        f >< g = {(7|->(11|->20)), (2|->(11|->42))};
        prj1(s,t) = {((1|->2)|->1),((1|->3)|->1),((4|->2)|->4),((4|->3)|->42)};
        prj2(s,t) = {((1|->2)|->2),((1|->3)|->3),((4|->2)|->2),((4|->3)|->42)};
        (h||k) = { (1,2) |-> (11,21), (1,7) |-> (11,22),
                    (4,2) |-> (12,21), (4,7) |-> (12,42) }
        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)  # eval CONSTANTS and PROPERTIES
        # eval ASSERTIONS (again)
        assert isinstance(root.children[3], AAssertionsMachineClause)
        assert not interpret(root.children[3].children[0], env)
        assert not interpret(root.children[3].children[1], env)
        assert not interpret(root.children[3].children[2], env)
        assert not interpret(root.children[3].children[3], env)
        assert not interpret(root.children[3].children[4], env)
        assert not interpret(root.children[3].children[5], env)
Пример #15
0
 def test_only_invariant(self):
     string = '''
     MACHINE Only
     INVARIANT 4=4   
     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) 
Пример #16
0
    def test_library_append(self):        
        string = '''
        MACHINE m
        DEFINITIONS
            EXTERNAL_FUNCTION_STRING_APPEND == STRING*STRING --> STRING;
            STRING_APPEND(x,y) == append(x,y);
            STRING_LENGTH(x) == length(x);
            EXTERNAL_FUNCTION_STRING_LENGTH == STRING --> INTEGER;
        ABSTRACT_CONSTANTS
            append, length
        PROPERTIES
            append = %(x,y).(x: STRING & y: STRING | STRING_APPEND(x,y)) &
            length: STRING --> INTEGER &
            length = %x.(x:STRING|STRING_LENGTH(x))  
        ASSERTIONS
            append("abc","abc") = "abcabc";
            append("","abc") = "abc";
            append("abc","") = "abc";
            /*{x|x:{"abc","abcabc","hello"} & #(prefx).(append(prefx,"c")=x)} = {"abcabc","abc"};*/
            {x|x/="" & #y.(append(x,y)="abc" & y/="")} = {"a","ab"}; /* compute true prefixes */
            {x|x/="" & #y.(append(y,x)="abc" & y/="")} = {"c","bc"}; /* compute true postfixes */
            {y|y/="" & #(x,z).(append(x,append(y,z))="abc" & length(x)+length(z)>0)} =  		
            /* compute true substrings */ {"a","ab","b","bc","c"};
            {y|y/="" & #(x,z).(append(append(x,y),z)="abc" & length(x)+length(z)>0)} = 
		 	/* compute true substrings */ {"a","ab","b","bc","c"}
        END'''
        # TODO: prolog-style args
        # {x|x:{"abc","abcabc","hello"} & #(prefx).(append(prefx,"c")=x)} = {"abcabc","abc"};
        # 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()
        dh = DefinitionHandler(env, str_ast_to_python_ast)                                   
        dh.repl_defs(root)
        mch = parse_ast(root, env)
        type_check_bmch(root, env, mch)
        assert isinstance(get_type_by_name(env, "append"), PowerSetType)
        assert isinstance(get_type_by_name(env, "append").data, CartType)
        assert isinstance(get_type_by_name(env, "append").data.left.data, CartType)
        assert isinstance(get_type_by_name(env, "append").data.left.data.left.data, StringType)
        assert isinstance(get_type_by_name(env, "append").data.left.data.right.data, StringType)
        assert isinstance(get_type_by_name(env, "append").data.right.data, StringType)
        arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT
        assert isinstance(root.children[4], AAssertionsMachineClause)
        interpret(root.children[4], env)
Пример #17
0
    def test_genAST_subst_def3(self):
        string='''
        MACHINE Test3
        SEES UsingDefinitions
        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)        
Пример #18
0
    def test_library_codes(self):
        string = '''
        MACHINE LibraryStrings
        CONSTANTS codes, append
        PROPERTIES
          append = %(x,y).(x: STRING & y: STRING | STRING_APPEND(x,y)) &
          /* obtain the characters of a string as a B sequence of Ascii codes; it is reversible */
		  codes: STRING --> (INTEGER <-> INTEGER) &
		  codes = %(s).(s:STRING|STRING_CODES(s))
        DEFINITIONS
		  STRING_CODES(x) == codes(x);
		  EXTERNAL_FUNCTION_STRING_CODES == (STRING --> (INTEGER<->INTEGER));
          EXTERNAL_FUNCTION_STRING_APPEND == STRING*STRING --> STRING;
          STRING_APPEND(x,y) == append(x,y)		  
        ASSERTIONS
		  codes("") = <>;
		  /* codes(" ") = [32]; the Java parser currently swallows whitespace within strings */
		  codes("abc") = [97,98,99];
		  {x| codes(x) = codes("abc") ^ codes("abc")} = {"abcabc"};
		  !(x,y).(x:{"abc","hello",""} & y:{"abc","hello",""}
			  => codes(append(x,y)) = codes(x)^codes(y))
        END
        '''	  
		# FIXME: composition typechecking bug
		#		  {x| codes(x) = (codes("abc") ; succ) } = {"bcd"};
		# TODO: prolog style args
		# {x| codes(x) = %i.(i:1..26|96+i)} = {"abcdefghijklmnopqrstuvwxyz"}
        # 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()
        dh = DefinitionHandler(env, str_ast_to_python_ast)                                   
        dh.repl_defs(root)
        mch = parse_ast(root, env)
        type_check_bmch(root, env, mch)
        assert isinstance(get_type_by_name(env, "codes"), PowerSetType)
        assert isinstance(get_type_by_name(env, "codes").data, CartType)
        assert isinstance(get_type_by_name(env, "codes").data.left.data, StringType)
        assert isinstance(get_type_by_name(env, "codes").data.right.data.data, CartType)  
        assert isinstance(get_type_by_name(env, "codes").data.right.data.data.left.data, IntegerType)
        assert isinstance(get_type_by_name(env, "codes").data.right.data.data.right.data, IntegerType) 
        arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT
        assert isinstance(root.children[4], AAssertionsMachineClause)
        interpret(root.children[4], env)
Пример #19
0
    def test_structs2(self):
        string = '''
        MACHINE Test
        VARIABLES RES
        INVARIANT RES:struct(Mark:NAT, Good_enough:BOOL)
        INITIALISATION RES := rec(Mark : 14, Good_enough : 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
Пример #20
0
    def test_abrial_book_page82_fail(self):
        # changes example: change 3|->5 to 1->5 in p (PROPERTIES-clause)
        string = """
        MACHINE BBook_Page80
        /* Translation of example from page 82 of Abrial's B-Book */
        CONSTANTS p,q,u,s,t
        PROPERTIES
        p = {1|->5, 3|->9, 6|->3, 9|->2} &
        q = {2|->7, 3|->4, 5|->1, 9|->5} &
        u = {1,2,3} &
        s = {4,7,3} &
        t = {4,8,1}
        ASSERTIONS
        p~ = {5|->3, 9|->3, 3|->6, 2|->9};
        dom(p) = {3,6,9};
        ran(p) = {5,9,3,2};
        (p;q) = {3|->1, 3|->5, 6|->4, 9|->7};
        id(u) = {1|->1, 2|->2, 3|->3};
        s <|p = {3|->5, 3|->9};
        p |> t = {};
        s <<| p = {6|->3, 9|->2};
        p |>> t = {3|->5, 3|->9, 6|->3, 9|->2}
        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)  # eval CONSTANTS and PROPERTIES
        # eval ASSERTIONS (again)
        assert isinstance(root.children[3], AAssertionsMachineClause)
        assert not interpret(root.children[3].children[0], env)
        assert not interpret(root.children[3].children[1], env)
        assert interpret(root.children[3].children[2], env)
        assert not interpret(root.children[3].children[3], env)
        assert interpret(root.children[3].children[4], env)
        assert not interpret(root.children[3].children[5], env)
        assert interpret(root.children[3].children[6], env)
        assert not interpret(root.children[3].children[7], env)
        assert not interpret(root.children[3].children[8], env)
Пример #21
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"
Пример #22
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    
Пример #23
0
    def test_examples_simple_invariant(self):
        string = '''
        MACHINE EMPTY
        INVARIANT  1<2
        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 isinstance(root.children[1], AInvariantMachineClause)
        assert interpret(root.children[1], env)
Пример #24
0
    def test_genAST_sub_pre2(self):
        # Build AST
        string = '''
        MACHINE Test
        VARIABLES xx
        INVARIANT xx:NAT 
        INITIALISATION BEGIN xx:=1; PRE 1+1=3 THEN xx:=2 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
        try:
            arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT
        except INITNotPossibleException:
            pass
Пример #25
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)])
Пример #26
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)])
Пример #27
0
    def test_library_split(self):
        string = '''
        MACHINE LibraryStrings
        CONSTANTS split
        PROPERTIES
          /* split a string according to a delimiter string into a sequence of strings */
		  split: STRING * STRING --> (INTEGER<->STRING) & 
		  split = %(x,y).(x:STRING & y:STRING|STRING_SPLIT(x,y)) 
        DEFINITIONS
		  STRING_SPLIT(x,y) == split(x,y);
		  EXTERNAL_FUNCTION_STRING_SPLIT == ((STRING*STRING) --> (INTEGER<->STRING));
        ASSERTIONS
		  split("filename.ext",".") = ["filename","ext"];
		  split("filename.ext","/") = ["filename.ext"];
		  split("/usr/local/lib","/") = ["","usr","local","lib"];
		  split("/","/") = ["",""];
		  split("abcabc","bc") = ["a","a",""]
        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()
        dh = DefinitionHandler(env, str_ast_to_python_ast)                                   
        dh.repl_defs(root)
        mch = parse_ast(root, env)
        type_check_bmch(root, env, mch)
        assert isinstance(get_type_by_name(env, "split"), PowerSetType)
        assert isinstance(get_type_by_name(env, "split").data, CartType)
        assert isinstance(get_type_by_name(env, "split").data.left.data, CartType)
        assert isinstance(get_type_by_name(env, "split").data.left.data.left.data, StringType)
        assert isinstance(get_type_by_name(env, "split").data.left.data.right.data, StringType)
        assert isinstance(get_type_by_name(env, "split").data.right.data.data, CartType)  
        assert isinstance(get_type_by_name(env, "split").data.right.data.data.left.data, IntegerType)
        assert isinstance(get_type_by_name(env, "split").data.right.data.data.right.data, StringType)      
        arbitrary_init_machine(root, env, mch) # init VARIABLES and eval INVARIANT 
        assert isinstance(root.children[4], AAssertionsMachineClause)
        interpret(root.children[4], env)
Пример #28
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"
        
Пример #29
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)
Пример #30
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