mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
fixed some test error: Encoder
This commit is contained in:
1 parent
5785f29ec3
commit
3daf0156e0
5 files changed
+97
-48
No files matched your search
@@ -26,7 +26,7 @@ public class Encoder {
|
||||
this.right=1;
|
||||
this.isInitial=true;
|
||||
this.isEstimator=true;
|
||||
this.totalReserve=new SizeT(Long.MAX_VALUE); //todo: 这里可能越界。
|
||||
this.totalReserve=new SizeT(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,8 +138,8 @@ public class Encoder {
|
||||
public byte[] buildNonNegativeIntegerArr(long uint64){
|
||||
VlInt vlInt=new VlInt(uint64);
|
||||
int len=vlInt.getVlIntBytes().length;
|
||||
if(len==1){
|
||||
return vlInt.getVlIntBytes();
|
||||
if(uint64<=255){
|
||||
return new byte[]{(byte) uint64};
|
||||
}else {
|
||||
byte[] res = new byte[len - 1];
|
||||
System.arraycopy(vlInt.getVlIntBytes(),1,res,0,len-1);
|
||||
@@ -275,13 +275,14 @@ public class Encoder {
|
||||
* @return
|
||||
*/
|
||||
public int appendByteArrayBlock(VlInt tlvType,byte[] array,SizeT size) throws EncoderException {
|
||||
int totalLength=this.appendByteArray(array,size);
|
||||
try {
|
||||
totalLength+=this.appendVarNumber(new VlInt(size.getVlIntValue2Long()));
|
||||
} catch (VlIntException e) {
|
||||
throw new EncoderException("appendByteArrayBlock: "+e.getMessage());
|
||||
}
|
||||
int totalLength=0;
|
||||
// Append TLV-TYPE
|
||||
totalLength+=this.appendVarNumber(tlvType);
|
||||
// Append TLV-LENGTH
|
||||
totalLength+=this.appendVarNumber(new VlInt(size));
|
||||
// Append TLV-VALUE
|
||||
totalLength+=this.appendByteArray(array,size);
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ public class VlInt {
|
||||
// 1. x < 240 => 1字节表示 => 230
|
||||
// 2. 241 =< x < uint16::Max => 第一个字节为241,后面连着2字节表示实际数值,总共用3个字节表示 => 246
|
||||
// 3. uint16::Max =< x < uint32::Max => 第一个字节为242,后面连着4字节表示实际数值,总共用5个字节表示
|
||||
// 4. int32::Max =< x < int64::Max => 第一个字节为243,后面连着8字节表示实际数值,总共用9个字节表示
|
||||
// 4. int32::Max =< x < uint64::Max => 第一个字节为243,后面连着8字节表示实际数值,总共用9个字节表示
|
||||
// 5. 后续还留有12个字节可以进行指数级扩展,目前留空
|
||||
public static final int VlIntExtend2=241;
|
||||
public static final int VlIntExtend4=242;
|
||||
|
||||
@@ -331,7 +331,8 @@ public class ByteHelper {
|
||||
|
||||
// 便于测试,新添加两个public方法:
|
||||
// 无符号byte[]转int
|
||||
public static int new_byteArrayToInt(byte[]bytes){
|
||||
// 传入的是长度为2或4的byte[]
|
||||
public static int new_byteArrayToInt(byte[] bytes){
|
||||
int value= 0;
|
||||
//由高位到低位
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
@@ -340,13 +341,25 @@ public class ByteHelper {
|
||||
}
|
||||
return value;
|
||||
}
|
||||
// 便于测试,新添加两个public方法:
|
||||
// 无符号byte[]转int(溢出部分)
|
||||
// 传入的是长度为4的byte[]
|
||||
public static long new_byteArrayToAllInt(byte[] bytes){
|
||||
int v=new_byteArrayToInt(bytes);
|
||||
if(v<0){
|
||||
return (long) (v+Math.pow(2,32));
|
||||
}else{
|
||||
return v;
|
||||
}
|
||||
}
|
||||
// 无符号byte[]转long
|
||||
public static long new_byteArrayToLong(byte[]bytes){
|
||||
int value= 0;
|
||||
// 传入的是长度为8的byte[]
|
||||
public static long new_byteArrayToLong(byte[] bytes){
|
||||
long value= 0;
|
||||
//由高位到低位
|
||||
for (int i = 0; i < 8; i++) {
|
||||
int offset= (bytes.length - 1 - i) * 8;
|
||||
value +=(bytes[i] & 0x000000000000ff) << offset; //往高位游
|
||||
int offset= (8 - 1 - i) * 8;
|
||||
value +=((long)bytes[i] & 0xff) << offset; //往高位游
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import util.ByteHelper;
|
||||
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -227,9 +228,17 @@ public class EncoderTest {
|
||||
assertEquals(buffer1[0], (byte) 243);
|
||||
assertEquals(num3,4294967296L);
|
||||
|
||||
// TODO 测试添加64位整型所能表示的最大值
|
||||
// 测试添加64位整型所能表示的最大值
|
||||
// uint64最大值在java中无法表示=>用Long.MaxValue
|
||||
|
||||
encoder.encoderReset(new SizeT(MaxPacketSize),new SizeT(0));
|
||||
encoder.prependVarNumber(new VlInt(Long.MAX_VALUE));
|
||||
buffer1 = encoder.getBuffer();
|
||||
byte[] buf_tmp4 = ByteHelper.getLenBytes(buffer1, new VlInt(1), 8);
|
||||
long num4 = ByteHelper.uint64ToLong(buf_tmp4);
|
||||
System.out.println("num4:"+num4);
|
||||
assertEquals(buffer1.length,9);
|
||||
assertEquals(buffer1[0], (byte) 243);
|
||||
assertEquals(num4,Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
|
||||
@@ -250,7 +259,7 @@ public class EncoderTest {
|
||||
assertEquals(addLen, 1);
|
||||
|
||||
// 测试添加16位整型所能表示的最大值
|
||||
encoder.encoderReset(new SizeT(MaxPacketSize),new SizeT(0));
|
||||
encoder.encoderReset(new SizeT(MaxPacketSize),new SizeT(MaxPacketSize));
|
||||
encoder.appendVarNumber(new VlInt(65535));
|
||||
buffer1 = encoder.getBuffer();
|
||||
byte[] buf_tmp = ByteHelper.getLenBytes(buffer1, new VlInt(1), 2);
|
||||
@@ -260,7 +269,7 @@ public class EncoderTest {
|
||||
assertEquals(num1,65535);
|
||||
|
||||
// 测试添加16位整型所能表示的最大值 + 1
|
||||
encoder.encoderReset(new SizeT(MaxPacketSize),new SizeT(0));
|
||||
encoder.encoderReset(new SizeT(MaxPacketSize),new SizeT(MaxPacketSize));
|
||||
encoder.appendVarNumber(new VlInt(65536));
|
||||
buffer1 = encoder.getBuffer();
|
||||
byte[] buf_tmp2 = ByteHelper.getLenBytes(buffer1, new VlInt(1), 4);
|
||||
@@ -270,7 +279,7 @@ public class EncoderTest {
|
||||
assertEquals(num2,65536);
|
||||
|
||||
// 测试添加32位整型所能表示的最大值 + 1
|
||||
encoder.encoderReset(new SizeT(MaxPacketSize),new SizeT(0));
|
||||
encoder.encoderReset(new SizeT(MaxPacketSize),new SizeT(MaxPacketSize));
|
||||
encoder.appendVarNumber(new VlInt(4294967296L));
|
||||
buffer1 = encoder.getBuffer();
|
||||
byte[] buf_tmp3 = ByteHelper.getLenBytes(buffer1, new VlInt(1), 8);
|
||||
@@ -280,8 +289,16 @@ public class EncoderTest {
|
||||
assertEquals(buffer1[0], (byte) 243);
|
||||
assertEquals(num3,4294967296L);
|
||||
|
||||
// TODO 测试添加64位整型所能表示的最大值
|
||||
// uint64最大值在java中无法表示
|
||||
// 测试添加64位整型所能表示的最大值
|
||||
encoder.encoderReset(new SizeT(MaxPacketSize),new SizeT(MaxPacketSize));
|
||||
encoder.appendVarNumber(new VlInt(Long.MAX_VALUE));
|
||||
buffer1 = encoder.getBuffer();
|
||||
byte[] buf_tmp4 = ByteHelper.getLenBytes(buffer1, new VlInt(1), 8);
|
||||
long num4 = ByteHelper.uint64ToLong(buf_tmp4);
|
||||
System.out.println("num4:"+num4);
|
||||
assertEquals(buffer1.length,9);
|
||||
assertEquals(buffer1[0], (byte) 243);
|
||||
assertEquals(num4,Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -317,12 +334,20 @@ public class EncoderTest {
|
||||
buffer1 = encoder.getBuffer();
|
||||
byte[] buf_tmp2 = ByteHelper.getLenBytes(buffer1, new VlInt(0), 4);
|
||||
// long num2 = ByteHelper.new_byteArrayToLong(buf_tmp2);
|
||||
int num2 = ByteHelper.new_byteArrayToInt(buf_tmp2);
|
||||
long num2 = ByteHelper.new_byteArrayToAllInt(buf_tmp2);
|
||||
assertEquals(buffer1.length,4);
|
||||
assertEquals(num2,4294967295L);
|
||||
|
||||
// 添加 MaxUint64
|
||||
// TODO 测试添加64位整型所能表示的最大值
|
||||
encoder.encoderReset(new SizeT(MaxPacketSize),new SizeT(0));
|
||||
int addLen4 = encoder.prependNonNegativeInteger(Long.MAX_VALUE);
|
||||
assertEquals(addLen4, 8);
|
||||
buffer1 = encoder.getBuffer();
|
||||
byte[] buf_tmp3 = ByteHelper.getLenBytes(buffer1, new VlInt(0), 8);
|
||||
System.out.println(Arrays.toString(buf_tmp3));
|
||||
long num3 = ByteHelper.new_byteArrayToLong(buf_tmp3);
|
||||
assertEquals(buffer1.length,8);
|
||||
assertEquals(num3,Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -333,7 +358,7 @@ public class EncoderTest {
|
||||
estimator.estimatorReset();
|
||||
|
||||
// 测试添加255
|
||||
encoder.encoderReset(new SizeT(MaxPacketSize), new SizeT(0));
|
||||
encoder.encoderReset(new SizeT(MaxPacketSize), new SizeT(MaxPacketSize));
|
||||
int addLen1 = encoder.appendNonNegativeInteger(255);
|
||||
assertEquals(addLen1, 1);
|
||||
byte[] buffer1 = encoder.getBuffer();
|
||||
@@ -341,7 +366,7 @@ public class EncoderTest {
|
||||
assertEquals(buffer1.length,1);
|
||||
|
||||
// 添加 MaxUint16
|
||||
encoder.encoderReset(new SizeT(MaxPacketSize),new SizeT(0));
|
||||
encoder.encoderReset(new SizeT(MaxPacketSize),new SizeT(MaxPacketSize));
|
||||
int addLen2 = encoder.appendNonNegativeInteger(65535);
|
||||
assertEquals(addLen2, 2);
|
||||
buffer1 = encoder.getBuffer();
|
||||
@@ -351,25 +376,24 @@ public class EncoderTest {
|
||||
assertEquals(num1,65535);
|
||||
|
||||
// 添加 MaxUint32
|
||||
encoder.encoderReset(new SizeT(MaxPacketSize),new SizeT(0));
|
||||
encoder.encoderReset(new SizeT(MaxPacketSize),new SizeT(MaxPacketSize));
|
||||
int addLen3 = encoder.appendNonNegativeInteger(4294967295L);
|
||||
assertEquals(addLen3, 4);
|
||||
buffer1 = encoder.getBuffer();
|
||||
byte[] buf_tmp2 = ByteHelper.getLenBytes(buffer1, new VlInt(0), 4);
|
||||
int num2 = ByteHelper.new_byteArrayToInt(buf_tmp2);
|
||||
long num2 = ByteHelper.new_byteArrayToAllInt(buf_tmp2);
|
||||
assertEquals(buffer1.length,4);
|
||||
assertEquals(num2,4294967295L);
|
||||
|
||||
// 添加 MaxUint64
|
||||
// TODO 测试添加64位整型所能表示的最大值
|
||||
// encoder.encoderReset(new SizeT(MaxPacketSize),new SizeT(0));
|
||||
// int addLen4 = encoder.appendNonNegativeInteger(18446744073709551615L);
|
||||
// assertEquals(addLen4, 8);
|
||||
// buffer1 = encoder.getBuffer();
|
||||
// byte[] buf_tmp3 = ByteHelper.getLenBytes(buffer1, new VlInt(0), 8);
|
||||
// long num3 = ByteHelper.new_byteArrayToLong(buf_tmp3);
|
||||
// assertEquals(buffer1.length,8);
|
||||
// assertEquals(num2,18446744073709551615L);
|
||||
encoder.encoderReset(new SizeT(MaxPacketSize),new SizeT(MaxPacketSize));
|
||||
int addLen4 = encoder.appendNonNegativeInteger(Long.MAX_VALUE);
|
||||
assertEquals(addLen4, 8);
|
||||
buffer1 = encoder.getBuffer();
|
||||
byte[] buf_tmp3 = ByteHelper.getLenBytes(buffer1, new VlInt(0), 8);
|
||||
long num3 = ByteHelper.new_byteArrayToLong(buf_tmp3);
|
||||
assertEquals(buffer1.length,8);
|
||||
assertEquals(num3,Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -403,7 +427,7 @@ public class EncoderTest {
|
||||
//测试字节数组插入数据是否正确
|
||||
int std = 0; //uint:std
|
||||
for (int i = 0; i < 8000; i++) {
|
||||
assertEquals(buf1[6+i],std);
|
||||
assertEquals(ByteHelper.uint8ToInt(buf1[6+i]),std);
|
||||
std = (std+1)%256;
|
||||
}
|
||||
|
||||
@@ -420,27 +444,29 @@ public class EncoderTest {
|
||||
for (int i = 1; i < 8000; i++) {
|
||||
buf[i] = (byte)(buf[i-1] +1);
|
||||
}
|
||||
System.out.println(Arrays.toString(buf));
|
||||
//测试插入数据正确性
|
||||
int addLen = encoder.appendByteArrayBlock(new VlInt(241), buf, new SizeT(buf.length));
|
||||
assertEquals(addLen,3+8000+3);
|
||||
byte[] buf1 = encoder.getBuffer();
|
||||
System.out.println(Arrays.toString(buf1));
|
||||
assertEquals(addLen,8006);
|
||||
assertEquals(buf1.length,8006);
|
||||
assertEquals(buf1[0],(byte)241);
|
||||
assertEquals(buf1[3],(byte)241);
|
||||
assertEquals(ByteHelper.uint8ToInt(buf1[0]),241);
|
||||
assertEquals(ByteHelper.uint8ToInt(buf1[3]),241);
|
||||
//buf1[1:3] == 241
|
||||
byte[] buf_tmp = ByteHelper.getLenBytes(buf1, new VlInt(1), 2);
|
||||
int num1 = ByteHelper.new_byteArrayToInt(buf_tmp);
|
||||
int num1 = ByteHelper.uint16ToInt(buf_tmp);
|
||||
assertEquals(num1,241);
|
||||
//buf1[4:6]==8000
|
||||
byte[] buf_tmp2 = ByteHelper.getLenBytes(buf1, new VlInt(4), 2);
|
||||
int num2 = ByteHelper.new_byteArrayToInt(buf_tmp2);
|
||||
int num2 = ByteHelper.uint16ToInt(buf_tmp2);
|
||||
assertEquals(num2,8000);
|
||||
|
||||
//测试字节数组插入数据是否正确
|
||||
int std = 0; //uint:std
|
||||
for (int i = 0; i < 8000; i++) {
|
||||
assertEquals(buf1[6+i],std);
|
||||
assertEquals(ByteHelper.uint8ToInt(buf1[6+i]),std);
|
||||
std = (std+1)%256;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import minsecurity.crypto.TestSM2;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import util.ByteHelper;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
@@ -48,11 +49,19 @@ public class VlIntTest {
|
||||
|
||||
@Test
|
||||
public void getVlIntBytes() {
|
||||
byte[] a = new byte[]{1,2,3,4};
|
||||
VlInt vlInt_byte = new VlInt(a);
|
||||
byte[] b = new byte[]{1,2,3,5};
|
||||
assertArrayEquals(vlInt_byte.getVlIntBytes(),a);
|
||||
assertFalse(vlInt_byte.getVlIntBytes().equals(b));
|
||||
// byte[] a = new byte[]{1,2,3,4};
|
||||
// VlInt vlInt_byte = new VlInt(a);
|
||||
// byte[] b = new byte[]{1,2,3,5};
|
||||
// assertArrayEquals(vlInt_byte.getVlIntBytes(),a);
|
||||
// assertFalse(vlInt_byte.getVlIntBytes().equals(b));
|
||||
VlInt vlInt_byte=new VlInt(Long.MAX_VALUE);
|
||||
// if(vlInt_byte.getVlIntBytes()==null){
|
||||
System.out.println(vlInt_byte.toString());
|
||||
// }
|
||||
// System.out.println();
|
||||
long value= ByteHelper.uint64ToLong(ByteHelper.getLenBytes(vlInt_byte.getVlIntBytes(),1,8));
|
||||
// System.out.println(value);
|
||||
assertEquals(value,Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user