Download:
child 11:5270185b6e41
parent 9:f3ef81a885aa
10:22909b024e86
Anton Shestakov <av6@dwimlabs.net>, Thu, 08 Sep 2016 20:46:39 +0800
hglib: add decode_i4() helper, test it

2 файлов изменено, 30 вставок(+), 0 удалений(-) [+]
hglib.lua file | annotate | diff | comparison | revisions
spec/hglib_spec.lua file | annotate | diff | comparison | revisions
--- a/hglib.lua Thu Sep 08 19:40:38 2016 +0800
+++ b/hglib.lua Thu Sep 08 20:46:39 2016 +0800
@@ -25,6 +25,18 @@
end
end
+local function decode_i4(bytestring)
+ local value = 0
+ for i = 1, sizes.i4 do
+ local byte = bytestring:sub(i, i):byte()
+ value = value + byte * (2 ^ ((sizes.i4 - i) * 8))
+ end
+ if value >= 2 ^ (sizes.i4 * 8 - 1) then
+ value = value - 2 ^ (sizes.i4 * 8)
+ end
+ return value
+end
+
local Client = {}
Client.__index = Client
@@ -103,5 +115,6 @@
read_u4 = read_u4,
read_c = read_c,
read_channel = read_channel,
+ decode_i4 = decode_i4,
Client = Client
}
--- a/spec/hglib_spec.lua Thu Sep 08 19:40:38 2016 +0800
+++ b/spec/hglib_spec.lua Thu Sep 08 20:46:39 2016 +0800
@@ -1,5 +1,22 @@
local hglib = require('hglib')
+describe('hglib helper functions', function()
+ describe('decode_i4', function()
+ it('can decode 0', function()
+ assert.are_equal(0, hglib.decode_i4('\0\0\0\0'))
+ end)
+ it('can decode 255', function()
+ assert.are_equal(255, hglib.decode_i4('\0\0\0\255'))
+ end)
+ it('can decode 256', function()
+ assert.are_equal(256, hglib.decode_i4('\0\0\1\0'))
+ end)
+ it('can decode -1', function()
+ assert.are_equal(-1, hglib.decode_i4('\255\255\255\255'))
+ end)
+ end)
+end)
+
describe('hglib client', function()
it('can connect using just file handles', function()
local rh = io.open('spec/data/hello', 'rb')