diff --git a/src/lua/effil.lua b/src/lua/effil.lua new file mode 100644 index 0000000..1dd69a9 --- /dev/null +++ b/src/lua/effil.lua @@ -0,0 +1,15 @@ +local function detect_native_lib_ext() + local home = os.getenv("HOME") + if not home then return "dll" end + if string.find(home, "/Users/") then return "dylib" end + if string.find(home, "/home/") then return "so" end + -- TODO: unable to detect os + -- how to reportabout error + return "so" +end + +package.cpath = package.cpath .. ";./?." .. detect_native_lib_ext() + +local api = require 'libeffil' + +return api \ No newline at end of file diff --git a/tests/lua/run_tests.lua b/tests/lua/run_tests.lua index c16f635..3d4d780 100755 --- a/tests/lua/run_tests.lua +++ b/tests/lua/run_tests.lua @@ -2,55 +2,6 @@ -- TODO: remove hardcode package.path = package.path .. ";../libs/luaunit/?.lua;../tests/lua/?.lua" -package.cpath = package.cpath .. ";./?.so;./?.dylib" - -test = require "luaunit" - -function log(...) - local msg = '@\t\t' .. os.date('%Y-%m-%d %H:%M:%S ',os.time()) - for _, val in ipairs({...}) do - msg = msg .. tostring(val) .. ' ' - end - io.write(msg .. '\n') - io.flush() -end - -function wait(timeInSec, condition, silent) - if not silent then - log("Start waiting for " .. tostring(timeInSec) .. "sec...") - end - local result = false - local startTime = os.time() - while ( (os.time() - startTime) <= timeInSec) do - if condition ~= nil then - if type(condition) == 'function' then - if condition() then - result = true - break - end - else - if condition then - result = true - break - end - end - end - end - if not silent then - log "Give up" - end - return result -end - -function sleep(timeInSec, silent) - if not silent then - log("Start sleep for " .. tostring(timeInSec) .. "sec...") - end - wait(timeInSec, nil, true) - if not silent then - log "Wake up" - end -end do -- Hack input arguments to make tests verbose by default @@ -71,7 +22,9 @@ end -- TESTS -- ----------- -require("smoke_test") +test = require "luaunit" +require 'test_utils' +require 'thread' +require 'shared_table' ------------ os.exit( test.LuaUnit.run() ) \ No newline at end of file diff --git a/tests/lua/shared_table.lua b/tests/lua/shared_table.lua new file mode 100644 index 0000000..d849bd5 --- /dev/null +++ b/tests/lua/shared_table.lua @@ -0,0 +1,49 @@ +TestSharedTable = {tearDown = tearDown} + +function TestSharedTable:testPairs() + local effil = require 'effil' + local share = effil.table() + local data = { 0, 0, 0, ["key1"] = 0, ["key2"] = 0, ["key3"] = 0 } + + for k, _ in pairs(data) do + share[k] = k .. "-value" + end + + for k,v in pairs(share) do + test.assertEquals(data[k], 0) + data[k] = 1 + test.assertEquals(v, k .. "-value") + end + + for k,v in pairs(data) do + log("Check: " .. k) + test.assertEquals(v, 1) + end + + for k,v in ipairs(share) do + test.assertEquals(data[k], 1) + data[k] = 2 + test.assertEquals(v, k .. "-value") + end + + for k,v in ipairs(data) do + log("Check: " .. k) + test.assertEquals(v, 2) + end +end + +function TestSharedTable:testLength() + local effil = require 'effil' + local share = effil.table() + share[1] = 10 + share[2] = 20 + share[3] = 30 + share[4] = 40 + share["str"] = 50 + log "Check values" + test.assertEquals(#share, 4) + share[3] = nil + test.assertEquals(#share, 2) + share[1] = nil + test.assertEquals(#share, 0) +end diff --git a/tests/lua/test_utils.lua b/tests/lua/test_utils.lua new file mode 100644 index 0000000..39930ec --- /dev/null +++ b/tests/lua/test_utils.lua @@ -0,0 +1,50 @@ +function log(...) + local msg = '@\t\t' .. os.date('%Y-%m-%d %H:%M:%S ',os.time()) + for _, val in ipairs({...}) do + msg = msg .. tostring(val) .. ' ' + end + io.write(msg .. '\n') + io.flush() +end + +function wait(timeInSec, condition, silent) + if not silent then + log("Start waiting for " .. tostring(timeInSec) .. "sec...") + end + local result = false + local startTime = os.time() + while ( (os.time() - startTime) <= timeInSec) do + if condition ~= nil then + if type(condition) == 'function' then + if condition() then + result = true + break + end + else + if condition then + result = true + break + end + end + end + end + if not silent then + log "Give up" + end + return result +end + +function sleep(timeInSec, silent) + if not silent then + log("Start sleep for " .. tostring(timeInSec) .. "sec...") + end + wait(timeInSec, nil, true) + if not silent then + log "Wake up" + end +end + +function tearDown() + log "TearDown() collect garbage" + collectgarbage() +end diff --git a/tests/lua/smoke_test.lua b/tests/lua/thread.lua similarity index 76% rename from tests/lua/smoke_test.lua rename to tests/lua/thread.lua index e5fa507..2d8fa2b 100644 --- a/tests/lua/smoke_test.lua +++ b/tests/lua/thread.lua @@ -1,39 +1,7 @@ -TestSmoke = {} +TestThread = {tearDown = tearDown } -function TestSmoke:tearDown() - log "TearDown() collect garbage" - collectgarbage() -end - -function TestSmoke:testSharedTableTypes() - local effil = require('libeffil') - local share = effil.table() - - share["number"] = 100500 - share["string"] = "string value" - share["bool"] = true - share["function"] = function(left, right) return left + right end - - local thread_factory = effil.thread( - function(share) - share["child.number"] = share["number"] - share["child.string"] = share["string"] - share["child.bool"] = share["bool"] - share["child.function"] = share["function"](11,45) - end - ) - local thread = thread_factory(share) - thread:wait() - - log "Check values" - test.assertEquals(share["child.number"], share["number"]) - test.assertEquals(share["child.string"], share["string"]) - test.assertEquals(share["child.bool"], share["bool"]) - test.assertEquals(share["child.function"], share["function"](11,45)) -end - -function TestSmoke:testThreadCancel() - local effil = require('libeffil') +function TestThread:testThreadCancel() + local effil = require 'effil' local thread_runner = effil.thread( function() local startTime = os.time() @@ -49,8 +17,8 @@ function TestSmoke:testThreadCancel() test.assertEquals(thread:status(), 'canceled') end -function TestSmoke:testThreadPauseAndResume() - local effil = require('libeffil') +function TestThread:testThreadPauseAndResume() + local effil = require 'effil' local data = effil.table() data.value = 0 local thread_runner = effil.thread( @@ -76,8 +44,8 @@ function TestSmoke:testThreadPauseAndResume() thread:wait() end -function TestSmoke:testThreadPauseAndStop() - local effil = require('libeffil') +function TestThread:testThreadPauseAndStop() + local effil = require 'effil' log "Create thread" local data = effil.table() data.value = 0 @@ -103,8 +71,8 @@ function TestSmoke:testThreadPauseAndStop() thread:wait() end -function TestSmoke:testThreadPauseAndStop() - local effil = require('libeffil') +function TestThread:testThreadPauseAndStop() + local effil = require 'effil' log "Create thread" local data = effil.table() data.value = 0 @@ -130,64 +98,8 @@ function TestSmoke:testThreadPauseAndStop() thread:wait() end -function TestSmoke:testRecursiveTables() - local effil = require('libeffil') - local share = effil.table() - - local magic_number = 42 - share["subtable1"] = effil.table() - share["subtable1"]["subtable1"] = effil.table() - share["subtable1"]["subtable2"] = share["subtable1"]["subtable1"] - share["subtable2"] = share["subtable1"]["subtable1"] - share["magic_number"] = magic_number - - local thread_factory = effil.thread( - function(share) - share["subtable1"]["subtable1"]["magic_number"] = share["magic_number"] - share["magic_number"] = nil - end - ) - local thread = thread_factory(share) - thread:wait() - - log "Check values" - test.assertEquals(share["subtable1"]["subtable1"]["magic_number"], magic_number) - test.assertEquals(share["subtable1"]["subtable2"]["magic_number"], magic_number) - test.assertEquals(share["subtable2"]["magic_number"], magic_number) - test.assertEquals(share["magic_number"], nil) -end - -function TestSmoke:testThisThreadFunctions() - local effil = require('libeffil') - local share = effil.table() - - local thread_factory = effil.thread( - function(share) - share["child.id"] = require('libeffil').thread_id() - end - ) - local thread = thread_factory(share) - thread:wait() - - log "Check values" - test.assertString(share["child.id"]) - test.assertNumber(tonumber(share["child.id"])) - test.assertNotEquals(share["child.id"], effil.thread_id()) - effil.yield() -- just call it - - local function check_time(real_time, use_time, metric) - local start_time = os.time() - effil.sleep(use_time, metric) - test.assertAlmostEquals(os.time(), start_time + real_time, 1) - end - check_time(4, 4, nil) -- seconds by default - check_time(4, 4, 's') - check_time(4, 4000, 'ms') - check_time(60, 1, 'm') -end - -function TestSmoke:testCheckThreadReturns() - local effil = require('libeffil') +function TestThread:testCheckThreadReturns() + local effil = require 'effil' local share = effil.table() share.value = "some value" @@ -218,50 +130,88 @@ function TestSmoke:testCheckThreadReturns() test.assertEquals(returns[5](11, 89), 100) end -function TestSmoke:testCheckPairsInterating() - local effil = require('libeffil') + +TestThreadWithTable = {tearDown = tearDown } + +function TestThreadWithTable:testSharedTableTypes() + local effil = require 'effil' local share = effil.table() - local data = { 0, 0, 0, ["key1"] = 0, ["key2"] = 0, ["key3"] = 0 } - for k, _ in pairs(data) do - share[k] = k .. "-value" - end + share["number"] = 100500 + share["string"] = "string value" + share["bool"] = true + share["function"] = function(left, right) return left + right end - for k,v in pairs(share) do - test.assertEquals(data[k], 0) - data[k] = 1 - test.assertEquals(v, k .. "-value") - end + local thread_factory = effil.thread( + function(share) + share["child.number"] = share["number"] + share["child.string"] = share["string"] + share["child.bool"] = share["bool"] + share["child.function"] = share["function"](11,45) + end + ) + local thread = thread_factory(share) + thread:wait() - for k,v in pairs(data) do - log("Check: " .. k) - test.assertEquals(v, 1) - end - - for k,v in ipairs(share) do - test.assertEquals(data[k], 1) - data[k] = 2 - test.assertEquals(v, k .. "-value") - end - - for k,v in ipairs(data) do - log("Check: " .. k) - test.assertEquals(v, 2) - end -end - -function TestSmoke:testCheckLengthOperator() - local effil = require('libeffil') - local share = effil.table() - share[1] = 10 - share[2] = 20 - share[3] = 30 - share[4] = 40 - share["str"] = 50 log "Check values" - test.assertEquals(#share, 4) - share[3] = nil - test.assertEquals(#share, 2) - share[1] = nil - test.assertEquals(#share, 0) + test.assertEquals(share["child.number"], share["number"]) + test.assertEquals(share["child.string"], share["string"]) + test.assertEquals(share["child.bool"], share["bool"]) + test.assertEquals(share["child.function"], share["function"](11,45)) +end + +function TestThreadWithTable:testRecursiveTables() + local effil = require 'effil' + local share = effil.table() + + local magic_number = 42 + share["subtable1"] = effil.table() + share["subtable1"]["subtable1"] = effil.table() + share["subtable1"]["subtable2"] = share["subtable1"]["subtable1"] + share["subtable2"] = share["subtable1"]["subtable1"] + share["magic_number"] = magic_number + + local thread_factory = effil.thread( + function(share) + share["subtable1"]["subtable1"]["magic_number"] = share["magic_number"] + share["magic_number"] = nil + end + ) + local thread = thread_factory(share) + thread:wait() + + log "Check values" + test.assertEquals(share["subtable1"]["subtable1"]["magic_number"], magic_number) + test.assertEquals(share["subtable1"]["subtable2"]["magic_number"], magic_number) + test.assertEquals(share["subtable2"]["magic_number"], magic_number) + test.assertEquals(share["magic_number"], nil) +end + +function TestThreadWithTable:testThisThreadFunctions() + local effil = require 'effil' + local share = effil.table() + + local thread_factory = effil.thread( + function(share) + share["child.id"] = require('libeffil').thread_id() + end + ) + local thread = thread_factory(share) + thread:wait() + + log "Check values" + test.assertString(share["child.id"]) + test.assertNumber(tonumber(share["child.id"])) + test.assertNotEquals(share["child.id"], effil.thread_id()) + effil.yield() -- just call it + + local function check_time(real_time, use_time, metric) + local start_time = os.time() + effil.sleep(use_time, metric) + test.assertAlmostEquals(os.time(), start_time + real_time, 1) + end + check_time(4, 4, nil) -- seconds by default + check_time(4, 4, 's') + check_time(4, 4000, 'ms') + check_time(60, 1, 'm') end