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