summaryrefslogtreecommitdiff
path: root/java
diff options
context:
space:
mode:
authorKazuki Ohta <kazuki.ohta@gmail.com>2010-04-17 22:10:41 +0900
committerKazuki Ohta <kazuki.ohta@gmail.com>2010-04-17 22:10:41 +0900
commit2807504a814fd54f91b32e58476a80b5ca40c2fc (patch)
tree79ce1583f48c94b9615e9f2a5edd03f9faa5813a /java
parent08b716c96d02e281cc096783cbe64932f70919ef (diff)
downloadmsgpack-python-2807504a814fd54f91b32e58476a80b5ca40c2fc.tar.gz
java: add tests for float, double, nil, boolean, string
Diffstat (limited to 'java')
-rw-r--r--java/test/org/msgpack/TestPackUnpack.java140
1 files changed, 130 insertions, 10 deletions
diff --git a/java/test/org/msgpack/TestPackUnpack.java b/java/test/org/msgpack/TestPackUnpack.java
index f8eeae7..1a02cc5 100644
--- a/java/test/org/msgpack/TestPackUnpack.java
+++ b/java/test/org/msgpack/TestPackUnpack.java
@@ -8,7 +8,7 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class TestPackUnpack {
- public Object unpackOne(ByteArrayOutputStream out) {
+ protected Object unpackOne(ByteArrayOutputStream out) {
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Unpacker upk = new Unpacker(in);
Iterator<Object> it = upk.iterator();
@@ -31,22 +31,142 @@ public class TestPackUnpack {
}
public void testInt(int val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
- Packer pk = new Packer(out);
- pk.pack(val);
+ new Packer(out).pack(val);
Object obj = unpackOne(out);
- int val2 = -1;
if (obj instanceof Byte)
- val2 = ((Byte)obj).intValue();
+ assertEquals(val, ((Byte)obj).intValue());
else if (obj instanceof Integer)
- val2 = ((Integer)obj).intValue();
+ assertEquals(val, ((Integer)obj).intValue());
else if (obj instanceof Short)
- val2 = ((Short)obj).intValue();
+ assertEquals(val, ((Short)obj).intValue());
else if (obj instanceof Long)
- val2 = ((Long)obj).intValue();
+ assertEquals(val, ((Long)obj).intValue());
else {
- System.out.println("obj = " + obj.getClass());
+ 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);
}
- assertEquals(val, val2);
}
};