Skip to content

Commit

Permalink
test require and metamethods with a simple decimal lib
Browse files Browse the repository at this point in the history
  • Loading branch information
f3c0 committed Aug 2, 2019
1 parent 4a4aaed commit 57b178f
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 5 deletions.
24 changes: 19 additions & 5 deletions test/luerl_return_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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() ->
[
Expand All @@ -26,7 +34,7 @@ all() ->

groups() ->
[
{return, [parallel], [simple_return, fun_return]}
{return, [parallel], [simple_return, fun_return, use_lib]}
].

simple_return(Config) ->
Expand All @@ -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}}.
56 changes: 56 additions & 0 deletions test/luerl_return_SUITE_data/decimal.lua
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions test/luerl_return_SUITE_data/decimal_test.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 57b178f

Please sign in to comment.