package.path = "./?/init.lua;".. package.path local vstruct = require"vstruct" local inf = math.huge -- work around a bug in the Lua constant table optimizer local z = 0 local nz = -z function to_hex(bytes, sep) local hexes = {} local append = table.insert for b in bytes:gmatch"." do append(hexes, ("%02x"):format(b:byte())) end return table.concat(hexes, sep) end function test(num, bytes) local packed_bytes = assert(vstruct.pack("< f8", { num })) local status = packed_bytes == bytes and "PASS" or "FAIL" io.write(("%s packing %-30.20g"):format(status, num)) if status == "FAIL" then io.write(("\twanted %-30s got %-30s"):format( to_hex(bytes), to_hex(packed_bytes))) end io.write"\n" local unpacked_num = unpack((assert(vstruct.unpack("< f8", bytes)))) local status = unpacked_num == num and "PASS" or "FAIL" io.write(("%s unpacking %-30s"):format(status, to_hex(bytes))) if status == "FAIL" then io.write(("\twanted %-30.20g got %-30.20g"):format( num, unpacked_num)) end io.write"\n" end test(0, "\000\000\000\000\000\000\000\000") test(4.9406564584124654418e-324, "\001\000\000\000\000\000\000\000") test(7.4169128616906696301e-309, "\085\085\085\085\085\085\005\000") test(1.483382572338133926e-308, "\170\170\170\170\170\170\010\000") test(2.225073858507200889e-308, "\255\255\255\255\255\255\015\000") test(2.2250738585072013831e-308, "\000\000\000\000\000\000\016\000") test(2.2250738585072018772e-308, "\001\000\000\000\000\000\016\000") test(2.9667651446762683461e-308, "\085\085\085\085\085\085\021\000") test(3.7084564308453353091e-308, "\170\170\170\170\170\170\026\000") test(4.4501477170144022721e-308, "\255\255\255\255\255\255\031\000") test(8.9884656743115795386e+307, "\000\000\000\000\000\000\224\127") test(8.9884656743115815345e+307, "\001\000\000\000\000\000\224\127") test(1.1984620899082105386e+308, "\085\085\085\085\085\085\229\127") test(1.4980776123852631234e+308, "\170\170\170\170\170\170\234\127") test(1.7976931348623157081e+308, "\255\255\255\255\255\255\239\127") test(4.9406564584124654418e-324, "\001\000\000\000\000\000\000\000") test(2.225073858507200889e-308, "\255\255\255\255\255\255\015\000") test(2.2250738585072013831e-308, "\000\000\000\000\000\000\016\000") test(1.7976931348623157081e+308, "\255\255\255\255\255\255\239\127") test(inf, "\000\000\000\000\000\000\240\127") test(0, "\000\000\000\000\000\000\000\000") test(nz, "\000\000\000\000\000\000\000\128") test(-4.9406564584124654418e-324, "\001\000\000\000\000\000\000\128") test(-7.4169128616906696301e-309, "\085\085\085\085\085\085\005\128") test(-1.483382572338133926e-308, "\170\170\170\170\170\170\010\128") test(-2.225073858507200889e-308, "\255\255\255\255\255\255\015\128") test(-2.2250738585072013831e-308, "\000\000\000\000\000\000\016\128") test(-2.2250738585072018772e-308, "\001\000\000\000\000\000\016\128") test(-2.9667651446762683461e-308, "\085\085\085\085\085\085\021\128") test(-3.7084564308453353091e-308, "\170\170\170\170\170\170\026\128") test(-4.4501477170144022721e-308, "\255\255\255\255\255\255\031\128") test(-8.9884656743115795386e+307, "\000\000\000\000\000\000\224\255") test(-8.9884656743115815345e+307, "\001\000\000\000\000\000\224\255") test(-1.1984620899082105386e+308, "\085\085\085\085\085\085\229\255") test(-1.4980776123852631234e+308, "\170\170\170\170\170\170\234\255") test(-1.7976931348623157081e+308, "\255\255\255\255\255\255\239\255") test(-4.9406564584124654418e-324, "\001\000\000\000\000\000\000\128") test(-2.225073858507200889e-308, "\255\255\255\255\255\255\015\128") test(-2.2250738585072013831e-308, "\000\000\000\000\000\000\016\128") test(-1.7976931348623157081e+308, "\255\255\255\255\255\255\239\255") test(-inf, "\000\000\000\000\000\000\240\255") test(nz, "\000\000\000\000\000\000\000\128")