From 8b79e6d3c72a02f4dc039799e3cd370c06e966b0 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Wed, 18 Aug 2010 18:10:30 +0900 Subject: java: uses MessagePackObject instead of Object for type of deserialized objects --- java/src/test/java/org/msgpack/TestPackUnpack.java | 455 ++++++++++----------- 1 file changed, 219 insertions(+), 236 deletions(-) (limited to 'java/src/test') diff --git a/java/src/test/java/org/msgpack/TestPackUnpack.java b/java/src/test/java/org/msgpack/TestPackUnpack.java index b02bbb4..ca3d235 100644 --- a/java/src/test/java/org/msgpack/TestPackUnpack.java +++ b/java/src/test/java/org/msgpack/TestPackUnpack.java @@ -8,240 +8,223 @@ 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 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 emptyList = new ArrayList(); - testArray(emptyList, Schema.parse("(array int)")); - - for (int i = 0; i < 1000; i++) { - Schema schema = Schema.parse("(array int)"); - List l = new ArrayList(); - 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 l = new ArrayList(); - 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 emptyMap = new HashMap(); - testMap(emptyMap, Schema.parse("(map int int)")); - - for (int i = 0; i < 1000; i++) { - Schema schema = Schema.parse("(map int int)"); - Map m = new HashMap(); - 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 m = new HashMap(); - 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 upk = new Unpacker(in); + Iterator it = upk.iterator(); + assertEquals(true, it.hasNext()); + MessagePackObject obj = it.next(); + assertEquals(false, it.hasNext()); + return obj; + } + + 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 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 testFloat(float val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + MessagePackObject obj = unpackOne(out); + assertEquals(val, obj.asFloat(), 10e-10); + } + @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 testDouble(double val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + MessagePackObject obj = unpackOne(out); + assertEquals(val, obj.asDouble(), 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()); + } + + @Test + public void testNil() throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).packNil(); + MessagePackObject obj = unpackOne(out); + assertTrue(obj.isNull()); + } + + @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()); + } + + 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 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()); + } + } + + @Test + public void testArray() throws Exception { + List emptyList = new ArrayList(); + { + 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 l = new ArrayList(); + 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 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 l = new ArrayList(); + 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 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 emptyMap = new HashMap(); + { + 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 m = new HashMap(); + 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 map = obj.asMap(); + assertEquals(m.size(), map.size()); + for (Map.Entry 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 m = new HashMap(); + 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 map = obj.asMap(); + assertEquals(m.size(), map.size()); + for (Map.Entry pair : map.entrySet()) { + Integer val = m.get(pair.getKey().asString()); + assertNotNull(val); + assertEquals(val.intValue(), pair.getValue().asInt()); + } + } + } }; + -- cgit v1.2.1 From 5658ca5b903b294dcccad0dfa6fa6a7bcd9acc12 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Wed, 18 Aug 2010 22:24:51 +0900 Subject: java: adds ObjectEquals test --- .../test/java/org/msgpack/TestObjectEquals.java | 96 ++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 java/src/test/java/org/msgpack/TestObjectEquals.java (limited to 'java/src/test') 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..b2b018d --- /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 { + 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 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 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 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()); + } + + @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))); + } + + public void testString(String str) throws Exception { + assertTrue(RawType.create(str).equals(RawType.create(str))); + } + @Test + public void testString() throws Exception { + testString(""); + testString("a"); + testString("ab"); + testString("abc"); + } +} + -- cgit v1.2.1 From fdfabc9f8862da5982830017e21b0628a00356eb Mon Sep 17 00:00:00 2001 From: frsyuki Date: Wed, 18 Aug 2010 22:51:23 +0900 Subject: java: adds cross-language test case --- java/src/test/java/org/msgpack/TestCases.java | 46 ++++++++++++++++++++++++++ java/src/test/resources/cases.json | 1 + java/src/test/resources/cases.mpac | Bin 0 -> 213 bytes java/src/test/resources/cases_compact.mpac | Bin 0 -> 116 bytes 4 files changed, 47 insertions(+) create mode 100644 java/src/test/java/org/msgpack/TestCases.java create mode 100644 java/src/test/resources/cases.json create mode 100644 java/src/test/resources/cases.mpac create mode 100644 java/src/test/resources/cases_compact.mpac (limited to 'java/src/test') 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..632645f --- /dev/null +++ b/java/src/test/java/org/msgpack/TestCases.java @@ -0,0 +1,46 @@ +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 { + System.out.println( new File(".").getAbsoluteFile().getParent() ); + + + 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) ); + System.out.println("obj: "+result_compact.getData()); + if(!result.getData().equals(result_compact.getData())) { + System.out.println("compact: "+result_compact.getData().asString()); + System.out.println("data : "+result.getData().asString()); + } + assertTrue( result.getData().equals(result_compact.getData()) ); + } + } +}; + 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 Binary files /dev/null and b/java/src/test/resources/cases.mpac 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 Binary files /dev/null and b/java/src/test/resources/cases_compact.mpac differ -- cgit v1.2.1 From d7469e469490b00f83b61d04e02cef856c805b93 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Wed, 18 Aug 2010 22:55:50 +0900 Subject: java: fixes cross-language test case --- java/src/test/java/org/msgpack/TestCases.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'java/src/test') diff --git a/java/src/test/java/org/msgpack/TestCases.java b/java/src/test/java/org/msgpack/TestCases.java index 632645f..c368972 100644 --- a/java/src/test/java/org/msgpack/TestCases.java +++ b/java/src/test/java/org/msgpack/TestCases.java @@ -21,9 +21,6 @@ public class TestCases { @Test public void testCases() throws Exception { - System.out.println( new File(".").getAbsoluteFile().getParent() ); - - Unpacker pac = new Unpacker(); Unpacker pac_compact = new Unpacker(); @@ -34,13 +31,10 @@ public class TestCases { while(pac.next(result)) { UnpackResult result_compact = new UnpackResult(); assertTrue( pac_compact.next(result_compact) ); - System.out.println("obj: "+result_compact.getData()); - if(!result.getData().equals(result_compact.getData())) { - System.out.println("compact: "+result_compact.getData().asString()); - System.out.println("data : "+result.getData().asString()); - } assertTrue( result.getData().equals(result_compact.getData()) ); } + + assertFalse( pac_compact.next(result) ); } }; -- cgit v1.2.1 From 40dc9de6c9bc90a95e55d5d2999f7e45d34cabc8 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Wed, 18 Aug 2010 23:37:47 +0900 Subject: java: supports packing/unpacking of BigInteger less than 0xffffffffffffffff --- java/src/test/java/org/msgpack/TestPackUnpack.java | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'java/src/test') diff --git a/java/src/test/java/org/msgpack/TestPackUnpack.java b/java/src/test/java/org/msgpack/TestPackUnpack.java index ca3d235..75c5fe7 100644 --- a/java/src/test/java/org/msgpack/TestPackUnpack.java +++ b/java/src/test/java/org/msgpack/TestPackUnpack.java @@ -3,6 +3,7 @@ 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.*; @@ -36,6 +37,32 @@ public class TestPackUnpack { testInt(rand.nextInt()); } + public void testBigInteger(BigInteger val) throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).pack(val); + MessagePackObject obj = unpackOne(out); + if(!val.equals(obj.asBigInteger())) { + System.out.println("expect: "+val); + System.out.println("but : "+obj.asBigInteger()); + } + assertEquals(val, obj.asBigInteger()); + } + @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 testFloat(float val) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).pack(val); -- cgit v1.2.1 From 48da2b8353a640de6bdc4e59c7fe91b640321b27 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Wed, 18 Aug 2010 23:47:31 +0900 Subject: java: adds Packer.pack() --- java/src/test/java/org/msgpack/TestPackUnpack.java | 24 ++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'java/src/test') diff --git a/java/src/test/java/org/msgpack/TestPackUnpack.java b/java/src/test/java/org/msgpack/TestPackUnpack.java index 75c5fe7..8163678 100644 --- a/java/src/test/java/org/msgpack/TestPackUnpack.java +++ b/java/src/test/java/org/msgpack/TestPackUnpack.java @@ -37,14 +37,30 @@ public class TestPackUnpack { testInt(rand.nextInt()); } + 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 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 testBigInteger(BigInteger val) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).pack(val); MessagePackObject obj = unpackOne(out); - if(!val.equals(obj.asBigInteger())) { - System.out.println("expect: "+val); - System.out.println("but : "+obj.asBigInteger()); - } assertEquals(val, obj.asBigInteger()); } @Test -- cgit v1.2.1 From 193a739749687e45a71a6821e625815587c4c4bf Mon Sep 17 00:00:00 2001 From: frsyuki Date: Thu, 19 Aug 2010 00:05:48 +0900 Subject: java: updates TestDirectConversion --- .../java/org/msgpack/TestDirectConversion.java | 381 +++++++++++++-------- .../test/java/org/msgpack/TestObjectEquals.java | 48 +-- java/src/test/java/org/msgpack/TestPackUnpack.java | 54 +-- 3 files changed, 296 insertions(+), 187 deletions(-) (limited to 'java/src/test') diff --git a/java/src/test/java/org/msgpack/TestDirectConversion.java b/java/src/test/java/org/msgpack/TestDirectConversion.java index 77bbc58..dbacf01 100644 --- a/java/src/test/java/org/msgpack/TestDirectConversion.java +++ b/java/src/test/java/org/msgpack/TestDirectConversion.java @@ -8,141 +8,248 @@ 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 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 emptyList = new ArrayList(); + { + 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 l = new ArrayList(); + 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 l = new ArrayList(); + 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 emptyMap = new HashMap(); + { + 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 m = new HashMap(); + 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 m = new HashMap(); + 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/TestObjectEquals.java b/java/src/test/java/org/msgpack/TestObjectEquals.java index b2b018d..25fe927 100644 --- a/java/src/test/java/org/msgpack/TestObjectEquals.java +++ b/java/src/test/java/org/msgpack/TestObjectEquals.java @@ -9,6 +9,17 @@ 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); @@ -23,18 +34,20 @@ public class TestObjectEquals { assertTrue(objBigInt.equals(objLong)); assertTrue(objBigInt.equals(objBigInt)); } + @Test - public void testInt() throws Exception { - testInt(0); - testInt(-1); - testInt(1); - testInt(Integer.MIN_VALUE); - testInt(Integer.MAX_VALUE); + 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++) - testInt(rand.nextInt()); + testLong(rand.nextLong()); } - public void testLong(long val) throws Exception { MessagePackObject objInt = IntegerType.create((int)val); MessagePackObject objLong = IntegerType.create(val); @@ -61,19 +74,6 @@ public class TestObjectEquals { 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()); - } @Test public void testNil() throws Exception { @@ -82,9 +82,6 @@ public class TestObjectEquals { assertFalse(NilType.create().equals(BooleanType.create(false))); } - public void testString(String str) throws Exception { - assertTrue(RawType.create(str).equals(RawType.create(str))); - } @Test public void testString() throws Exception { testString(""); @@ -92,5 +89,8 @@ public class TestObjectEquals { 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 8163678..7edd411 100644 --- a/java/src/test/java/org/msgpack/TestPackUnpack.java +++ b/java/src/test/java/org/msgpack/TestPackUnpack.java @@ -11,20 +11,14 @@ import static org.junit.Assert.*; public class TestPackUnpack { public MessagePackObject unpackOne(ByteArrayOutputStream out) { ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - Unpacker upk = new Unpacker(in); - Iterator it = upk.iterator(); + Unpacker pac = new Unpacker(in); + Iterator it = pac.iterator(); assertEquals(true, it.hasNext()); MessagePackObject obj = it.next(); assertEquals(false, it.hasNext()); return obj; } - 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 testInt() throws Exception { testInt(0); @@ -36,13 +30,13 @@ public class TestPackUnpack { for (int i = 0; i < 1000; i++) testInt(rand.nextInt()); } - - public void testLong(long val) throws Exception { + public void testInt(int val) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).pack(val); MessagePackObject obj = unpackOne(out); - assertEquals(val, obj.asLong()); + assertEquals(val, obj.asInt()); } + @Test public void testLong() throws Exception { testLong(0); @@ -56,13 +50,13 @@ public class TestPackUnpack { for (int i = 0; i < 1000; i++) testLong(rand.nextLong()); } - - public void testBigInteger(BigInteger val) throws Exception { + public void testLong(long val) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).pack(val); MessagePackObject obj = unpackOne(out); - assertEquals(val, obj.asBigInteger()); + assertEquals(val, obj.asLong()); } + @Test public void testBigInteger() throws Exception { testBigInteger(BigInteger.valueOf(0)); @@ -78,13 +72,13 @@ public class TestPackUnpack { for (int i = 0; i < 1000; i++) testBigInteger( max.subtract(BigInteger.valueOf( Math.abs(rand.nextLong()) )) ); } - - public void testFloat(float val) throws Exception { + public void testBigInteger(BigInteger val) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).pack(val); MessagePackObject obj = unpackOne(out); - assertEquals(val, obj.asFloat(), 10e-10); + assertEquals(val, obj.asBigInteger()); } + @Test public void testFloat() throws Exception { testFloat((float)0.0); @@ -100,13 +94,14 @@ public class TestPackUnpack { for (int i = 0; i < 1000; i++) testFloat(rand.nextFloat()); } - - public void testDouble(double val) throws Exception { + public void testFloat(float val) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).pack(val); MessagePackObject obj = unpackOne(out); - assertEquals(val, obj.asDouble(), 10e-10); + float f = obj.asFloat(); + assertEquals(val, f, 10e-10); } + @Test public void testDouble() throws Exception { testDouble((double)0.0); @@ -122,6 +117,13 @@ public class TestPackUnpack { 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 { @@ -143,12 +145,6 @@ public class TestPackUnpack { assertEquals(val, obj.asBoolean()); } - 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 testString() throws Exception { testString(""); @@ -183,6 +179,12 @@ public class TestPackUnpack { 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 { -- cgit v1.2.1 From 1d17836b7d2eb4e5e0f25d2466df70e137642061 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Thu, 19 Aug 2010 00:54:23 +0900 Subject: java: NilType::create() returns NilType's one and only instance --- java/src/test/java/org/msgpack/TestPackUnpack.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'java/src/test') diff --git a/java/src/test/java/org/msgpack/TestPackUnpack.java b/java/src/test/java/org/msgpack/TestPackUnpack.java index 7edd411..494c8a8 100644 --- a/java/src/test/java/org/msgpack/TestPackUnpack.java +++ b/java/src/test/java/org/msgpack/TestPackUnpack.java @@ -130,7 +130,7 @@ public class TestPackUnpack { ByteArrayOutputStream out = new ByteArrayOutputStream(); new Packer(out).packNil(); MessagePackObject obj = unpackOne(out); - assertTrue(obj.isNull()); + assertTrue(obj.isNil()); } @Test -- cgit v1.2.1 From b4c98584db002cbb4d3960b6ed025212248dcce8 Mon Sep 17 00:00:00 2001 From: frsyuki Date: Thu, 19 Aug 2010 01:19:34 +0900 Subject: java: adds Unpacker.unpackBigInteger() --- .../java/org/msgpack/TestDirectConversion.java | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'java/src/test') diff --git a/java/src/test/java/org/msgpack/TestDirectConversion.java b/java/src/test/java/org/msgpack/TestDirectConversion.java index dbacf01..1822ecb 100644 --- a/java/src/test/java/org/msgpack/TestDirectConversion.java +++ b/java/src/test/java/org/msgpack/TestDirectConversion.java @@ -3,6 +3,7 @@ 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.*; @@ -48,6 +49,29 @@ public class TestDirectConversion { 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); -- cgit v1.2.1 From c8e351b31e28042c10f7406238636c92a64a696c Mon Sep 17 00:00:00 2001 From: frsyuki Date: Sat, 21 Aug 2010 03:36:44 +0900 Subject: java: adds TestMessageUnpackable test --- java/src/test/java/org/msgpack/Image.java | 50 ++++++++++++++++++++++ .../java/org/msgpack/TestMessageUnpackable.java | 34 +++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 java/src/test/java/org/msgpack/Image.java create mode 100644 java/src/test/java/org/msgpack/TestMessageUnpackable.java (limited to 'java/src/test') 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/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); + } +} + -- cgit v1.2.1