summaryrefslogtreecommitdiff
path: root/java
diff options
context:
space:
mode:
authorMuga Nishizawa <muga@f11vm.(none)>2010-10-17 18:57:00 +0900
committerMuga Nishizawa <muga@f11vm.(none)>2010-10-17 18:57:00 +0900
commitd6c2a97931b5c71142b9923fe5b8359f32707485 (patch)
treeb81b516e4caee3f9ed8a1f16540b21fac9c6465c /java
parent17def94ba4c2ee1a748b60fb48b1066f39286a67 (diff)
downloadmsgpack-python-d6c2a97931b5c71142b9923fe5b8359f32707485.tar.gz
java: write test programs for org.msgpack.packer.*.java
Diffstat (limited to 'java')
-rw-r--r--java/src/test/java/org/msgpack/packer/TestPackConvert.java201
-rw-r--r--java/src/test/java/org/msgpack/packer/TestPackUnpack.java208
2 files changed, 409 insertions, 0 deletions
diff --git a/java/src/test/java/org/msgpack/packer/TestPackConvert.java b/java/src/test/java/org/msgpack/packer/TestPackConvert.java
new file mode 100644
index 0000000..98caf82
--- /dev/null
+++ b/java/src/test/java/org/msgpack/packer/TestPackConvert.java
@@ -0,0 +1,201 @@
+package org.msgpack.packer;
+
+import java.io.ByteArrayOutputStream;
+import java.math.BigInteger;
+import java.util.Random;
+
+import junit.framework.TestCase;
+
+import org.junit.Test;
+import org.msgpack.MessagePackObject;
+import org.msgpack.MessagePacker;
+import org.msgpack.Packer;
+import org.msgpack.Util;
+
+public class TestPackConvert extends TestCase {
+ @Test
+ public void testInteger() throws Exception {
+ // _testInteger(null); // FIXME
+ _testInteger(0);
+ _testInteger(-1);
+ _testInteger(1);
+ _testInteger(Integer.MIN_VALUE);
+ _testInteger(Integer.MAX_VALUE);
+ Random rand = new Random();
+ for (int i = 0; i < 1000; i++) {
+ _testInteger(rand.nextInt());
+ }
+ }
+
+ static void _testInteger(Integer src) throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ MessagePacker packer = IntegerPacker.getInstance();
+ packer.pack(new Packer(out), src);
+ MessagePackObject obj = Util.unpackOne(out.toByteArray());
+ assertEquals(src.intValue(), obj.asInt());
+ }
+
+ @Test
+ public void testLong() throws Exception {
+ // _testLong((null); // FIXME
+ _testLong((long) 0);
+ _testLong((long) -1);
+ _testLong((long) 1);
+ _testLong((long) Integer.MIN_VALUE);
+ _testLong((long) 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());
+ }
+ }
+
+ static void _testLong(Long src) throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ MessagePacker packer = LongPacker.getInstance();
+ packer.pack(new Packer(out), src);
+ MessagePackObject obj = Util.unpackOne(out.toByteArray());
+ assertEquals(src.longValue(), obj.asLong());
+ }
+
+ @Test
+ public void testBigInteger() throws Exception {
+ // _testBigInteger(null); // FIXME
+ _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()))));
+ }
+ }
+
+ static void _testBigInteger(BigInteger src) throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ MessagePacker packer = BigIntegerPacker.getInstance();
+ packer.pack(new Packer(out), src);
+ MessagePackObject obj = Util.unpackOne(out.toByteArray());
+ assertEquals(src, obj.asBigInteger());
+ }
+
+ @Test
+ public void testFloat() throws Exception {
+ // _testFloat(null); // FIXME
+ _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());
+ }
+ }
+
+ static void _testFloat(Float src) throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ MessagePacker packer = FloatPacker.getInstance();
+ packer.pack(new Packer(out), src);
+ MessagePackObject obj = Util.unpackOne(out.toByteArray());
+ assertEquals(src.floatValue(), obj.asFloat(), 10e-10);
+ }
+
+ @Test
+ public void testDouble() throws Exception {
+ // _testDouble(null); // FIXME
+ _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());
+ }
+
+ static void _testDouble(Double src) throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ MessagePacker packer = DoublePacker.getInstance();
+ packer.pack(new Packer(out), src);
+ MessagePackObject obj = Util.unpackOne(out.toByteArray());
+ assertEquals(src.doubleValue(), obj.asDouble(), 10e-10);
+ }
+
+ @Test
+ public void testBoolean() throws Exception {
+ // _testBoolean(null); // FIXME
+ _testBoolean(false);
+ _testBoolean(true);
+ }
+
+ static void _testBoolean(Boolean src) throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ MessagePacker packer = BooleanPacker.getInstance();
+ packer.pack(new Packer(out), src);
+ MessagePackObject obj = Util.unpackOne(out.toByteArray());
+ assertEquals(src.booleanValue(), obj.asBoolean());
+ }
+
+ @Test
+ public void testString() throws Exception {
+ // _testString(null); // FIXME
+ _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());
+ }
+ }
+
+ static void _testString(String src) throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ MessagePacker packer = StringPacker.getInstance();
+ packer.pack(new Packer(out), src);
+ MessagePackObject obj = Util.unpackOne(out.toByteArray());
+ assertEquals(src, obj.asString());
+ }
+}
diff --git a/java/src/test/java/org/msgpack/packer/TestPackUnpack.java b/java/src/test/java/org/msgpack/packer/TestPackUnpack.java
new file mode 100644
index 0000000..f1d7842
--- /dev/null
+++ b/java/src/test/java/org/msgpack/packer/TestPackUnpack.java
@@ -0,0 +1,208 @@
+package org.msgpack.packer;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.math.BigInteger;
+import java.util.Random;
+
+import junit.framework.TestCase;
+
+import org.junit.Test;
+import org.msgpack.MessagePacker;
+import org.msgpack.Packer;
+import org.msgpack.Unpacker;
+
+public class TestPackUnpack extends TestCase {
+ @Test
+ public void testInteger() throws Exception {
+ // _testInteger(null); // FIXME
+ _testInteger(0);
+ _testInteger(-1);
+ _testInteger(1);
+ _testInteger(Integer.MIN_VALUE);
+ _testInteger(Integer.MAX_VALUE);
+ Random rand = new Random();
+ for (int i = 0; i < 1000; i++) {
+ _testInteger(rand.nextInt());
+ }
+ }
+
+ static void _testInteger(Integer src) throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ MessagePacker packer = IntegerPacker.getInstance();
+ packer.pack(new Packer(out), src);
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker unpacker = new Unpacker(in);
+ assertEquals(src.intValue(), unpacker.unpackInt());
+ }
+
+ @Test
+ public void testLong() throws Exception {
+ // _testLong((null); // FIXME
+ _testLong((long) 0);
+ _testLong((long) -1);
+ _testLong((long) 1);
+ _testLong((long) Integer.MIN_VALUE);
+ _testLong((long) 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());
+ }
+ }
+
+ static void _testLong(Long src) throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ MessagePacker packer = LongPacker.getInstance();
+ packer.pack(new Packer(out), src);
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker unpacker = new Unpacker(in);
+ assertEquals(src.longValue(), unpacker.unpackLong());
+ }
+
+ @Test
+ public void testBigInteger() throws Exception {
+ // _testBigInteger(null); // FIXME
+ _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()))));
+ }
+ }
+
+ static void _testBigInteger(BigInteger src) throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ MessagePacker packer = BigIntegerPacker.getInstance();
+ packer.pack(new Packer(out), src);
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker unpacker = new Unpacker(in);
+ assertEquals(src, unpacker.unpackBigInteger());
+ }
+
+ @Test
+ public void testFloat() throws Exception {
+ // _testFloat(null); // FIXME
+ _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());
+ }
+ }
+
+ static void _testFloat(Float src) throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ MessagePacker packer = FloatPacker.getInstance();
+ packer.pack(new Packer(out), src);
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker unpacker = new Unpacker(in);
+ assertEquals(src.floatValue(), unpacker.unpackFloat(), 10e-10);
+ }
+
+ @Test
+ public void testDouble() throws Exception {
+ // _testDouble(null); // FIXME
+ _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());
+ }
+
+ static void _testDouble(Double src) throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ MessagePacker packer = DoublePacker.getInstance();
+ packer.pack(new Packer(out), src);
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker unpacker = new Unpacker(in);
+ assertEquals(src.doubleValue(), unpacker.unpackDouble(), 10e-10);
+ }
+
+ @Test
+ public void testBoolean() throws Exception {
+ // _testBoolean(null); // FIXME
+ _testBoolean(false);
+ _testBoolean(true);
+ }
+
+ static void _testBoolean(Boolean src) throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ MessagePacker packer = BooleanPacker.getInstance();
+ packer.pack(new Packer(out), src);
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker unpacker = new Unpacker(in);
+ assertEquals(src.booleanValue(), unpacker.unpackBoolean());
+ }
+
+ @Test
+ public void testString() throws Exception {
+ // _testString(null); // FIXME
+ _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());
+ }
+ }
+
+ static void _testString(String src) throws Exception {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ MessagePacker packer = StringPacker.getInstance();
+ packer.pack(new Packer(out), src);
+ ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+ Unpacker unpacker = new Unpacker(in);
+ assertEquals(src, unpacker.unpackString());
+ }
+}