From abeed3be84b7b12b71598c3535c4c9a6e70c534f Mon Sep 17 00:00:00 2001 From: Kazuki Ohta Date: Sat, 17 Apr 2010 23:25:42 +0900 Subject: [PATCH] java: add tests for array and map --- java/test/org/msgpack/TestPackUnpack.java | 69 +++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/java/test/org/msgpack/TestPackUnpack.java b/java/test/org/msgpack/TestPackUnpack.java index 1a02cc56..68778539 100644 --- a/java/test/org/msgpack/TestPackUnpack.java +++ b/java/test/org/msgpack/TestPackUnpack.java @@ -9,8 +9,13 @@ 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(); @@ -169,4 +174,68 @@ public class TestPackUnpack { assertTrue(false); } } + + @Test + public void testArray() throws Exception { + 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 { + 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); + } + } };