Example #1
0
        public void TestShrink2()
        {
            IoBuffer buf = IoBuffer.Allocate(36);

            buf.Put(Encoding.Default.GetBytes("012345"));
            buf.Flip();
            buf.Position        = 4;
            buf.MinimumCapacity = 8;

            IoBuffer newBuf = buf.Shrink();

            Assert.AreEqual(4, newBuf.Position);
            Assert.AreEqual(6, newBuf.Limit);
            Assert.AreEqual(9, newBuf.Capacity);
            Assert.AreEqual(8, newBuf.MinimumCapacity);

            buf = IoBuffer.Allocate(6);
            buf.Put(Encoding.Default.GetBytes("012345"));
            buf.Flip();
            buf.Position = 4;

            newBuf = buf.Shrink();
            Assert.AreEqual(4, newBuf.Position);
            Assert.AreEqual(6, newBuf.Limit);
            Assert.AreEqual(6, newBuf.Capacity);
            Assert.AreEqual(6, newBuf.MinimumCapacity);
        }
Example #2
0
        public void TestCapacity()
        {
            IoBuffer buffer = IoBuffer.Allocate(10);

            buffer.Put(Encoding.Default.GetBytes("012345"));
            buffer.Flip();

            // See if we can decrease the capacity (we shouldn't be able to go under the minimul capacity)
            buffer.Capacity = 7;
            Assert.AreEqual(10, buffer.Capacity);

            // See if we can increase the capacity
            buffer = IoBuffer.Allocate(10);

            buffer.Put(Encoding.Default.GetBytes("012345"));
            buffer.Flip();
            buffer.Capacity = 14;
            Assert.AreEqual(14, buffer.Capacity);
            buffer.Put(0, (byte)'9');
            Assert.AreEqual((byte)'9', buffer.Get(0));
            Assert.AreEqual((byte)'9', buffer.Get(0));

            // See if we can go down when the minimum capacity is below the current capacity
            // We should not.
            buffer          = IoBuffer.Allocate(10);
            buffer.Capacity = 5;
            Assert.AreEqual(10, buffer.MinimumCapacity);
            Assert.AreEqual(10, buffer.Capacity);
        }
Example #3
0
        public void TestGetSlice()
        {
            IoBuffer buf = IoBuffer.Allocate(36);

            for (Byte i = 0; i < 36; i++)
            {
                buf.Put(i);
            }

            IoBuffer res = buf.GetSlice(1, 3);

            // The limit should be 3, the pos should be 0 and the bytes read
            // should be 0x01, 0x02 and 0x03
            Assert.AreEqual(0, res.Position);
            Assert.AreEqual(3, res.Limit);
            Assert.AreEqual(0x01, res.Get());
            Assert.AreEqual(0x02, res.Get());
            Assert.AreEqual(0x03, res.Get());

            // Now test after a flip
            buf.Flip();

            res = buf.GetSlice(1, 3);
            // The limit should be 3, the pos should be 0 and the bytes read
            // should be 0x01, 0x02 and 0x03
            Assert.AreEqual(0, res.Position);
            Assert.AreEqual(3, res.Limit);
            Assert.AreEqual(0x01, res.Get());
            Assert.AreEqual(0x02, res.Get());
            Assert.AreEqual(0x03, res.Get());
        }
Example #4
0
        public void TestShrink()
        {
            IoBuffer buf = IoBuffer.Allocate(36);

            buf.MinimumCapacity = 0;

            buf.Limit = 18;
            buf.Shrink();
            Assert.AreEqual(18, buf.Capacity);

            buf.Limit = 9;
            buf.Shrink();
            Assert.AreEqual(9, buf.Capacity);

            buf.Limit = 4;
            buf.Shrink();
            Assert.AreEqual(4, buf.Capacity);

            buf.Limit = 2;
            buf.Shrink();
            Assert.AreEqual(2, buf.Capacity);

            buf.Limit = 1;
            buf.Shrink();
            Assert.AreEqual(1, buf.Capacity);

            buf.Limit = 0;
            buf.Shrink();
            Assert.AreEqual(0, buf.Capacity);
        }
