From 43699e6c7cb03f04da15d3001f018d92b52c8f87 Mon Sep 17 00:00:00 2001 From: zhaofeng233 Date: Sun, 14 Mar 2021 17:44:34 +0800 Subject: [PATCH] =?UTF-8?q?Block=20=E6=B5=8B=E8=AF=95=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/encoding/BlockTest.java | 149 ++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 src/test/java/encoding/BlockTest.java diff --git a/src/test/java/encoding/BlockTest.java b/src/test/java/encoding/BlockTest.java new file mode 100644 index 0000000..4ebdff8 --- /dev/null +++ b/src/test/java/encoding/BlockTest.java @@ -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.byteArrayToInt16(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.byteArrayToInt16(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.byteArrayToInt16(ByteHelper.getLenBytes(rawBuf, new VlInt(1),2)); + assertEquals(a,241); + assertEquals(rawBuf[3],(byte) 241); + int b = ByteHelper.byteArrayToInt16(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.byteArrayToInt16(ByteHelper.getLenBytes(rawBuf, new VlInt(1),2)); + assertEquals(a,241); + assertEquals(rawBuf[3],(byte) 241); + int b = ByteHelper.byteArrayToInt16(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(); + + } +}