From 9af2d64ad9e110acf6f1fc33a5480a45a658c465 Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Mon, 9 Aug 2021 13:46:15 +0200 Subject: [PATCH] Bluon: Fix nan reading --- bluon.lua | 1 + test.lua | 58 +++++++++++++++++++++++++++---------------------------- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/bluon.lua b/bluon.lua index 43611e9..8e6742b 100644 --- a/bluon.lua +++ b/bluon.lua @@ -260,6 +260,7 @@ function write(self, value, stream) end local constants_flipped = modlib.table.flip(constants) +constants_flipped[constant_nan] = 0/0 -- See https://www.lua.org/manual/5.1/manual.html#2.2 function read(self, stream) diff --git a/test.lua b/test.lua index 63c9a37..7b52e28 100644 --- a/test.lua +++ b/test.lua @@ -228,9 +228,17 @@ for _ = 1, 1000 do assert(distance == min_distance) end -local function serializer_test(assert_preserves) - -- TODO nan - for _, constant in pairs{true, false, huge, -huge} do +local function serializer_test(preserve) + local function assert_preserves(obj) + local preserved = preserve(obj) + if obj ~= obj then + assert(preserved ~= preserved) + else + assert(table.equals_references(preserved, obj), luon:write_string(preserved)) + end + end + -- TODO proper deep table comparison with nan support + for _, constant in pairs{true, false, huge, -huge, 0/0} do assert_preserves(constant) end -- Strings @@ -265,37 +273,27 @@ local function serializer_test(assert_preserves) assert_preserves(a) end --- bluon -do - local bluon = bluon - serializer_test(function(object) - local rope = table.rope{} - local written, read, input - local _, err = pcall(function() - bluon:write(object, rope) - written = rope:to_text() - input = text.inputstream(written) - read = bluon:read(input) - local remaining = input:read(1000) - assert(not remaining) - end) - assertdump(table.equals_references(object, read) and not err, { - object = object, - read = read, - written = written and text.hexdump(written), - err = err - }) - end) -end - -- luon do serializer_test(function(object) - local serialized = luon:write_string(object) - assert(table.equals_references(object, luon:read_string(serialized)), serialized) + return luon:read_string(luon:write_string(object)) + end) +end + +-- bluon +do + -- TODO 1.1496387980481e-07 fails due to precision issues + serializer_test(function(object) + local rope = table.rope{} + local written, read, input + bluon:write(object, rope) + written = rope:to_text() + input = text.inputstream(written) + read = bluon:read(input) + local remaining = input:read(1) + assert(not remaining) + return read end) - local nan = luon:read_string(luon:write_string(0/0)) - assert(nan ~= nan) end if not _G.minetest then return end