Esempio n. 1
0
        public void TestAutoShrink()
        {
            IoBuffer buf = ByteBufferAllocator.Instance.Allocate(8);

            buf.AutoShrink = true;

            // Make sure the buffer doesn't shrink too much (less than the initial
            // capacity.)
            buf.Sweep((byte)1);
            buf.Fill(7);
            buf.Compact();
            Assert.AreEqual(8, buf.Capacity);
            Assert.AreEqual(1, buf.Position);
            Assert.AreEqual(8, buf.Limit);
            buf.Clear();
            Assert.AreEqual(1, buf.Get());

            // Expand the buffer.
            buf.Capacity = 32;
            buf.Clear();
            Assert.AreEqual(32, buf.Capacity);

            // Make sure the buffer shrinks when only 1/4 is being used.
            buf.Sweep((byte)1);
            buf.Fill(24);
            buf.Compact();
            Assert.AreEqual(16, buf.Capacity);
            Assert.AreEqual(8, buf.Position);
            Assert.AreEqual(16, buf.Limit);
            buf.Clear();
            for (int i = 0; i < 8; i++)
            {
                Assert.AreEqual(1, buf.Get());
            }

            // Expand the buffer.
            buf.Capacity = 32;
            buf.Clear();
            Assert.AreEqual(32, buf.Capacity);

            // Make sure the buffer shrinks when only 1/8 is being used.
            buf.Sweep((byte)1);
            buf.Fill(28);
            buf.Compact();
            Assert.AreEqual(8, buf.Capacity);
            Assert.AreEqual(4, buf.Position);
            Assert.AreEqual(8, buf.Limit);
            buf.Clear();
            for (int i = 0; i < 4; i++)
            {
                Assert.AreEqual(1, buf.Get());
            }

            // Expand the buffer.
            buf.Capacity = 32;
            buf.Clear();
            Assert.AreEqual(32, buf.Capacity);

            // Make sure the buffer shrinks when 0 byte is being used.
            buf.Fill(32);
            buf.Compact();
            Assert.AreEqual(8, buf.Capacity);
            Assert.AreEqual(0, buf.Position);
            Assert.AreEqual(8, buf.Limit);

            // Expand the buffer.
            buf.Capacity = 32;
            buf.Clear();
            Assert.AreEqual(32, buf.Capacity);

            // Make sure the buffer doesn't shrink when more than 1/4 is being used.
            buf.Sweep((byte)1);
            buf.Fill(23);
            buf.Compact();
            Assert.AreEqual(32, buf.Capacity);
            Assert.AreEqual(9, buf.Position);
            Assert.AreEqual(32, buf.Limit);
            buf.Clear();
            for (int i = 0; i < 9; i++)
            {
                Assert.AreEqual(1, buf.Get());
            }
        }
Esempio n. 2
0
 /// <inheritdoc/>
 public override IoBuffer Compact()
 {
     _buf.Compact();
     return(this);
 }