Ejemplo n.º 1
0
def test_simpleIf():
    b = ast_parse.parse(simpleIf).body
    p = Path(b,source=simpleIf)
    # Step through the "if" statement
    p = p.step()[0]
    p = p.step()[0]
    p2 = p.step()
    ifSide = p2[0]
    elseSide = p2[1]
    
    # ifSide's path should now be inside, meaning only the print statement
    assert len(ifSide.state.path) == 1
    # Else should be in the else statement
    assert len(elseSide.state.path) == 2
    
    # Neither have anything to do after the if statement
    assert len(ifSide.state.callStack) == 1
    assert len(elseSide.state.callStack) == 0
    
    # If side should not be possible
    assert not ifSide.state.isSat()
    assert elseSide.state.isSat()
    
    # Track expected number of assertions
    #assert len(ifSide.state.solver.assertions()) == 3
    #assert len(elseSide.state.solver.assertions()) == 3
    
    # Make sure the answer makes sense
    assert ifSide.state.any_int('x') == None
    assert elseSide.state.any_int('x') == 1
Ejemplo n.º 2
0
def test_simpleIf():
    b = ast_parse.parse(simpleIf).body
    p = Path(b, source=simpleIf)
    # Step through the "if" statement
    p = p.step()[0]
    p = p.step()[0]
    p2 = p.step()
    ifSide = p2[0]
    elseSide = p2[1]

    # ifSide's path should now be inside, meaning only the print statement
    assert len(ifSide.state.path) == 1
    # Else should be in the else statement
    assert len(elseSide.state.path) == 2

    # Neither have anything to do after the if statement
    assert len(ifSide.state.callStack) == 1
    assert len(elseSide.state.callStack) == 0

    # If side should not be possible
    assert not ifSide.state.isSat()
    assert elseSide.state.isSat()

    # Track expected number of assertions
    #assert len(ifSide.state.solver.assertions()) == 3
    #assert len(elseSide.state.solver.assertions()) == 3

    # Make sure the answer makes sense
    assert ifSide.state.any_int('x') == None
    assert elseSide.state.any_int('x') == 1
Ejemplo n.º 3
0
def test_pySym_returnToAssign():
    # Testing that we can return a function to a variable
    b = ast_parse.parse(test5).body
    p = Path(b,source=test5)
    p = p.step()[0]
    p = p.step()[0]
    p = p.step()[0]
    p = p.step()[0]
    p = p.step()[0]
    p = p.step()[0]
    assert p.state.any_int('x') == 5
Ejemplo n.º 4
0
def test_ifReturn():
    b = ast_parse.parse(testIfReturn).body
    p = Path(b,source=testIfReturn)
    p = p.step()[0].step()[0]
    ifSide,elseSide = p.step()
    ifSide = ifSide.step()[0].step()[0]
    elseSide = elseSide.step()[0].step()[0]
    
    assert ifSide.state.any_int('x') == 2
    assert ifSide.state.any_real('y') == 2.2
    assert elseSide.state.any_int('x') == None
    assert elseSide.state.any_real('y') == None # Else side is wonky because it's not a possible path
Ejemplo n.º 5
0
def test_ifReturn():
    b = ast_parse.parse(testIfReturn).body
    p = Path(b, source=testIfReturn)
    p = p.step()[0].step()[0]
    ifSide, elseSide = p.step()
    ifSide = ifSide.step()[0].step()[0]
    elseSide = elseSide.step()[0].step()[0]

    assert ifSide.state.any_int('x') == 2
    assert ifSide.state.any_real('y') == 2.2
    assert elseSide.state.any_int('x') == None
    assert elseSide.state.any_real(
        'y') == None  # Else side is wonky because it's not a possible path
Ejemplo n.º 6
0
def test_pySym_AugAssign_MixedTypes():
    #######
    # Add #
    #######
    b = ast_parse.parse(test2).body
    p = Path(b,source=test2)
    # Step through program
    p = p.step()[0]
    p = p.step()[0]
    ifSide,elseSide = p.step()
    elseSide = elseSide.step()[0]

    assert elseSide.state.isSat()
    assert elseSide.state.any_real('x') == 9.0
    assert elseSide.state.objectManager.getVar('x',ctx=elseSide.state.ctx).getZ3Object().is_real()
Ejemplo n.º 7
0
def test_pySym_pass():
    # Testing we can pass :-)
    b = ast_parse.parse(test1).body
    p = Path(b,source=test1)
    p = p.step()[0]
    p = p.step()[0]
    
    assert p.state.any_int('x') == 4
    assert len(p.state.backtrace) == 2
