Esempio n. 1
0
def test_canonical_mapping():
    ax = USet(3, "a")
    a0, a1, a2 = ax.all()

    bx = USet(2, "b")
    b0, b1 = bx.all()

    m1 = hd.Map([(b0, b0), (b1, b0)])
    m2 = hd.Map([(b0, b0), (b1, b1)])
    m3 = hd.Map([(b0, b1), (b1, b0)])
    result = list(hd.Mappings(bx, bx).create_cn_iter())
    assert result == [m1, m2, m3]

    m1 = hd.Map([(b0, a0), (b1, a0)])
    m2 = hd.Map([(b0, a0), (b1, a1)])
    result = list(hd.Mappings(bx, ax).create_cn_iter())
    assert result == [m1, m2]

    m1 = hd.Map([(0, a0), (1, a0)])
    m2 = hd.Map([(0, a0), (1, a1)])
    result = list(hd.Mappings(hd.Range(2), ax).create_cn_iter())
    assert result == [m1, m2]

    m1 = hd.Map([(0, b0), (1, b0)])
    m2 = hd.Map([(0, b0), (1, b1)])
    result = list(hd.Mappings(hd.Range(2), bx).create_cn_iter())
    assert result == [m1, m2]
Esempio n. 2
0
def test_mappings_map_class():
    r = hd.Range(2)
    m = hd.Mappings(r, r, map_class=tuple)
    assert m.strict
    result = set(m)
    expected = {((0, 0), (1, 0)), ((0, 0), (1, 1)), ((0, 1), (1, 0)),
                ((0, 1), (1, 1))}
    assert result == expected

    m = hd.Mappings(r, r, map_class=dict)
    assert not m.strict
Esempio n. 3
0
def test_canonical_map_map():
    ax = USet(2, "a")
    bx = USet(2, "b")

    m1 = hd.Mappings(ax, bx)
    m2 = hd.Mappings(ax, ax)

    m = hd.Mappings(m1, m2)
    bf_check(m)

    m = hd.Mappings(m1, m1)
    bf_check(m)

    m = hd.Mappings(m2, m1)
    bf_check(m)
Esempio n. 4
0
def test_mapping_set():
    d1 = hd.Range(3)
    d2 = hd.Range(4)
    m = hd.Mappings(d1, d2)

    a = list(m)
    for i in xrange(70):
        it = m.create_iter(i)
        assert list(it) == a[i:]
Esempio n. 5
0
def test_mapping_generate():
    r1 = hd.Range(2)
    p = hd.Mappings(r1, r1)

    result = list(p.generate(200))
    for r in result:
        assert len(r) == 2
        assert r.get(0) == 0 or r.get(0) == 1
        assert r.get(1) == 0 or r.get(1) == 1
Esempio n. 6
0
def test_canonical_map_prod():
    ax = USet(3, "a")
    bx = USet(2, "b")
    cx = USet(1, "b")

    m1 = hd.Mappings(bx, cx)
    m2 = hd.Mappings(ax, ax)
    m3 = hd.Mappings(ax, bx)

    bf_check(m1 * bx)
    bf_check(m1 * cx)
    bf_check(bx * m1)
    bf_check(cx * m1)
    bf_check(m1 * m1)

    bf_check(m3 * m3)
    bf_check(m3 * m3 * m3)
    bf_check(m2 * m3)
Esempio n. 7
0
def test_cannonical_prod_map():
    ax = USet(3, "a")
    bx = USet(2, "b")

    pb = bx * bx
    pab = ax * bx

    m = hd.Mappings(bx, pb)
    bf_check(m)

    m = hd.Mappings(pb, bx)
    bf_check(m)

    m = hd.Mappings(pb, pb)
    bf_check(m)

    m = hd.Mappings(pab, bx)
    bf_check(m)
Esempio n. 8
0
def test_mapping_to_values():
    a = hd.Range(5)
    b = hd.Range(6)
    c = hd.Mappings(a, b)

    v = c.to_values()

    assert isinstance(v, hd.Values)
    assert list(c) == list(v)
Esempio n. 9
0
def test_dist_precompute(cluster4):
    states = hd.USet(2, "q")
    alphabet = hd.USet(2, "a")

    delta = hd.Mappings(states * alphabet, states)
    r1 = delta.cnfs().run()
    r2 = delta.cnfs().run(cluster4.ctx)

    assert len(r1) == len(r2)
