Merge branch 'master' of github.com:msgpack/msgpack

This commit is contained in:
frsyuki 2010-04-18 00:08:17 +09:00
commit fd31ff772f
3 changed files with 207 additions and 31 deletions

View File

@ -5,25 +5,18 @@
<!-- Load user's default properties. -->
<property file="${user.home}/build.properties" />
<property name="Org" value="MessagePack"/>
<property name="org" value="msgpack123"/>
<property name="Name" value="MessagePack"/>
<property name="name" value="msgpack456"/>
<property name="src.dir" value="${basedir}/src"/>
<property name="java.src.dir" value="${src.dir}/"/>
<property name="build.dir" value="${basedir}/build"/>
<property name="lib.dir" value="${basedir}/lib"/>
<property name="dist.dir" value="${basedir}/dist"/>
<property name="build.classes" value="${build.dir}/classes"/>
<property name="test.count" value="100"/>
<property name="test.junit.output.format" value="plain"/>
<property name="test.java.src.dir" value="${basedir}/test"/>
<property name="test.java.build.dir" value="${build.dir}/test"/>
<property name="test.java.generated.build.dir" value="${test.java.build.dir}/generated"/>
<property name="test.java.generated.dir" value="${test.java.generated.build.dir}/src"/>
<property name="test.java.classes" value="${test.java.build.dir}/classes"/>
<property name="test.java.generated.classes" value="${test.java.generated.build.dir}/classes"/>
<property name="test.java.include" value="Test*"/>
<property name="javac.encoding" value="ISO-8859-1"/>
@ -65,13 +58,9 @@
<fileset dir="${ivy.test.lib}">
<include name="**/*.jar" />
</fileset>
<fileset dir="${dist.dir}">
<include name="**/*.jar" />
</fileset>
</path>
<path id="test.java.classpath">
<pathelement location="${test.java.classes}" />
<pathelement location="${test.java.generated.classes}" />
<path refid="java.classpath"/>
<path refid="test.libs"/>
</path>
@ -80,10 +69,10 @@
<target name="init">
<mkdir dir="${build.dir}" />
<mkdir dir="${lib.dir}" />
<mkdir dir="${build.classes}" />
<mkdir dir="${test.java.build.dir}"/>
<mkdir dir="${test.java.classes}"/>
<mkdir dir="${test.java.generated.classes}"/>
<mkdir dir="${ivy.lib}"/>
<mkdir dir="${ivy.test.lib}"/>
@ -145,12 +134,12 @@
<!-- compile -->
<target name="compile" depends="init,ivy-retrieve-build">
<javac srcdir="src" destdir="build" source="1.5" target="1.5">
<compilerarg value="-Xlint:unchecked" />
</javac>
<java-compiler>
<src path="${java.src.dir}"/>
</java-compiler>
</target>
<target name="jar" depends="compile">
<jar jarfile="${dist.dir}/msgpack-0.0.1.jar" basedir="build" />
<jar jarfile="${dist.dir}/msgpack-0.0.1.jar" basedir="${build.classes}" />
</target>
<!-- test -->
@ -166,8 +155,6 @@
errorProperty="tests.failed" failureProperty="tests.failed">
<sysproperty key="test.count" value="${test.count}"/>
<sysproperty key="test.dir" value="@{test.dir}"/>
<sysproperty key="share.dir" value="${share.dir}"/>
<sysproperty key="test.validate" value="${test.validate}"/>
<classpath refid="test.java.classpath"/>
<formatter type="${test.junit.output.format}"/>
<batchtest todir="${test.java.build.dir}" unless="testcase">
@ -182,7 +169,7 @@
<fail if="tests.failed">Tests Failed!</fail>
</sequential>
</macrodef>
<target name="compile-test" depends="ivy-retrieve-test,jar">
<target name="compile-test" depends="ivy-retrieve-test,compile">
<java-compiler dest="${test.java.classes}"
classpath="test.java.classpath">
<src path="${test.java.src.dir}/org" />

View File

@ -1,7 +1,7 @@
<ivy-module version="2.0"
xmlns:e="http://ant.apache.org/ivy/extra">
<info organisation="org.messagepack"
<info organisation="org.msgpack"
module="${name}" revision="${version}">
<ivyauthor name="MessagePack Project" url="http://msgpack.sourceforge.net/"/>
<description>MessagePack</description>

View File

