Example #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
Example #2
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
Example #3
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')
Example #4
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
Example #5
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
Example #6
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
Example #7
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
Example #8
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')
Example #9
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()
Example #10
0
def test_funcion_abs():
    b = ast_parse.parse(test1).body
    p = Path(b, source=test1)
    pg = PathGroup(p)

    pg.explore()

    assert len(pg.completed) == 1
    assert pg.completed[0].state.any_int('a') == 10
    assert pg.completed[0].state.any_int('b') == 10
    assert pg.completed[0].state.any_int('c') == 10
    assert pg.completed[0].state.any_int('d') == 10
def test_function_String_rstrip():
    b = ast_parse.parse(test1).body
    p = Path(b, source=test1)
    pg = PathGroup(p)

    pg.explore()
    assert len(pg.completed) == 1

    assert pg.completed[0].state.any_str('x') == "test ".rstrip()
    assert pg.completed[0].state.any_str('y') == "test".rstrip()
    assert pg.completed[0].state.any_str('z') == "test".rstrip("t")
    assert pg.completed[0].state.any_str('d') == "test".rstrip("st")
def test_function_String_rstrip_statesplit():
    b = ast_parse.parse(test2).body
    p = Path(b, source=test2)
    pg = PathGroup(p)

    pg.explore()
    assert len(pg.completed) == 8
    o = [p.state.any_str('x') for p in pg.completed]
    o.sort()
    assert o == [
        'test', 'test1', 'test1', 'test1', 'test1', 'test1', 'test1', 'test1'
    ]
Example #13
0
def test_pyObjectManager_BitVec_isStatic():
    b = ast_parse.parse(test1).body
    p = Path(b, source=test1)
    pg = PathGroup(p)

    pg.explore()
    assert len(pg.completed) == 1

    x = pg.completed[0].state.getVar('x')

    assert x.isStatic()
    assert x.getValue() == 5
Example #14
0
def test_pySym_funcInWhileTest():
    b = ast_parse.parse(test2).body
    p = Path(b, source=test2)
    pg = PathGroup(p)
    pg.explore()

    assert len(pg.active) == 0
    assert len(pg.completed) == 1
    assert len(pg.errored) == 0
    assert len(pg.deadended) == 6

    assert pg.completed[0].state.any_int('x') == 5
Example #15
0
def test_pySym_callThreeArgs():
    b = ast_parse.parse(test2).body
    p = Path(b,source=test2)
    pg = PathGroup(p)
    
    pg.explore()
    
    assert len(pg.completed) == 1
    
    assert pg.completed[0].state.any_int('x') == 1
    assert pg.completed[0].state.any_int('y') == 1
    assert pg.completed[0].state.any_list('l') == [1,2.2,3.5]
Example #16
0
def test_pyState_SubscriptSlice():
    b = ast_parse.parse(test6).body
    p = Path(b,source=test6)
    pg = PathGroup(p)
    
    pg.explore()
    assert len(pg.completed) == 1
    assert pg.completed[0].state.any_list('x') == [1, 3.1415, 123]

    b = ast_parse.parse(test7).body
    p = Path(b,source=test7)
    pg = PathGroup(p)
    
    pg.explore()
    assert len(pg.completed) == 1
    assert pg.completed[0].state.any_list('x') == [123, 4, 3.1415, 2.2, 1]

    b = ast_parse.parse(test8).body
    p = Path(b,source=test8)
    pg = PathGroup(p)

    pg.explore()
    assert len(pg.completed) == 1
    assert pg.completed[0].state.any_list('x') == [1,2.2,3.1415,4,123,8,[1,2,3]]

    b = ast_parse.parse(test9).body
    p = Path(b,source=test9)
    pg = PathGroup(p)

    pg.explore()
    assert len(pg.completed) == 1
    assert pg.completed[0].state.any_list('x') == [1,2.2,3.1415,4,123,8,[1,2,3]][::-1]

    b = ast_parse.parse(test10).body
    p = Path(b,source=test10)
    pg = PathGroup(p)

    pg.explore()
    assert len(pg.completed) == 1
    assert pg.completed[0].state.any_list('x') == [2.2, 4, 8]
