def test_or1():
circuit = Circuit('OR',False, True)
assert circuit.eval({}) == True
def test_or2():
circuit = Circuit('OR',False, 'x')
assert circuit.eval({'x' : True}) == True
def test_and1():
circuit = Circuit('AND', True, 'x')
assert circuit.eval({'x' : True}) == True
assert circuit.eval({'x' : False}) == False
def test_not1():
circuit = Circuit('NOT', 'x')
assert circuit.eval({'x' : True}) == False
assert circuit.eval({'x' : False}) == True
def test_demorgan():
circuit1 = Circuit('NOT', Circuit('AND','x','y'))
circuit2 = Circuit('OR', Circuit('NOT','x'), Circuit('NOT','y'))
for x,y in [(False, False), (False, True), (True, False), (True, True)]:
assert circuit1.eval({'x': x, 'y': y}) == \
circuit2.eval({'x': x, 'y': y})
def test_xor():
circuit = Circuit('AND', Circuit('OR','x','y'),
Circuit('OR', Circuit('NOT','x'), Circuit('NOT','y')))
assert circuit.eval({'x' : True, 'y' : True})==False
assert circuit.eval({'x' : True, 'y' : False})==True
def test_list():
nodes = 256
circuit=Circuit('NOT', f'y{nodes-1}')
for j in range(0,nodes-1):
circuit = Circuit('OR', f'y{j}', circuit)
assert circuit.eval({f'y{j}' : False for j in range(nodes)}) == True
assert circuit.eval({f'y{j}' : (j>=nodes-1) for j in range(nodes)}) == False
def test_tree1():
circuit=tree(2)
assert circuit == Circuit('AND',
Circuit('AND', Circuit('NOT', 'x0'),
Circuit('NOT', 'x1')),
Circuit('AND', Circuit('NOT', 'x2'),
Circuit('NOT', 'x3')))
def test_tree2():
levels = 8
circuit = tree(levels)
assert circuit.eval({f'x{j}' : False for j in range(2**levels)})
assert not circuit.eval({f'x{j}' : (j<1) for j in range(2**levels)})
import pytest
# undefined variable
def test_or4():
circuit = Circuit('OR','x', True )
with pytest.raises(RuntimeError) as e:
circuit.eval({}) == True
# bad operator
def test_and5():
circuit = Circuit('MAYBE','x', True )
with pytest.raises(ValueError) as e:
circuit.eval({}) == True