test_string_to_table/init.lua

54 lines
1.5 KiB
Lua

test_string_to_table ={}
test_string_to_table.modpath = minetest.get_modpath("test_string_to_table") .. DIR_DELIM
dofile(test_string_to_table.modpath .. "alg1.lua")
dofile(test_string_to_table.modpath .. "alg2.lua")
dofile(test_string_to_table.modpath .. "alg3.lua")
dofile(test_string_to_table.modpath .. "algtour.lua")
local tests = {
'{1, 2, 3}',
'{a = 1; b = 2; c = 3}',
'{a = {b = {c = 3}}}',
'{1, "hello", true, false, nil, 3.14}',
'{[1] = "one", [2] = "two", ["three"] = 3}',
'{"quoted string with an escaped quote \\"", \'single quoted\'}',
'{nested = {tables = {are = {supported = true}}}}',
"{key1 = 123, key2 = true, key3 = {nestedKey1 = 'value1', nestedKey2 = false}}",
"{}", "{ }",
"{[{'tables can be'}] = 'keys too'}",
'{\'{table = "that", "contains", a = "string", ["looking"] = {"like", "a"; "table"}}\'}',
'{[-42.42e42] = 42.42E-42}',
'{1, 2, 3, }',
-- invalid tables, the code should return an error
--[[
"{a = 1",
"{'unclosed string\\'}",
"{invalid key = 3}",
"{false = 2}", -- invalid key
"{1 = 2}", -- invalid key
"{,}",
"{[nil] = 1}"
--]]
}
local function test(alg)
-- alg1
local outcome = {}
local t1 = minetest.get_us_time()
for _, case in ipairs(tests) do
local ret = {case, test_string_to_table[alg](case)}
table.insert(outcome, ret)
end
local t2 = minetest.get_us_time()
minetest.log("action",alg .. " = " .. (t2-t1))
-- print(dump(outcome))
end
test("s2t1")
test("s2t2")
-- test("s2t3")
test("s2ttour")