Ejemplo n.º 1
0
def test_product_copy():
    p = Product("P", (Range(4), "x"), (Range(4), "y"))

    p2 = p.copy()
    p2.set("x", Range(2))

    q = Product("Q", (p, "p1"), (p, "p2"))

    q2 = q.copy()
    q2.set_generator("p2", p2.generator)
    q2.set_iterator("p1", p2.iterator)

    v_r4 = list(range(4))
    v_r2 = list(range(2))
    v_p = list(itertools.product(v_r4, v_r4))
    v_p2 = list(itertools.product(v_r2, v_r4))
    v_q2_generator = set(itertools.product(v_p, v_p2))
    v_q2_iterator = set(itertools.product(v_p2, v_p))

    c = Qit()
    for v in c.run(q2.generate().take(200).collect()):
        assert v in v_q2_generator

    result = c.run(q2.iterate().collect())
    assert len(v_q2_iterator) == len(result)
    assert v_q2_iterator == set(result)
Ejemplo n.º 2
0
def test_filter_product():
    p = Product((Range(5), "x"), (Range(5), "y"))
    q = Qit()

    f = Function("filter").returns(Bool()).takes(p.type, "p").code("return p.x == p.y;")

    q.run(p.iterate().filter(f)) == [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
Ejemplo n.º 3
0
def test_functor_variable():
    ctx = Qit()
    ftype = Functor("f_functor", Int(), (Int(), "x"), (Int(), "y"))

    # functions
    fplus = Function().takes(Int(), "x").takes(Int(), "y").returns(Int())
    fplus.code("return x + y;")

    ftimes = Function().takes(Int(), "x").takes(Int(), "y").returns(Int())
    ftimes.code("return x * y;")

    fmod = Function().takes(Int(), "x").takes(Int(), "y").returns(Int())
    fmod.code("return x % y;")

    # apply function in variable fvar to the given list of pairs
    fvar = ftype.variable("f")
    p = Product((Range(1, 4), "x"), (Range(1, 3), "y"))
    apply_f = Function().takes(p, "p").reads(fvar).returns(Int()).code("""
        return f(p.x, p.y);
    """)
    g = p.iterate().map(apply_f).make_function((fvar, ))

    bind_function = Function().takes(ftype, "f")\
                              .returns(Vector(ftype.return_type))
    bind_function.code("return {{g}}(f);", g=g)
    res = ctx.run(ftype.values(fplus, ftimes, fmod).iterate().map(bind_function))
    assert res == [[2, 3, 4, 3, 4, 5], [1, 2, 3, 2, 4, 6], [0, 0, 0, 1, 0, 1]]
Ejemplo n.º 4
0
def test_map():
    p = Product((Range(4), "x"), (Range(4), "y"))
    f = Function("f").takes(p.type, "p").returns(Int()).code("return p.x + p.y;")
    result = Qit().run(p.iterate().take(6).map(f).take(4))
    assert result == [0, 1, 2, 3]
    result = Qit().run(p.generate().map(f).take(4))
    assert all(x >= 0 and x <= 8 for x in result)
Ejemplo n.º 5
0
def test_product_iterate():
    p = Product("MyProduct", (Range(3), "x"), (Range(3), "y"))
    r = list(range(3))
    pairs = set(itertools.product(r, r))
    c = Qit()
    result = c.run(p.iterate().collect())
    assert len(result) == len(pairs)
    assert set(result) == pairs
Ejemplo n.º 6
0
def test_product_generator_iterator_variables():
    x = Variable(Int(), "x")
    y = Variable(Int(), "x")

    p = Product((Range(x), "x"), (Range(y), "y"))

    assert set() == set(p.get_variables())
    assert {x, y} == set(p.iterate().get_variables())
    assert {x, y} == set(p.generate().get_variables())
Ejemplo n.º 7
0
def test_qit_declaration():
    p = Product(Range(1), Range(1))
    f = Function("f").takes(p.type, "p").returns(Int()).from_file("x.h")
    g = Function("g").takes(Int(), "x").returns(Int()).from_file("y.h")

    q = Qit()

    assert q.declarations(g) == ["qint g(qint x)"]
    assert len(q.declarations(p.iterate().map(f).map(g).map(g))) == 2
Ejemplo n.º 8
0
def test_random_product():
    p = Product("P", (Range(2), "x"), (Range(2), "y"))
    q = Product("Q", (p, "p1"), (p, "p2"))
    c = Qit()
    result = c.run(q.generate().take(100).collect())
    assert len(result) == 100
    for ((a, b), (c, d)) in result:
        assert a >= 0 and a < 2
        assert b >= 0 and b < 2
        assert c >= 0 and c < 2
        assert d >= 0 and d < 2
Ejemplo n.º 9
0
def test_product_in_product():
    p = Product("P", (Range(2), "x"), (Range(2), "y"))
    q = Product("Q", (p, "p1"), (p, "p2"))

    r = list(range(2))
    p_all = list(itertools.product(r, r))
    q_all = set(itertools.product(p_all, p_all))

    c = Qit()
    result = c.run(q.iterate().collect())
    assert set(result) == q_all
    assert len(result) == len(q_all)
Ejemplo n.º 10
0
def test_system_in_product():
    ctx = Qit()
    f = Function("f").takes(Int(), "x").returns(Int()).code("return x * 10;")
    g = Function("g").takes(Int(), "x").returns(Vector(Int())).code("return { x + 1, x + 2 };")

    v = Int().values(10, 20)
    s = System(v, (f,g))

    D = s.states(2)
    P = Product(D, D)

    values = ctx.run(D.iterate().take(1000))
    pairs = set(itertools.product(values, values))
    result = ctx.run(P.iterate().take(1000))
    assert len(result) == 484 # 22 * 22
    assert pairs == set(result)
Ejemplo n.º 11
0
def test_product_no_name():
    p = Product(None, Range(2), Range(3))
    Qit().run(p.iterate().print_all())
Ejemplo n.º 12
0
            for (auto it = output_arcs.begin(); it != output_arcs.end(); it++) {
                if (it->first.transition == {{tid}}) {
                    new_marking[it->first.place] += it->second;
                }
            }
            return {new_marking};
        }
        return {};
    """, is_enabled=fs_enabled[t], t_marking=t_marking, tid=Int().value(t))
    for t in range(N_EVENTS))

statespace = ActionSystem(Values(t_marking, [v_marking]), fs_fire)
states = statespace.states(DEPTH)
f_states = states.iterate().make_function((v_marking, v_input_arcs, v_output_arcs))

init_values = Product((M0, "init_marking"), (Wi, "input"), (Wo, "output"))

t_element = statespace.sas_type
t_states = Vector(t_element)
t_input = init_values.as_type()
t_result = Struct((init_values, "init_values"), (t_states, "lts"))

f_process_input = Function("map_variables").takes(init_values, "init_values").returns(t_result)
f_process_input.code("""
    return {{t_result}}(init_values, {{f_states}}(init_values.init_marking, init_values.input, init_values.output));
""", f_states=f_states, t_result=t_result,)


f_eq_states = Function("eq_states").takes(t_element, "s1").takes(t_element, "s2").returns(Bool())
f_eq_states.code("""
    return s1.s1_id == s2.s1_id && s1.action == s2.action && s1.s2_id == s2.s2_id;
Ejemplo n.º 13
0
def test_product_no_name():
    p = Product(Range(2), Range(3))
    result = Qit().run(p.iterate())
    assert set(itertools.product(range(2), range(3))) == set(result)