Пример #1
0
def test_compose():
    ordering = {'x': 0, 'y': 1, 'z': 2}
    g = BDD(ordering)
    # x & (x | z)
    a = g.add_expr('x && y')
    b = g.add_expr('x || z')
    c = g.let({'y': b}, a)
    d = g.add_expr('x && (x || z)')
    assert c == d, (c, d)
    # (y | z) & x
    ordering = {'x': 0, 'y': 1, 'z': 2, 'w': 3}
    g = BDD(ordering)
    a = g.add_expr('(x && y) || z')
    b = g.add_expr('(y || z) && x')
    c = g.let({'z': b}, a)
    assert c == b, (c, b)
    # long expr
    ordering = {'x': 0, 'y': 1, 'z': 2, 'w': 3}
    g = BDD(ordering)
    a = g.add_expr('(x && y) || (!z || (w && y && x))')
    b = g.add_expr('(y || !z) && x')
    c = g.let({'y': b}, a)
    d = g.add_expr('(x && ((y || !z) && x)) ||'
                   ' (!z || (w && ((y || !z) && x) && x))')
    assert c == d, (c, d)
    # complemented edges
    ordering = {'x': 0, 'y': 1}
    g = BDD(ordering)
    f = g.add_expr('x <-> y')
    var = 'y'
    new_level = 0
    var_node = g.find_or_add(new_level, -1, 1)
    u = g.let({var: var_node}, f)
    assert u == 1, g.to_expr(u)
Пример #2
0
def test_rename():
    ordering = {'x': 0, 'xp': 1}
    g = BDD(ordering)
    x = g.add_expr('x')
    xp = g.add_expr('xp')
    dvars = {'x': 'xp'}
    xrenamed = g.let(dvars, x)
    assert xrenamed == xp, xrenamed
    ordering = {'x': 0, 'xp': 1, 'y': 2, 'yp': 3, 'z': 4, 'zp': 5}
    g = BDD(ordering)
    u = g.add_expr('x && y && ! z')
    dvars = {'x': 'xp', 'y': 'yp', 'z': 'zp'}
    urenamed = g.let(dvars, u)
    up = g.add_expr('xp && yp && ! zp')
    assert urenamed == up, urenamed
    # assertion violations
    # non-neighbors
    dvars = {'x': 'yp'}
    r = g.let(dvars, u)
    r_ = g.add_expr('yp && y && ! z')
    assert r == r_, (r, r_)
    # u not in bdd
    dvars = {'x': 'xp'}
    with nt.assert_raises(AssertionError):
        g.let(dvars, 1000)
    # y essential for u
    dvars = {'xp': 'y'}
    with nt.assert_raises(AssertionError):
        g.let(dvars, u)
    # old and new vars intersect
    dvars = {'x': 'x'}
    with nt.assert_raises(AssertionError):
        g.let(dvars, u)
Пример #3
0
def test_cofactor():
    ordering = {'x': 0, 'y': 1, 'z': 2}
    g = BDD(ordering)
    # u not in g
    with nt.assert_raises(AssertionError):
        g.let({'x': False, 'y': True, 'z': False}, 5)
    # x & y
    e = g.add_expr('x && y')
    x = g.add_expr('x')
    assert g.let({'x': False}, x) == -1
    assert g.let({'x': True}, x) == 1
    assert g.let({'x': False}, -x) == 1
    assert g.let({'x': True}, -x) == -1
    y = g.add_expr('y')
    assert g.let({'x': True}, e) == y
    assert g.let({'x': False}, e) == -1
    assert g.let({'y': True}, e) == x
    assert g.let({'y': False}, e) == -1

    assert g.let({'x': False}, -e) == 1
    assert g.let({'x': True}, -e) == -y
    assert g.let({'y': False}, -e) == 1
    assert g.let({'y': True}, -e) == -x