Ejemplo n.º 8
0
def test_pathCopy():
    b = ast_parse.parse(test1).body
    p = Path(b, source=test1)
    p2 = p.copy()
    assert p2 != p
    p = p.step()[0]
    assert p.state.any_int('x') == 1
    with pytest.raises(Exception):
        p2.state.any_int('x')
Ejemplo n.º 9
0
def test_pySym_FuncionDef():
    b = ast_parse.parse(test1).body
    p = Path(b,source=test1)
    # Step through program
    p = p.step()[0]
    p = p.step()[0]
    # We should have two defs now
    assert "test" in p.state.functions
    assert "test2" in p.state.functions
Ejemplo n.º 10
0
def test_pySym_FuncionDef():
    b = ast_parse.parse(test1).body
    p = Path(b, source=test1)
    # Step through program
    p = p.step()[0]
    p = p.step()[0]
    # We should have two defs now
    assert "test" in p.state.functions
    assert "test2" in p.state.functions
Ejemplo n.º 11
0
def test_pySym_pass():
    # Testing we can pass :-)
    b = ast_parse.parse(test1).body
    p = Path(b, source=test1)
    p = p.step()[0]
    p = p.step()[0]

    assert p.state.any_int('x') == 4
    assert len(p.state.backtrace) == 2
Ejemplo n.º 12
0
def test_pathCopy():
    b = ast_parse.parse(test1).body
    p = Path(b,source=test1)
    p2 = p.copy()
    assert p2 != p
    p = p.step()[0]
    assert p.state.any_int('x') == 1
    with pytest.raises(Exception):
        p2.state.any_int('x')
Ejemplo n.º 13
0
def test_mixedTypes():
    b = ast_parse.parse(test3).body
    p = Path(b, source=test3)
    # Step through program
    p = p.step()[0]
    p = p.step()[0]
    print(p.state.solver)
    print(p.state.isSat())
    assert p.state.isSat()
    assert p.state.any_real('x') == 5.5
Ejemplo n.º 14
0
def test_mixedTypes():
    b = ast_parse.parse(test3).body
    p = Path(b,source=test3)
    # Step through program
    p = p.step()[0]
    p = p.step()[0]
    print(p.state.solver)
    print(p.state.isSat())
    assert p.state.isSat()
    assert p.state.any_real('x') == 5.5
Ejemplo n.º 15
0
def test_basicPathStep():
    b = ast_parse.parse(test1).body
    p1 = Path(b,source=test1)
    p2 = p1.step()[0].step()[0]
    p1.printBacktrace()
    p2.printBacktrace()
    assert p2.state.any_int('x') == 1
    assert p2.state.any_int('y') == 2
    with pytest.raises(Exception):
        p1.state.any_int('x')
    with pytest.raises(Exception):
        p1.state.any_int('y')
Ejemplo n.º 16
0
def test_basicPathStep():
    b = ast_parse.parse(test1).body
    p1 = Path(b, source=test1)
    p2 = p1.step()[0].step()[0]
    p1.printBacktrace()
    p2.printBacktrace()
    assert p2.state.any_int('x') == 1
    assert p2.state.any_int('y') == 2
    with pytest.raises(Exception):
        p1.state.any_int('x')
    with pytest.raises(Exception):
        p1.state.any_int('y')