def test_function_String_zfill_statesplit():
    b = ast_parse.parse(test2).body
    p = Path(b,source=test2)
    pg = PathGroup(p)

    pg.explore()

    # Every index should be a possibility
    assert len(pg.completed) == 8

    o = [p.state.any_str('y') for p in pg.completed]
    o.sort()
    assert o == ['000test', '00test', '0test', 'test', 'test', 'test', 'test', 'test']
def test_function_pyState_BVS_basic():
    b = ast_parse.parse(test1).body
    p = Path(b, source=test1)
    pg = PathGroup(p)

    pg.explore()
    assert len(pg.completed) == 1

    s1 = pg.completed[0].state.getVar('s1')
    s2 = pg.completed[0].state.getVar('s2')

    # Make sure that we're not duplicating variables on creation
    assert str(s1.getZ3Object()) != str(s2.getZ3Object())
Example #19
0
def test_function_pyState_String():
    b = ast_parse.parse(test1).body
    p = Path(b, source=test1)
    pg = PathGroup(p)

    pg.explore()
    assert len(pg.completed) == 1

    s = pg.completed[0].state.getVar('s')
    s2 = pg.completed[0].state.getVar('s2')

    assert len(s) == 32
    assert len(s2) == Z3_MAX_STRING_LENGTH
Example #20
0
def test_pyState_ListComp_MultipleFor():
    b = ast_parse.parse(test4).body
    p = Path(b, source=test4)
    pg = PathGroup(p)

    pg.explore()
    assert len(pg.completed) == 1
    assert pg.completed[0].state.any_list('l') == [
        x for x in [1, 2, 3, 4, 5] for y in [1, 2, 3]
    ]

    with pytest.raises(Exception):
        pg.completed[0].state.any_int('x')
def test_function_pyState_BVV_basic():
    b = ast_parse.parse(test1).body
    p = Path(b,source=test1)
    pg = PathGroup(p)

    pg.explore()
    assert len(pg.completed) == 1
    
    x = pg.completed[0].state.getVar('x')
    y = pg.completed[0].state.getVar('y')

    assert x.isStatic() and x.mustBe(1) and x.size == 1
    assert y.isStatic() and y.mustBe(1) and y.size == 1024
Example #22
0
def test_assignInt():
    b = ast_parse.parse(test1).body
    p = Path(b, source=test1)
    pg = PathGroup(p)
    pg.explore()
    assert len(pg.completed) == 1

    s = pg.completed[0].state
    # Basic dict checks
    assert type(s.getVar("x")) == Int
    assert s.getVar("x").getZ3Object().is_int()
    #assert len(s.solver.assertions()) == 1

    # Try solving it to ensure that works correctly
    assert s.isSat()

    # Ensure we're getting expected output
    assert s.any_int('x') == 1

    # Try assigning again
    b = ast_parse.parse(test2).body
    p = Path(b, source=test2)
    pg = PathGroup(p)
    pg.explore()
    assert len(pg.completed) == 1

    s = pg.completed[0].state

    # Basic dict checks
    assert type(s.getVar("x")) == Int
    assert s.getVar("x").getZ3Object().is_int()
    #assert len(s.solver.assertions()) == 2

    # Try solving it to ensure that works correctly
    assert s.isSat()

    # Ensure we're getting expected output
    assert s.any_int('x') == 2
Example #23
0
def test_pySym_BinOp_StrConcat():
    b = ast_parse.parse(test10).body
    p = Path(b, source=test10)
    pg = PathGroup(p)
    pg.explore()

    assert len(pg.completed) == 1
    assert pg.completed[0].state.any_str('s') == "test" + " blerg"

    assert len(pg.completed[0].state.any_str('s2')) == 8 + len(" blerg")
    assert pg.completed[0].state.any_str('s2').endswith(" blerg")

    assert pg.completed[0].state.any_str(
        's3') == "This " + "is " + "a " + "test"
Example #24
0
def test_pyPath_stepThroughProgram():
    b = ast_parse.parse(test1).body
    p = Path(b, source=test1)
    pg = PathGroup(p)
    pg.explore()

    assert len(pg.active) == 0
    assert len(pg.completed) == 1
    assert len(pg.errored) == 0
    assert len(pg.deadended) == 0
    assert len(pg.found) == 0

    assert pg.completed[0].state.any_int('x') == 10
    assert pg.completed[0].state.any_int('z') == 1
