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_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)
def test_iterator_first_default_value(): ctx = Qit() fn = Function().takes(Int(), "a").returns(Int()).code("return a > 100;") result = ctx.run(Range(91, 120, 2).iterate().filter(fn).first(111)) assert result == 101 result = ctx.run(Range(10, 20, 2).iterate().filter(fn).first(111)) assert result == 111
def test_iterator_first_maybe(): ctx = Qit() fn = Function().takes(Int(), "a").returns(Int()).code("return a > 100;") result = ctx.run(Range(91, 120, 2).iterate().filter(fn).first_maybe()) assert result == ("Just", 101) result = ctx.run(Range(10, 20, 2).iterate().filter(fn).first_maybe()) assert result == ("Nothing", None)
def test_iterator_first_no_default(): ctx = Qit() fn = Function().takes(Int(), "a").returns(Int()).code("return a > 100;") result = ctx.run(Range(91, 120, 2).iterate().filter(fn).first()) assert result == 101 with pytest.raises(ProgramCrashed): ctx.run(Range(10, 20, 2).iterate().filter(fn).first())
def test_map_value(): ctx = Qit() m = { 1: 1, 2: 2, 3: 3 } assert ctx.run(Map(Int(), Int()).value(m)) == m m = { (1, 2) : (2, 1), (3, 4): (4, 3), (5, 6): (6, 5) } s = Struct(Int(), Int()) assert ctx.run(Map(s, s).value(m)) == m
def test_map_variable(): ctx = Qit() m = Variable(Map(Int(), Int()), "m") assert ctx.run(m, args={m: { 2: 1, 1: 2 }}) == { 2: 1, 1: 2 } m = Map(Int(), Int() * Int()) x = Variable(Int(), "x") assert ctx.run(m.value({1: (x, 20)}), args={x: 123}) == {1: (123, 20)}
def test_filter_map(): r = Range(5) q = Qit() f = Function("f").returns(Int()).takes(Int(), "r").code("return r * 2;") g = Function("g").returns(Bool()).takes(Int(), "x").code("return x == 4;") h = Function("h").returns(Int()).takes(Int(), "r").code("return r + 1;") q.run(r.iterate().map(f).filter(g).map(h)) == 5
def test_bool_variable(): ctx = Qit() x = Variable(Bool(), "x") result = ctx.run(x, args={x: True}) assert result is True result = ctx.run(x, args={x: False}) assert result is False
def test_product_values(): ctx = Qit() p = Range(4) * Range(10) v = p.values((0, 0), (1, 7), (3, 2)) result = ctx.run(v.iterate()) assert [(0, 0), (1, 7), (3, 2)] == result result = ctx.run(v.generate().take(100)) assert all(i in ((0, 0), (1, 7), (3, 2)) for i in result)
def test_range_size(): ctx = Qit() r = Range(11) assert ctx.run(r.size) == 11 start = Variable(Int(), "start") end = Variable(Int(), "end") step = Variable(Int(), "step") r = Range(start, end, step) assert ctx.run(r.size, args={ "start" : 5, "end" : 10, "step" : 1 }) == 5 assert ctx.run(r.size, args={ "start" : 5, "end" : 10, "step" : 2 }) == 2
def test_range_indexer(): ctx = Qit() r = Range(10) ctx.run(r.indexer(5)) == 5 start = Variable(Int(), "start") end = Variable(Int(), "end") step = Variable(Int(), "step") r = Range(start, end, step) result = ctx.run(r.iterate().map(r.indexer), args={"start" : 5, "end": 21, "step" : 3}) assert result == list(range(6))
def test_run_circular_deps(): ctx = Qit() x = Int().variable("x") y = Int().variable("y") with pytest.raises(QitException): ctx.run(x, args = { x : y, y : x, })
def test_union_different_types(): ctx = Qit() a = Int() b = Int() * Int() c = Vector(b) d = Set(a) e = Map(b, c) u = Union(A=a, B=b, C=c, D=d, E=e, G=None) assert ctx.run(u.value(("B", (25, 16)))) == ("B", (25, 16)) assert ctx.run(u.value(("C", [(25, 16), (3, -2)]))) == ("C", [(25, 16), (3, -2)]) assert ctx.run(u.value(("G", None))) == ("G", None)
def test_basic_functor(): ctx = Qit() f_functor = Functor("f_functor", Int(), (Int(), "x"), (Int(), "y")) f = Function("f").takes(Int(), "x").takes(Int(), "y").returns(Int()).code("return x + y;") g = Function("g").takes(f_functor, "f")\ .takes(Int(), "x")\ .takes(Int(), "y")\ .returns(Int()) g.code("return f(x, y);") assert ctx.run(g(f_functor.value(f), 3, 4)) == 7 f_functor = FunctorFromFunction(f) assert ctx.run(g(f_functor.value(f), -2, 4)) == 2
def test_filter_empty(): r = Range(5) q = Qit() f = Function("filter").returns(Bool()).takes(r.type, "r").code("return false;") assert len(q.run(r.iterate().filter(f))) == 0
def test_union_sort(): ctx = Qit() u = Union(A=Int(), B=None, C=Int()) v = (("A", 10), ("B", None), ("C", 5), ("B", None), ("C", 5), ("A", 0), ("A", 20)) values = u.values(*v) result = ctx.run(values.iterate().sort()) assert result == sorted(v)
def test_system_rule_variable(): ctx = Qit() x = Variable(Int(), "x") f = Function("f").takes(Int(), "a").returns(Int()).reads(x).code("return x;") s = System(Int().values(5), (f,)) result = ctx.run(s.states(3).iterate(), args={"x": 5}) assert result == [5]
def test_filter_even(): r = Range(5) q = Qit() f = Function("filter").returns(Bool()).takes(r.type, "r").code("return r % 2 == 0;") assert q.run(r.iterate().filter(f)) == [0, 2, 4]
def test_vector_values(): ctx = Qit() s1 = [(1, 2), (3, 4), (7, 7)] s2 = [(1, 1), (2, 2)] s3 = [] s4 = [(4, 4), (4, 4), (4, 4)] p = Int() * Int() s = Vector(p) v = s.values(s1, s2, s3, s4) result = ctx.run(v.iterate()) assert result == [s1, s2, s3, s4] result = ctx.run(v.generate().take(150)) assert all(i in [s1, s2, s3, s4] for i in result)
def test_range_variable_generate(): c = Qit() x = Variable(Int(), "x") r = Range(x).generate().take(30) result = c.run(r, { "x": 3 }) for i in result: assert 0 <= i < 3
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_map_in_map(): def prepare_map (start, size): return dict((start + i, start * i) for i in range(size)) ctx = Qit() d = dict((i, prepare_map(i, 10)) for i in range(3)) assert ctx.run(Map(Int(), Map(Int(), Int())).value(d)) == d
def test_min_step_range_iterate_variables(): x = Variable(Int(), "x") y = Variable(Int(), "y") z = Variable(Int(), "z") expr = Range(x, y, z).iterate() c = Qit() assert list(range(10, 20, 4)) == c.run(expr, args={"x": 10, "y": 20, "z": 4})
def test_range_function_iterate(): c = Qit() x = Variable(Int(), "x") r = Range(x).iterate().to_vector() f = r.make_function() assert [[], [0], [0,1], [0,1,2]] == c.run(Range(4).iterate().map(f))
def test_outer_variables(): c = Qit() x = Variable(Int(), "x") f = Function().takes(Int(), "a").returns(Int()).reads(x) f.code("return a * x;") r = Range(x).iterate().map(f) assert [0, 5, 10, 15, 20] == c.run(r, args={ x: 5 })
def test_vector_variable(): ctx = Qit() s = Struct(Int(), Int()) v = Vector(s) x = Variable(v, "x") result = ctx.run(x, args={x: [(11,12), (5, 2)]}) assert result == [(11,12), (5, 2)]
def test_struct_variable(): ctx = Qit() s = Struct(Int(), Int()) s2 = Struct(s, Int(), s) x = Variable(s2, "x") result = ctx.run(x, args={x: ((11,12), 13, (5, 2))}) assert result == ((11,12), 13, (5, 2))
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_mapping_product_sequence(): ctx = Qit() r = Range(2) * Range(1) s = Sequence(Enumerate("X", "Y"), 2) m = Mapping(r, s) result = ctx.run(m.iterate()) expected = [ {(1, 0): ['X', 'X'], (0, 0): ['X', 'X']}, {(1, 0): ['X', 'X'], (0, 0): ['Y', 'X']}, {(1, 0): ['X', 'X'], (0, 0): ['X', 'Y']}, {(1, 0): ['X', 'X'], (0, 0): ['Y', 'Y']}, {(1, 0): ['Y', 'X'], (0, 0): ['X', 'X']}, {(1, 0): ['Y', 'X'], (0, 0): ['Y', 'X']}, {(1, 0): ['Y', 'X'], (0, 0): ['X', 'Y']}, {(1, 0): ['Y', 'X'], (0, 0): ['Y', 'Y']}, {(1, 0): ['X', 'Y'], (0, 0): ['X', 'X']}, {(1, 0): ['X', 'Y'], (0, 0): ['Y', 'X']}, {(1, 0): ['X', 'Y'], (0, 0): ['X', 'Y']}, {(1, 0): ['X', 'Y'], (0, 0): ['Y', 'Y']}, {(1, 0): ['Y', 'Y'], (0, 0): ['X', 'X']}, {(1, 0): ['Y', 'Y'], (0, 0): ['Y', 'X']}, {(1, 0): ['Y', 'Y'], (0, 0): ['X', 'Y']}, {(1, 0): ['Y', 'Y'], (0, 0): ['Y', 'Y']} ] def to_tuple(d): for key, value in d.items(): return (tuple(key), tuple(value)) assert set(map(to_tuple, result)) == set(map(to_tuple, expected))