Example #5
0
        public void TestPutPrefixedStringWithPrefixLength()
        {
            Encoding encoding = Encoding.GetEncoding("ISO-8859-1");
            IoBuffer buf      = IoBuffer.Allocate(16).Sweep();

            buf.AutoExpand = true;

            buf.PutPrefixedString("A", 1, encoding);
            Assert.AreEqual(2, buf.Position);
            Assert.AreEqual(1, buf.Get(0));
            Assert.AreEqual((byte)'A', buf.Get(1));

            buf.Sweep();
            buf.PutPrefixedString("A", 2, encoding);
            Assert.AreEqual(3, buf.Position);
            Assert.AreEqual(0, buf.Get(0));
            Assert.AreEqual(1, buf.Get(1));
            Assert.AreEqual((byte)'A', buf.Get(2));

            buf.Sweep();
            buf.PutPrefixedString("A", 4, encoding);
            Assert.AreEqual(5, buf.Position);
            Assert.AreEqual(0, buf.Get(0));
            Assert.AreEqual(0, buf.Get(1));
            Assert.AreEqual(0, buf.Get(2));
            Assert.AreEqual(1, buf.Get(3));
            Assert.AreEqual((byte)'A', buf.Get(4));
        }
Example #6
0
        public void TestGetPrefixedString()
        {
            IoBuffer buf      = IoBuffer.Allocate(16);
            Encoding encoding = Encoding.GetEncoding("ISO-8859-1");

            buf.PutInt16((short)3);
            buf.PutString("ABCD", encoding);
            buf.Clear();
            Assert.AreEqual("ABC", buf.GetPrefixedString(encoding));
        }
        public void TestFillByteSize()
        {
            int      length = 1024 * 1020;
            IoBuffer buffer = IoBuffer.Allocate(length);

            buffer.Fill((byte)0x80, length);

            buffer.Flip();
            for (int i = 0; i < length; i++)
            {
                Assert.AreEqual((byte)0x80, buffer.Get());
            }
        }
Example #8
0
        public void TestPutPrefixedString()
        {
            Encoding encoding = Encoding.GetEncoding("ISO-8859-1");
            IoBuffer buf      = IoBuffer.Allocate(16);

            buf.FillAndReset(buf.Remaining);

            // Without autoExpand
            buf.PutPrefixedString("ABC", encoding);
            Assert.AreEqual(5, buf.Position);
            Assert.AreEqual(0, buf.Get(0));
            Assert.AreEqual(3, buf.Get(1));
            Assert.AreEqual((byte)'A', buf.Get(2));
            Assert.AreEqual((byte)'B', buf.Get(3));
            Assert.AreEqual((byte)'C', buf.Get(4));

            buf.Clear();
            try
            {
                buf.PutPrefixedString("123456789012345", encoding);
                Assert.Fail();
            }
            catch (OverflowException)
            {
                // Expected an Exception, signifies test success
                Assert.IsTrue(true);
            }

            // With autoExpand
            buf.Clear();
            buf.AutoExpand = true;
            buf.PutPrefixedString("123456789012345", encoding);
            Assert.AreEqual(17, buf.Position);
            Assert.AreEqual(0, buf.Get(0));
            Assert.AreEqual(15, buf.Get(1));
            Assert.AreEqual((byte)'1', buf.Get(2));
            Assert.AreEqual((byte)'2', buf.Get(3));
            Assert.AreEqual((byte)'3', buf.Get(4));
            Assert.AreEqual((byte)'4', buf.Get(5));
            Assert.AreEqual((byte)'5', buf.Get(6));
            Assert.AreEqual((byte)'6', buf.Get(7));
            Assert.AreEqual((byte)'7', buf.Get(8));
            Assert.AreEqual((byte)'8', buf.Get(9));
            Assert.AreEqual((byte)'9', buf.Get(10));
            Assert.AreEqual((byte)'0', buf.Get(11));
            Assert.AreEqual((byte)'1', buf.Get(12));
            Assert.AreEqual((byte)'2', buf.Get(13));
            Assert.AreEqual((byte)'3', buf.Get(14));
            Assert.AreEqual((byte)'4', buf.Get(15));
            Assert.AreEqual((byte)'5', buf.Get(16));
        }
Example #9
0
        public void TestAllocateNegative()
        {
            Exception expected = null;

            try
            {
                IoBuffer.Allocate(-1);
            }
            catch (Exception e)
            {
                expected = e;
            }
            Assert.IsTrue(expected is ArgumentException);
        }
