Merge branch 'master' of gitee.com:willfree/min-dev-java

This commit is contained in:
free will committed 2021-03-15 17:28:09 +08:00
commit 92408238aa
8 files changed
+327 -33

No files matched your search

+149
View File
@@ -0,0 +1,149 @@
package encoding;
import org.junit.Test;
import util.ByteHelper;
import static org.junit.Assert.*;
/*
* @Author: feng Zhao
* @Description:
* @Version: 1.0.0
* @Date: 14:52 2021/3/14
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class BlockTest {
@Test
public void TestCreateBlockByTypeLengthBuffer() {
byte[] buf = new byte[10000];
Block block = Block.createBlockByTypeLengthBuffer(new VlInt(241), new VlInt(8000),ByteHelper.getLenBytes(buf, new VlInt(0),8000),true);
//测试type与size
assertEquals(block.getType().getVlIntValue2Int(),241);
assertEquals(block.getLength().getVlIntValue2Int(),8000);
//测试rawBuf数据正确性
//rawBuf[0] == 241
byte[] rawBuf = block.getRaw();
assertEquals(rawBuf[0],(byte)241);
// rawBuf[1:3] == 241
int a = ByteHelper.new_byteArrayToInt(ByteHelper.getLenBytes(rawBuf, new VlInt(1),2));
assertEquals(a,241);
//rawBuf[3] == 241
assertEquals(rawBuf[3],(byte) 241);
//rawBuf[4:6] == 8000
int b = ByteHelper.new_byteArrayToInt(ByteHelper.getLenBytes(rawBuf,new VlInt(4),2));
assertEquals(b,8000);
//rawBuf.length == 8006
assertEquals(rawBuf.length,8006);
}
//error:block.Size().size()错误。
@Test
public void TestCreateBlockByBuffer() {
byte[] buf = new byte[10000];
buf[0] = (byte)212;
buf[1] = (byte)241;
byte[] tmp = ByteHelper.uint16ToByteArray(8000);
System.arraycopy(tmp,0,buf,2,2);
Block block = Block.createBlockByBuffer(ByteHelper.getLenBytes(buf,new VlInt(0),8004),true);
assertEquals(block.getType().getVlIntValue2Int(),212);
System.out.println("测试block.Size().size()");
assertEquals(block.getLength().getVlIntValue2Int(),8000);
//block.Size()返回一个SizeT,再调用SizeT.size()
assertEquals(block.Size().size(),8004);
assertTrue(block.hasValue());
}
@Test
public void TestBuildByBlock(){
//Block构造方法测试
byte[] buf = new byte[10000];
Block block1 = Block.createBlockByTypeLengthBuffer(new VlInt(241), new VlInt(8000),ByteHelper.getLenBytes(buf, new VlInt(0),8000),true);
Block block = new Block(block1);
//测试block数据正确性
assertEquals(block.getType().getVlIntValue2Int(),241);
assertEquals(block.getLength().getVlIntValue2Int(),8000);
/*测试rawBuf数据正确性
rawBuf[0] == 241
rawBuf[1:3] == 241
rawBuf[3] == 241
rawBuf[4:6] == 8000
rawBuf.length == 8006
*/
byte[] rawBuf = block.getRaw();
assertEquals(rawBuf[0],(byte)241);
int a = ByteHelper.new_byteArrayToInt(ByteHelper.getLenBytes(rawBuf, new VlInt(1),2));
assertEquals(a,241);
assertEquals(rawBuf[3],(byte) 241);
int b = ByteHelper.new_byteArrayToInt(ByteHelper.getLenBytes(rawBuf,new VlInt(4),2));
assertEquals(b,8000);
assertEquals(rawBuf.length,8006);
}
@Test
public void TestBuildByTypeLengthBuffer(){
//Block构造方法测试
byte[] buf = new byte[10000];
Block block = new Block(new VlInt(241),new VlInt(8000),ByteHelper.getLenBytes(buf,new VlInt(0),8000),true);
//测试block数据正确性
assertEquals(block.getType().getVlIntValue2Int(),241);
assertEquals(block.getLength().getVlIntValue2Int(),8000);
/*测试rawBuf数据正确性
rawBuf[0] == 241
rawBuf[1:3] == 241
rawBuf[3] == 241
rawBuf[4:6] == 8000
rawBuf.length == 8006
*/
byte[] rawBuf = block.getRaw();
assertEquals(rawBuf[0],(byte)241);
int a = ByteHelper.new_byteArrayToInt(ByteHelper.getLenBytes(rawBuf, new VlInt(1),2));
assertEquals(a,241);
assertEquals(rawBuf[3],(byte) 241);
int b = ByteHelper.new_byteArrayToInt(ByteHelper.getLenBytes(rawBuf,new VlInt(4),2));
assertEquals(b,8000);
assertEquals(rawBuf.length,8006);
}
//error
//TODO 错误处理
@Test
public void TestParseSubElements(){
byte[] buf = new byte[10000];
for (int i = 0; i < 240; i++) {
buf[i*8] = (byte)(i+1);
buf[i*8+1] = 6;
}
Block block = new Block(new VlInt(241),new VlInt(240*8),ByteHelper.getLenBytes(buf,new VlInt(0),240*8),true);
block.parseSubElements();
//TODO 错误处理
ElementContainer elementContainer = block.getSubElements();
//测试解析数据正确性
for (int i = 0; i < elementContainer.length(); i++) {
assertEquals(elementContainer.getBlock(i).getType(),new VlInt(i+1));
assertEquals(elementContainer.getBlock(i).getLength(),new VlInt(6));
}
}
//error
//TODO 没测完
@Test
public void TestSubElements(){
byte[] buf = new byte[10000];
Block block = new Block();
block.setType(new VlInt(7));
for (int i = 0; i < 240; i++) {
Block b = Block.createBlockByTypeLengthBuffer(new VlInt(i+1),new VlInt(6),ByteHelper.getLenBytes(buf,new VlInt(0),6),true);
block.addElement(b);
}
// System.out.println("block.hasValue() = "+block.hasValue());
// System.out.println("block.hasSubElement() = "+block.hasSubElement());
ElementContainer elements = block.getSubElements();
}
}
+36 -16
View File
@@ -44,51 +44,71 @@ public class VlIntTest {
public void getVlIntValue2Int() {
//vlint(int)的size小于4时,测试是否能准确转为int输出
int a = 222;
int b = 22222222;
int a2 = 22222222;
byte[] b1 = new byte[]{0x00,0x7f, (byte) 0xff}; //32767
byte[] b2 = new byte[]{0x00,(byte) 0x80, 0x00}; //32768
byte[] b3 = new byte[]{0x00,(byte) 0xff, (byte) 0xff};//65535
VlInt vlInt_int1 = new VlInt(a);
VlInt vlInt_int2 = new VlInt(b);
VlInt vlInt_int2 = new VlInt(a2);
VlInt vlInt_b1 = new VlInt(b1);
VlInt vlInt_b2 = new VlInt(b2);
VlInt vlInt_b3 = new VlInt(b3);
//测试size大小
assertTrue(vlInt_int1.size()<4);
assertFalse(vlInt_int2.size()<4);
//测试VlIntValue是否转为int
assertEquals(vlInt_int1.getVlIntValue2Int(),a);
assertEquals(vlInt_b1.getVlIntValue2Int(),32767);
assertEquals(vlInt_b2.getVlIntValue2Int(),32768);
assertEquals(vlInt_b3.getVlIntValue2Int(),65535);
}
//error:以bytes[]形式构造的VlInt,2^31-1 到 2^32-1之间的数会出错
@Test
public void getVlIntValue2Long() {
long l1 = new Long(0);
long l2 = new Long(1);
long l3 = new Long(4294967295L); //2^32-1
long l4 = new Long(4294967296L); //2^32
byte[] b1 = new byte[]{0x00,0x7f,(byte) 0xff,(byte) 0xff,(byte) 0xff};//2^31-1
byte[] b2= new byte[]{0x00,(byte) 0x80,0x00,0x00,0x00};//2^31
VlInt vlInt_long1 = new VlInt(l1); // size = 1
VlInt vlInt_long2 = new VlInt(l2); // size = 1
VlInt vlInt_long3 = new VlInt(l3); // size = 5
VlInt vlInt_long4 = new VlInt(l4); // size = 9
assertTrue(vlInt_long3.size()<8);
assertFalse(vlInt_long4.size()<8);
VlInt vlInt_long4 = new VlInt(b1);
VlInt vlInt_long5 = new VlInt(b2);
//以VlInt(long)形式构造的VlInt
assertEquals(vlInt_long1.getVlIntValue2Long(),l1);
assertEquals(vlInt_long2.getVlIntValue2Long(),l2);
assertEquals(vlInt_long3.getVlIntValue2Long(),l3);
///以VlInt(bytes[])形式构造的VlInt
assertEquals(vlInt_long4.getVlIntValue2Long(),2147483647L);//2^31-1
assertEquals(vlInt_long5.getVlIntValue2Long(),2147483648L);//2^31
}
//error:以bytes[]形式构造的VlInt,在大于2^31-1的数会出错
@Test
public void getVlIntValue() {
long l1 = new Long(4294967295L); //2^32-1
long l2 = new Long(4294967296L); //2^32
BigInteger b_0 = BigInteger.valueOf(2147483647L);//2^31-1
BigInteger b_1 = BigInteger.valueOf(2147483648L);//2^31
BigInteger b = BigInteger.valueOf(4294967296L);//2^32
//2^31-1
byte[] b0 = new byte[]{0x00,0x00,0x00,0x00,0x00,0x7f,(byte) 0xff,(byte) 0xff,(byte) 0xff};//2147483647
byte[] b1 = new byte[]{0x00,0x00,0x00,0x00,0x00,(byte) 0x80,0x00,0x00,0x00};
//2^32-1
byte[] b2 = new byte[]{0x00,0x00,0x00,0x00,0x00,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};
byte[] b3 = new byte[]{0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00};//2^32
VlInt vlInt_long = new VlInt(l1);
VlInt vlInt_long2 = new VlInt(l2);
VlInt vlInt_bigInteger = new VlInt(b);
//判断size
assertFalse(vlInt_long.size()>=9);
assertTrue(vlInt_long2.size()>=9);
assertTrue(vlInt_bigInteger.size()>=9);
//判断值是否相等
VlInt vlInt_b0 = new VlInt(b0);
VlInt vlInt_b1 = new VlInt(b1);
//以VlInt(BigInteger)形式构造的VlInt
assertEquals(vlInt_bigInteger.getVlIntValue(),b);
///以VlInt(bytes[])形式构造的VlInt
assertEquals(vlInt_b0.getVlIntValue(),b_0);
assertEquals(vlInt_b1.getVlIntValue(),b_1);
}
@Test
+19 -7
View File
@@ -29,7 +29,7 @@ import static org.junit.Assert.*;
assertArrayEquals(b2,test2);
}
//error
//todo:对start值进行限定,应当小于Byte数组的length
@Test
public void getFirstByte() {
byte[] b0 = new byte[]{0x00,0x00,0x01};
@@ -39,9 +39,17 @@ import static org.junit.Assert.*;
assertEquals(firstbyte,test[0]);
}
//error
@Test
public void getLenBytes() {
byte[] buffer = new byte[]{0x00,0x00,0x01, (byte) 0xff}; //255
byte[] test = new byte[]{0x00,0x00,0x01};
VlInt start = new VlInt(0);
VlInt len = new VlInt(3);
byte[]b1 = ByteHelper.getLenBytes(buffer,start,len);
byte[] b2= ByteHelper.getLenBytes(buffer,start,3);
assertArrayEquals(test,b1);
assertArrayEquals(test,b2);
}
// @Test
@@ -298,7 +306,7 @@ import static org.junit.Assert.*;
assertEquals(a9,65535);
}
//error
@Test
public void uint16ToByteArray() {
ByteHelper byteHelper = new ByteHelper();
@@ -368,7 +376,7 @@ import static org.junit.Assert.*;
assertEquals(a9,65535);
}
//error
@Test
public void uint32ToLong() {
byte[] b1 = new byte[]{0x00,0x00,0x00,0x00}; //0
@@ -415,7 +423,7 @@ import static org.junit.Assert.*;
}
//error
@Test
public void uint32ToByteArray() {
byte[] b1 = new byte[]{0x00,0x00,0x00,0x00}; //0
@@ -464,7 +472,7 @@ import static org.junit.Assert.*;
assertArrayEquals(a13,b13);
}
//error
//todo 大于2^64次方无法表示
@Test
public void uint64ToLong() {
@@ -485,7 +493,11 @@ import static org.junit.Assert.*;
byte[] b15 = new byte[]{0x7f,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};//9223372036854775807
byte[] b16 = new byte[]{(byte) 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//9223372036854775808
byte[] b17 = new byte[]{(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff,(byte) 0xff};//18446744073709551615
byte[] b = new byte[]{(byte)0,(byte)0,(byte)0,(byte)0,(byte)0,(byte)0,(byte)0,(byte)1};//1
ByteHelper byteHelper = new ByteHelper();
long a = byteHelper.uint64ToLong(b);
assertEquals(a,1);
// byte[] b_test = new byte[]{0x00,0x00,0x00,0x00,(byte) 0x80,0x00,0x00,0x01};//2147483649
// long a_test = byteHelper.uint64ToLong(b_test);
@@ -530,7 +542,7 @@ import static org.junit.Assert.*;
}
//error
//todo 大于2^64次方无法表示
@Test
public void uint64ToByteArray() {