@ -8,9 +8,14 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class TestPackUnpack {
public Object unpackOne(ByteArrayOutputStream out) {
protected Object unpackOne(ByteArrayOutputStream out) {
return unpackOne(out, null);
}
protected Object unpackOne(ByteArrayOutputStream out, Schema schema) {
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Unpacker upk = new Unpacker(in);
if (schema != null)
upk = upk.useSchema(schema);
Iterator<Object> it = upk.iterator();
assertEquals(true, it.hasNext());
Object obj = it.next();
@ -31,22 +36,206 @@ public class TestPackUnpack {
}
public void testInt(int val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Packer pk = new Packer(out);
pk.pack(val);
new Packer(out).pack(val);
Object obj = unpackOne(out);
int val2 = -1;
if (obj instanceof Byte)
val2 = ((Byte)obj).intValue();
assertEquals(val, ((Byte)obj).intValue());
else if (obj instanceof Integer)
val2 = ((Integer)obj).intValue();
assertEquals(val, ((Integer)obj).intValue());
else if (obj instanceof Short)
val2 = ((Short)obj).intValue();
assertEquals(val, ((Short)obj).intValue());
else if (obj instanceof Long)
val2 = ((Long)obj).intValue();
assertEquals(val, ((Long)obj).intValue());
else {
System.out.println("obj = " + obj.getClass());
System.out.println("Got unexpected class: " + obj.getClass());
assertTrue(false);
}
assertEquals(val, val2);
}
@Test
public void testFloat() throws Exception {
testFloat((float)0.0);
testFloat((float)-0.0);
testFloat((float)1.0);
testFloat((float)-1.0);
testFloat((float)Float.MAX_VALUE);
testFloat((float)Float.MIN_VALUE);
testFloat((float)Float.NaN);
testFloat((float)Float.NEGATIVE_INFINITY);
testFloat((float)Float.POSITIVE_INFINITY);
Random rand = new Random();
for (int i = 0; i < 1000; i++)
testFloat(rand.nextFloat());
}
public void testFloat(float val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val);
Object obj = unpackOne(out);
if (obj instanceof Float)
assertEquals(val, ((Float)obj).floatValue(), 10e-10);
else {
System.out.println("Got unexpected class: " + obj.getClass());
assertTrue(false);
}
}
@Test
public void testDouble() throws Exception {
testDouble((double)0.0);
testDouble((double)-0.0);
testDouble((double)1.0);
testDouble((double)-1.0);
testDouble((double)Double.MAX_VALUE);
testDouble((double)Double.MIN_VALUE);
testDouble((double)Double.NaN);
testDouble((double)Double.NEGATIVE_INFINITY);
testDouble((double)Double.POSITIVE_INFINITY);
Random rand = new Random();
for (int i = 0; i < 1000; i++)
testDouble(rand.nextDouble());
}
public void testDouble(double val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val);
Object obj = unpackOne(out);
if (obj instanceof Double)
assertEquals(val, ((Double)obj).doubleValue(), 10e-10);
else {
System.out.println("Got unexpected class: " + obj.getClass());
assertTrue(false);
}
}
@Test
public void testNil() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).packNil();
Object obj = unpackOne(out);
assertEquals(null, obj);
}
@Test
public void testBoolean() throws Exception {
testBoolean(false);
testBoolean(true);
}
public void testBoolean(boolean val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val);
Object obj = unpackOne(out);
if (obj instanceof Boolean)
assertEquals(val, ((Boolean)obj).booleanValue());
else {
System.out.println("Got unexpected class: " + obj.getClass());
assertTrue(false);
}
}
@Test
public void testString() throws Exception {
testString("");
testString("a");
testString("ab");
testString("abc");
// small size string
for (int i = 0; i < 100; i++) {
StringBuilder sb = new StringBuilder();
int len = (int)Math.random() % 31 + 1;
for (int j = 0; j < len; j++)
sb.append('a' + ((int)Math.random()) & 26);
testString(sb.toString());
}
// medium size string
for (int i = 0; i < 100; i++) {
StringBuilder sb = new StringBuilder();
int len = (int)Math.random() % 100 + (1 << 15);
for (int j = 0; j < len; j++)
sb.append('a' + ((int)Math.random()) & 26);
testString(sb.toString());
}
// large size string
for (int i = 0; i < 10; i++) {
StringBuilder sb = new StringBuilder();
int len = (int)Math.random() % 100 + (1 << 31);
for (int j = 0; j < len; j++)
sb.append('a' + ((int)Math.random()) & 26);
testString(sb.toString());
}
}
public void testString(String val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val);
Object obj = unpackOne(out);
if (obj instanceof byte[])
assertEquals(val, new String((byte[])obj));
else {
System.out.println("obj=" + obj);
System.out.println("Got unexpected class: " + obj.getClass());
assertTrue(false);
}
}
@Test
public void testArray() throws Exception {
for (int i = 0; i < 1000; i++) {
Schema schema = Schema.parse("(array int)");
List<Integer> l = new ArrayList<Integer>();
int len = (int)Math.random() % 1000 + 1;
for (int j = 0; j < len; j++)
l.add(j);
testArray(l, schema);
}
for (int i = 0; i < 1000; i++) {
Schema schema = Schema.parse("(array string)");
List<String> l = new ArrayList<String>();
int len = (int)Math.random() % 1000 + 1;
for (int j = 0; j < len; j++)
l.add(Integer.toString(j));
testArray(l, schema);
}
}
public void testArray(List val, Schema schema) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val);
Object obj = unpackOne(out, schema);
if (obj instanceof List)
assertTrue(val.equals(obj));
else {
System.out.println("obj=" + obj);
System.out.println("Got unexpected class: " + obj.getClass());
assertTrue(false);
}
}
@Test
public void testMap() throws Exception {
for (int i = 0; i < 1000; i++) {
Schema schema = Schema.parse("(map int int)");
Map<Integer, Integer> m = new HashMap<Integer, Integer>();
int len = (int)Math.random() % 1000 + 1;
for (int j = 0; j < len; j++)
m.put(j, j);
testMap(m, schema);
}
for (int i = 0; i < 1000; i++) {
Schema schema = Schema.parse("(map string int)");
Map<String, Integer> m = new HashMap<String, Integer>();
int len = (int)Math.random() % 1000 + 1;
for (int j = 0; j < len; j++)
m.put(Integer.toString(j), j);
testMap(m, schema);
}
}
public void testMap(Map val, Schema schema) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val);
Object obj = unpackOne(out, schema);
if (obj instanceof Map)
assertTrue(val.equals(obj));
else {
System.out.println("obj=" + obj);
System.out.println("Got unexpected class: " + obj.getClass());
assertTrue(false);
}
}
};