mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-10-23 08:31:45 +02:00
java: write test programs for org.msgpack.packer.*.java
This commit is contained in:
201
java/src/test/java/org/msgpack/packer/TestPackConvert.java
Normal file
201
java/src/test/java/org/msgpack/packer/TestPackConvert.java
Normal file
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
208
java/src/test/java/org/msgpack/packer/TestPackUnpack.java
Normal file
208
java/src/test/java/org/msgpack/packer/TestPackUnpack.java
Normal file
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user