summaryrefslogtreecommitdiff
path: root/java/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/test')
-rw-r--r--java/src/test/java/org/msgpack/Image.java50
-rw-r--r--java/src/test/java/org/msgpack/TestCases.java40
-rw-r--r--java/src/test/java/org/msgpack/TestDirectConversion.java405
-rw-r--r--java/src/test/java/org/msgpack/TestMessageUnpackable.java34
-rw-r--r--java/src/test/java/org/msgpack/TestObjectEquals.java96
-rw-r--r--java/src/test/java/org/msgpack/TestPackUnpack.java500
-rw-r--r--java/src/test/resources/cases.json1
-rw-r--r--java/src/test/resources/cases.mpacbin0 -> 213 bytes
-rw-r--r--java/src/test/resources/cases_compact.mpacbin0 -> 116 bytes
9 files changed, 753 insertions, 373 deletions
diff --git a/java/src/test/java/org/msgpack/Image.java b/java/src/test/java/org/msgpack/Image.java
new file mode 100644
index 0000000..3fcfe38
--- /dev/null
+++ b/java/src/test/java/org/msgpack/Image.java
@@ -0,0 +1,50 @@
+package org.msgpack;
+
+import org.msgpack.*;
+import java.io.*;
+import java.util.*;
+
+public class Image implements MessagePackable, MessageUnpackable {
+ public String uri = "";
+ public String title = "";
+ public int width = 0;
+ public int height = 0;
+ public int size = 0;
+
+ public void messagePack(Packer pk) throws IOException {
+ pk.packArray(5);
+ pk.pack(uri);
+ pk.pack(title);
+ pk.pack(width);
+ pk.pack(height);
+ pk.pack(size);
+ }
+
+ public void messageUnpack(Unpacker pac) throws IOException, MessageTypeException {
+ int length = pac.unpackArray();
+ if(length != 5) {
+ throw new MessageTypeException();
+ }
+ uri = pac.unpackString();
+ title = pac.unpackString();
+ width = pac.unpackInt();
+ height = pac.unpackInt();
+ size = pac.unpackInt();
+ }
+
+ public boolean equals(Image obj) {
+ return uri.equals(obj.uri) &&
+ title.equals(obj.title) &&
+ width == obj.width &&
+ height == obj.height &&
+ size == obj.size;
+ }
+
+ public boolean equals(Object obj) {
+ if(obj.getClass() != Image.class) {
+ return false;
+ }
+ return equals((Image)obj);
+ }
+}
+
diff --git a/java/src/test/java/org/msgpack/TestCases.java b/java/src/test/java/org/msgpack/TestCases.java
new file mode 100644
index 0000000..c368972
--- /dev/null
+++ b/java/src/test/java/org/msgpack/TestCases.java
@@ -0,0 +1,40 @@
+package org.msgpack;
+
+import java.io.*;
+import java.util.*;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class TestCases {
+ public void feedFile(Unpacker pac, String path) throws Exception {
+ FileInputStream input = new FileInputStream(path);
+ byte[] buffer = new byte[32*1024];
+ while(true) {
+ int count = input.read(buffer);
+ if(count < 0) {
+ break;
+ }
+ pac.feed(buffer, 0, count);
+ }
+ }
+
+ @Test
+ public void testCases() throws Exception {
+ Unpacker pac = new Unpacker();
+ Unpacker pac_compact = new Unpacker();
+
+ feedFile(pac, "src/test/resources/cases.mpac");
+ feedFile(pac_compact, "src/test/resources/cases_compact.mpac");
+
+ UnpackResult result = new UnpackResult();
+ while(pac.next(result)) {
+ UnpackResult result_compact = new UnpackResult();
+ assertTrue( pac_compact.next(result_compact) );
+ assertTrue( result.getData().equals(result_compact.getData()) );
+ }
+
+ assertFalse( pac_compact.next(result) );
+ }
+};
+
diff --git a/java/src/test/java/org/msgpack/TestDirectConversion.java b/java/src/test/java/org/msgpack/TestDirectConversion.java
index 77bbc58..1822ecb 100644
--- a/java/src/test/java/org/msgpack/TestDirectConversion.java
+++ b/java/src/test/java/org/msgpack/TestDirectConversion.java
@@ -3,146 +3,277 @@ package org.msgpack;
import org.msgpack.*;
import java.io.*;
import java.util.*;
+import java.math.BigInteger;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestDirectConversion {
- @Test
- public void testInt() throws Exception {
- testInt(0);
- testInt(-1);
- testInt(1);
- testInt(Integer.MIN_VALUE);
- testInt(Integer.MAX_VALUE);
- Random rand = new Random();
- for (int i = 0; i < 1000; i++)
- testInt(rand.nextInt());
- }
- public void testInt(int val) throws Exception {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- new Packer(out).pack(val);
- ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
- Unpacker upk = new Unpacker(in);
- assertEquals(val, upk.unpackInt());
- }
-
- @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);
- ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
- Unpacker upk = new Unpacker(in);
- float f = upk.unpackFloat();
- if(Float.isNaN(val)) {
- assertTrue(Float.isNaN(f));
- } else {
- assertEquals(val, f, 10e-10);
- }
- }
-
- @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);
- ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
- Unpacker upk = new Unpacker(in);
- double f = upk.unpackDouble();
- if(Double.isNaN(val)) {
- assertTrue(Double.isNaN(f));
- } else {
- assertEquals(val, f, 10e-10);
- }
- }
-
- @Test
- public void testNil() throws Exception {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- new Packer(out).packNil();
- ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
- Unpacker upk = new Unpacker(in);
- assertEquals(null, upk.unpackNull());
- }
-
- @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);
- ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
- Unpacker upk = new Unpacker(in);
- assertEquals(val, upk.unpackBoolean());
- }
-
- @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);
- ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
- Unpacker upk = new Unpacker(in);
- assertEquals(val, upk.unpackString());
- }
-
- // FIXME container types
+ @Test
+ public void testInt() throws Exception {
+ testInt(0);
+ testInt(-1);
+ testInt(1);
+ testInt(Integer.MIN_VALUE);
+ testInt(Integer.MAX_VALUE);
+ Random rand = new Random();
+ for (int i = 0; i < 1000; i++)
+ testInt(rand.nextInt());
+ }
+ public void testInt(int val) throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ new Packer(out).pack(val);
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker pac = new Unpacker(in);
+ assertEquals(val, pac.unpackInt());
+ }
+
+ @Test
+ public void testLong() throws Exception {
+ testLong(0);
+ testLong(-1);
+ testLong(1);
+ testLong(Integer.MIN_VALUE);
+ testLong(Integer.MAX_VALUE);
+ testLong(Long.MIN_VALUE);
+ testLong(Long.MAX_VALUE);
+ Random rand = new Random();
+ for (int i = 0; i < 1000; i++)
+ testLong(rand.nextLong());
+ }
+ public void testLong(long val) throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ new Packer(out).pack(val);
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker pac = new Unpacker(in);
+ assertEquals(val, pac.unpackLong());
+ }
+
+ @Test
+ public void testBigInteger() throws Exception {
+ testBigInteger(BigInteger.valueOf(0));
+ testBigInteger(BigInteger.valueOf(-1));
+ testBigInteger(BigInteger.valueOf(1));
+ testBigInteger(BigInteger.valueOf(Integer.MIN_VALUE));
+ testBigInteger(BigInteger.valueOf(Integer.MAX_VALUE));
+ testBigInteger(BigInteger.valueOf(Long.MIN_VALUE));
+ testBigInteger(BigInteger.valueOf(Long.MAX_VALUE));
+ BigInteger max = BigInteger.valueOf(Long.MAX_VALUE).setBit(63);
+ testBigInteger(max);
+ Random rand = new Random();
+ for (int i = 0; i < 1000; i++)
+ testBigInteger( max.subtract(BigInteger.valueOf( Math.abs(rand.nextLong()) )) );
+ }
+ public void testBigInteger(BigInteger val) throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ new Packer(out).pack(val);
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker pac = new Unpacker(in);
+ assertEquals(val, pac.unpackBigInteger());
+ }
+
+ @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);
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker pac = new Unpacker(in);
+ assertEquals(val, pac.unpackFloat(), 10e-10);
+ }
+
+ @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);
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker pac = new Unpacker(in);
+ assertEquals(val, pac.unpackDouble(), 10e-10);
+ }
+
+ @Test
+ public void testNil() throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ new Packer(out).packNil();
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker pac = new Unpacker(in);
+ assertEquals(null, pac.unpackNull());
+ }
+
+ @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);
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker pac = new Unpacker(in);
+ assertEquals(val, pac.unpackBoolean());
+ }
+
+ @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);
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker pac = new Unpacker(in);
+ assertEquals(val, pac.unpackString());
+ }
+
+ @Test
+ public void testArray() throws Exception {
+ List<Integer> emptyList = new ArrayList<Integer>();
+ {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ new Packer(out).pack(emptyList);
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker pac = new Unpacker(in);
+ int ulen = pac.unpackArray();
+ assertEquals(0, ulen);
+ }
+
+ for (int i = 0; i < 1000; i++) {
+ List<Integer> l = new ArrayList<Integer>();
+ int len = (int)Math.random() % 1000 + 1;
+ for (int j = 0; j < len; j++)
+ l.add(j);
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ new Packer(out).pack(l);
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker pac = new Unpacker(in);
+ int ulen = pac.unpackArray();
+ assertEquals(len, ulen);
+ for (int j = 0; j < len; j++) {
+ assertEquals(l.get(j).intValue(), pac.unpackInt());
+ }
+ }
+
+ for (int i = 0; i < 1000; i++) {
+ 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));
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ new Packer(out).pack(l);
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker pac = new Unpacker(in);
+ int ulen = pac.unpackArray();
+ assertEquals(len, ulen);
+ for (int j = 0; j < len; j++) {
+ assertEquals(l.get(j), pac.unpackString());
+ }
+ }
+ }
+
+ @Test
+ public void testMap() throws Exception {
+ Map<Integer, Integer> emptyMap = new HashMap<Integer, Integer>();
+ {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ new Packer(out).pack(emptyMap);
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker pac = new Unpacker(in);
+ int ulen = pac.unpackMap();
+ assertEquals(0, ulen);
+ }
+
+ for (int i = 0; i < 1000; i++) {
+ 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);
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ new Packer(out).pack(m);
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker pac = new Unpacker(in);
+ int ulen = pac.unpackMap();
+ assertEquals(len, ulen);
+ for (int j = 0; j < len; j++) {
+ Integer val = m.get(pac.unpackInt());
+ assertNotNull(val);
+ assertEquals(val.intValue(), pac.unpackInt());
+ }
+ }
+
+ for (int i = 0; i < 1000; i++) {
+ 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);
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ new Packer(out).pack(m);
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker pac = new Unpacker(in);
+ int ulen = pac.unpackMap();
+ assertEquals(len, ulen);
+ for (int j = 0; j < len; j++) {
+ Integer val = m.get(pac.unpackString());
+ assertNotNull(val);
+ assertEquals(val.intValue(), pac.unpackInt());
+ }
+ }
+ }
};
+
diff --git a/java/src/test/java/org/msgpack/TestMessageUnpackable.java b/java/src/test/java/org/msgpack/TestMessageUnpackable.java
new file mode 100644
index 0000000..9099f91
--- /dev/null
+++ b/java/src/test/java/org/msgpack/TestMessageUnpackable.java
@@ -0,0 +1,34 @@
+package org.msgpack;
+
+import org.msgpack.*;
+import java.io.*;
+import java.util.*;
+import java.math.BigInteger;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class TestMessageUnpackable {
+ @Test
+ public void testImage() throws Exception {
+ Image src = new Image();
+ src.title = "msgpack";
+ src.uri = "http://msgpack.org/";
+ src.width = 2560;
+ src.height = 1600;
+ src.size = 4096000;
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ src.messagePack(new Packer(out));
+
+ Image dst = new Image();
+
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker pac = new Unpacker(in);
+
+ dst.messageUnpack(pac);
+
+ assertEquals(src, dst);
+ }
+}
+
diff --git a/java/src/test/java/org/msgpack/TestObjectEquals.java b/java/src/test/java/org/msgpack/TestObjectEquals.java
new file mode 100644
index 0000000..25fe927
--- /dev/null
+++ b/java/src/test/java/org/msgpack/TestObjectEquals.java
@@ -0,0 +1,96 @@
+package org.msgpack;
+
+import org.msgpack.*;
+import org.msgpack.object.*;
+import java.math.BigInteger;
+import java.util.*;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class TestObjectEquals {
+ @Test
+ public void testInt() throws Exception {
+ testInt(0);
+ testInt(-1);
+ testInt(1);
+ testInt(Integer.MIN_VALUE);
+ testInt(Integer.MAX_VALUE);
+ Random rand = new Random();
+ for (int i = 0; i < 1000; i++)
+ testInt(rand.nextInt());
+ }
+ public void testInt(int val) throws Exception {
+ MessagePackObject objInt = IntegerType.create(val);
+ MessagePackObject objLong = IntegerType.create((long)val);
+ MessagePackObject objBigInt = IntegerType.create(BigInteger.valueOf((long)val));
+ assertTrue(objInt.equals(objInt));
+ assertTrue(objInt.equals(objLong));
+ assertTrue(objInt.equals(objBigInt));
+ assertTrue(objLong.equals(objInt));
+ assertTrue(objLong.equals(objLong));
+ assertTrue(objLong.equals(objBigInt));
+ assertTrue(objBigInt.equals(objInt));
+ assertTrue(objBigInt.equals(objLong));
+ assertTrue(objBigInt.equals(objBigInt));
+ }
+
+ @Test
+ public void testLong() throws Exception {
+ testLong(0);
+ testLong(-1);
+ testLong(1);
+ testLong(Integer.MIN_VALUE);
+ testLong(Integer.MAX_VALUE);
+ testLong(Long.MIN_VALUE);
+ testLong(Long.MAX_VALUE);
+ Random rand = new Random();
+ for (int i = 0; i < 1000; i++)
+ testLong(rand.nextLong());
+ }
+ public void testLong(long val) throws Exception {
+ MessagePackObject objInt = IntegerType.create((int)val);
+ MessagePackObject objLong = IntegerType.create(val);
+ MessagePackObject objBigInt = IntegerType.create(BigInteger.valueOf(val));
+ if(val > (long)Integer.MAX_VALUE || val < (long)Integer.MIN_VALUE) {
+ assertTrue(objInt.equals(objInt));
+ assertFalse(objInt.equals(objLong));
+ assertFalse(objInt.equals(objBigInt));
+ assertFalse(objLong.equals(objInt));
+ assertTrue(objLong.equals(objLong));
+ assertTrue(objLong.equals(objBigInt));
+ assertFalse(objBigInt.equals(objInt));
+ assertTrue(objBigInt.equals(objLong));
+ assertTrue(objBigInt.equals(objBigInt));
+ } else {
+ assertTrue(objInt.equals(objInt));
+ assertTrue(objInt.equals(objLong));
+ assertTrue(objInt.equals(objBigInt));
+ assertTrue(objLong.equals(objInt));
+ assertTrue(objLong.equals(objLong));
+ assertTrue(objLong.equals(objBigInt));
+ assertTrue(objBigInt.equals(objInt));
+ assertTrue(objBigInt.equals(objLong));
+ assertTrue(objBigInt.equals(objBigInt));
+ }
+ }
+
+ @Test
+ public void testNil() throws Exception {
+ assertTrue(NilType.create().equals(NilType.create()));
+ assertFalse(NilType.create().equals(IntegerType.create(0)));
+ assertFalse(NilType.create().equals(BooleanType.create(false)));
+ }
+
+ @Test
+ public void testString() throws Exception {
+ testString("");
+ testString("a");
+ testString("ab");
+ testString("abc");
+ }
+ public void testString(String str) throws Exception {
+ assertTrue(RawType.create(str).equals(RawType.create(str)));
+ }
+}
+
diff --git a/java/src/test/java/org/msgpack/TestPackUnpack.java b/java/src/test/java/org/msgpack/TestPackUnpack.java
index b02bbb4..494c8a8 100644
--- a/java/src/test/java/org/msgpack/TestPackUnpack.java
+++ b/java/src/test/java/org/msgpack/TestPackUnpack.java
@@ -3,245 +3,273 @@ package org.msgpack;
import org.msgpack.*;
import java.io.*;
import java.util.*;
+import java.math.BigInteger;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestPackUnpack {
- 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();
- assertEquals(false, it.hasNext());
- return obj;
- }
-
- @Test
- public void testInt() throws Exception {
- testInt(0);
- testInt(-1);
- testInt(1);
- testInt(Integer.MIN_VALUE);
- testInt(Integer.MAX_VALUE);
- Random rand = new Random();
- for (int i = 0; i < 1000; i++)
- testInt(rand.nextInt());
- }
- public void testInt(int val) throws Exception {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- new Packer(out).pack(val);
- Object obj = unpackOne(out);
- if (obj instanceof Byte)
- assertEquals(val, ((Byte)obj).intValue());
- else if (obj instanceof Integer)
- assertEquals(val, ((Integer)obj).intValue());
- else if (obj instanceof Short)
- assertEquals(val, ((Short)obj).intValue());
- else if (obj instanceof Long)
- assertEquals(val, ((Long)obj).intValue());
- else {
- System.out.println("Got unexpected class: " + obj.getClass());
- assertTrue(false);
- }
- }
-
- @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 {
- List<Integer> emptyList = new ArrayList<Integer>();
- testArray(emptyList, Schema.parse("(array int)"));
-
- 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 {
- Map<Integer, Integer> emptyMap = new HashMap<Integer, Integer>();
- testMap(emptyMap, Schema.parse("(map int int)"));
-
- 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);
- }
- }
+ public MessagePackObject unpackOne(ByteArrayOutputStream out) {
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker pac = new Unpacker(in);
+ Iterator<MessagePackObject> it = pac.iterator();
+ assertEquals(true, it.hasNext());
+ MessagePackObject obj = it.next();
+ assertEquals(false, it.hasNext());
+ return obj;
+ }
+
+ @Test
+ public void testInt() throws Exception {
+ testInt(0);
+ testInt(-1);
+ testInt(1);
+ testInt(Integer.MIN_VALUE);
+ testInt(Integer.MAX_VALUE);
+ Random rand = new Random();
+ for (int i = 0; i < 1000; i++)
+ testInt(rand.nextInt());
+ }
+ public void testInt(int val) throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ new Packer(out).pack(val);
+ MessagePackObject obj = unpackOne(out);
+ assertEquals(val, obj.asInt());
+ }
+
+ @Test
+ public void testLong() throws Exception {
+ testLong(0);
+ testLong(-1);
+ testLong(1);
+ testLong(Integer.MIN_VALUE);
+ testLong(Integer.MAX_VALUE);
+ testLong(Long.MIN_VALUE);
+ testLong(Long.MAX_VALUE);
+ Random rand = new Random();
+ for (int i = 0; i < 1000; i++)
+ testLong(rand.nextLong());
+ }
+ public void testLong(long val) throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ new Packer(out).pack(val);
+ MessagePackObject obj = unpackOne(out);
+ assertEquals(val, obj.asLong());
+ }
+
+ @Test
+ public void testBigInteger() throws Exception {
+ testBigInteger(BigInteger.valueOf(0));
+ testBigInteger(BigInteger.valueOf(-1));
+ testBigInteger(BigInteger.valueOf(1));
+ testBigInteger(BigInteger.valueOf(Integer.MIN_VALUE));
+ testBigInteger(BigInteger.valueOf(Integer.MAX_VALUE));
+ testBigInteger(BigInteger.valueOf(Long.MIN_VALUE));
+ testBigInteger(BigInteger.valueOf(Long.MAX_VALUE));
+ BigInteger max = BigInteger.valueOf(Long.MAX_VALUE).setBit(63);
+ testBigInteger(max);
+ Random rand = new Random();
+ for (int i = 0; i < 1000; i++)
+ testBigInteger( max.subtract(BigInteger.valueOf( Math.abs(rand.nextLong()) )) );
+ }
+ public void testBigInteger(BigInteger val) throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ new Packer(out).pack(val);
+ MessagePackObject obj = unpackOne(out);
+ assertEquals(val, obj.asBigInteger());
+ }
+
+ @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);
+ MessagePackObject obj = unpackOne(out);
+ float f = obj.asFloat();
+ assertEquals(val, f, 10e-10);
+ }
+
+ @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);
+ MessagePackObject obj = unpackOne(out);
+ double f = obj.asDouble();
+ assertEquals(val, f, 10e-10);
+ }
+
+ @Test
+ public void testNil() throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ new Packer(out).packNil();
+ MessagePackObject obj = unpackOne(out);
+ assertTrue(obj.isNil());
+ }
+
+ @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);
+ MessagePackObject obj = unpackOne(out);
+ assertEquals(val, obj.asBoolean());
+ }
+
+ @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);
+ MessagePackObject obj = unpackOne(out);
+ assertEquals(val, obj.asString());
+ }
+
+ @Test
+ public void testArray() throws Exception {
+ List<Integer> emptyList = new ArrayList<Integer>();
+ {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ new Packer(out).pack(emptyList);
+ MessagePackObject obj = unpackOne(out);
+ assertEquals(emptyList, obj.asList());
+ }
+
+ for (int i = 0; i < 1000; i++) {
+ List<Integer> l = new ArrayList<Integer>();
+ int len = (int)Math.random() % 1000 + 1;
+ for (int j = 0; j < len; j++)
+ l.add(j);
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ new Packer(out).pack(l);
+ MessagePackObject obj = unpackOne(out);
+ List<MessagePackObject> list = obj.asList();
+ assertEquals(l.size(), list.size());
+ for (int j = 0; j < len; j++) {
+ assertEquals(l.get(j).intValue(), list.get(j).asInt());
+ }
+ }
+
+ for (int i = 0; i < 1000; i++) {
+ 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));
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ new Packer(out).pack(l);
+ MessagePackObject obj = unpackOne(out);
+ List<MessagePackObject> list = obj.asList();
+ assertEquals(l.size(), list.size());
+ for (int j = 0; j < len; j++) {
+ assertEquals(l.get(j), list.get(j).asString());
+ }
+ }
+ }
+
+ @Test
+ public void testMap() throws Exception {
+ Map<Integer, Integer> emptyMap = new HashMap<Integer, Integer>();
+ {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ new Packer(out).pack(emptyMap);
+ MessagePackObject obj = unpackOne(out);
+ assertEquals(emptyMap, obj.asMap());
+ }
+
+ for (int i = 0; i < 1000; i++) {
+ 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);
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ new Packer(out).pack(m);
+ MessagePackObject obj = unpackOne(out);
+ Map<MessagePackObject, MessagePackObject> map = obj.asMap();
+ assertEquals(m.size(), map.size());
+ for (Map.Entry<MessagePackObject, MessagePackObject> pair : map.entrySet()) {
+ Integer val = m.get(pair.getKey().asInt());
+ assertNotNull(val);
+ assertEquals(val.intValue(), pair.getValue().asInt());
+ }
+ }
+
+ for (int i = 0; i < 1000; i++) {
+ 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);
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ new Packer(out).pack(m);
+ MessagePackObject obj = unpackOne(out);
+ Map<MessagePackObject, MessagePackObject> map = obj.asMap();
+ assertEquals(m.size(), map.size());
+ for (Map.Entry<MessagePackObject, MessagePackObject> pair : map.entrySet()) {
+ Integer val = m.get(pair.getKey().asString());
+ assertNotNull(val);
+ assertEquals(val.intValue(), pair.getValue().asInt());
+ }
+ }
+ }
};
+
diff --git a/java/src/test/resources/cases.json b/java/src/test/resources/cases.json
new file mode 100644
index 0000000..fd390d4
--- /dev/null
+++ b/java/src/test/resources/cases.json
@@ -0,0 +1 @@
+[false,true,null,0,0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,127,127,255,65535,4294967295,-32,-32,-128,-32768,-2147483648,0.0,-0.0,1.0,-1.0,"a","a","a","","","",[0],[0],[0],[],[],[],{},{},{},{"a":97},{"a":97},{"a":97},[[]],[["a"]]] \ No newline at end of file
diff --git a/java/src/test/resources/cases.mpac b/java/src/test/resources/cases.mpac
new file mode 100644
index 0000000..5ec08c6
--- /dev/null
+++ b/java/src/test/resources/cases.mpac
Binary files differ
diff --git a/java/src/test/resources/cases_compact.mpac b/java/src/test/resources/cases_compact.mpac
new file mode 100644
index 0000000..8812442
--- /dev/null
+++ b/java/src/test/resources/cases_compact.mpac
Binary files differ