Skip to content
This repository has been archived by the owner on Apr 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #19 from zhucebuliaopx/master
Browse files Browse the repository at this point in the history
fix pr#17 && unit test && add Travis CI
  • Loading branch information
iryont authored Nov 22, 2020
2 parents 294e7ef + c787329 commit a860460
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: c

env:
- LUA=""

branches:
only:
- master

install:
- sudo apt-get install luarocks
- sudo luarocks install busted
- sudo luarocks make *.rockspec;

script: "busted spec"
7 changes: 4 additions & 3 deletions lua-struct.rockspec → lua-struct-0.9.2-1.rockspec
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package = "lua-struct"
version = "@VERSION@-@REVISION@"
version = "0.9.2-1"

source = {
url = "git://github.com/iryont/lua-struct.git"
url = "git://github.com/iryont/lua-struct.git",
tag = "0.9.2-1",
}

description = {
Expand All @@ -21,7 +22,7 @@ build = {
type = 'none',
install = {
lua = {
['struct'] = 'struct.lua'
['struct'] = 'src/struct.lua'
}
}
}
70 changes: 70 additions & 0 deletions spec/struct_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
local struct = require "struct"

describe("struct test", function ()

it("test ss", function ()
local s1 = "test1"
local s2 = "test2"
local packed = struct.pack("<ss", s1, s2)
local uns1, uns2 = struct.unpack("<ss", packed)
assert.same(uns1, s1)
assert.same(uns2, s2)
end)

it("test s", function ()
local value = "test"
local packed = struct.pack("<s", value)
local unpacked = struct.unpack("<s", packed)
assert.same(unpacked, value)
end)

it("test L", function ()
local value = 12345678912345678
local packed = struct.pack('<L', value)
local unpacked = struct.unpack('<L', packed)
assert.same(unpacked, value)
end)

it("test I", function ()
local value = 123456789
local packed = struct.pack('<I', value)
local unpacked = struct.unpack('<I', packed)
assert.same(unpacked, value)
end)

it("test h", function ()
local value = -3200
local packed = struct.pack('<h', value)
local unpacked = struct.unpack('<h', packed)
assert.same(unpacked, value)
end)

it("test B", function ()
local value = 255
local packed = struct.pack('<B', value)
local unpacked = struct.unpack('<B', packed)
assert.same(unpacked, value)
end)

it("test b", function ()
local value = -1
local packed = struct.pack('<b', value)
local unpacked = struct.unpack('<b', packed)
assert.same(unpacked, value)
end)

it("test f", function ()
local value = 1.56789
local packed = struct.pack('<f', value)
local unpacked = struct.unpack('<f', packed)
assert.is.truthy(unpacked < value)
end)

it("test d", function ()
local value = 1.56789
local packed = struct.pack('<d', value)
local unpacked = struct.unpack('<d', packed)
assert.same(unpacked, value)
end)

end)
2 changes: 1 addition & 1 deletion struct.lua → src/struct.lua
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ function struct.unpack(format, stream, pos)
elseif opt == 's' then
local bytes = {}
for j = iterator, stream:len() do
if stream:sub(j) == '' then
if stream:sub(j,j) == string.char(0) or stream:sub(j) == '' then
break
end

Expand Down

0 comments on commit a860460

Please sign in to comment.