add test: TLVTest

This commit is contained in:
free will committed 2021-03-24 17:34:56 +08:00
1 parent adf834a4e9
commit 8a42d7a7de
3 files changed
+80 -9

No files matched your search

+8 -7
View File
@@ -39,6 +39,10 @@ public class VlInt {
public static final int VlIntSize5=5;
public static final int VlIntSize9=9;
public static final long uint16Max= (long) (pow(2, 16)-1);
public static final long uint32Max= (long) (pow(2, 32)-1);
public static final long uint64Max= Long.MAX_VALUE; //MIN1.0中此数用long的最大值表示
/**
* 测量一个变长整型实际用几个字节表示
* @return
@@ -52,15 +56,11 @@ public class VlInt {
}
if(bigInteger.compareTo(BigInteger.valueOf(241)) < 0){
return 1;
}else if(bigInteger.compareTo(BigInteger.valueOf(
new Double(pow(2, 16)).longValue())) < 0){
}else if(bigInteger.compareTo(BigInteger.valueOf(uint16Max)) <= 0){
return 3;
}else if(bigInteger.compareTo(BigInteger.valueOf(
new Double(pow(2, 32)).longValue())) < 0){
}else if(bigInteger.compareTo(BigInteger.valueOf(uint32Max))<= 0){
return 5;
}else if(bigInteger.compareTo(BigInteger.valueOf(
new Double(pow(2, 64)).longValue())) <= 0){
// 这里其实是与long的最大值做对比,所以可以等
}else if(bigInteger.compareTo(BigInteger.valueOf(uint64Max)) <= 0){
return 9;
}else{
//MIN1.0中,VlInt的size最大为9
@@ -287,6 +287,7 @@ public class VlInt {
/**
* 将BigInteger转为byte[按照变长无符号整数方式转]
*
* @param bigInteger
* @return
*/
+60 -2
View File
@@ -1,6 +1,7 @@
package encoding;
import org.junit.Test;
import util.ByteHelper;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@@ -14,12 +15,69 @@ import static org.junit.Assert.assertTrue;
*/
public class TLVTest {
@Test
public void testReadVarNumber() throws TLVException {
public void testReadVarNumber() throws TLVException, VlIntException {
byte[] buf=new byte[10];
// 测试单个字节表示
buf[0]=(byte)240;
VlInt res=TLV.readVarNumber(buf,new VlInt(0));
assertTrue(res.isEqual(240));
// assertEquals();
// 测试两个字节表示
VlInt vlInt=new VlInt(241);
System.arraycopy(vlInt.getVlIntBytes(),0,buf,0,3);
res=TLV.readVarNumber(buf,new VlInt(0));
assertTrue(res.isEqual(241));
// 测试四个字节表示
long maxuint32=(long) (Math.pow(2,32)-1);
vlInt=new VlInt(maxuint32);
System.arraycopy(vlInt.getVlIntBytes(),0,buf,0,5);
res=TLV.readVarNumber(buf,new VlInt(0));
assertEquals(res.getVlIntValue2Long(),maxuint32);
// 测试八个字节表示
long maxlong=Long.MAX_VALUE;
vlInt=new VlInt(maxlong);
System.arraycopy(vlInt.getVlIntBytes(),0,buf,0,9);
res=TLV.readVarNumber(buf,new VlInt(0));
assertTrue(res.isEqual(maxlong));
// 测试不支持的情况
buf[0]=(byte) 244;
try {
res=TLV.readVarNumber(buf,new VlInt(0));
}catch (Exception | Error e){
System.out.println(e.getMessage());
}
}
@Test
public void testReadType() throws TLVException {
byte[] buf=new byte[10];
// 测试单个字节表示
buf[0]=(byte)240;
VlInt res=TLV.readType(buf,new VlInt(0));
assertTrue(res.isEqual(240));
// 测试两个字节表示
VlInt vlInt=new VlInt(300);
System.arraycopy(vlInt.getVlIntBytes(),0,buf,0,3);
res=TLV.readType(buf,new VlInt(0));
assertTrue(res.isEqual(300));
}
@Test
public void testReadNonNegativeInteger() throws TLVException {
byte[] buf=new byte[240];
buf[0]=(byte)240;
System.arraycopy(ByteHelper.uint16ToByteArray(1234),0,buf,1,2);
System.arraycopy(ByteHelper.uint32ToByteArray(1234),0,buf,3,4);
System.arraycopy(ByteHelper.uint64ToByteArray(1234),0,buf,7,8);
long res=TLV.readNonNegativeInteger(buf,1,3);
assertEquals(1234,res);
res=TLV.readNonNegativeInteger(buf,3,7);
assertEquals(1234,res);
res=TLV.readNonNegativeInteger(buf,7,15);
assertEquals(1234,res);
}
}
+12
View File
@@ -217,6 +217,18 @@ public class VlIntTest {
assertEquals(vlInt1.add(a2),sum1);
}
@Test
public void testSizeOfNumber(){
VlInt vlInt=new VlInt(1);
assertEquals(vlInt.size(),1);
vlInt=new VlInt(VlInt.uint16Max);
assertEquals(vlInt.size(),3);
vlInt=new VlInt(VlInt.uint32Max);
assertEquals(vlInt.size(),5);
vlInt=new VlInt(VlInt.uint64Max);
assertEquals(vlInt.size(),9);
}
// @Test
// public void bigInteger2vlintBytes(){
// BigInteger a1 = BigInteger.valueOf(0);