Пример #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]);
            }
        }
Пример #2
0
        public void TestByteBuffer()
        {
            checkBufferEmpty(ByteBuffer.Empty);

            var buffer = new ByteBuffer();

            checkBufferEmpty(buffer);

            buffer = new ByteBuffer(checkCount);
            checkBufferNotEmpty(ref buffer, null, 0, checkCount);

            var tempBuffer = buffer;

            var array = new byte[checkCount];

            buffer = new ByteBuffer(array);
            checkBufferNotEmpty(ref buffer, array, 0, checkCount);
            Assert.IsTrue(buffer != tempBuffer);

            tempBuffer = buffer;

            buffer = new ByteBuffer(array, 1, 3);
            checkBufferNotEmpty(ref buffer, array, 1, 3);
            Assert.IsTrue(buffer != tempBuffer);

            buffer = new ByteBuffer(new ArraySegment <byte>(array));
            checkBufferNotEmpty(ref buffer, array, 0, checkCount);
            Assert.IsTrue(buffer == tempBuffer);

            buffer = new ByteBuffer(new ByteBufferNode(array));
            checkBufferNotEmpty(ref buffer, array, 0, checkCount);
            Assert.IsTrue(buffer == tempBuffer);

            buffer = new ByteBuffer(new ByteBuffer(array));
            checkBufferNotEmpty(ref buffer, array, 0, checkCount);
            Assert.IsTrue(buffer == tempBuffer);

            buffer = new ByteBuffer(array);
            buffer = new ByteBuffer(ref buffer, 1);
            checkBufferNotEmpty(ref buffer, array, 1, checkCount - 1);
            Assert.IsTrue(buffer != tempBuffer);

            buffer = new ByteBuffer(checkCount);
            checkBufferNotEmpty(ref buffer, null, 0, checkCount);

            for (int i = 0; i < checkCount; i++)
            {
                buffer[i] = (byte)i;
            }

            for (int i = 0; i < checkCount; i++)
            {
                Assert.AreEqual(buffer[i], (byte)i);
            }

            int advanceOffset = 10;

            tempBuffer = ByteBuffer.AdvanceOffset(ref buffer, advanceOffset);
            checkBufferNotEmpty(ref tempBuffer, null, advanceOffset, checkCount - advanceOffset);
            Assert.IsTrue(tempBuffer.CheckPtr(0, ref buffer, advanceOffset));

            tempBuffer = ByteBuffer.AdvanceSize(ref buffer, advanceOffset);
            checkBufferNotEmpty(ref tempBuffer, null, 0, checkCount - advanceOffset);
            Assert.IsTrue(tempBuffer.CheckPtr(ref buffer));

            tempBuffer = ByteBuffer.Resize(ref buffer, checkCount - advanceOffset);
            checkBufferNotEmpty(ref tempBuffer, null, 0, checkCount - advanceOffset);
            Assert.IsTrue(tempBuffer.CheckPtr(ref buffer));

            tempBuffer = buffer;
            tempBuffer.AdvanceOffset(advanceOffset);
            checkBufferNotEmpty(ref tempBuffer, null, advanceOffset, checkCount - advanceOffset);
            Assert.IsTrue(tempBuffer.CheckPtr(0, ref buffer, advanceOffset));

            tempBuffer = buffer;
            tempBuffer.AdvanceSize(advanceOffset);
            checkBufferNotEmpty(ref tempBuffer, null, 0, checkCount - advanceOffset);
            Assert.IsTrue(tempBuffer.CheckPtr(ref buffer));

            tempBuffer = buffer;
            tempBuffer.Resize(checkCount - advanceOffset);
            checkBufferNotEmpty(ref tempBuffer, null, 0, checkCount - advanceOffset);
            Assert.IsTrue(tempBuffer.CheckPtr(ref buffer));

            tempBuffer = buffer;
            buffer     = new ByteBuffer(checkCount);
            Assert.IsFalse(buffer.CheckPtr(ref tempBuffer));

            tempBuffer.CopyTo(ref buffer);
            for (int i = 0; i < checkCount; i++)
            {
                Assert.AreEqual(buffer[i], (byte)i);
            }

            tempBuffer.CopyTo(10, ref buffer, 20, 30);

            Assert.AreEqual(buffer[20 - 1], 20 - 1);
            for (int i = 0; i < 30; i++)
            {
                Assert.AreEqual(buffer[20 + i], (byte)(10 + i));
            }
            Assert.AreEqual(buffer[20 + 30], 20 + 30);

            buffer = new ByteBuffer(checkCount);

            for (int i = 0; i < checkCount; i++)
            {
                buffer.WriteByte(i, (byte)i);
            }

            for (int i = 0; i < checkCount; i++)
            {
                Assert.AreEqual(buffer.ReadByte(i), (byte)i);
            }

            var blockBuffer = new BlockBuffer();

            blockBuffer.Add(buffer);
            tempBuffer = blockBuffer.ToBuffer();
            checkBufferNotEmpty(ref tempBuffer, null, 0, buffer.Count);
            Assert.IsTrue(tempBuffer.CheckPtr(ref buffer));

            for (int i = 0; i < checkCount; i++)
            {
                Assert.AreEqual(tempBuffer.ReadByte(i), (byte)i);
            }

            buffer = new ByteBuffer(checkCount);
            long startValue = Byte.MaxValue * 2;
            int  count      = checkCount / 2;

            for (int i = 0; i < count; i++)
            {
                buffer.WriteUint16(i * 2, (ushort)(startValue + i));
            }

            for (int i = 0; i < count; i++)
            {
                Assert.AreEqual(buffer.ReadUint16(i * 2), (ushort)(startValue + i));
            }

            buffer     = new ByteBuffer(checkCount);
            startValue = ushort.MaxValue * 2;
            count      = checkCount / 4;
            for (int i = 0; i < count; i++)
            {
                buffer.WriteUint32(i * 4, (uint)(startValue + i));
            }

            for (int i = 0; i < count; i++)
            {
                Assert.AreEqual(buffer.ReadUint32(i * 4), (uint)(startValue + i));
            }

            buffer     = new ByteBuffer(checkCount);
            startValue = (long)uint.MaxValue * 2;
            count      = checkCount / 8;
            for (int i = 0; i < count; i++)
            {
                buffer.WriteUint64(i * 8, (uint)(startValue + i));
            }

            for (int i = 0; i < count; i++)
            {
                Assert.AreEqual(buffer.ReadUint64(i * 8), (uint)(startValue + i));
            }

            tempBuffer = buffer;

            buffer = new ByteBuffer(checkCount);
            for (int i = 0; i < count; i++)
            {
                buffer.WriteBuffer(i * 8, ref tempBuffer, i * 8, 8);
            }

            for (int i = 0; i < count; i++)
            {
                Assert.AreEqual(buffer.ReadUint64(i * 8), (uint)(startValue + i));
            }

            tempBuffer = buffer;
            buffer     = new ByteBuffer(checkCount);

            for (int i = 0; i < count; i++)
            {
                tempBuffer.ReadBuffer(i * 8, ref buffer, i * 8, 8);
            }

            for (int i = 0; i < count; i++)
            {
                Assert.AreEqual(buffer.ReadUint64(i * 8), (uint)(startValue + i));
            }

            string checkString = "";

            for (int i = 0; i < checkCount / 2; i++)
            {
                checkString += i.ToString();
            }

            int stringSize = Encoding.UTF8.GetByteCount(checkString);

            Assert.IsTrue(stringSize > 0 && stringSize < checkCount);

            int index = Rand.Default.RandInt(checkCount - stringSize);

            buffer = new ByteBuffer(checkCount);
            buffer.WriteString(index, checkString);

            int readOffset;
            var value = buffer.ReadString(index, out readOffset);

            Assert.AreEqual(value, checkString);
            Assert.Less(stringSize, readOffset);

            buffer = new ByteBuffer(checkCount);
            count  = checkCount / 4;
            float[] checkFloatSet = new float[count];
            for (int i = 0; i < count; i++)
            {
                checkFloatSet[i] = Rand.Default.RandFloat();
            }

            for (int i = 0; i < count; i++)
            {
                buffer.WriteFloat(i * 4, checkFloatSet[i]);
            }

            for (int i = 0; i < count; i++)
            {
                Assert.AreEqual(buffer.ReadFloat(i * 4), checkFloatSet[i]);
            }

            buffer = new ByteBuffer(checkCount);
            count  = checkCount / 8;
            double[] checkDoubleSet = new double[count];
            for (int i = 0; i < count; i++)
            {
                checkDoubleSet[i] = Rand.Default.RandFloat();
            }

            for (int i = 0; i < count; i++)
            {
                buffer.WriteDouble(i * 8, checkDoubleSet[i]);
            }

            for (int i = 0; i < count; i++)
            {
                Assert.AreEqual(buffer.ReadDouble(i * 8), checkDoubleSet[i]);
            }

            buffer = new ByteBuffer(checkCount);
            count  = checkCount / 5;
            uint[] checkUintSet = new uint[count];
            for (int i = 0; i < count; i++)
            {
                checkUintSet[i] = Rand.Default.RandUint();
            }

            int offset = 0;

            for (int i = 0; i < count; i++)
            {
                offset += buffer.WriteVarint32(offset, checkUintSet[i]);
            }

            offset = 0;
            for (int i = 0; i < count; i++)
            {
                int size;
                Assert.AreEqual(buffer.ReadVarint32(offset, out size), checkUintSet[i]);
                offset += size;
            }

            count = checkCount / 9;
            long[] checkLongSet = new long[count];
            for (int i = 0; i < count; i++)
            {
                checkLongSet[i] = Rand.Default.RandLong();
            }

            offset = 0;
            for (int i = 0; i < count; i++)
            {
                offset += buffer.WriteVarint64(offset, (ulong)checkLongSet[i]);
            }

            offset = 0;
            for (int i = 0; i < count; i++)
            {
                int size;
                Assert.AreEqual(buffer.ReadVarint32(offset, out size), (uint)checkLongSet[i]);
                offset += size;
            }

            offset = 0;
            for (int i = 0; i < count; i++)
            {
                int size;
                Assert.AreEqual(buffer.ReadVarint64(offset, out size), checkLongSet[i]);
                offset += size;
            }
        }