#!/usr/bin/env lua5.1 -- $Id: teststruct.lua,v 1.4 2012/07/04 18:54:29 roberto Exp $ -- load library local lib = require"struct" -- Lua 5.1 x Lua 5.2 local unpack = unpack or table.unpack -- -- auxiliar function to print an hexadecimal `dump' of a given string -- (not used by the test) -- local function bp (s) s = string.gsub(s, "(.)", function(c) return string.format("\\%02x", string.byte(c)) end) print(s) end local a,b,c,d,e,f,x -- assume sizeof(int) == 4 assert(#lib.pack("I4", 67324752) == 4 and lib.size("I4", 4) == 4) assert(lib.size('bbb') == 3) assert(lib.pack('b', 10) == string.char(10)) assert(lib.pack('bbb', 10, 20, 30) == string.char(10, 20, 30)) assert(lib.size('h') == 2) -- assume sizeof(short) == 2 assert(lib.pack('h', 10) == string.char(0, 10)) assert(lib.pack('l', 10) == string.char(0):rep(x) .. string.char(10)) assert(lib.pack('h', string.char(0, 10)) == 10) assert(lib.unpack('i4', string.char(0, 1, 0, 10)) == 10 + 2^(2*8)) assert(lib.unpack('LIHT")) assert(lib.size("!4bi") > lib.size("!1bi")) x = lib.size("T") - 1 assert(lib.pack('T', 10) == string.char(0):rep(x) .. string.char(10)) assert(lib.pack('', '<'} do for _, l in pairs(lims) do local fmt = a .. l[1] assert(lib.unpack(fmt, lib.pack(fmt, l[2])) == l[2]) end end -- tests for fixed-sized ints for _, i in pairs{1,2,4} do x = lib.pack('I3", string.char(255, 255, 256 - 21)) == 2^(3*8) - 21) -- tests for long long if lib.unpack("i8", string.rep("\255", 8)) ~= -1 then print("no support for 'long long'") else local lim = 800 assert(lib.pack(">i8", 2^52) == "\0\16\0\0\0\0\0\0") local t = {}; for i = 1, lim do t[i] = 2^52 end assert(lib.pack(">" .. string.rep("i8", lim), unpack(t, 1, lim)) == string.rep("\0\16\0\0\0\0\0\0", lim)) assert(lib.pack("i8", -2^52 - 1) == "\255\239\255\255\255\255\255\255") assert(lib.pack("i8", "\255\239\255\255\255\255\255\255") == -2^52 - 1) assert(lib.unpack("i8", "\000\015\255\255\255\255\254\254") == 2^52 - 258) local fmt = ">" .. string.rep("i16", lim) local t1 = {lib.unpack(fmt, lib.pack(fmt, unpack(t)))} assert(t1[#t1] == 16*lim + 1 and #t == #t1 - 1) for i = 1, lim do assert(t[i] == t1[i]) end print'+' end -- strings assert(lib.pack("c", "alo alo") == "a") assert(lib.pack("c4", "alo alo") == "alo ") assert(lib.pack("c5", "alo alo") == "alo a") assert(lib.pack("!4b>c7", 1, "alo alo") == "\1alo alo") assert(lib.pack("!2iiiii", 1, 2, 3, 4, 5) local i = 1 local k = 1 while i < #x do local v, j = lib.unpack("!>i", x, i) assert(j == i + 4 and v == k) i = j; k = k + 1 end -- alignments are relative to 'absolute' positions x = lib.pack("!8 xd", 12) assert(lib.unpack("!8d", x, 3) == 12) a,b,c,d = lib.unpack("lBxxH", lib.pack(">lBxxH", -20, 10, 250)) assert(a == -20 and b == 10 and c == 250 and d == 10) a,b,c,d,e = lib.unpack(">fdfH", '000'..lib.pack(">fdfH", 3.5, -24e-5, 200.5, 30000), 4) assert(a == 3.5 and b == -24e-5 and c == 200.5 and d == 30000 and e == 22) a,b,c,d,e = lib.unpack(" I2 f i4 I2 ", 10, 20, -30, 40001) assert(string.len(x) == 2 + lib.size("f") + 4 + 2) assert(lib.unpack(">f", x, 3) == 20) a,b,c,d = lib.unpack(">i2fi4I2", x) assert(a == 10 and b == 20 and c == -30 and d == 40001) local s = "hello hello" x = lib.pack(" b c0 ", string.len(s), s) assert(lib.unpack("bc0", x) == s) x = lib.pack("Lc0", string.len(s), s) assert(lib.unpack(" L c0 ", x) == s) x = lib.pack("cc3b", s, s, 0) assert(x == "hhel\0") assert(lib.unpack("xxxxb", x) == 0) assert(lib.pack("sh", "aloi", 3) == "aloi\0\0\3") assert(lib.pack(">!sh", "aloi", 3) == "aloi\0\0\0\3") x = "aloi\0\0\0\0\3\2\0\0" a, b, c = lib.unpack(">>h >><<i8", 1000) == string.char(0, 0, 0, 0, 0, 0, 3, 232)) assert(lib.pack("i32", 10000000) == string.rep('\0', 29) .. string.char(0x98, 0x96, 0x80)) print'OK'