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_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_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