Example #10
0
        public void TestGetUnsigned()
        {
            IoBuffer buf = IoBuffer.Allocate(16);

            buf.Put((byte)0xA4);
            buf.Put((byte)0xD0);
            buf.Put((byte)0xB3);
            buf.Put((byte)0xCD);
            buf.Flip();

            buf.Order = ByteOrder.LittleEndian;

            buf.Mark();
            Assert.AreEqual(0xA4, buf.Get());
            buf.Reset();
            Assert.AreEqual(0xD0A4, (UInt16)buf.GetInt16());
            buf.Reset();
            Assert.AreEqual(0xCDB3D0A4L, (UInt32)buf.GetInt32());
        }
Example #11
0
        public void TestIndexOf()
        {
            for (int i = 0; i < 2; i++)
            {
                IoBuffer buf = IoBuffer.Allocate(16);
                buf.Put((byte)0x1);
                buf.Put((byte)0x2);
                buf.Put((byte)0x3);
                buf.Put((byte)0x4);
                buf.Put((byte)0x1);
                buf.Put((byte)0x2);
                buf.Put((byte)0x3);
                buf.Put((byte)0x4);
                buf.Position = 2;
                buf.Limit    = 5;

                Assert.AreEqual(4, buf.IndexOf((byte)0x1));
                Assert.AreEqual(-1, buf.IndexOf((byte)0x2));
                Assert.AreEqual(2, buf.IndexOf((byte)0x3));
                Assert.AreEqual(3, buf.IndexOf((byte)0x4));
            }
        }
Example #12
0
        public void TestExpandPos()
        {
            IoBuffer buffer = IoBuffer.Allocate(10);

            buffer.Put(Encoding.Default.GetBytes("012345"));
            buffer.Flip();

            Assert.AreEqual(6, buffer.Remaining);

            // See if we can expand with a lower number of remaining bytes. We should not.
            IoBuffer newBuffer = buffer.Expand(3, 2);

            Assert.AreEqual(6, newBuffer.Limit);
            Assert.AreEqual(10, newBuffer.Capacity);
            Assert.AreEqual(0, newBuffer.Position);

            // Now, let's expand the buffer above the number of current bytes but below the limit
            buffer = IoBuffer.Allocate(10);
            buffer.Put(Encoding.Default.GetBytes("012345"));
            buffer.Flip();

            newBuffer = buffer.Expand(3, 5);
            Assert.AreEqual(8, newBuffer.Limit);
            Assert.AreEqual(10, newBuffer.Capacity);
            Assert.AreEqual(0, newBuffer.Position);

            // Last, expand the buffer above the limit
            buffer = IoBuffer.Allocate(10);

            buffer.Put(Encoding.Default.GetBytes("012345"));
            buffer.Flip();
            newBuffer = buffer.Expand(3, 9);
            Assert.AreEqual(12, newBuffer.Limit);
            Assert.AreEqual(12, newBuffer.Capacity);
            Assert.AreEqual(0, newBuffer.Position);

            // Now, move forward in the buffer
            buffer = IoBuffer.Allocate(10);

            buffer.Put(Encoding.Default.GetBytes("012345"));
            buffer.Flip();
            buffer.Position = 4;

            // See if we can expand with a lower number of remaining bytes. We should not be.
            newBuffer = buffer.Expand(5, 1);
            Assert.AreEqual(6, newBuffer.Limit);
            Assert.AreEqual(10, newBuffer.Capacity);
            Assert.AreEqual(4, newBuffer.Position);

            // Expand above the current limit
            buffer = IoBuffer.Allocate(10);

            buffer.Put(Encoding.Default.GetBytes("012345"));
            buffer.Flip();
            buffer.Position = 4;
            newBuffer       = buffer.Expand(5, 2);
            Assert.AreEqual(7, newBuffer.Limit);
            Assert.AreEqual(10, newBuffer.Capacity);
            Assert.AreEqual(4, newBuffer.Position);

            // Expand above the current capacity
            buffer = IoBuffer.Allocate(10);

            buffer.Put(Encoding.Default.GetBytes("012345"));
            buffer.Flip();
            buffer.Position = 4;
            newBuffer       = buffer.Expand(5, 6);
            Assert.AreEqual(11, newBuffer.Limit);
            Assert.AreEqual(11, newBuffer.Capacity);
            Assert.AreEqual(4, newBuffer.Position);
        }