Ejemplo n.º 17
0
def test_pySym_AugAssign():
    #######
    # Add #
    #######
    b = ast_parse.parse(test1.format("+=")).body
    p = Path(b,source=test1.format("+="))
    # Step through program
    p = p.step()[0]
    p = p.step()[0]
    ifSide,elseSide = p.step()
    elseSide = elseSide.step()[0]
    
    assert elseSide.state.isSat()
    assert elseSide.state.any_int('x') == 14

    ############
    # Subtract #
    ############
    b = ast_parse.parse(test1.format("-=")).body
    p = Path(b,source=test1.format("-="))
    # Step through program
    p = p.step()[0]
    p = p.step()[0]
    ifSide,elseSide = p.step()
    elseSide = elseSide.step()[0]

    assert elseSide.state.isSat()
    assert elseSide.state.any_int('x') == 0

    ############
    # Multiply #
    ############
    b = ast_parse.parse(test1.format("*=")).body
    p = Path(b,source=test1.format("*="))
    # Step through program
    p = p.step()[0]
    p = p.step()[0]
    ifSide,elseSide = p.step()
    elseSide = elseSide.step()[0]

    assert elseSide.state.isSat()
    assert elseSide.state.any_int('x') == 49

    ##########
    # Divide #
    ##########
    b = ast_parse.parse(test1.format("/=")).body
    p = Path(b,source=test1.format("/="))
    # Step through program
    p = p.step()[0]
    p = p.step()[0]
    ifSide,elseSide = p.step()
    elseSide = elseSide.step()[0]

    assert elseSide.state.isSat()
    assert elseSide.state.any_int('x') == 1

    ##########
    # Modulo #
    ##########
    b = ast_parse.parse(test1.format("%=")).body
    p = Path(b,source=test1.format("%="))
    # Step through program
    p = p.step()[0]
    p = p.step()[0]
    ifSide,elseSide = p.step()
    elseSide = elseSide.step()[0]

    assert elseSide.state.isSat()
    assert elseSide.state.any_int('x') == 0
Ejemplo n.º 18
0
def test_pySym_Compare():
    ################
    # Greater Than #
    ################
    b = ast_parse.parse(compare1.format(1,5,">")).body
    p = Path(b,source=compare1.format(1,5,">"))
    # Step through the "if" statement
    p = p.step()[0]
    p = p.step()[0]
    p2 = p.step()
    ifSide = p2[0]
    elseSide = p2[1]

    # If side should not be possible
    assert not ifSide.state.isSat()
    assert elseSide.state.isSat()

    # Track expected number of assertions
    #assert len(ifSide.state.solver.assertions()) == 3
    #assert len(elseSide.state.solver.assertions()) == 3

    # Make sure the answer makes sense
    assert ifSide.state.any_int('x') == None
    assert elseSide.state.any_int('x') == 1

    #########################
    # Greater Than Or Equal #
    #########################
    b = ast_parse.parse(compare1.format(2,2,">=")).body
    p = Path(b,source=compare1.format(2,2,">="))
    # Step through the "if" statement
    p = p.step()[0]
    p = p.step()[0]
    p2 = p.step()
    ifSide = p2[0]
    elseSide = p2[1]

    # If side should be correct
    assert ifSide.state.isSat()
    assert not elseSide.state.isSat()

    # Track expected number of assertions
    #assert len(ifSide.state.solver.assertions()) == 3
    #assert len(elseSide.state.solver.assertions()) == 3

    # Make sure the answer makes sense
    assert ifSide.state.any_int('x') == 2
    assert elseSide.state.any_int('x') == None

    #############
    # Less Than #
    #############
    b = ast_parse.parse(compare1.format(1,5,"<")).body
    p = Path(b,source=compare1.format(1,5,"<"))
    # Step through the "if" statement
    p = p.step()[0]
    p = p.step()[0]
    p2 = p.step()
    ifSide = p2[0]
    elseSide = p2[1]

    # If side should be correct
    assert ifSide.state.isSat()
    assert not elseSide.state.isSat()

    # Track expected number of assertions
    #assert len(ifSide.state.solver.assertions()) == 3
    #assert len(elseSide.state.solver.assertions()) == 3

    # Make sure the answer makes sense
    assert ifSide.state.any_int('x') == 1
    assert elseSide.state.any_int('x') == None

    ######################
    # Less Than Or Equal #
    ######################
    b = ast_parse.parse(compare1.format(3,5,"<=")).body
    p = Path(b,source=compare1.format(3,5,"<="))
    # Step through the "if" statement
    p = p.step()[0]
    p = p.step()[0]
    p2 = p.step()
    ifSide = p2[0]
    elseSide = p2[1]
    
    # If side should be correct
    assert ifSide.state.isSat()
    assert not elseSide.state.isSat()

    # Track expected number of assertions
    #assert len(ifSide.state.solver.assertions()) == 3
    #assert len(elseSide.state.solver.assertions()) == 3

    # Make sure the answer makes sense
    assert ifSide.state.any_int('x') == 3
    assert elseSide.state.any_int('x') == None

    #########
    # Equal #
    #########
    b = ast_parse.parse(compare1.format(1,5,"==")).body
    p = Path(b,source=compare1.format(1,5,"=="))
    # Step through the "if" statement
    p = p.step()[0]
    p = p.step()[0]
    p2 = p.step()
    ifSide = p2[0]
    elseSide = p2[1]

    # If side should not be correct
    assert not ifSide.state.isSat()
    assert elseSide.state.isSat()

    # Track expected number of assertions
    #assert len(ifSide.state.solver.assertions()) == 3
    #assert len(elseSide.state.solver.assertions()) == 3

    # Make sure the answer makes sense
    assert ifSide.state.any_int('x') == None
    assert elseSide.state.any_int('x') == 1

    #############
    # Not Equal #
    #############
    b = ast_parse.parse(compare1.format(1,5,"!=")).body
    p = Path(b,source=compare1.format(1,5,"!="))
    # Step through the "if" statement
    p = p.step()[0]
    p = p.step()[0]
    p2 = p.step()
    ifSide = p2[0]
    elseSide = p2[1]

    # If side should be correct
    assert ifSide.state.isSat()
    assert not elseSide.state.isSat()

    # Track expected number of assertions
    #assert len(ifSide.state.solver.assertions()) == 3
    #assert len(elseSide.state.solver.assertions()) == 3

    # Make sure the answer makes sense
    assert ifSide.state.any_int('x') == 1
    assert elseSide.state.any_int('x') == None
