mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
add test: TLVTest
This commit is contained in:
1 parent
adf834a4e9
commit
8a42d7a7de
3 files changed
+80
-9
No files matched your search
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user