Commits

Jon S committed dcc3af6

fixes and unit test devel

  • Participants
  • Parent commits 90498bf

Comments (0)

Files changed (3)

 <?xml version="1.0" encoding="UTF-8"?>
-<project name="msgpack-java-lite" default="run" basedir=".">
+<project name="msgpack-java-lite" default="package" basedir=".">
 	<description>Alternate msppack implementation for Java.</description>
 
 	<target name="clean">
 		<mkdir dir="dist" />
 		<jar destfile="dist/msgpack-java-lite">
 			<fileset dir="build">
-				<exclude name="new/asdfa/msgpack/Test.java" />
+				<exclude name="new/asdfa/msgpack/Test.*" />
 			</fileset>
 			<fileset dir="src"></fileset>
 		</jar>
 	</target>
 
 	<target name="test" depends="compile">
-		<java fork="true" jar="dist/thunderzipper.jar" classname="net.asdfa.msgpack.Test">
+		<java fork="true" classname="net.asdfa.msgpack.Test">
 			<classpath path="build" />
+			<jvmarg value="-enableassertions" />
 		</java>
 	</target>
 </project>

File src/net/asdfa/msgpack/MsgPack.java

 	public static final int MAX_15BIT = 0x7fff;
 	public static final int MAX_16BIT = 0xffff;
 	public static final int MAX_31BIT = 0x7fffffff;
-	public static final int MAX_32BIT = 0xffffffff;
+	public static final long MAX_32BIT = 0xffffffffL;
 
 	//these values are from http://wiki.msgpack.org/display/MSGPACK/Format+specification
 	public static final byte MP_NULL = (byte)0xc0;
 					}
 				} else {
 					if (value >= -(MAX_5BIT + 1)) {
-						out.write((-(int)value) | MP_NEGATIVE_FIXNUM);
+						out.write((int)(value & 0xff));
 					} else if (value >= -(MAX_7BIT + 1)) {
 						out.write(MP_INT8);
 						out.write((int)value);
 
 		try {
 			int size;
-			switch (value) {
+			switch ((byte)value) {
 				case MP_NULL:
 					return null;
 				case MP_FALSE:
 				case MP_RAW16:
 					return unpackRaw(in.readShort() & MAX_16BIT, in);
 				case MP_RAW32:
-					return unpackRaw(in.readInt() & MAX_32BIT, in);
+					return unpackRaw(in.readInt(), in);
 			}
 			if (value <= MAX_7BIT) {//MP_FIXNUM - the value is value as an int
 				return value;

File src/net/asdfa/msgpack/Test.java

 package net.asdfa.msgpack;
 
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.Map;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.math.BigInteger;
 
 /**
- *
+ * Small an simple unit test for the packer.
+ * TODO: these tests test "consistency with self", but doesn't do many
+ * "consistency with spec" checks
  * @author jon
  */
 public class Test {
-	public static void Main(String[] args) throws Exception {
-		//Pack
-		Map<String, Object> objectOrig = new HashMap<String, Object>();
-		objectOrig.put("aString", "Some String text");
-		objectOrig.put("number1", 1);
-		objectOrig.put("number100", 100);
-		objectOrig.put("number10000", 10000);
-		objectOrig.put("array", Arrays.asList(2, 4, 6, 8));
-		byte[] data = MsgPack.pack(objectOrig);
 
-		//Unpack
-		Object objectUnpacked = MsgPack.unpack(data);
+	public static void main(String[] args) throws Exception {
+		try {
+			assert false;
+			System.err.println("Assertions must be on to run this (try java -ea)");
+			System.exit(1);
+		} catch (AssertionError ex) {
+		}
+		System.out.println("Testing everything");
+		Test t = new Test();
+		for (Method m : Test.class.getMethods()) {
+			if (m.getDeclaringClass().equals(Object.class)) continue;
+			if (!Modifier.isPublic(m.getModifiers())) continue;
+			if (Modifier.isStatic(m.getModifiers())) continue;
+			System.out.println("Test " + m.getName());
+			try {
+				m.invoke(t);
+			} catch (InvocationTargetException ex) {
+				System.out.flush();
+				System.err.println("Failed:");
+				ex.getCause().printStackTrace();
+				System.exit(1);
+			}
+		}
+		System.out.println("Tests passed!");
+	}
 
-		assert objectUnpacked.equals(objectOrig);
+
+	public void testBasics() throws Exception {
+		assert MsgPack.unpack(MsgPack.pack(null)) == null : "null failed";
+		assert Boolean.FALSE.equals(MsgPack.unpack(MsgPack.pack(false))) : "False failed";
+		assert Boolean.TRUE.equals(MsgPack.unpack(MsgPack.pack(true))) : "True failed";
 	}
+
+	public void testInts() throws Exception {
+		/** Some key numbers we want to make sure work right. */
+		Number[] numbers = new Number[] {
+			0, 1, 2,
+			-1, -2, -3,
+			31, 32, 33,
+			-31, -32, -33,
+			0xFA1L,
+			127, 128, 129,
+			-127, -128, -129,
+			254, 255, 256,
+			-254, -255, -256,
+			0xfffeL, 0xffffL, 0x10000L,
+			-0x7ffeL, -0x7fffL, 0x8000L,
+			0xfffffffeL, 0xffffffffL, 0x100000000L,
+			-0x7ffffffeL, -0x7fffffffL, -0x800000000L,
+			0x7ffffffffffffffeL, 0x7fffffffffffffffL, new BigInteger("8000000000000000", 16),
+			-0x7ffffffffffffffeL, -0x7fffffffffffffffL,
+			new BigInteger("fffffffffffffffe", 16), new BigInteger("ffffffffffffffff", 16),
+		};
+		for (Number n : numbers) {
+			byte[] packed = MsgPack.pack(n);
+			Object unpacked = MsgPack.unpack(packed);
+
+			//System.out.println(n + " => " + toHex(packed) + " => " + unpacked);
+
+			assert unpacked instanceof Number;
+			if (unpacked instanceof BigInteger)
+				assert unpacked.equals(n) : "Failed on bigint " + n;
+			else
+				assert ((Number)unpacked).longValue() == n.longValue() : "Failed on int " + n;
+		}
+	}
+
+	public void testFloats() throws Exception {
+		Number[] numbers = new Number[] {
+			0.0, 1.0, -1.0,
+			0.0f, 1.0f, -1.0f,
+			Double.NaN,
+			Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY,
+			Double.MIN_NORMAL, Double.MIN_VALUE, Double.MAX_VALUE,
+			Double.MIN_NORMAL, Double.MIN_VALUE, Double.MAX_VALUE,
+			Float.NaN,
+			Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY,
+			Float.MIN_NORMAL, Float.MIN_VALUE, Float.MAX_VALUE,
+			Float.MIN_NORMAL, Float.MIN_VALUE, Float.MAX_VALUE,
+		};
+
+		for (Number n : numbers) {
+			byte[] packed = MsgPack.pack(n);
+			Object unpacked = MsgPack.unpack(packed);
+
+			//System.out.println(n + " => " + toHex(packed) + " => " + unpacked);
+
+			assert unpacked instanceof Number;
+			assert ((Number)unpacked).longValue() == n.longValue() : "Failed on float " + n;
+		}
+	}
+
+
+
+	public static String toHex(byte[] data) {
+		StringBuilder sb = new StringBuilder("0x");
+		for (byte b : data) {
+			if (b >= 0 && b < 0x10) sb.append("0");
+			sb.append(Integer.toString(b & 0xff, 16));
+		}
+		return sb.toString();
+	}
+
 }