diff options
| author | frsyuki <frsyuki@users.sourceforge.jp> | 2010-05-20 03:49:26 +0900 |
|---|---|---|
| committer | frsyuki <frsyuki@users.sourceforge.jp> | 2010-05-20 03:49:26 +0900 |
| commit | 979ff809827ab25005364dad41d2fd043b8eaa4d (patch) | |
| tree | 7ff28f97752207791c44268a081e968bcb9a4488 /java/src/test | |
| parent | 6cde9f3a9d3ce03db1f67ea8169b8c7c10be2532 (diff) | |
| download | msgpack-python-979ff809827ab25005364dad41d2fd043b8eaa4d.tar.gz | |
java: redesign
Diffstat (limited to 'java/src/test')
| -rw-r--r-- | java/src/test/java/org/msgpack/TestDirectConversion.java | 154 | ||||
| -rw-r--r-- | java/src/test/java/org/msgpack/TestPackUnpack.java | 2 |
2 files changed, 155 insertions, 1 deletions
diff --git a/java/src/test/java/org/msgpack/TestDirectConversion.java b/java/src/test/java/org/msgpack/TestDirectConversion.java new file mode 100644 index 0000000..d77fe13 --- /dev/null +++ b/java/src/test/java/org/msgpack/TestDirectConversion.java @@ -0,0 +1,154 @@ +package org.msgpack; + +import org.msgpack.*; +import java.io.*; +import java.util.*; + +import org.junit.Test; +import static org.junit.Assert.*; + +public class TestDirectConversion { + private UnpackCursor prepareCursor(ByteArrayOutputStream out) { + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + Unpacker upk = new Unpacker(in); + return upk.begin(); + } + + @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); + UnpackCursor c = prepareCursor(out); + assertEquals(val, c.unpackInt()); + c.commit(); + } + + @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); + UnpackCursor c = prepareCursor(out); + float f = c.unpackFloat(); + if(Float.isNaN(val)) { + assertTrue(Float.isNaN(f)); + } else { + assertEquals(val, f, 10e-10); + } + c.commit(); + } + + @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); + UnpackCursor c = prepareCursor(out); + double f = c.unpackDouble(); + if(Double.isNaN(val)) { + assertTrue(Double.isNaN(f)); + } else { + assertEquals(val, f, 10e-10); + } + c.commit(); + } + + @Test + public void testNil() throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + new Packer(out).packNil(); + UnpackCursor c = prepareCursor(out); + assertEquals(null, c.unpackNull()); + c.commit(); + } + + @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); + UnpackCursor c = prepareCursor(out); + assertEquals(val, c.unpackBoolean()); + c.commit(); + } + + @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); + UnpackCursor c = prepareCursor(out); + assertEquals(val, c.unpackString()); + c.commit(); + } + + // FIXME container types +}; diff --git a/java/src/test/java/org/msgpack/TestPackUnpack.java b/java/src/test/java/org/msgpack/TestPackUnpack.java index 6877853..a16b5b1 100644 --- a/java/src/test/java/org/msgpack/TestPackUnpack.java +++ b/java/src/test/java/org/msgpack/TestPackUnpack.java @@ -237,5 +237,5 @@ public class TestPackUnpack { System.out.println("Got unexpected class: " + obj.getClass()); assertTrue(false); } - } + } }; |