Esempio n. 10
0
def test_map_is_canonical():
    ax = USet(2, "a")
    a1, a2 = ax.all()
    bx = USet(2, "b")
    b1, b2 = bx.all()

    ms = hd.Mappings(ax, bx)
    c = [item for item in ms if hdc.is_canonical(item)]

    m1 = hd.Map(((a1, b1), (a2, b1)))
    m2 = hd.Map(((a1, b1), (a2, b2)))
    assert hdt.compare_sequence([m1, m2], c) == 0

    ms = hd.Mappings(ax, ax)
    c = [item for item in ms if hdc.is_canonical(item)]
    m1 = hd.Map(((a1, a1), (a2, a1)))
    m2 = hd.Map(((a1, a1), (a2, a2)))
    m3 = hd.Map(((a1, a2), (a2, a1)))
    assert hdt.compare_sequence([m1, m2, m3], c) == 0
Esempio n. 11
0
def test_mapping_to_values_maxsize():
    a = hd.Range(5)
    b = hd.Range(6)
    c = hd.Mappings(a, b)

    v = c.to_values(max_size=6)

    assert isinstance(v, hd.Mappings)
    assert isinstance(v.key_domain, hd.Values)
    assert isinstance(v.value_domain, hd.Values)
    assert list(c) == list(v)
Esempio n. 12
0
def test_mapping_int_int():
    r = hd.Range(2)
    m = hd.Mappings(r, r)
    result = list(m)

    e1 = hd.Map(((0, 0), (1, 0)))
    e2 = hd.Map(((0, 0), (1, 1)))
    e3 = hd.Map(((0, 1), (1, 0)))
    e4 = hd.Map(((0, 1), (1, 1)))

    assert result == [e1, e2, e3, e4]
    assert m.size == 4
Esempio n. 13
0
def test_domain_filter():
    def fn(x):
        return x > 2 and x < 5

    r = hd.Range(6)
    d = r.filter(fn)
    result = list(d)
    assert result == [3, 4]

    result = list(d.generate(5))
    assert len(result) == 5
    for x in result:
        assert x in [3, 4]

    assert d.filtered
    assert not r.filtered
    assert d.size == 6

    p = d * d
    result = list(p)
    assert set(result) == {(3, 3), (4, 3), (3, 4), (4, 4)}

    assert p.filtered
    assert not (r * r).filtered

    assert hd.Sequences(d, 2).filtered
    assert not hd.Sequences(r, 2).filtered

    assert not hd.Mappings(d, r).filtered
    assert hd.Mappings(r, d).filtered
    assert not hd.Mappings(r, r).filtered

    assert hd.Product((d, d), unordered=True).filtered
    assert not hd.Product((r, r), unordered=True).filtered

    result = list(hd.Product((d, d), unordered=True))
    assert set(result) == {(4, 3)}
Esempio n. 14
0
def main():

    n_states = 6  # Number of states
    n_symbols = 2  # Number of symbols in alphabet

    states = hd.USet(n_states, "q")  # set of states q0, q1, ..., q_{n_states}
    alphabet = hd.USet(n_symbols, "a")  # set of symbols a0, ..., a_{a_symbols}

    # Mappings (states * alphabet) -> states
    delta = hd.Mappings(states * alphabet, states)

    # Let us precompute some values that will be repeatedly used
    init_state = frozenset(states)
    max_steps = (n_states**3 - n_states) / 6

    def check_automaton(delta):
        # This function takes automaton as a transition function and
        # returns the minimal length of synchronizing word or 0 if there
        # is no such word

        def step(state, depth):
            # A step in bread-first search; gives a set of states
            # and return a set reachable by one step
            for a in alphabet:
                yield frozenset(delta[(s, a)] for s in state)

        delta = delta.to_dict()
        return search.bfs(
            init_state,  # Initial state
            step,  # Step
            lambda state, depth: depth if len(state) == 1 else None,
            # Run until we reach a single state
            max_depth=max_steps,  # Limit depth of search
            not_found_value=0)  # Return 0 when we exceed depth limit

    # Create & run pipeline
    pipeline = delta.cnfs().map(check_automaton).max(size=1)
    result = pipeline.run()

    print("The maximal length of a minimal reset word for an "
          "automaton with {} states and {} symbols is {}.".format(
              n_states, n_symbols, result[0]))
Esempio n. 15
0
def test_mapping_strict():
    r = hd.Range(2)
    s = hd.Mappings(r, r)
    assert s.strict
Esempio n. 16
0
def test_mapping_name():
    r = hd.Range(10)
    m = hd.Mappings(r, r, name="TestMappings")
    assert m.name == "TestMappings"
Esempio n. 17
0
def test_mapping_size():
    m = hd.Mappings(hd.Range(10), hd.Range(2))
    assert m.size == 1024
Esempio n. 18
0
def test_mapping_flags():
    d1 = hd.Range(3)
    d2 = hd.Range(3).filter(lambda x: True)
    assert hd.Mappings(d1, d2).filtered
    assert not hd.Mappings(d1, d1).filtered