Example #25
0
def test_function_chr_symbolic_input():
    b = ast_parse.parse(test2).body
    p = Path(b, source=test2)
    pg = PathGroup(p)
    pg.explore()

    assert len(pg.completed) == 1

    i = pg.completed[0].state.getVar('i')
    c = pg.completed[0].state.getVar('c')

    # Set i, check that it propagates to c.
    i.setTo(ord('X'))
    assert str(c) == "X"
Example #26
0
def test_pyObjectManager_Int_PostSet():
    b = ast_parse.parse(test5).body
    p = Path(b,source=test5)
    pg = PathGroup(p)
    
    pg.explore()
    assert len(pg.completed) == 1

    for q in range(6):
        s = pg.completed[0].state.copy()
        x = s.getVar('x')
        i = s.getVar('i')
        i.setTo(q)
        assert x.getValue() == q
Example #27
0
def test_pySym_simpleWhile():
    b = ast_parse.parse(test1).body
    p = Path(b, source=test1)
    pg = PathGroup(p)
    assert pg.explore(find=6)

    assert len(pg.active) == 0
    assert len(pg.completed) == 0
    assert len(pg.errored) == 0
    assert len(pg.deadended) == 11
    assert len(pg.found) == 1

    assert pg.found[0].state.isSat()
    assert pg.found[0].state.any_int('x') == 10
Example #28
0
def test_longer_one():
    b = ast_parse.parse(test).body
    p = Path(b,source=test)
    pg = PathGroup(p)

    pg.explore()

    assert len(pg.completed) == 1

    assert pg.completed[0].state.any_list('CRC_POLY') == [1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1]

    assert pg.completed[0].state.any_list('INNER') == [0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0]

    assert pg.completed[0].state.any_list('PLAIN_1_BITS') == [0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0]
Example #29
0
def test_function_zip():
    b = ast_parse.parse(test1).body
    p = Path(b,source=test1)
    pg = PathGroup(p)

    pg.explore()
    assert len(pg.completed) == 1
    
    assert pg.completed[0].state.any_list('l') == [[1, 4], [2, 5], [3, 6]]
    assert pg.completed[0].state.any_list('l3') == [[1, 4], [2, 5], [3, 6]]
    assert pg.completed[0].state.any_list('l6') == [["1", "4"], ["2", "5"], ["3", "6"]]
    assert pg.completed[0].state.any_list('l7') == [["1","5"], ["2","6"]]
    assert pg.completed[0].state.any_list('l8') == [["1","3"], ["2","4"]] 
    assert pg.completed[0].state.any_list('l9') == [[1,"a"],[2,"b"],[3,"c"]]
def test_function_String_zfill_static():
    b = ast_parse.parse(test1).body
    p = Path(b,source=test1)
    pg = PathGroup(p)

    pg.explore()

    # Every index should be a possibility
    assert len(pg.completed) == 1

    assert pg.completed[0].state.any_str('x') == "test"
    assert pg.completed[0].state.any_str('y') == "0test"
    assert pg.completed[0].state.any_str('z') == "test"
    assert pg.completed[0].state.any_str('d') == "000000test"
Example #31
0
def test_pyPathGroup_exploreFunctionCompare():
    b = ast_parse.parse(test3).body
    p = Path(b, source=test3)
    pg = PathGroup(p)

    # Execute to the end
    assert pg.explore(find=10)

    assert len(pg.active) == 0
    assert len(pg.completed) == 0
    assert len(pg.errored) == 0
    assert len(pg.deadended) == 1
    assert len(pg.found) == 1

    assert pg.found[0].state.any_int('x') == 1
Example #32
0
def test_pySym_AugAssign_MultipleStates():
    b = ast_parse.parse(test13).body
    p = Path(b,source=test13)
    pg = PathGroup(p)
    pg.explore()

    # There should be 10 possible states here
    assert len(pg.completed) == 10
    
    # Get the output states
    rets = []
    for p in pg.completed:
        rets.append(p.state.any_int('x'))
    
    assert set(rets) == set([1+x for x in range(10)])
Example #33
0
def test_pyObjectManager_Char_isStatic():
    b = ast_parse.parse(test1).body
    p = Path(b, source=test1)
    pg = PathGroup(p)

    pg.explore()
    assert len(pg.completed) == 1

    s = pg.completed[0].state.getVar('s')
    d = pg.completed[0].state.getVar('d')

    assert s[0].isStatic()
    assert s[0].getValue() == "T"

    assert not d.isStatic()