Ejemplo n.º 19
0
def test_pySym_BinOp():
    #######
    # Add #
    #######
    b = ast_parse.parse(test1.format("+")).body
    p = Path(b, source=test1.format("+"))
    # Step through program
    p = p.step()[0]
    p = p.step()[0]
    p = p.step()[0]

    assert p.state.isSat()
    assert p.state.any_real('x') == 4.0

    #######
    # Sub #
    #######
    b = ast_parse.parse(test1.format("-")).body
    p = Path(b, source=test1.format("-"))
    # Step through program
    p = p.step()[0]
    p = p.step()[0]
    p = p.step()[0]

    assert p.state.isSat()
    assert p.state.any_real('x') == -1.0

    #######
    # Mul #
    #######
    b = ast_parse.parse(test1.format("*")).body
    p = Path(b, source=test1.format("*"))
    # Step through program
    p = p.step()[0]
    p = p.step()[0]
    p = p.step()[0]

    assert p.state.isSat()
    assert p.state.any_real('x') == 3.75

    #######
    # Div #
    #######
    b = ast_parse.parse(test1.format("/")).body
    p = Path(b, source=test1.format("/"))
    # Step through program
    p = p.step()[0]
    p = p.step()[0]
    p = p.step()[0]

    assert p.state.isSat()
    assert p.state.any_real('x') == 0.6

    #######
    # Mod #
    #######
    b = ast_parse.parse(test2.format("%")).body
    p = Path(b, source=test2.format("%"))
    # Step through program
    p = p.step()[0]
    p = p.step()[0]
    p = p.step()[0]

    assert p.state.isSat()
    assert p.state.any_int('x') == 2
Ejemplo n.º 20
0
def test_pySym_BinOp():
    #######
    # Add #
    #######
    b = ast_parse.parse(test1.format("+")).body
    p = Path(b,source=test1.format("+"))
    # Step through program
    p = p.step()[0]
    p = p.step()[0]
    p = p.step()[0]
    
    assert p.state.isSat()
    assert p.state.any_real('x') == 4.0

    #######
    # Sub #
    #######
    b = ast_parse.parse(test1.format("-")).body
    p = Path(b,source=test1.format("-"))
    # Step through program
    p = p.step()[0]
    p = p.step()[0]
    p = p.step()[0]

    assert p.state.isSat()
    assert p.state.any_real('x') == -1.0

    #######
    # Mul #
    #######
    b = ast_parse.parse(test1.format("*")).body
    p = Path(b,source=test1.format("*"))
    # Step through program
    p = p.step()[0]
    p = p.step()[0]
    p = p.step()[0]

    assert p.state.isSat()
    assert p.state.any_real('x') == 3.75

    #######
    # Div #
    #######
    b = ast_parse.parse(test1.format("/")).body
    p = Path(b,source=test1.format("/"))
    # Step through program
    p = p.step()[0]
    p = p.step()[0]
    p = p.step()[0]

    assert p.state.isSat()
    assert p.state.any_real('x') == 0.6

    #######
    # Mod #
    #######
    b = ast_parse.parse(test2.format("%")).body
    p = Path(b,source=test2.format("%"))
    # Step through program
    p = p.step()[0]
    p = p.step()[0]
    p = p.step()[0]

    assert p.state.isSat()
    assert p.state.any_int('x') == 2