java: supports packing/unpacking of BigInteger less than 0xffffffffffffffff

This commit is contained in:
frsyuki 2010-08-18 23:37:47 +09:00
parent d7469e4694
commit 40dc9de6c9
4 changed files with 42 additions and 14 deletions

View file

@ -3,6 +3,7 @@ package org.msgpack;
import org.msgpack.*;
import java.io.*;
import java.util.*;
import java.math.BigInteger;
import org.junit.Test;
import static org.junit.Assert.*;
@ -36,6 +37,32 @@ public class TestPackUnpack {
testInt(rand.nextInt());
}
public void testBigInteger(BigInteger val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val);
MessagePackObject obj = unpackOne(out);
if(!val.equals(obj.asBigInteger())) {
System.out.println("expect: "+val);
System.out.println("but : "+obj.asBigInteger());
}
assertEquals(val, obj.asBigInteger());
}
@Test
public void testBigInteger() throws Exception {
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()) )) );
}
public void testFloat(float val) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
new Packer(out).pack(val);