From 21e3e22017f509af0e1e0f543c1a02ccfec58f7f Mon Sep 17 00:00:00 2001 From: Leonardo de Moura Date: Thu, 8 May 2014 18:52:15 -0700 Subject: [PATCH] test(lua/threads): re-activate Lua thread examples Signed-off-by: Leonardo de Moura --- tests/lua/threads/tactic1.lua | 37 ----------------------------------- tests/lua/threads/tactic2.lua | 33 ------------------------------- tests/lua/threads/th2.lua | 25 ++++++++++++++++------- tests/lua/threads/th5.lua | 6 +----- 4 files changed, 19 insertions(+), 82 deletions(-) delete mode 100644 tests/lua/threads/tactic1.lua delete mode 100644 tests/lua/threads/tactic2.lua diff --git a/tests/lua/threads/tactic1.lua b/tests/lua/threads/tactic1.lua deleted file mode 100644 index d7aec3bbc..000000000 --- a/tests/lua/threads/tactic1.lua +++ /dev/null @@ -1,37 +0,0 @@ -import("util.lua") -local env = environment() -local ios = io_state() -local Bool = Const("Bool") -env:add_var("p", Bool) -env:add_var("q", Bool) -local p, q = Consts("p, q") -local ctx = context() - -S = State() --- Create tactic t1 in a different Lua State. --- So, t1 can be executed by a different execution --- thread -local t1 = S:eval([[ - counter1 = 0 - return tactic(function(env, ios, s) - while true do - print("tactic 1") - counter1 = counter1 + 1 - sleep(10) - end - end) -]]) - -counter2 = 0 -local t2 = tactic(function(env, ios, s) - while true do - print("tactic 2") - counter2 = counter2 + 1 - sleep(10) - end - end) - -local T = (t1:par(t2)):try_for(500) -T:solve(env, ios, ctx, p) -assert(counter2 > 2) -S:eval([[ assert(counter1 > 2) ]]) diff --git a/tests/lua/threads/tactic2.lua b/tests/lua/threads/tactic2.lua deleted file mode 100644 index 6ed016ba5..000000000 --- a/tests/lua/threads/tactic2.lua +++ /dev/null @@ -1,33 +0,0 @@ -import("util.lua") -local env = environment() -local ios = io_state() -local Bool = Const("Bool") -env:add_var("p", Bool) -env:add_var("q", Bool) -local p, q = Consts("p, q") -local ctx = context() - -S = State() --- tactics t1 and t2 uses yield to implement cooperative --- multitasking -local counter1 = 0 -local t1 = tactic(function(env, ios, s) - while true do - counter1 = counter1 + 1 - coroutine.yield() - end -end) - -local counter2 = 0 -local t2 = tactic(function(env, ios, s) - while true do - counter2 = counter2 + 1 - coroutine.yield() - end - end) - -local T = (t1:par(t2)):try_for(150) -T:solve(env, ios, ctx, p) -print(counter1, counter2) -assert(counter1 > 2) -assert(counter2 > 2) diff --git a/tests/lua/threads/th2.lua b/tests/lua/threads/th2.lua index 0582c44e8..a0e742ec1 100644 --- a/tests/lua/threads/th2.lua +++ b/tests/lua/threads/th2.lua @@ -2,21 +2,32 @@ S1 = State() S2 = State() code = [[ function f(env, prefix, num, type) + local r = list_certified_definition() for i = 1, num do - env:add_var(prefix .. "_" .. i, type) + r = list_certified_definition(type_check(env, mk_var_decl(prefix .. "_" .. i, type)), r) end + return r end ]] S1:dostring(code) S2:dostring(code) -e = environment() -e:add_var("N", Type()) +local e = empty_environment() +e = add_decl(e, mk_var_decl("N", Type)) code2 = [[ e, prefix = ... - f(e, prefix, 1000, Const("N")) + return f(e, prefix, 1000, Const("N")) ]] T1 = thread(S1, code2, e, "x") T2 = thread(S2, code2, e, "y") -T1:wait() -T2:wait() -print(e) +local r1 = T1:wait() +local r2 = T2:wait() +while not r1:is_nil() do + e = e:add(r1:car()) + r1 = r1:cdr() +end +while not r2:is_nil() do + e = e:add(r2:car()) + r2 = r2:cdr() +end +assert(e:find("x_" .. 10)) +assert(e:find("y_" .. 100)) diff --git a/tests/lua/threads/th5.lua b/tests/lua/threads/th5.lua index 43d127060..341074571 100644 --- a/tests/lua/threads/th5.lua +++ b/tests/lua/threads/th5.lua @@ -9,17 +9,13 @@ local val = 10 S:eval([[ local f = ...; assert(f(1) == 11) ]], function (x) return x + val end) S:eval([[ local o = ...; assert(o == name("a")) ]], name("a")) S:eval([[ local o = ...; assert(o == Const("a")) ]], Const("a")) -S:eval([[ local o = ...; assert(is_context(o)) ]], context()) -S:eval([[ local o = ...; assert(is_environment(o)) ]], environment()) +S:eval([[ local o = ...; assert(is_environment(o)) ]], empty_environment()) S:eval([[ local o = ...; assert(o == mpz(100)) ]], mpz(100)) S:eval([[ local o = ...; assert(o == mpq(100)/3) ]], mpq(100)/3) S:eval([[ local o = ...; assert(is_options(o)) ]], options()) S:eval([[ local o = ...; assert(is_sexpr(o)) ]], sexpr()) S:eval([[ local o = ...; assert(o:is_cons()) ]], sexpr(1, 2)) S:eval([[ local o = ...; assert(is_format(o)) ]], format("1")) -S:eval([[ local o = ...; assert(is_context_entry(o)) ]], context_entry("a", Const("T"))) -S:eval([[ local o = ...; assert(is_local_entry(o)) ]], mk_lift(1, 2)) -S:eval([[ local o = ...; assert(is_local_context(o)) ]], local_context()) S:eval([[ local o1, o2, o3 = ...; assert(is_sexpr(o1)); assert(is_name(o2)); assert(o3 == 10) ]], sexpr(), name("foo"), 10) assert(not pcall(function() S:eval([[ x = ]]) end)) local T = thread(S, [[ local x = ...; return x + 10, x - 10 ]], 10)