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)]
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]]
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)
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
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())
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
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)
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)
def test_product_no_name(): p = Product(None, Range(2), Range(3)) Qit().run(p.iterate().print_all())
# solution = t_input.value(( # triangle t0 -> t0 -> t1 -> t1 -> t2 -> t2 -> t0 -> ... # {0: 2, 1: 0, 2: 2}, # { # (0, 0): 2, # (0, 1): 1, # (0, 2): 0, # (1, 0): 0, # (1, 1): 2, # (1, 2): 1, # (2, 0): 1, # (2, 1): 0, # (2, 2): 2 # }, # { # (0, 0): 2, # (0, 1): 1, # (0, 2): 0, # (1, 0): 0, # (1, 1): 2, # (1, 2): 1, # (2, 0): 1, # (2, 1): 0, # (2, 2): 2 # })) # res = ctx.run(t_input.values(solution).iterate().map(f_map_variables)) empty_value = t_result.value((({}, {}, {}), [])) res = ctx.run(init_values.iterate().map(f_process_input).filter(f_eq_statespaces).first(empty_value)) print ("Result: ", res)
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)