示例#1
0
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)
示例#2
0
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
示例#3
0
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())
示例#4
0
文件: test_union.py 项目: spirali/qit
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)
示例#5
0
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
示例#6
0
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))
示例#7
0
文件: test_queue.py 项目: spirali/qit
def test_queue_variable():
    ctx = Qit()
    s = Struct(Int(), Int())
    q = Queue(s)
    x = Variable(q, "x")
    result = ctx.run(x, args={x: [(11,12), (5, 2)] })
    assert result == [(11, 12), (5, 2)]
示例#8
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)
示例#9
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)]
示例#10
0
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 })
示例#11
0
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]
示例#12
0
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)]
示例#13
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]]
示例#14
0
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))
示例#15
0
文件: test_map.py 项目: spirali/qit
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
示例#16
0
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))
示例#17
0
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]
示例#18
0
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
示例#19
0
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})
示例#20
0
def test_vector_constructor():
    ctx = Qit()
    s = Struct(Int(), Int())
    v = Vector(s)
    x = Variable(s, "x")
    result = ctx.run(v.value([x, s.value((7, 2)), (1, 2)]),
            args={x: (11,12)})
    assert result == [ (11, 12), (7, 2), (1, 2) ]
示例#21
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
示例#22
0
def test_product_size():
    ctx = Qit()
    r1 = Range(4)
    r2 = Range(Variable(Int(), "x"))
    r3 = Range(Variable(Int(), "y"))
    assert ctx.run((r1 * r2 * r3).size,
            args={Variable(Int(), "x"): 10,
                  Variable(Int(), "y"): 2}) == 80
示例#23
0
def test_values_int_variable():
    ctx = Qit()
    x = Variable(Int(), "x")
    y = Variable(Int(), "y")

    r = Int().values(x, 7, y)
    result = ctx.run(r.iterate(), args={"x": 2, "y": 11})
    assert result == [2, 7, 11]
示例#24
0
def test_mapping_generator():
    ctx = Qit()
    m = Mapping(Range(5, 7), Range(2, 8))
    result = ctx.run(m.generate().take(100))
    assert len(result) == 100
    for r in result:
        assert set(r.keys()) == set((5, 6))
        assert set(r.values()).issubset(set((2, 3, 4, 5, 6, 7)))
示例#25
0
文件: test_class.py 项目: spirali/qit
def test_class_method():
    ctx = Qit()
    p = Struct((Int(), "x"), (Int(), "y"))
    m1 = p.method("get").returns(Int()).code("return self.x + self.y;")
    cls = Class(p, (m1,))
    f = Function().returns(Int()).takes(cls, "a").code("return a.get();")
    result = ctx.run(f(cls.value((20, 31))))
    assert result == 51
示例#26
0
def test_mapping_int_int():
    ctx = Qit()
    r = Range(2)
    m = Mapping(r, r)
    result = ctx.run(m.iterate())
    assert set(map(hdict, result)) == \
           set(map(hdict, [{0: 0, 1: 0}, {0: 1, 1: 0},
                           {0: 0, 1: 1}, {0: 1, 1: 1}]))
示例#27
0
文件: test_map.py 项目: spirali/qit
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)}
示例#28
0
def test_domain_constant_generator():
    c = Qit()
    s = Int() * Int()
    f = s.value((7, 8)).make_function()
    d = Domain(Int(), generator=f())

    result = c.run(d.generate().take(5))
    assert result == [ (7, 8) ] * 5
示例#29
0
文件: test_map.py 项目: spirali/qit
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
示例#30
0
def test_reduce_keyval():
    ctx = Qit()
    kv = KeyValue(Int(), Int())
    values = ((12, 3), (17, 1), (5, 0), (12, 4), (2, 0))
    a = kv.values(*values).iterate().reduce(kv.max_fn)
    b = kv.values(*values).iterate().reduce(kv.min_fn)
    result = ctx.run((Struct(kv, kv)).value((a, b)))
    assert ((12, 4), (5, 0)) == result