Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for int64 (type 'h') and add workaround for lua < 5.3 #12

Merged
merged 1 commit into from
Jan 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .luacov
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
include = {
'src'
'src%/losc$',
'src%/losc%/.+$',
'src%/losc%/plugins%/.+$',
}
exclude = {
'src/losc/lib/.+$',
'src/losc/serializer'
'src/losc/serializer',
}
53 changes: 53 additions & 0 deletions spec/types/double_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,56 @@ describe('double', function()
end)
end)
end)

-- https://github.com/iryont/lua-struct/issues/3
-- lua < 5.3 will truncate the value to a signed 32-bit integer.
describe('int64', function()
local data, expected_bytes
local value = 9223372036854775807
if string.pack then
expected_bytes = {127, 255, 255, 255, 255, 255, 255, 255}
else
expected_bytes = {127, 255, 255, 255}
end

describe('pack', function()
setup(function()
data = Types.pack.h(value)
end)

it('returns the correct byte representation', function()
local bytes = {string.byte(data, 1, -1)}
for i, byte in ipairs(bytes) do
assert.are.equal(expected_bytes[i], byte)
end
end)

it('returns a multiple of 32', function()
assert.are.equal(#data * 8 % 32, 0)
end)
end)

describe('unpack', function()
local h, offset

setup(function()
h, offset = Types.unpack.h(data)
end)

it('returns the correct offset', function()
if string.pack then
assert.are.equal(offset, 9)
else
assert.are.equal(offset, 5)
end
end)

it('returns the correct value', function()
if string.pack then
assert.are.equal(value, h)
else
assert.are.equal(2147483647, h)
end
end)
end)
end)
29 changes: 23 additions & 6 deletions src/losc/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -162,21 +162,38 @@ end
--- Extended types.
-- @section extended-types

--- 64 bit big-endian two's complement integer
--- 64 bit big-endian two's complement integer.
--
-- WARNING This type is only supported for lua >= 5.3.
-- versions < 5.3 versions will truncate the value to a signed 32-bit integer.
-- @param value The value to pack.
-- @return Binary string buffer.
Types.pack.h = function(value)
local fmt = has_string_pack and '>i8' or '>l'
return _pack(fmt, value)
if has_string_pack then
return _pack('>i8', value)
else
if value > 2147483647 then
value = 2147483647
elseif value < -2147483648 then
value = -2147483648
end
return _pack('>i4', value)
end
end

--- 64 bit big-endian two's complement integer
--- 64 bit big-endian two's complement integer.
--
-- WARNING This type is only supported for lua >= 5.3.
-- versions < 5.3 versions will truncate the value to a signed 32-bit integer.
-- @param data The data to unpack.
-- @param[opt] offset Initial offset into data.
-- @return value, index of the bytes read + 1.
Types.unpack.h = function(data, offset)
local fmt = has_string_pack and '>i8' or '>l'
return _unpack(fmt, data, offset)
if has_string_pack then
return _unpack('>i8', data, offset)
else
return _unpack('>i4', data, offset)
end
end

--- Timetag (64-bit integer divided into upper and lower part)
Expand Down