mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-26 09:13:53 +01:00
Luon: Rename (de)serialize to read/write
This commit is contained in:
parent
60dd1be82c
commit
4026f6dd0d
20
luon.lua
20
luon.lua
@ -15,7 +15,7 @@ end
|
|||||||
local _ENV = {}
|
local _ENV = {}
|
||||||
setfenv(1, _ENV)
|
setfenv(1, _ENV)
|
||||||
|
|
||||||
function serialize(object, write)
|
function write(object, write)
|
||||||
local reference = {"A"}
|
local reference = {"A"}
|
||||||
local function increment_reference(place)
|
local function increment_reference(place)
|
||||||
if not reference[place] then
|
if not reference[place] then
|
||||||
@ -129,21 +129,21 @@ function serialize(object, write)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function serialize_file(object, file)
|
function write_file(object, file)
|
||||||
return serialize(object, function(text)
|
return write(object, function(text)
|
||||||
file:write(text)
|
file:write(text)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
function serialize_string(object)
|
function write_string(object)
|
||||||
local rope = {}
|
local rope = {}
|
||||||
serialize(object, function(text)
|
write(object, function(text)
|
||||||
table_insert(rope, text)
|
table_insert(rope, text)
|
||||||
end)
|
end)
|
||||||
return table_concat(rope)
|
return table_concat(rope)
|
||||||
end
|
end
|
||||||
|
|
||||||
function deserialize(...)
|
function read(...)
|
||||||
local read = assert(...)
|
local read = assert(...)
|
||||||
-- math.huge is serialized to inf, 0/0 is serialized to -nan
|
-- math.huge is serialized to inf, 0/0 is serialized to -nan
|
||||||
setfenv(read, {inf = math_huge, nan = 0/0})
|
setfenv(read, {inf = math_huge, nan = 0/0})
|
||||||
@ -154,12 +154,12 @@ function deserialize(...)
|
|||||||
return nil, value_or_err
|
return nil, value_or_err
|
||||||
end
|
end
|
||||||
|
|
||||||
function deserialize_file(path)
|
function read_file(path)
|
||||||
return deserialize(loadfile(path))
|
return read(loadfile(path))
|
||||||
end
|
end
|
||||||
|
|
||||||
function deserialize_string(string)
|
function read_string(string)
|
||||||
return deserialize(loadstring(string))
|
return read(loadstring(string))
|
||||||
end
|
end
|
||||||
|
|
||||||
return _ENV
|
return _ENV
|
69
test.lua
69
test.lua
@ -166,10 +166,41 @@ for _ = 1, 1000 do
|
|||||||
assert(distance == min_distance)
|
assert(distance == min_distance)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function serializer_test(assert_preserves)
|
||||||
|
-- TODO nan
|
||||||
|
for _, constant in pairs{true, false, huge, -huge} do
|
||||||
|
assert_preserves(constant)
|
||||||
|
end
|
||||||
|
-- Strings
|
||||||
|
for i = 1, 1000 do
|
||||||
|
assert_preserves(_G.table.concat(table.repetition(_G.string.char(i % 256), i)))
|
||||||
|
end
|
||||||
|
-- Numbers
|
||||||
|
for _ = 1, 1000 do
|
||||||
|
local int = random(-2^50, 2^50)
|
||||||
|
assert(int % 1 == 0)
|
||||||
|
assert_preserves(int)
|
||||||
|
assert_preserves((random() - 0.5) * 2^random(-20, 20))
|
||||||
|
end
|
||||||
|
-- Simple tables
|
||||||
|
assert_preserves{hello = "world", welt = "hallo"}
|
||||||
|
assert_preserves{"hello", "hello", "hello"}
|
||||||
|
local circular = {}
|
||||||
|
circular[circular] = circular
|
||||||
|
circular[1] = circular
|
||||||
|
assert_preserves(circular)
|
||||||
|
local mixed = {1, 2, 3}
|
||||||
|
mixed[mixed] = mixed
|
||||||
|
mixed.vec = {x = 1, y = 2, z = 3}
|
||||||
|
mixed.vec2 = modlib.table.copy(mixed.vec)
|
||||||
|
mixed.blah = "blah"
|
||||||
|
assert_preserves(mixed)
|
||||||
|
end
|
||||||
|
|
||||||
-- bluon
|
-- bluon
|
||||||
do
|
do
|
||||||
local bluon = bluon
|
local bluon = bluon
|
||||||
local function assert_preserves(object)
|
serializer_test(function(object)
|
||||||
local rope = table.rope{}
|
local rope = table.rope{}
|
||||||
local written, read, input
|
local written, read, input
|
||||||
local _, err = pcall(function()
|
local _, err = pcall(function()
|
||||||
@ -186,39 +217,17 @@ do
|
|||||||
written = written and text.hexdump(written),
|
written = written and text.hexdump(written),
|
||||||
err = err
|
err = err
|
||||||
})
|
})
|
||||||
end
|
end)
|
||||||
for _, constant in pairs{true, false, huge, -huge} do
|
|
||||||
assert_preserves(constant)
|
|
||||||
end
|
|
||||||
for i = 1, 1000 do
|
|
||||||
assert_preserves(_G.table.concat(table.repetition(_G.string.char(i % 256), i)))
|
|
||||||
end
|
|
||||||
for _ = 1, 1000 do
|
|
||||||
local int = random(-2^50, 2^50)
|
|
||||||
assert(int % 1 == 0)
|
|
||||||
assert_preserves(int)
|
|
||||||
assert_preserves((random() - 0.5) * 2^random(-20, 20))
|
|
||||||
end
|
|
||||||
assert_preserves{hello = "world", welt = "hallo"}
|
|
||||||
assert_preserves{"hello", "hello", "hello"}
|
|
||||||
local a = {}
|
|
||||||
a[a] = a
|
|
||||||
a[1] = a
|
|
||||||
assert_preserves(a)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- luon
|
-- luon
|
||||||
do
|
do
|
||||||
local function assert_preserves(object)
|
serializer_test(function(object)
|
||||||
local serialized = luon.serialize_string(object)
|
local serialized = luon.write_string(object)
|
||||||
assert(table.equals_references(object, luon.deserialize_string(serialized)), serialized)
|
assert(table.equals_references(object, luon.read_string(serialized)), serialized)
|
||||||
end
|
end)
|
||||||
local tab = {}
|
local nan = luon.read_string(luon.write_string(0/0))
|
||||||
tab[tab] = tab
|
assert(nan ~= nan)
|
||||||
tab.vec = {x = 1, y = 2, z = 3}
|
|
||||||
tab.vec2 = modlib.table.copy(tab.vec)
|
|
||||||
tab.blah = "blah"
|
|
||||||
assert_preserves(tab)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if not _G.minetest then return end
|
if not _G.minetest then return end
|
||||||
|
Loading…
Reference in New Issue
Block a user