Exemplo n.º 1
0
        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]);
            }
        }