tpack.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. -- $Id: testes/tpack.lua $
  2. -- See Copyright Notice in file lua.h
  3. local pack = string.pack
  4. local packsize = string.packsize
  5. local unpack = string.unpack
  6. print "testing pack/unpack"
  7. -- maximum size for integers
  8. local NB = 16
  9. local sizeshort = packsize("h")
  10. local sizeint = packsize("i")
  11. local sizelong = packsize("l")
  12. local sizesize_t = packsize("T")
  13. local sizeLI = packsize("j")
  14. local sizefloat = packsize("f")
  15. local sizedouble = packsize("d")
  16. local sizenumber = packsize("n")
  17. local little = (pack("i2", 1) == "\1\0")
  18. local align = packsize("!xXi16")
  19. assert(1 <= sizeshort and sizeshort <= sizeint and sizeint <= sizelong and
  20. sizefloat <= sizedouble)
  21. print("platform:")
  22. print(string.format(
  23. "\tshort %d, int %d, long %d, size_t %d, float %d, double %d,\n\z
  24. \tlua Integer %d, lua Number %d",
  25. sizeshort, sizeint, sizelong, sizesize_t, sizefloat, sizedouble,
  26. sizeLI, sizenumber))
  27. print("\t" .. (little and "little" or "big") .. " endian")
  28. print("\talignment: " .. align)
  29. -- check errors in arguments
  30. local function checkerror (msg, f, ...)
  31. local status, err = pcall(f, ...)
  32. -- print(status, err, msg)
  33. assert(not status and string.find(err, msg))
  34. end
  35. -- minimum behavior for integer formats
  36. assert(unpack("B", pack("B", 0xff)) == 0xff)
  37. assert(unpack("b", pack("b", 0x7f)) == 0x7f)
  38. assert(unpack("b", pack("b", -0x80)) == -0x80)
  39. assert(unpack("H", pack("H", 0xffff)) == 0xffff)
  40. assert(unpack("h", pack("h", 0x7fff)) == 0x7fff)
  41. assert(unpack("h", pack("h", -0x8000)) == -0x8000)
  42. assert(unpack("L", pack("L", 0xffffffff)) == 0xffffffff)
  43. assert(unpack("l", pack("l", 0x7fffffff)) == 0x7fffffff)
  44. assert(unpack("l", pack("l", -0x80000000)) == -0x80000000)
  45. for i = 1, NB do
  46. -- small numbers with signal extension ("\xFF...")
  47. local s = string.rep("\xff", i)
  48. assert(pack("i" .. i, -1) == s)
  49. assert(packsize("i" .. i) == #s)
  50. assert(unpack("i" .. i, s) == -1)
  51. -- small unsigned number ("\0...\xAA")
  52. s = "\xAA" .. string.rep("\0", i - 1)
  53. assert(pack("<I" .. i, 0xAA) == s)
  54. assert(unpack("<I" .. i, s) == 0xAA)
  55. assert(pack(">I" .. i, 0xAA) == s:reverse())
  56. assert(unpack(">I" .. i, s:reverse()) == 0xAA)
  57. end
  58. do
  59. local lnum = 0x13121110090807060504030201
  60. local s = pack("<j", lnum)
  61. assert(unpack("<j", s) == lnum)
  62. assert(unpack("<i" .. sizeLI + 1, s .. "\0") == lnum)
  63. assert(unpack("<i" .. sizeLI + 1, s .. "\0") == lnum)
  64. for i = sizeLI + 1, NB do
  65. local s = pack("<j", -lnum)
  66. assert(unpack("<j", s) == -lnum)
  67. -- strings with (correct) extra bytes
  68. assert(unpack("<i" .. i, s .. ("\xFF"):rep(i - sizeLI)) == -lnum)
  69. assert(unpack(">i" .. i, ("\xFF"):rep(i - sizeLI) .. s:reverse()) == -lnum)
  70. assert(unpack("<I" .. i, s .. ("\0"):rep(i - sizeLI)) == -lnum)
  71. -- overflows
  72. checkerror("does not fit", unpack, "<I" .. i, ("\x00"):rep(i - 1) .. "\1")
  73. checkerror("does not fit", unpack, ">i" .. i, "\1" .. ("\x00"):rep(i - 1))
  74. end
  75. end
  76. for i = 1, sizeLI do
  77. local lstr = "\1\2\3\4\5\6\7\8\9\10\11\12\13"
  78. local lnum = 0x13121110090807060504030201
  79. local n = lnum & (~(-1 << (i * 8)))
  80. local s = string.sub(lstr, 1, i)
  81. assert(pack("<i" .. i, n) == s)
  82. assert(pack(">i" .. i, n) == s:reverse())
  83. assert(unpack(">i" .. i, s:reverse()) == n)
  84. end
  85. -- sign extension
  86. do
  87. local u = 0xf0
  88. for i = 1, sizeLI - 1 do
  89. assert(unpack("<i"..i, "\xf0"..("\xff"):rep(i - 1)) == -16)
  90. assert(unpack(">I"..i, "\xf0"..("\xff"):rep(i - 1)) == u)
  91. u = u * 256 + 0xff
  92. end
  93. end
  94. -- mixed endianness
  95. do
  96. assert(pack(">i2 <i2", 10, 20) == "\0\10\20\0")
  97. local a, b = unpack("<i2 >i2", "\10\0\0\20")
  98. assert(a == 10 and b == 20)
  99. assert(pack("=i4", 2001) == pack("i4", 2001))
  100. end
  101. print("testing invalid formats")
  102. checkerror("out of limits", pack, "i0", 0)
  103. checkerror("out of limits", pack, "i" .. NB + 1, 0)
  104. checkerror("out of limits", pack, "!" .. NB + 1, 0)
  105. checkerror("%(17%) out of limits %[1,16%]", pack, "Xi" .. NB + 1)
  106. checkerror("invalid format option 'r'", pack, "i3r", 0)
  107. checkerror("16%-byte integer", unpack, "i16", string.rep('\3', 16))
  108. checkerror("not power of 2", pack, "!4i3", 0);
  109. checkerror("missing size", pack, "c", "")
  110. checkerror("variable%-length format", packsize, "s")
  111. checkerror("variable%-length format", packsize, "z")
  112. -- overflow in option size (error will be in digit after limit)
  113. checkerror("invalid format", packsize, "c1" .. string.rep("0", 40))
  114. do
  115. local maxsize = (packsize("j") <= packsize("T")) and
  116. math.maxinteger or (1 << (packsize("T") * 8))
  117. assert (packsize(string.format("c%d", maxsize - 9)) == maxsize - 9)
  118. checkerror("too large", packsize, string.format("c%dc10", maxsize - 9))
  119. checkerror("too long", pack, string.format("xxxxxxxxxx c%d", maxsize - 9))
  120. end
  121. -- overflow in packing
  122. for i = 1, sizeLI - 1 do
  123. local umax = (1 << (i * 8)) - 1
  124. local max = umax >> 1
  125. local min = ~max
  126. checkerror("overflow", pack, "<I" .. i, -1)
  127. checkerror("overflow", pack, "<I" .. i, min)
  128. checkerror("overflow", pack, ">I" .. i, umax + 1)
  129. checkerror("overflow", pack, ">i" .. i, umax)
  130. checkerror("overflow", pack, ">i" .. i, max + 1)
  131. checkerror("overflow", pack, "<i" .. i, min - 1)
  132. assert(unpack(">i" .. i, pack(">i" .. i, max)) == max)
  133. assert(unpack("<i" .. i, pack("<i" .. i, min)) == min)
  134. assert(unpack(">I" .. i, pack(">I" .. i, umax)) == umax)
  135. end
  136. -- Lua integer size
  137. assert(unpack(">j", pack(">j", math.maxinteger)) == math.maxinteger)
  138. assert(unpack("<j", pack("<j", math.mininteger)) == math.mininteger)
  139. assert(unpack("<J", pack("<j", -1)) == -1) -- maximum unsigned integer
  140. if little then
  141. assert(pack("f", 24) == pack("<f", 24))
  142. else
  143. assert(pack("f", 24) == pack(">f", 24))
  144. end
  145. print "testing pack/unpack of floating-point numbers"
  146. for _, n in ipairs{0, -1.1, 1.9, 1/0, -1/0, 1e20, -1e20, 0.1, 2000.7} do
  147. assert(unpack("n", pack("n", n)) == n)
  148. assert(unpack("<n", pack("<n", n)) == n)
  149. assert(unpack(">n", pack(">n", n)) == n)
  150. assert(pack("<f", n) == pack(">f", n):reverse())
  151. assert(pack(">d", n) == pack("<d", n):reverse())
  152. end
  153. -- for non-native precisions, test only with "round" numbers
  154. for _, n in ipairs{0, -1.5, 1/0, -1/0, 1e10, -1e9, 0.5, 2000.25} do
  155. assert(unpack("<f", pack("<f", n)) == n)
  156. assert(unpack(">f", pack(">f", n)) == n)
  157. assert(unpack("<d", pack("<d", n)) == n)
  158. assert(unpack(">d", pack(">d", n)) == n)
  159. end
  160. print "testing pack/unpack of strings"
  161. do
  162. local s = string.rep("abc", 1000)
  163. assert(pack("zB", s, 247) == s .. "\0\xF7")
  164. local s1, b = unpack("zB", s .. "\0\xF9")
  165. assert(b == 249 and s1 == s)
  166. s1 = pack("s", s)
  167. assert(unpack("s", s1) == s)
  168. checkerror("does not fit", pack, "s1", s)
  169. checkerror("contains zeros", pack, "z", "alo\0");
  170. checkerror("unfinished string", unpack, "zc10000000", "alo")
  171. for i = 2, NB do
  172. local s1 = pack("s" .. i, s)
  173. assert(unpack("s" .. i, s1) == s and #s1 == #s + i)
  174. end
  175. end
  176. do
  177. local x = pack("s", "alo")
  178. checkerror("too short", unpack, "s", x:sub(1, -2))
  179. checkerror("too short", unpack, "c5", "abcd")
  180. checkerror("out of limits", pack, "s100", "alo")
  181. end
  182. do
  183. assert(pack("c0", "") == "")
  184. assert(packsize("c0") == 0)
  185. assert(unpack("c0", "") == "")
  186. assert(pack("<! c3", "abc") == "abc")
  187. assert(packsize("<! c3") == 3)
  188. assert(pack(">!4 c6", "abcdef") == "abcdef")
  189. assert(pack("c3", "123") == "123")
  190. assert(pack("c0", "") == "")
  191. assert(pack("c8", "123456") == "123456\0\0")
  192. assert(pack("c88 c1", "", "X") == string.rep("\0", 88) .. "X")
  193. assert(pack("c188 c2", "ab", "X\1") ==
  194. "ab" .. string.rep("\0", 188 - 2) .. "X\1")
  195. local a, b, c = unpack("!4 z c3", "abcdefghi\0xyz")
  196. assert(a == "abcdefghi" and b == "xyz" and c == 14)
  197. checkerror("longer than", pack, "c3", "1234")
  198. end
  199. -- testing multiple types and sequence
  200. do
  201. local x = pack("<b h b f d f n i", 1, 2, 3, 4, 5, 6, 7, 8)
  202. assert(#x == packsize("<b h b f d f n i"))
  203. local a, b, c, d, e, f, g, h = unpack("<b h b f d f n i", x)
  204. assert(a == 1 and b == 2 and c == 3 and d == 4 and e == 5 and f == 6 and
  205. g == 7 and h == 8)
  206. end
  207. print "testing alignment"
  208. do
  209. assert(pack(" < i1 i2 ", 2, 3) == "\2\3\0") -- no alignment by default
  210. local x = pack(">!8 b Xh i4 i8 c1 Xi8", -12, 100, 200, "\xEC")
  211. assert(#x == packsize(">!8 b Xh i4 i8 c1 Xi8"))
  212. assert(x == "\xf4" .. "\0\0\0" ..
  213. "\0\0\0\100" ..
  214. "\0\0\0\0\0\0\0\xC8" ..
  215. "\xEC" .. "\0\0\0\0\0\0\0")
  216. local a, b, c, d, pos = unpack(">!8 c1 Xh i4 i8 b Xi8 XI XH", x)
  217. assert(a == "\xF4" and b == 100 and c == 200 and d == -20 and (pos - 1) == #x)
  218. x = pack(">!4 c3 c4 c2 z i4 c5 c2 Xi4",
  219. "abc", "abcd", "xz", "hello", 5, "world", "xy")
  220. assert(x == "abcabcdxzhello\0\0\0\0\0\5worldxy\0")
  221. local a, b, c, d, e, f, g, pos = unpack(">!4 c3 c4 c2 z i4 c5 c2 Xh Xi4", x)
  222. assert(a == "abc" and b == "abcd" and c == "xz" and d == "hello" and
  223. e == 5 and f == "world" and g == "xy" and (pos - 1) % 4 == 0)
  224. x = pack(" b b Xd b Xb x", 1, 2, 3)
  225. assert(packsize(" b b Xd b Xb x") == 4)
  226. assert(x == "\1\2\3\0")
  227. a, b, c, pos = unpack("bbXdb", x)
  228. assert(a == 1 and b == 2 and c == 3 and pos == #x)
  229. -- only alignment
  230. assert(packsize("!8 xXi8") == 8)
  231. local pos = unpack("!8 xXi8", "0123456701234567"); assert(pos == 9)
  232. assert(packsize("!8 xXi2") == 2)
  233. local pos = unpack("!8 xXi2", "0123456701234567"); assert(pos == 3)
  234. assert(packsize("!2 xXi2") == 2)
  235. local pos = unpack("!2 xXi2", "0123456701234567"); assert(pos == 3)
  236. assert(packsize("!2 xXi8") == 2)
  237. local pos = unpack("!2 xXi8", "0123456701234567"); assert(pos == 3)
  238. assert(packsize("!16 xXi16") == 16)
  239. local pos = unpack("!16 xXi16", "0123456701234567"); assert(pos == 17)
  240. checkerror("invalid next option", pack, "X")
  241. checkerror("invalid next option", unpack, "XXi", "")
  242. checkerror("invalid next option", unpack, "X i", "")
  243. checkerror("invalid next option", pack, "Xc1")
  244. end
  245. do -- testing initial position
  246. local x = pack("i4i4i4i4", 1, 2, 3, 4)
  247. for pos = 1, 16, 4 do
  248. local i, p = unpack("i4", x, pos)
  249. assert(i == pos//4 + 1 and p == pos + 4)
  250. end
  251. -- with alignment
  252. for pos = 0, 12 do -- will always round position to power of 2
  253. local i, p = unpack("!4 i4", x, pos + 1)
  254. assert(i == (pos + 3)//4 + 1 and p == i*4 + 1)
  255. end
  256. -- negative indices
  257. local i, p = unpack("!4 i4", x, -4)
  258. assert(i == 4 and p == 17)
  259. local i, p = unpack("!4 i4", x, -7)
  260. assert(i == 4 and p == 17)
  261. local i, p = unpack("!4 i4", x, -#x)
  262. assert(i == 1 and p == 5)
  263. -- limits
  264. for i = 1, #x + 1 do
  265. assert(unpack("c0", x, i) == "")
  266. end
  267. checkerror("out of string", unpack, "c0", x, #x + 2)
  268. end
  269. print "OK"