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 return
end end
if object % 1 == 0 then 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 return
end end
float(object) float(object)
@ -337,11 +337,11 @@ function read(self, stream)
end end
type = type:byte() type = type:byte()
if type <= type_ranges.number then 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) return uint(type - type_ranges.number_constant)
end end
if type <= type_ranges.number_negative then if type <= type_ranges.number_positive then
return -uint(type - type_ranges.number_positive) return -uint(type - type_ranges.number_negative)
end end
return float(type == type_ranges.number) return float(type == type_ranges.number)
end end

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