def test_infix(debug=False): if debug: print("Testing infix of formula 'x12'") assert Formula('x12').infix() == 'x12' if debug: print("Testing infix of formula '(p|p)'") assert Formula('|',Formula('p'),Formula('p')).infix() == '(p|p)' if debug: print("Testing infix of formula '~(p&q7)'") assert Formula('~', Formula('&', Formula('p'), Formula('q7'))).infix() == '~(p&q7)'
def test_prefix(debug=False): if debug: print("Testing prefix of formula 'x12'") assert Formula('x12').prefix() == 'x12' if debug: print("Testing prefix of formula '|pp' (in infix: '(p|p)')") assert Formula('|',Formula('p'),Formula('p')).prefix() == '|pp' if debug: print("Testing prefix of formula '~&pq7' (in infix: '~(p&q7)')") assert Formula('~', Formula('&', Formula('p'), Formula('q7'))).prefix() == '~&pq7'
def test_infix_all_operators(debug=False): for binary_operator in ['->', '<->', '-&', '-|']: infix = '(p' + binary_operator + 'q)' if debug: print("Testing infix of formula '" + infix + "'") assert Formula(binary_operator, Formula('p'), Formula('q')).infix() == infix if debug: print("Testing infix of formula '(p?q:r)'") assert Formula('?:', Formula('p'), Formula('q'), Formula('r')).infix() == '(p?q:r)'
def test_prefix_all_operators(debug=False): for binary_operator in ['->', '<->', '-&', '-|']: infix = '(p' + binary_operator + 'q)' prefix = binary_operator + 'pq' if debug: print("Testing prefix of formula '" + prefix + "' (in infix: '" + infix + "')") assert Formula(binary_operator, Formula('p'), Formula('q')).prefix() == prefix if debug: print("Testing prefix of formula '?:pqr' (in infix: '(p?q:r)')") assert Formula('?:', Formula('p'), Formula('q'), Formula('r')).prefix() == '?:pqr'
################################################## # FILE: testFile.py # WRITER : Erez Kaufman, erez, 305605255. # EXERCISE : intro2cs ex# 2015-2016 # DESCRIPTION : # ################################################## from propositions.syntax import Formula if __name__ == '__main__': q = Formula('q') z = Formula('z') nz = Formula('~', z) f = Formula('&', q, z) t = Formula('~', f) s = '(((~(y10|F)|T)&(F|T))|s2)' prefixString = '~|~~&pq2&pq5' q = Formula('|', t, f) infixTest = Formula.from_infix(s) print(infixTest) # h = Formula(t,q,f) q.from_infix('~(~~(q&r4)|(q&r6))') # if q.from_prefix(prefixString ).prefix() == prefixString : # print(q.from_prefix(prefixString )) # print(q.from_prefix(prefixString ).prefix()) # print("variables of prefix:", q.from_prefix(prefixString ).variables()) # # print('variables of q: ', q.variables()) # # exit(0)