From 57b178fda7aa5ec5eea6286426429abbc8cc38dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferenc=20B=C3=B6r=C3=B6czki?= Date: Fri, 2 Aug 2019 16:34:11 +0200 Subject: [PATCH] test require and metamethods with a simple decimal lib --- test/luerl_return_SUITE.erl | 24 ++++++-- test/luerl_return_SUITE_data/decimal.lua | 56 +++++++++++++++++++ test/luerl_return_SUITE_data/decimal_test.lua | 31 ++++++++++ 3 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 test/luerl_return_SUITE_data/decimal.lua create mode 100644 test/luerl_return_SUITE_data/decimal_test.lua diff --git a/test/luerl_return_SUITE.erl b/test/luerl_return_SUITE.erl index 59c0c0c3..4c30eebd 100644 --- a/test/luerl_return_SUITE.erl +++ b/test/luerl_return_SUITE.erl @@ -16,8 +16,16 @@ -include_lib("common_test/include/ct.hrl"). --export([all/0, groups/0]). --export([simple_return/1, fun_return/1]). +-export([all/0, groups/0, init_per_suite/1, end_per_suite/1]). +-export([simple_return/1, fun_return/1, use_lib/1]). + +init_per_suite(Config) -> + DataDir = ?config(data_dir, Config), + os:putenv("LUA_PATH", DataDir ++ "?.lua;" ++ DataDir ++ "?/init.lua"), + Config. + +end_per_suite(Config) -> + Config. all() -> [ @@ -26,7 +34,7 @@ all() -> groups() -> [ - {return, [parallel], [simple_return, fun_return]} + {return, [parallel], [simple_return, fun_return, use_lib]} ]. simple_return(Config) -> @@ -39,11 +47,17 @@ simple_return(Config) -> fun_return(Config) -> run_and_check(Config, "fun_return_multi.lua", [7, <<"str 1">>, 5.5, 11.0]). +use_lib(Config) -> + LuaDecimal = fun(B, E) -> [{<<"b">>, B}, {<<"e">>, E}] end, + Expected = [LuaDecimal(B, E) || {B, E} <- [{13, 1}, {7, 1}, {3, 3}]], + run_and_check(Config, "decimal_test.lua", Expected). + run_tests(Config, Tests) -> [run_and_check(Config, Script, Expected) || {Script, Expected} <- Tests]. run_and_check(Config, Script, Expected) -> DataDir = ?config(data_dir, Config), ScriptFile = DataDir ++ Script, - {Result, _St} = luerl:dofile(ScriptFile), - Expected = Result. + {ok, Result} = luerl:evalfile(ScriptFile), + {true, {expected, Expected}, {result, Result}} = + {Result =:= Expected, {expected, Expected}, {result, Result}}. diff --git a/test/luerl_return_SUITE_data/decimal.lua b/test/luerl_return_SUITE_data/decimal.lua new file mode 100644 index 00000000..bb5737a9 --- /dev/null +++ b/test/luerl_return_SUITE_data/decimal.lua @@ -0,0 +1,56 @@ +local Decimal = {} + +Decimal.mt = { + __tostring = function(d) + return d.b .. "e" .. d.e + end, + + __add = function(a, b) + a, b = Decimal.same_e(a, b) + + if a.e == b.e then + return Decimal.new(a.b + b.b, a.e) + end + end, + __sub = function(a, b) + return a + Decimal.new(-b.b, b.e) + end, + __mul = function(a, b) + return Decimal.new(a.b * b.b, a.e + b.e) + end, + __div = function(a, b) + -- @todo fix + return Decimal.new(a.b / b.b, a.e - b.e) + end, + + __eq = function(a, b) + a, b = Decimal.same_e(a, b) + return a.b == b.b and a.e == b.e + end +} + +Decimal.new = function(b, e) + local d = { + b = b, + e = e + } + setmetatable(d, Decimal.mt) + return d +end + +Decimal.change_e = function(d, e) + if d.e == e then + return d + end + if d.e > e then + return Decimal.change_e(Decimal.new(10 * d.b, d.e - 1), e) + end + error("can't increment exponential") +end + +Decimal.same_e = function(a, b) + local min_e = math.min(a.e, b.e) + return Decimal.change_e(a, min_e), Decimal.change_e(b, min_e) +end + +return Decimal diff --git a/test/luerl_return_SUITE_data/decimal_test.lua b/test/luerl_return_SUITE_data/decimal_test.lua new file mode 100644 index 00000000..00fae813 --- /dev/null +++ b/test/luerl_return_SUITE_data/decimal_test.lua @@ -0,0 +1,31 @@ +local d = require("decimal") + +a = d.new(1, 2) -- 100 +b = d.new(3, 1) -- 30 + +print(a, "+", b, "=", a + b) +print(a, "-", b, "=", a - b) +print(a, "*", b, "=", a * b) +print(a, "/", b, "=", a / b) + +x1 = d.new(1, 2) +x2 = d.new(2, 2) +x3 = d.new(2, 1) +x4 = d.new(10, 1) +x5 = d.new(10, 2) +print(a, "==", b, "=", a == b) +print(a, "==", x1, "=", a == x1) +print(a, "==", x2, "=", a == x2) +print(a, "==", x3, "=", a == x3) +print(a, "==", x4, "=", a == x4) +print(a, "==", x5, "=", a == x5) + +assert(a + b == d.new(130, 0)) +assert(a + b == d.new(13, 1)) +assert(a - b == d.new(70, 0)) +assert(a - b == d.new(7, 1)) +assert(a * b == d.new(3000, 0)) +assert(a * b == d.new(3, 3)) +--assert(a / b == d.new(33333333333333, -15)) + +return a + b, a - b, a * b