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_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_functor_as_function_paramter(): ctx = Qit() s = Struct((Int(), "x"), (Int(), "y")) f = Function("f").takes(s, "s").returns(Int()).code("return s.x;") ftype = FunctorFromFunction(f) g = Function("g").takes(Int(), "x")\ .takes(Int(), "y")\ .takes(ftype, "f")\ .returns(Int()) g.code(""" {{stype}} s(x, y); return y * f(s); """, stype=s) assert ctx.run(g(3, 2, ftype.value(f))) == 6
def test_map_with_own_key_comparator2(): ctx = Qit() m = { (1, 1): (1, 2), (2, 1): (2, 3), (3, 5): (3, 8), (4, 7): (4, 11) } z = Int().variable("z") mtype = Struct((Int(), "x"), (Int(), "y")) cmp_keys = Function().takes(mtype, "key1")\ .takes(mtype, "key2")\ .reads(z)\ .returns(Bool()) cmp_keys.code("return key1.x < key2.x && key1.x <= z;") qm = Map(mtype, mtype, cmp_keys) assert ctx.run(qm.value(m), args={z: 6}) == m
def test_map_with_own_key_comparator(): ctx = Qit() m = { (1, 1): (1, 2), (2, 1): (2, 3), (3, 5): (3, 8), (4, 7): (4, 11) } mtype = Struct((Int(), "x"), (Int(), "y")) cmp_keys = Function().takes(mtype, "key1")\ .takes(mtype, "key2")\ .returns(Bool()) cmp_keys.code("return key1.x < key2.x;") qm = Map(mtype, mtype, cmp_keys) assert ctx.run(qm.value(m)) == m cmp_keys.code("return key1.y < key2.y;") del m[(1, 1)] # this value is replace by (2,1) because of changed function assert ctx.run(qm.value(m)) == m
def test_system_basic(): ctx = Qit() # Rules f = Function("f").takes(Int(), "x").returns(Int()).code("return x * 10;") g = Function("g").takes(Int(), "x").returns(Vector(Int())) g.code("if(x % 2 == 0) return {}; else return { x + 1, x + 2 };") h = Function("h").takes(Int(), "x").returns(Int()).code("return -x;") # Initial states v = Int().values(10, 21) # System s = System(v, (f, g, h)) result = ctx.run(s.states(2).iterate().sort()) expected = [-210, # 21, f, h -100, # 10, f, h -23, # 21, g, h -22, # 21, g, h -21, # 21, h -20, # 20, h, g -19, # 20, h, g -10, # 10, h 10, # 10 21, # 21 22, # 21, g 23, # 21, g 24, # 21, g, g 25, # 21, g, g 100, # 10, f 210, # 21, f 220, # 21, g, f 230, # 21, g, f 1000,# 10, f, f 2100,# 21, f, f ] assert result == expected
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; """) input_lts = t_states.value([t_element.value((1, "t0", 2)), t_element.value((2, "t0", 3)), t_element.value((3, "t1", 4)), t_element.value((4, "t1", 5)), t_element.value((5, "t2", 6)),
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]]