Bluon: Fix integer serialization & tests

This commit is contained in:
Lars Mueller 2021-03-13 16:19:57 +01:00
parent 7dea86fec2
commit 7a92f3b492
2 changed files with 17 additions and 10 deletions

@ -217,7 +217,7 @@ function write(self, object, stream)
return
end
if object % 1 == 0 then
uint_with_type(object > 0 and type_ranges.number_constant or type_ranges.number_positive, object > 0 and object or -object)
uint_with_type(object > 0 and type_ranges.number_constant or type_ranges.number_negative, object > 0 and object or -object)
return
end
float(object)
@ -337,11 +337,11 @@ function read(self, stream)
end
type = type:byte()
if type <= type_ranges.number then
if type <= type_ranges.number_positive then
if type <= type_ranges.number_negative then
return uint(type - type_ranges.number_constant)
end
if type <= type_ranges.number_negative then
return -uint(type - type_ranges.number_positive)
if type <= type_ranges.number_positive then
return -uint(type - type_ranges.number_negative)
end
return float(type == type_ranges.number)
end

@ -134,20 +134,27 @@ do
written = rope:to_text()
input = modlib.text.inputstream(written)
read = bluon:read(input)
local remaining = input:read(1000)
assert(not remaining)
end)
-- TODO assertdump
local remaining = input:read(69)
assert(not remaining, remaining and modlib.text.hexdump(remaining))
assert(modlib.table.equals_references(object, read), dump{object = object, read = read, written = written and modlib.text.hexdump(written), err = err})
assert(modlib.table.equals_references(object, read) and not err, dump{
object = object,
read = read,
written = written and modlib.text.hexdump(written),
err = err
})
end
for _, constant in pairs{true, false, math.huge, -math.huge} do
assert_preserves(constant)
end
for i = 1, 69 do
assert_preserves(table.concat(modlib.table.repetition("x", i)))
for i = 1, 1000 do
assert_preserves(table.concat(modlib.table.repetition(string.char(i % 256), i)))
end
for _ = 1, 1000 do
assert_preserves(math.random(2^-50, 2^50))
local int = math.random(-2^50, 2^50)
assert(int % 1 == 0)
assert_preserves(int)
assert_preserves((math.random() - 0.5) * 2^math.random(-20, 20))
end
assert_preserves{hello = "world", welt = "hallo"}