def test_unate(): # ~c & (~a | ~b) f = truthtable([aa, bb, cc], "11100000") assert f.is_neg_unate([aa, bb, cc]) assert f.is_neg_unate([aa, bb]) assert f.is_neg_unate([aa, cc]) assert f.is_neg_unate([bb, cc]) assert f.is_neg_unate(aa) assert f.is_neg_unate(bb) assert f.is_neg_unate(cc) assert f.is_neg_unate() # c & (a | b) f = truthtable([a, b, c], "00000111") assert f.is_pos_unate([aa, bb, cc]) assert f.is_pos_unate([aa, bb]) assert f.is_pos_unate([aa, cc]) assert f.is_pos_unate([bb, cc]) assert f.is_pos_unate(aa) assert f.is_pos_unate(bb) assert f.is_pos_unate(cc) assert f.is_pos_unate() # Xor(a, b, c) f = truthtable([a, b, c], "01101001") assert f.is_binate([aa, bb, cc]) assert f.is_binate([aa, bb]) assert f.is_binate([aa, cc]) assert f.is_binate([bb, cc]) assert f.is_binate(aa) assert f.is_binate(bb) assert f.is_binate(cc)
def test_table(): assert truthtable([], [0]).is_zero() assert truthtable([], [1]).is_one() f = Xor(a, b, c, d) tt = expr2truthtable(f) assert truthtable2expr(tt).equivalent(f) assert truthtable2expr(tt, conj=True).equivalent(f) assert str(tt) == XOR_STR assert repr(tt) == XOR_STR assert tt.support == {aa, bb, cc, dd} assert tt.inputs == (aa, bb, cc, dd) assert truthtable2expr(tt.restrict({aa: 0})).equivalent(Xor(b, c, d)) assert tt.restrict({ee: 0}) == tt assert tt.satisfy_one() == {aa: 1, bb: 0, cc: 0, dd: 0} assert [p for p in tt.satisfy_all()] == [{aa: 1, bb: 0, cc: 0, dd: 0}, {aa: 0, bb: 1, cc: 0, dd: 0}, {aa: 0, bb: 0, cc: 1, dd: 0}, {aa: 1, bb: 1, cc: 1, dd: 0}, {aa: 0, bb: 0, cc: 0, dd: 1}, {aa: 1, bb: 1, cc: 0, dd: 1}, {aa: 1, bb: 0, cc: 1, dd: 1}, {aa: 0, bb: 1, cc: 1, dd: 1}] assert tt.satisfy_count() == 8 assert truthtable((a, b), "0000").satisfy_one() == None
def test_espresso(): A = bitvec('a', 16) B = bitvec('b', 16) S, C = ripple_carry_add(A, B) s0, s1, s2, s3 = espresso_exprs(S[0].to_dnf(), S[1].to_dnf(), S[2].to_dnf(), S[3].to_dnf()) assert s0.equivalent(S[0]) assert s1.equivalent(S[1]) assert s2.equivalent(S[2]) assert s3.equivalent(S[3]) X = bitvec('x', 4) f1 = truthtable(X, "0000011111------") f2 = truthtable(X, "0001111100------") f1m, f2m = espresso_tts(f1, f2) truthtable2expr(f1).equivalent(f1m) truthtable2expr(f2).equivalent(f2m)
def test_espresso(): assert espresso.FTYPE == 1 assert espresso.DTYPE == 2 assert espresso.RTYPE == 4 A = exprvars('a', 16) B = exprvars('b', 16) S, C = ripple_carry_add(A, B) s0, s1, s2, s3 = espresso_exprs(S[0].to_dnf(), S[1].to_dnf(), S[2].to_dnf(), S[3].to_dnf()) assert s0.equivalent(S[0]) assert s1.equivalent(S[1]) assert s2.equivalent(S[2]) assert s3.equivalent(S[3]) X = exprvars('x', 4) f1 = truthtable(X, "0000011111------") f2 = truthtable(X, "0001111100------") f1m, f2m = espresso_tts(f1, f2) truthtable2expr(f1).equivalent(f1m) truthtable2expr(f2).equivalent(f2m)
def test_ops(): f = truthtable([aa, bb], "0001") assert str(f) == "b a\n0 0 : 0\n0 1 : 0\n1 0 : 0\n1 1 : 1\n" assert str(~f) == "b a\n0 0 : 1\n0 1 : 1\n1 0 : 1\n1 1 : 0\n" assert str(f | 0) == "b a\n0 0 : 0\n0 1 : 0\n1 0 : 0\n1 1 : 1\n" assert str(f | 1) == "b a\n0 0 : 1\n0 1 : 1\n1 0 : 1\n1 1 : 1\n" assert str(0 | f) == "b a\n0 0 : 0\n0 1 : 0\n1 0 : 0\n1 1 : 1\n" assert str(1 | f) == "b a\n0 0 : 1\n0 1 : 1\n1 0 : 1\n1 1 : 1\n" assert str(f & 0) == "b a\n0 0 : 0\n0 1 : 0\n1 0 : 0\n1 1 : 0\n" assert str(f & 1) == "b a\n0 0 : 0\n0 1 : 0\n1 0 : 0\n1 1 : 1\n" assert str(0 & f) == "b a\n0 0 : 0\n0 1 : 0\n1 0 : 0\n1 1 : 0\n" assert str(1 & f) == "b a\n0 0 : 0\n0 1 : 0\n1 0 : 0\n1 1 : 1\n" assert str(f ^ 0) == "b a\n0 0 : 0\n0 1 : 0\n1 0 : 0\n1 1 : 1\n" assert str(f ^ 1) == "b a\n0 0 : 1\n0 1 : 1\n1 0 : 1\n1 1 : 0\n" f = truthtable([aa, bb], "01-0") assert str(~f) == "b a\n0 0 : 1\n0 1 : 0\n1 0 : -\n1 1 : 1\n" f = truthtable([aa, bb], "0011") g = truthtable([aa, bb], "0101") assert str(f | g) == "b a\n0 0 : 0\n0 1 : 1\n1 0 : 1\n1 1 : 1\n" assert str(f & g) == "b a\n0 0 : 0\n0 1 : 0\n1 0 : 0\n1 1 : 1\n" assert str(f ^ g) == "b a\n0 0 : 0\n0 1 : 1\n1 0 : 1\n1 1 : 0\n" f = truthtable([a, b, c], "00011-00") g = truthtable([a, b, c], "01-1--00") assert str(f | g) == "c b a\n0 0 0 : 0\n0 0 1 : 1\n0 1 0 : -\n0 1 1 : 1\n1 0 0 : 1\n1 0 1 : -\n1 1 0 : 0\n1 1 1 : 0\n" assert str(f & g) == "c b a\n0 0 0 : 0\n0 0 1 : 0\n0 1 0 : 0\n0 1 1 : 1\n1 0 0 : -\n1 0 1 : -\n1 1 0 : 0\n1 1 1 : 0\n" assert str(f ^ g) == "c b a\n0 0 0 : 0\n0 0 1 : 1\n0 1 0 : -\n0 1 1 : 0\n1 0 0 : -\n1 0 1 : -\n1 1 0 : 0\n1 1 1 : 0\n"
def test_ops(): f = truthtable([aa, bb], "0001") assert str(f) == "inputs: b a\n00 0\n01 0\n10 0\n11 1\n" assert str(~f) == "inputs: b a\n00 1\n01 1\n10 1\n11 0\n" assert str(f | 0) == "inputs: b a\n00 0\n01 0\n10 0\n11 1\n" assert str(f | 1) == "inputs: b a\n00 1\n01 1\n10 1\n11 1\n" assert str(0 | f) == "inputs: b a\n00 0\n01 0\n10 0\n11 1\n" assert str(1 | f) == "inputs: b a\n00 1\n01 1\n10 1\n11 1\n" assert str(f & 0) == "inputs: b a\n00 0\n01 0\n10 0\n11 0\n" assert str(f & 1) == "inputs: b a\n00 0\n01 0\n10 0\n11 1\n" assert str(0 & f) == "inputs: b a\n00 0\n01 0\n10 0\n11 0\n" assert str(1 & f) == "inputs: b a\n00 0\n01 0\n10 0\n11 1\n" assert str(f ^ 0) == "inputs: b a\n00 0\n01 0\n10 0\n11 1\n" assert str(f ^ 1) == "inputs: b a\n00 1\n01 1\n10 1\n11 0\n" f = truthtable([aa, bb], "01-0") assert str(~f) == "inputs: b a\n00 1\n01 0\n10 -\n11 1\n" f = truthtable([aa, bb], "0011") g = truthtable([aa, bb], "0101") assert str(f | g) == "inputs: b a\n00 0\n01 1\n10 1\n11 1\n" assert str(f & g) == "inputs: b a\n00 0\n01 0\n10 0\n11 1\n" assert str(f ^ g) == "inputs: b a\n00 0\n01 1\n10 1\n11 0\n" f = truthtable([a, b, c], "00011-00") g = truthtable([a, b, c], "01-1--00") assert str(f | g) == "inputs: c b a\n000 0\n001 1\n010 -\n011 1\n100 1\n101 -\n110 0\n111 0\n" assert str(f & g) == "inputs: c b a\n000 0\n001 0\n010 0\n011 1\n100 -\n101 -\n110 0\n111 0\n" assert str(f ^ g) == "inputs: c b a\n000 0\n001 1\n010 -\n011 0\n100 -\n101 -\n110 0\n111 0\n"
def test_ops(): f = truthtable([aa, bb], "0001") assert str(f) == "b a\n0 0 : 0\n0 1 : 0\n1 0 : 0\n1 1 : 1\n" assert str(~f) == "b a\n0 0 : 1\n0 1 : 1\n1 0 : 1\n1 1 : 0\n" assert str(f | 0) == "b a\n0 0 : 0\n0 1 : 0\n1 0 : 0\n1 1 : 1\n" assert str(f | 1) == "b a\n0 0 : 1\n0 1 : 1\n1 0 : 1\n1 1 : 1\n" assert str(0 | f) == "b a\n0 0 : 0\n0 1 : 0\n1 0 : 0\n1 1 : 1\n" assert str(1 | f) == "b a\n0 0 : 1\n0 1 : 1\n1 0 : 1\n1 1 : 1\n" assert str(f & 0) == "b a\n0 0 : 0\n0 1 : 0\n1 0 : 0\n1 1 : 0\n" assert str(f & 1) == "b a\n0 0 : 0\n0 1 : 0\n1 0 : 0\n1 1 : 1\n" assert str(0 & f) == "b a\n0 0 : 0\n0 1 : 0\n1 0 : 0\n1 1 : 0\n" assert str(1 & f) == "b a\n0 0 : 0\n0 1 : 0\n1 0 : 0\n1 1 : 1\n" assert str(f ^ 0) == "b a\n0 0 : 0\n0 1 : 0\n1 0 : 0\n1 1 : 1\n" assert str(f ^ 1) == "b a\n0 0 : 1\n0 1 : 1\n1 0 : 1\n1 1 : 0\n" f = truthtable([aa, bb], "01-0") assert str(~f) == "b a\n0 0 : 1\n0 1 : 0\n1 0 : -\n1 1 : 1\n" f = truthtable([aa, bb], "0011") g = truthtable([aa, bb], "0101") assert str(f | g) == "b a\n0 0 : 0\n0 1 : 1\n1 0 : 1\n1 1 : 1\n" assert str(f & g) == "b a\n0 0 : 0\n0 1 : 0\n1 0 : 0\n1 1 : 1\n" assert str(f ^ g) == "b a\n0 0 : 0\n0 1 : 1\n1 0 : 1\n1 1 : 0\n" f = truthtable([a, b, c], "00011-00") g = truthtable([a, b, c], "01-1--00") assert str( f | g ) == "c b a\n0 0 0 : 0\n0 0 1 : 1\n0 1 0 : -\n0 1 1 : 1\n1 0 0 : 1\n1 0 1 : -\n1 1 0 : 0\n1 1 1 : 0\n" assert str( f & g ) == "c b a\n0 0 0 : 0\n0 0 1 : 0\n0 1 0 : 0\n0 1 1 : 1\n1 0 0 : -\n1 0 1 : -\n1 1 0 : 0\n1 1 1 : 0\n" assert str( f ^ g ) == "c b a\n0 0 0 : 0\n0 0 1 : 1\n0 1 0 : -\n0 1 1 : 0\n1 0 0 : -\n1 0 1 : -\n1 1 0 : 0\n1 1 1 : 0\n"
def test_table(): assert truthtable([], [0]).is_zero() assert truthtable([], [1]).is_one() f = Xor(a, b, c, d) tt = expr2truthtable(f) assert truthtable2expr(tt).equivalent(f) assert truthtable2expr(tt, conj=True).equivalent(f) assert str(tt) == XOR_STR assert repr(tt) == XOR_STR assert tt.support == {aa, bb, cc, dd} assert tt.inputs == (aa, bb, cc, dd) assert truthtable2expr(tt.restrict({aa: 0})).equivalent(Xor(b, c, d)) assert tt.restrict({ee: 0}) == tt assert tt.satisfy_one() == {aa: 1, bb: 0, cc: 0, dd: 0} assert [p for p in tt.satisfy_all()] == [{ aa: 1, bb: 0, cc: 0, dd: 0 }, { aa: 0, bb: 1, cc: 0, dd: 0 }, { aa: 0, bb: 0, cc: 1, dd: 0 }, { aa: 1, bb: 1, cc: 1, dd: 0 }, { aa: 0, bb: 0, cc: 0, dd: 1 }, { aa: 1, bb: 1, cc: 0, dd: 1 }, { aa: 1, bb: 0, cc: 1, dd: 1 }, { aa: 0, bb: 1, cc: 1, dd: 1 }] assert tt.satisfy_count() == 8 assert truthtable((a, b), "0000").satisfy_one() == None