lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Roberto's small but useful 'struct' library, here:


http://www.inf.puc-rio.br/~roberto/struct/struct.c


needs to have line 83 changed from:


-    case 'i': return getnum(fmt, sizeof(int));


to:


+    case 'I': case 'i': return getnum(fmt, sizeof(int));


Otherwise the pack/unpack operator "In" (which is supposed to return an 
unsigned integer of n bytes, or the native sizeof int if n is omitted) 
doesn't work -- only "in" (a signed integer of n bytes) will work.

Also, in the test suite the lines:


-for _, l in {1,2,4,8} do
-  for _, a1 in options do
-    for _, a2 in options do
-      for _, a3 in options do
-        for _, a4 in options do
-          for _, d in {"<", "", ">"} do


need to be Lua-5.1-ised to:


+for _, l in ipairs({1,2,4,8}) do
+  for _, a1 in ipairs(options) do
+    for _, a2 in ipairs(options) do
+      for _, a3 in ipairs(options) do
+        for _, a4 in ipairs(options) do
+          for _, d in ipairs({"<", "", ">"}) do


I have added to my tests (since the original ones didn't spot the "I" 
buggette :-) this set of asserts, straight after the above loops:

+ 
+local hp = { 2^7,2^15,2^23,2^31,2^39,2^47 }
+
+-- we go only to i4, since the code uses 'int' when accumulating numbers
+-- and is limited to the native sizeof int when collecting values (fixme?)
+
+assert(struct.pack("<i1",  hp[1]-1 ) == "\127")             --7F
+assert(struct.pack("<i1",-(hp[1]-1)) == "\129")             --81
+assert(struct.pack("<i2",  hp[2]-1 ) == "\255\127")         --FF 7F
+assert(struct.pack("<i2",-(hp[2]-1)) == "\1\128")           --01 80
+assert(struct.pack("<i3",  hp[3]-1 ) == "\255\255\127")     --FF FF 7F
+assert(struct.pack("<i3",-(hp[3]-1)) == "\1\0\128")         --01 00 80
+assert(struct.pack("<i4",  hp[4]-1 ) == "\255\255\255\127") --FF FF FF 7F
+assert(struct.pack("<i4",-(hp[4]-1)) == "\1\0\0\128")       --01 00 00 80
+
+assert(struct.pack(">i1",  hp[1]-1 ) == "\127")             --7F
+assert(struct.pack(">i1",-(hp[1]-1)) == "\129")             --81
+assert(struct.pack(">i2",  hp[2]-1 ) == "\127\255")         --7F FF
+assert(struct.pack(">i2",-(hp[2]-1)) == "\128\1")           --80 01
+assert(struct.pack(">i3",  hp[3]-1 ) == "\127\255\255")     --7F FF FF
+assert(struct.pack(">i3",-(hp[3]-1)) == "\128\0\1")         --80 00 01
+assert(struct.pack(">i4",  hp[4]-1 ) == "\127\255\255\255") --7F FF FF FF
+assert(struct.pack(">i4",-(hp[4]-1)) == "\128\0\0\1")       --80 00 00 01
+
+assert(struct.pack("<I1",  hp[1]-1 ) == "\127")             --7F
+assert(struct.pack("<I1",  hp[1]+1 ) == "\129")             --81
+assert(struct.pack("<I2",  hp[2]-1 ) == "\255\127")         --FF 7F
+assert(struct.pack("<I2",  hp[2]+1 ) == "\1\128")           --01 80
+assert(struct.pack("<I3",  hp[3]-1 ) == "\255\255\127")     --FF FF 7F
+assert(struct.pack("<I3",  hp[3]+1 ) == "\1\0\128")         --01 00 80
+assert(struct.pack("<I4",  hp[4]-1 ) == "\255\255\255\127") --FF FF FF 7F
+assert(struct.pack("<I4",  hp[4]+1 ) == "\1\0\0\128")       --01 00 00 80
+
+assert(struct.pack(">I1",  hp[1]-1 ) == "\127")             --7F
+assert(struct.pack(">I1",  hp[1]+1 ) == "\129")             --81
+assert(struct.pack(">I2",  hp[2]-1 ) == "\127\255")         --7F FF
+assert(struct.pack(">I2",  hp[2]+1 ) == "\128\1")           --80 01
+assert(struct.pack(">I3",  hp[3]-1 ) == "\127\255\255")     --7F FF FF
+assert(struct.pack(">I3",  hp[3]+1 ) == "\128\0\1")         --80 00 01
+assert(struct.pack(">I4",  hp[4]-1 ) == "\127\255\255\255") --7F FF FF FF
+assert(struct.pack(">I4",  hp[4]+1 ) == "\128\0\0\1")       --80 00 00 01
+
+
+assert(struct.pack("<b",   hp[1]-1 ) == "\127")             --7F
+assert(struct.pack("<b", -(hp[1]-1)) == "\129")             --81
+assert(struct.pack("<h",   hp[2]-1 ) == "\255\127")         --FF 7F
+assert(struct.pack("<h", -(hp[2]-1)) == "\1\128")           --01 80
+assert(struct.pack("<l",   hp[4]-1 ) == "\255\255\255\127") --FF FF FF 7F
+assert(struct.pack("<l", -(hp[4]-1)) == "\1\0\0\128")       --01 00 00 80
+
+assert(struct.pack(">b",   hp[1]-1 ) == "\127")             --7F
+assert(struct.pack(">b", -(hp[1]-1)) == "\129")             --81
+assert(struct.pack(">h",   hp[2]-1 ) == "\127\255")         --7F FF
+assert(struct.pack(">h", -(hp[2]-1)) == "\128\1")           --80 01
+assert(struct.pack(">l",   hp[4]-1 ) == "\127\255\255\255") --7F FF FF FF
+assert(struct.pack(">l", -(hp[4]-1)) == "\128\0\0\1")       --80 00 00 01  
+
+assert(struct.pack("<B",   hp[1]-1 ) == "\127")             --7F
+assert(struct.pack("<B",   hp[1]+1 ) == "\129")             --81
+assert(struct.pack("<H",   hp[2]-1 ) == "\255\127")         --FF 7F
+assert(struct.pack("<H",   hp[2]+1 ) == "\1\128")           --01 80
+assert(struct.pack("<L",   hp[4]-1 ) == "\255\255\255\127") --FF FF FF 7F
+assert(struct.pack("<L",   hp[4]+1 ) == "\1\0\0\128")       --01 00 00 80
+
+assert(struct.pack(">B",   hp[1]-1 ) == "\127")             --7F
+assert(struct.pack(">B",   hp[1]+1 ) == "\129")             --81
+assert(struct.pack(">H",   hp[2]-1 ) == "\127\255")         --7F FF
+assert(struct.pack(">H",   hp[2]+1 ) == "\128\1")           --80 01
+assert(struct.pack(">L",   hp[4]-1 ) == "\127\255\255\255") --7F FF FF FF
+assert(struct.pack(">L",   hp[4]+1 ) == "\128\0\0\1")       --80 00 00 01
+