mirror of
https://github.com/gvx/bitser.git
synced 2024-11-24 06:54:20 +00:00
Add more tests
This commit is contained in:
parent
2b4022fa2d
commit
9aabbdbf0e
10
main.lua
10
main.lua
@ -1,5 +1,4 @@
|
|||||||
local found_bitser, bitser = pcall(require, 'bitser')
|
local found_bitser, bitser = pcall(require, 'bitser')
|
||||||
local found_bitser_nonreentrant, bitser_nonreentrant = pcall(require, 'bitser_nonreentrant')
|
|
||||||
local found_binser, binser = pcall(require, 'binser')
|
local found_binser, binser = pcall(require, 'binser')
|
||||||
local found_ser, ser = pcall(require, 'ser')
|
local found_ser, ser = pcall(require, 'ser')
|
||||||
local found_serpent, serpent = pcall(require, 'serpent')
|
local found_serpent, serpent = pcall(require, 'serpent')
|
||||||
@ -15,12 +14,7 @@ local desers = {}
|
|||||||
if found_bitser then
|
if found_bitser then
|
||||||
sers.bitser = bitser.dumps
|
sers.bitser = bitser.dumps
|
||||||
desers.bitser = bitser.loads
|
desers.bitser = bitser.loads
|
||||||
end
|
bitser.reserveBuffer(1024 * 1024)
|
||||||
|
|
||||||
if found_bitser_nonreentrant then
|
|
||||||
sers.bitser_nonreentrant = bitser_nonreentrant.dumps
|
|
||||||
desers.bitser_nonreentrant = bitser_nonreentrant.loads
|
|
||||||
bitser_nonreentrant.reserveBuffer(1024 * 1024)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if found_binser then
|
if found_binser then
|
||||||
@ -123,7 +117,6 @@ function love.draw()
|
|||||||
end
|
end
|
||||||
local outputs = {}
|
local outputs = {}
|
||||||
for try = 1, tries do
|
for try = 1, tries do
|
||||||
love.window.setTitle("Running benchmark... [serializing " .. try .. '/' .. tries .. "]")
|
|
||||||
for sername, serializer in pairs(sers) do
|
for sername, serializer in pairs(sers) do
|
||||||
local output
|
local output
|
||||||
local success, diff = pcall(function()
|
local success, diff = pcall(function()
|
||||||
@ -143,7 +136,6 @@ function love.draw()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
for try = 1, tries do
|
for try = 1, tries do
|
||||||
love.window.setTitle("Running benchmark... [deserializing " .. try .. '/' .. tries .. "]")
|
|
||||||
for sername, deserializer in pairs(desers) do
|
for sername, deserializer in pairs(desers) do
|
||||||
local input = outputs[sername]
|
local input = outputs[sername]
|
||||||
local success, diff = pcall(function()
|
local success, diff = pcall(function()
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
local bitser = require 'bitser'
|
local bitser = require 'bitser'
|
||||||
|
local ffi = require 'ffi'
|
||||||
|
|
||||||
local function serdeser(value)
|
local function serdeser(value)
|
||||||
return bitser.loads(bitser.dumps(value))
|
return bitser.loads(bitser.dumps(value))
|
||||||
@ -8,6 +9,18 @@ local function test_serdeser(value)
|
|||||||
assert.are.same(serdeser(value), value)
|
assert.are.same(serdeser(value), value)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
love = {filesystem = {newFileData = function()
|
||||||
|
return {getPointer = function()
|
||||||
|
local buf = ffi.new("uint8_t[?]", #love.s)
|
||||||
|
ffi.copy(buf, love.s, #love.s)
|
||||||
|
return buf
|
||||||
|
end, getSize = function()
|
||||||
|
return #love.s
|
||||||
|
end}
|
||||||
|
end, write = function(s)
|
||||||
|
love.s = s
|
||||||
|
end}}
|
||||||
|
|
||||||
describe("bitser", function()
|
describe("bitser", function()
|
||||||
it("serializes simple values", function()
|
it("serializes simple values", function()
|
||||||
test_serdeser(true)
|
test_serdeser(true)
|
||||||
@ -188,11 +201,21 @@ describe("bitser", function()
|
|||||||
assert.has_error(function() bitser.loads("\255") end, "unsupported serialized type 255")
|
assert.has_error(function() bitser.loads("\255") end, "unsupported serialized type 255")
|
||||||
end)
|
end)
|
||||||
it("can load from raw data", function()
|
it("can load from raw data", function()
|
||||||
local ffi = require 'ffi'
|
|
||||||
assert.are.same(bitser.loadData(ffi.new("uint8_t[4]", 195, 103, 118, 120), 4), "gvx")
|
assert.are.same(bitser.loadData(ffi.new("uint8_t[4]", 195, 103, 118, 120), 4), "gvx")
|
||||||
end)
|
end)
|
||||||
it("will not read past the end of the buffer", function()
|
it("will not read past the end of the buffer", function()
|
||||||
local ffi = require 'ffi'
|
|
||||||
assert.has_error(function() bitser.loadData(ffi.new("uint8_t[4]", 196, 103, 118, 120), 4) end)
|
assert.has_error(function() bitser.loadData(ffi.new("uint8_t[4]", 196, 103, 118, 120), 4) end)
|
||||||
end)
|
end)
|
||||||
|
it("can clear the buffer", function()
|
||||||
|
bitser.clearBuffer()
|
||||||
|
end)
|
||||||
|
it("can write after reading from read-only buffer", function()
|
||||||
|
bitser.loadData(ffi.new("uint8_t[4]", 195, 103, 118, 120), 4)
|
||||||
|
test_serdeser("bitser")
|
||||||
|
end)
|
||||||
|
it("it can dump and load LÖVE files", function()
|
||||||
|
local v = {value = "value"}
|
||||||
|
bitser.dumpLoveFile("some_file_name", v)
|
||||||
|
assert.are.same(v, bitser.loadLoveFile("some_file_name"))
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
Loading…
Reference in New Issue
Block a user