public void TestBufferReaderWriter() { var writer = new BlockBufferWriter(); int loop = 10000; var data = generateCheckData(); Assert.IsNotNull(data); for (int i = 0; i < loop; i++) { var index = Rand.Default.RandInt(data.Count); writeData(writer, index, data[index]); } var reader = new BlockBufferReader(writer.Block, writer.Position); var count = readData(reader, data); Assert.AreEqual(count, loop); // check read block var block = writer.Block; var size = writer.Position; writer = new BlockBufferWriter(); writer.WriteBlock(block, size); var blockWrite = writer.ToBlock(); reader = new BlockBufferReader(blockWrite); Assert.AreEqual(reader.Limit, size); var checkBlock = new BlockBuffer(); Assert.AreEqual(reader.ReadBlock(checkBlock), size); Assert.IsTrue(reader.IsEnd); var array = block.ToArray(); var arrayCheck = checkBlock.ToArray(); for (int i = 0; i < size; i++) { Assert.AreEqual(array[i], arrayCheck[i]); } }
public void TestBufferBlock() { var data = new byte[checkCount]; for (int i = 0; i < data.Length; i++) { data[i] = Rand.Default.RandByte(); } var block = new BlockBuffer(); int offset = 0; Assert.IsTrue(block.IsEmpty); while (offset < data.Length) { int size = Rand.Default.RandInt(data.Length - offset + 1); if (size > 0) { var unit = new ByteBufferNode(data, offset, size); offset += size; block.Add(unit); } } Assert.IsFalse(block.IsEmpty); Assert.AreEqual(block.Count, checkCount); var array = block.ToArray(); Assert.IsNotNull(array); Assert.AreEqual(array.Length, checkCount); for (int i = 0; i < checkCount; i++) { Assert.AreEqual(data[i], array[i]); } for (int i = 0; i < checkCount; i++) { block[i] = 0; } for (int i = 0; i < checkCount; i++) { Assert.AreEqual(0, block[i]); } for (int i = 0; i < checkCount; i++) { block[i] = data[i]; } for (int i = 0; i < checkCount; i++) { Assert.AreEqual(data[i], block[i]); } var tempBlock = block; block = (BlockBuffer)tempBlock.Clone(); int checkOffset = 10; block.AdvanceOffset(checkOffset); Assert.AreEqual(block.Count, checkCount - checkOffset); array = block.ToArray(); Assert.IsNotNull(array); Assert.AreEqual(array.Length, checkCount - checkOffset); for (int i = 0; i < checkCount - checkOffset; i++) { Assert.AreEqual(data[i + checkOffset], array[i]); } block = (BlockBuffer)tempBlock.Clone(); block.AdvanceSize(checkOffset); Assert.AreEqual(block.Count, checkCount - checkOffset); array = block.ToArray(); Assert.IsNotNull(array); Assert.AreEqual(array.Length, checkCount - checkOffset); for (int i = 0; i < checkCount - checkOffset; i++) { Assert.AreEqual(data[i], array[i]); } block = (BlockBuffer)tempBlock.Clone(); block.Resize(checkCount - checkOffset); Assert.AreEqual(block.Count, checkCount - checkOffset); array = block.ToArray(); Assert.IsNotNull(array); Assert.AreEqual(array.Length, checkCount - checkOffset); for (int i = 0; i < checkCount - checkOffset; i++) { Assert.AreEqual(data[i], array[i]); } block = tempBlock; for (int i = 0; i < checkCount; i++) { int index = Rand.Default.Range(1, checkCount - 1); int length = Rand.Default.Range(1, checkCount - index); var value = block.ToArray(index, length); Assert.IsNotNull(value); Assert.AreEqual(value.Length, length); for (int j = 0; j < length; j++) { Assert.AreEqual(data[j + index], value[j]); } } block = (BlockBuffer)block.Clone(); Assert.AreEqual(block.Count, checkCount); array = block.ToArray(); Assert.IsNotNull(array); Assert.AreEqual(array.Length, checkCount); for (int i = 0; i < checkCount; i++) { Assert.AreEqual(data[i], array[i]); } }