Example #34
0
def test_pyPathGroup_exploreWithIf():
    b = ast_parse.parse(test2).body
    p = Path(b, source=test2)
    pg = PathGroup(p)

    # Explore to the end
    assert pg.explore(find=15)

    assert len(pg.active) == 0
    assert len(pg.completed) == 0
    assert len(pg.errored) == 0
    assert len(pg.deadended) == 2
    assert len(pg.found) == 1

    assert pg.found[0].state.any_int('x') == 1337
Example #35
0
def test_function_random_randint_basic():
    b = ast_parse.parse(test1).body
    p = Path(b, source=test1)
    pg = PathGroup(p)

    pg.explore()
    assert len(pg.completed) == 1

    s = pg.completed[0].state.copy()
    r = s.getVar('r')

    assert r.canBe(1337)
    assert r.canBe(31337)
    assert not r.canBe(31338)
    assert not r.canBe(1336)
def test_function_String_Index():
    b = ast_parse.parse(test1.format("T")).body
    p = Path(b, source=test1.format("T"))
    pg = PathGroup(p)

    pg.explore()
    assert len(pg.completed) == 1

    assert pg.completed[0].state.any_int('x') == 0

    b = ast_parse.parse(test1.format("t")).body
    p = Path(b, source=test1.format("t"))
    pg = PathGroup(p)

    pg.explore()
    assert len(pg.completed) == 1

    assert pg.completed[0].state.any_int('x') == 3

    b = ast_parse.parse(test1.format("es")).body
    p = Path(b, source=test1.format("es"))
    pg = PathGroup(p)

    pg.explore()
    assert len(pg.completed) == 1

    assert pg.completed[0].state.any_int('x') == 1

    b = ast_parse.parse(test1.format("st")).body
    p = Path(b, source=test1.format("st"))
    pg = PathGroup(p)

    pg.explore()
    assert len(pg.completed) == 1

    assert pg.completed[0].state.any_int('x') == 2
Example #37
0
def test_any_n_real():
    b = ast_parse.parse(test3).body
    p = Path(b, source=test3)
    pg = PathGroup(p)

    pg.explore()

    assert len(pg.completed) == 1
    assert pg.completed[0].state.any_real('x') == 3.1415
    # Duplicate test to ensure we're not destroying state
    assert pg.completed[0].state.any_n_real('x', 10) == [3.1415]
    assert pg.completed[0].state.any_n_real('x', 10) == [3.1415]

    b = ast_parse.parse(test5).body
    p = Path(b, source=test5)
    pg = PathGroup(p)

    pg.explore()

    assert len(pg.completed) == 1
    assert pg.completed[0].state.any_real('x') != None
    # Duplicate test to ensure we're not destroying state
    assert len(pg.completed[0].state.any_n_real('x', 10)) == 10
    assert len(pg.completed[0].state.any_n_real('x', 10)) == 10
Example #38
0
def test_any_n_int():
    b = ast_parse.parse(test1).body
    p = Path(b, source=test1)
    pg = PathGroup(p)

    pg.explore()

    assert len(pg.completed) == 1
    assert pg.completed[0].state.any_int('x') == 1
    # Duplicate test to ensure we're not destroying state
    assert pg.completed[0].state.any_n_int('x', 10) == [1]
    assert pg.completed[0].state.any_n_int('x', 10) == [1]

    b = ast_parse.parse(test4).body
    p = Path(b, source=test4)
    pg = PathGroup(p)

    pg.explore()

    assert len(pg.completed) == 1
    assert pg.completed[0].state.any_int('x') != None
    # Duplicate test to ensure we're not destroying state
    assert len(pg.completed[0].state.any_n_int('x', 10)) == 10
    assert len(pg.completed[0].state.any_n_int('x', 10)) == 10
def test_pyObjectManager_String_canBe():
    b = ast_parse.parse(test1).body
    p = Path(b, source=test1)
    pg = PathGroup(p)

    pg.explore()
    assert len(pg.completed) == 1

    s = pg.completed[0].state.getVar('s')

    assert s.canBe("Test")
    assert not s.canBe("test")
    assert s[0:1].canBe("T")
    assert not s[0:2].canBe("T3")
    assert s[:3].canBe("Tes")
Example #40
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
Example #41
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
Example #42
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