示例#1
0
        public void GetSegmentsTest8()
        {
            //Single slab test:
            {
                MemorySlab    slab   = new MemorySlab(blockSize * 3, null);
                ManagedBuffer target = GetNewBuffer(slab);
                var           actual = target.GetSegments();
                Assert.AreEqual <int>(1, actual.Count);
                Assert.AreEqual <long>(target.MemoryBlocks[0].StartLocation, actual[0].Offset);
                Assert.AreEqual <long>(target.Size, actual[0].Count);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[0].Slab.Array, actual[0].Array);
            }

            //Multi slab test:
            {
                MemorySlab[]  slabs  = { new MemorySlab(blockSize * 2, null), new MemorySlab(blockSize * 2, null), new MemorySlab(blockSize * 2, null) };
                ManagedBuffer target = GetNewBuffer(slabs);
                var           actual = target.GetSegments();
                Assert.AreEqual(3, actual.Count);
                Assert.AreEqual <long>(target.MemoryBlocks[0].StartLocation, actual[0].Offset);
                Assert.AreEqual <long>(target.MemoryBlocks[1].StartLocation, actual[1].Offset);
                Assert.AreEqual <long>(target.MemoryBlocks[2].StartLocation, actual[2].Offset);
                Assert.AreEqual <long>(target.Size, actual[0].Count + actual[1].Count + actual[2].Count);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[0].Slab.Array, actual[0].Array);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[1].Slab.Array, actual[1].Array);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[2].Slab.Array, actual[2].Array);
            }
        }
示例#2
0
        public void GetArraySegmentTest()
        {
            MemorySlab          slab   = new MemorySlab(blockSize * 3, null);
            ManagedBuffer       target = GetNewBuffer(slab);
            int                 Offset = 0;
            int                 Length = (int)(blockSize - 1);
            ArraySegment <byte> actual;

            actual = target.GetSegments(Offset, Length)[0];
            Assert.AreEqual <long>(actual.Offset, Offset + target.memoryBlock.StartLocation);
            Assert.AreEqual <long>(actual.Count, Length);
            Assert.AreEqual <byte[]>(actual.Array, target.memoryBlock.Slab.Array);

            //Test for full blocksize
            Offset = 0;
            Length = (int)blockSize;
            actual = target.GetSegments(Offset, Length)[0];
            Assert.AreEqual <long>(actual.Offset, Offset + target.memoryBlock.StartLocation);
            Assert.AreEqual <long>(actual.Count, Length);
            Assert.AreEqual <byte[]>(actual.Array, target.memoryBlock.Slab.Array);


            //Test for offset of 1
            Offset = 1;
            Length = (int)(blockSize - 1);
            actual = target.GetSegments(Offset, Length)[0];
            Assert.AreEqual <long>(actual.Offset, Offset + target.memoryBlock.StartLocation);
            Assert.AreEqual <long>(actual.Count, Length);
            Assert.AreEqual <byte[]>(actual.Array, target.memoryBlock.Slab.Array);
        }
示例#3
0
        public void CopyToTest()
        {
            MemorySlab    slab = new MemorySlab(blockSize * 3, null);
            ManagedBuffer target1, target2;

            {
                ManagedBuffer target = GetNewBuffer(slab);
                target1 = target;
                target.FillWith(GetRandomizedByteArray(blockSize));
                byte[] DestArray = new byte[blockSize];
                target.CopyTo(DestArray);
                byte[] copyOfSource      = new byte[blockSize];
                byte[] copyOfDestination = new byte[blockSize];
                Array.Copy(DestArray, 0, copyOfDestination, 0, copyOfDestination.LongLength);
                Array.Copy(target.GetSegments()[0].Array, target.GetSegments()[0].Offset, copyOfSource, 0, copyOfSource.LongLength);
                Assert.IsTrue(ArraysMatch(copyOfSource, copyOfDestination));

                //Destination Array is larger than ManagedBuffer Size
                target.FillWith(GetRandomizedByteArray(blockSize));
                DestArray = new byte[blockSize + 100];
                target.CopyTo(DestArray);
                copyOfSource      = new byte[blockSize];
                copyOfDestination = new byte[blockSize];
                Array.Copy(DestArray, 0, copyOfDestination, 0, copyOfDestination.LongLength);
                Array.Copy(target.GetSegments()[0].Array, target.GetSegments()[0].Offset, copyOfSource, 0, copyOfSource.LongLength);
                Assert.IsTrue(ArraysMatch(copyOfSource, copyOfDestination));
            }

            //Repeat test with a new buffer to confirm that offsets within slab arrays are accurately tracked
            //and to make sure there is no off by 1 error

            {
                ManagedBuffer target = GetNewBuffer(slab);
                target2 = target;
                target.FillWith(GetRandomizedByteArray(blockSize));
                byte[] DestArray = new byte[blockSize];
                target.CopyTo(DestArray);
                byte[] copyOfSource      = new byte[blockSize];
                byte[] copyOfDestination = new byte[blockSize];
                Array.Copy(DestArray, 0, copyOfDestination, 0, copyOfDestination.LongLength);
                Array.Copy(target.GetSegments()[0].Array, target.GetSegments()[0].Offset, copyOfSource, 0, copyOfSource.LongLength);
                Assert.IsTrue(ArraysMatch(copyOfSource, copyOfDestination));

                //Destination Array is larger than ManagedBuffer Size
                target.FillWith(GetRandomizedByteArray(blockSize));
                DestArray = new byte[blockSize + 1];
                target.CopyTo(DestArray);
                copyOfSource      = new byte[blockSize];
                copyOfDestination = new byte[blockSize];
                Array.Copy(DestArray, 0, copyOfDestination, 0, copyOfDestination.LongLength);
                Array.Copy(target.GetSegments()[0].Array, target.GetSegments()[0].Offset, copyOfSource, 0, copyOfSource.LongLength);
                Assert.IsTrue(ArraysMatch(copyOfSource, copyOfDestination));
            }

            target2.Dispose();
            target1.Dispose();
        }
示例#4
0
        public void DisposeTest()
        {
            MemorySlab slab = new MemorySlab(blockSize * 3, null);

            ManagedBuffer target = GetNewBuffer(slab);

            target.Dispose();

            //Call Dispose again, shouldn't cause any exceptions to be thrown
            target.Dispose();

            //Try to work with disposed object. should throw exceptions
            {
                bool exceptionThrown = false;

                try
                {
                    target.FillWith(new byte[] { 0, 1, 2 });
                }
                catch (ObjectDisposedException)
                {
                    exceptionThrown = true;
                }

                Assert.IsTrue(exceptionThrown);
            }

            {
                bool exceptionThrown = false;

                try
                {
                    target.CopyTo(new byte[] { 0, 1, 2 });
                }
                catch (ObjectDisposedException)
                {
                    exceptionThrown = true;
                }

                Assert.IsTrue(exceptionThrown);
            }

            {
                bool exceptionThrown = false;

                try
                {
                    target.GetSegments();
                }
                catch (ObjectDisposedException)
                {
                    exceptionThrown = true;
                }

                Assert.IsTrue(exceptionThrown);
            }
        }
示例#5
0
        public void GetSegmentsTest7()
        {
            MemorySlab          slab   = new MemorySlab(blockSize * 3, null);
            ManagedBuffer       target = GetNewBuffer(slab);
            int                 Length = -1;
            ArraySegment <byte> actual;

            actual = target.GetSegments(Length)[0];
        }
示例#6
0
        public void LengthTest()
        {
            MemorySlab slab = new MemorySlab(blockSize * 3, null);

            ManagedBuffer target = GetNewBuffer(slab);

            Assert.AreEqual <long>(blockSize, target.Size);
            target.Dispose();
        }
示例#7
0
        //Gets a single slab buffer
        private static ManagedBuffer GetNewBuffer(IMemorySlab Slab)
        {
            IMemoryBlock allocatedMemoryBlock;

            Slab.TryAllocate(blockSize, out allocatedMemoryBlock);

            ManagedBuffer target = new ManagedBuffer(new IMemoryBlock[] { allocatedMemoryBlock });

            return(target);
        }
示例#8
0
        public void IsDisposedTest()
        {
            MemorySlab slab = new MemorySlab(blockSize * 3, null);

            ManagedBuffer target = GetNewBuffer(slab);

            Assert.IsFalse(target.IsDisposed);
            target.Dispose();
            Assert.IsTrue(target.IsDisposed);
        }
示例#9
0
        public void Validate(bool write, int dataSize)
        {
            bool valid = false;

            if (write)
            {
                if (this.Size < dataSize && this.autoGrow)
                {
                    if (this.references != 1)
                    {
                        throw new InvalidOperationException("Cannot grow the current buffer because it has more than one references");
                    }

                    int           newSize = Math.Max(this.Capacity * 2, this.Capacity + dataSize);
                    ManagedBuffer newBuffer;
                    if (this.bufferManager != null)
                    {
                        newBuffer = ByteBuffer.AllocateBuffer(newSize, this.bufferManager);
                    }
                    else
                    {
                        newBuffer = new ManagedBuffer(new byte[newSize], null);
                    }

                    System.Buffer.BlockCopy(this.buffer, this.start, newBuffer.Buffer, 0, this.Capacity);

                    int consumed = this.read - this.start;
                    int written  = this.write - this.start;

                    this.start = 0;
                    this.read  = consumed;
                    this.write = written;
                    this.end   = newSize;

                    if (this.bufferManager != null)
                    {
                        this.bufferManager.ReturnBuffer(this.buffer);
                    }
                    this.buffer        = newBuffer.Buffer;
                    this.bufferManager = newBuffer.BufferManager;
                }

                valid = this.Size >= dataSize;
            }
            else
            {
                valid = this.Length >= dataSize;
            }

            if (!valid)
            {
                throw new AmqpException(AmqpErrorCode.DecodeError, AmqpResources.GetString(AmqpResources.AmqpInsufficientBufferSize, dataSize, write ? this.Size : this.Length));
            }
        }
示例#10
0
        public void GetArraySegmentTest8()
        {
            MemorySlab          slab   = new MemorySlab(blockSize * 3, null);
            ManagedBuffer       target = GetNewBuffer(slab);
            ArraySegment <byte> actual;

            actual = target.GetSegments()[0];
            Assert.AreEqual <long>(actual.Offset, target.memoryBlock.StartLocation);
            Assert.AreEqual <long>(actual.Count, Math.Min(target.Size, int.MaxValue));
            Assert.AreEqual <byte[]>(actual.Array, target.memoryBlock.Slab.Array);
        }
示例#11
0
        public void CopyToTest2()
        {
            MemorySlab slab = new MemorySlab(blockSize * 3, null);

            ManagedBuffer target = GetNewBuffer(slab);

            //Destination array is smaller than buffer size

            byte[] DestArray = new byte[blockSize - 1];
            target.CopyTo(DestArray);
        }
示例#12
0
        public void GetArraySegmentTest6()
        {
            MemorySlab          slab   = new MemorySlab(blockSize * 3, null);
            ManagedBuffer       target = GetNewBuffer(slab);
            int                 Length = 0;
            ArraySegment <byte> actual;

            actual = target.GetSegments(Length)[0];
            Assert.AreEqual <long>(actual.Offset, 0 + target.memoryBlock.StartLocation);
            Assert.AreEqual <long>(actual.Count, Length);
            Assert.AreEqual <byte[]>(actual.Array, target.memoryBlock.Slab.Array);
        }
示例#13
0
        public void CopyFromTest2()
        {
            MemorySlab slab = new MemorySlab(blockSize * 3, null);

            ManagedBuffer target = GetNewBuffer(slab);
            //Source Array is larger than buffer size
            long blockSizeMore = blockSize + 1;

            byte[] SourceArray = GetRandomizedByteArray(blockSizeMore);

            target.FillWith(SourceArray);
        }
示例#14
0
        public void FillWithTest3()
        {
            MemorySlab    slab   = new MemorySlab(blockSize * 3, null);
            ManagedBuffer target = GetNewBuffer(slab);

            byte[] SourceArray = GetRandomizedByteArray(blockSize);
            target.FillWith(SourceArray, 1, blockSize - 2);
            byte[] copyOfDestination = new byte[blockSize - 2];
            byte[] copyOfSource      = new byte[blockSize - 2];
            Array.Copy(target.GetSegments()[0].Array, target.GetSegments()[0].Offset, copyOfDestination, 0, copyOfDestination.LongLength);
            Array.Copy(SourceArray, 1, copyOfSource, 0, copyOfSource.LongLength);
            Assert.IsTrue(ArraysMatch(copyOfSource, copyOfDestination));
        }
示例#15
0
        public void TestDispose()
        {
            const int maxBufferPoolSize = 2 << 32;
            const int maxBufferSize     = 2 << 16;
            var       bufferManager     = BufferManager.CreateBufferManager(maxBufferPoolSize, maxBufferSize);

            var len = 13;
            var buf = bufferManager.TakeBuffer(len);

            var managedBuffer = new ManagedBuffer(buf, len, bufferManager);

            managedBuffer.Dispose();
            managedBuffer.Dispose();
        }
示例#16
0
        //Gets a multi slab buffer
        private static ManagedBuffer GetNewBuffer(IMemorySlab[] Slabs)
        {
            List <IMemoryBlock> blockList = new List <IMemoryBlock>();
            IMemoryBlock        allocatedMemoryBlock;

            foreach (var slab in Slabs)
            {
                slab.TryAllocate(blockSize, out allocatedMemoryBlock);
                blockList.Add(allocatedMemoryBlock);
            }

            ManagedBuffer target = new ManagedBuffer(blockList);

            return(target);
        }
示例#17
0
        public void FillWithTest()
        {
            MemorySlab    slab = new MemorySlab(blockSize * 3, null);
            ManagedBuffer target1, target2;

            {
                ManagedBuffer target = GetNewBuffer(slab);
                target1 = target;
                byte[] SourceArray = GetRandomizedByteArray(blockSize);
                target.FillWith(SourceArray);
                byte[] copyOfDestination = new byte[blockSize];
                Array.Copy(target.GetSegments()[0].Array, target.GetSegments()[0].Offset, copyOfDestination, 0, copyOfDestination.LongLength);
                Assert.IsTrue(ArraysMatch(SourceArray, copyOfDestination));

                //Source Array is smaller than ManagedBuffer Size
                long blockSizeLess = blockSize - 100;
                SourceArray = GetRandomizedByteArray(blockSizeLess);
                target.FillWith(SourceArray);
                copyOfDestination = new byte[blockSizeLess];
                Array.Copy(target.GetSegments()[0].Array, target.GetSegments()[0].Offset, copyOfDestination, 0, copyOfDestination.LongLength);
                Assert.IsTrue(ArraysMatch(SourceArray, copyOfDestination));
            }

            //Repeat test with a new buffer to confirm that offsets within slab arrays are accurately tracked
            //and to make sure there is no off by 1 error

            {
                ManagedBuffer target = GetNewBuffer(slab);
                target2 = target;
                byte[] SourceArray = GetRandomizedByteArray(blockSize);
                target.FillWith(SourceArray);
                byte[] copyOfDestination = new byte[blockSize];
                Array.Copy(target.GetSegments()[0].Array, target.GetSegments()[0].Offset, copyOfDestination, 0, copyOfDestination.LongLength);
                Assert.IsTrue(ArraysMatch(SourceArray, copyOfDestination));

                //Source Array is smaller than ManagedBuffer Size
                long blockSizeLess = blockSize - 1;
                SourceArray = GetRandomizedByteArray(blockSizeLess);
                target.FillWith(SourceArray);
                copyOfDestination = new byte[blockSizeLess];
                Array.Copy(target.GetSegments()[0].Array, target.GetSegments()[0].Offset, copyOfDestination, 0, copyOfDestination.LongLength);
                Assert.IsTrue(ArraysMatch(SourceArray, copyOfDestination));
            }

            target2.Dispose();
            target1.Dispose();
        }
示例#18
0
        public void TestFinalise()
        {
            const int maxBufferPoolSize = 2 << 32;
            const int maxBufferSize     = 2 << 16;
            var       bufferManager     = BufferManager.CreateBufferManager(maxBufferPoolSize, maxBufferSize);

            const int len = 13;
            var       buf = bufferManager.TakeBuffer(len);

            var managedBuffer = new ManagedBuffer(buf, len, bufferManager);

            managedBuffer.Dispose();
            managedBuffer = null;

            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
示例#19
0
        public void GetSegmentsTest2()
        {
            //Single Slab test
            {
                MemorySlab    slab   = new MemorySlab(blockSize * 3, null);
                ManagedBuffer target = GetNewBuffer(slab);
                int           Offset = 0;
                int           Length = 0;
                var           actual = target.GetSegments(Offset, Length);
                Assert.AreEqual <int>(1, actual.Count);
                Assert.AreEqual <long>(Offset + target.MemoryBlocks[0].StartLocation, actual[0].Offset);
                Assert.AreEqual <long>(Length, actual[0].Count);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[0].Slab.Array, actual[0].Array);

                //Test again at a different offset
                Offset = 10;
                Length = 0;
                actual = target.GetSegments(Offset, Length);
                Assert.AreEqual <int>(1, actual.Count);
                Assert.AreEqual <long>(Offset + target.MemoryBlocks[0].StartLocation, actual[0].Offset);
                Assert.AreEqual <long>(Length, actual[0].Count);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[0].Slab.Array, actual[0].Array);
            }

            //Multi slab test:
            {
                MemorySlab[]  slabs  = { new MemorySlab(blockSize * 2, null), new MemorySlab(blockSize * 2, null), new MemorySlab(blockSize * 2, null) };
                ManagedBuffer target = GetNewBuffer(slabs);
                int           Offset = 0;
                int           Length = 0;
                var           actual = target.GetSegments(Offset, Length);
                Assert.AreEqual(1, actual.Count);
                Assert.AreEqual <long>(Offset + target.MemoryBlocks[0].StartLocation, actual[0].Offset);
                Assert.AreEqual <long>(Length, actual[0].Count);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[0].Slab.Array, actual[0].Array);

                //Test again at a different offset
                Offset = 10;
                Length = 0;
                actual = target.GetSegments(Offset, Length);
                Assert.AreEqual <int>(1, actual.Count);
                Assert.AreEqual <long>(Offset + target.MemoryBlocks[0].StartLocation, actual[0].Offset);
                Assert.AreEqual <long>(Length, actual[0].Count);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[0].Slab.Array, actual[0].Array);
            }
        }
示例#20
0
        public void NewBufferIsFreshTest()
        {
            MemorySlab slab = new MemorySlab(blockSize * 3, null);

            //Save some random junk into a buffer and dispose it.
            {
                ManagedBuffer target1     = GetNewBuffer(slab);
                byte[]        SourceArray = GetRandomizedByteArray(blockSize);
                target1.FillWith(SourceArray);
                target1.Dispose();
            }

            //Get a fresh buffer and confirm that it's zero-filled.
            {
                ManagedBuffer target2   = GetNewBuffer(slab);
                byte[]        DestArray = new byte[blockSize];
                target2.CopyTo(DestArray);
                Assert.IsTrue(ArraysMatch(DestArray, new byte[blockSize]));
                target2.Dispose();
            }
        }
示例#21
0
 public void TestBuffer()
 {
     var buffer = default(ManagedBuffer);
      // invalid constructor
      try
      {
     new ManagedBuffer(null, 0);
     Assert.Fail("Expected: Exception");
      }
      catch (AssertFailedException) { throw; }
      catch { }
      try
      {
     new ManagedBuffer(null, 8192);
     Assert.Fail("Expected: Exception");
      }
      catch (AssertFailedException) { throw; }
      catch { }
      try
      {
     new ManagedBuffer(manager, 0);
     Assert.Fail("Expected: Exception");
      }
      catch (AssertFailedException) { throw; }
      catch { }
      try
      {
     new ManagedBuffer(manager, -1);
     Assert.Fail("Expected: Exception");
      }
      catch (AssertFailedException) { throw; }
      catch { }
      try
      {
     new ManagedBuffer(null, default(ArraySegment<Byte>));
     Assert.Fail("Expected: Exception");
      }
      catch (AssertFailedException) { throw; }
      catch { }
      try
      {
     new ManagedBuffer(null, new ArraySegment<Byte>(new Byte[8192]));
     Assert.Fail("Expected: Exception");
      }
      catch (AssertFailedException) { throw; }
      catch { }
      try
      {
     new ManagedBuffer(manager, default(ArraySegment<Byte>));
     Assert.Fail("Expected: Exception");
      }
      catch (AssertFailedException) { throw; }
      catch { }
      // size constructor
      buffer = new ManagedBuffer(manager, 8192);
      Assert.IsNotNull(buffer.Raw);
      Assert.IsNotNull(buffer.Data.Array);
      Assert.AreEqual(buffer.Offset, 0);
      Assert.AreEqual(buffer.Data.Offset, 0);
      Assert.AreEqual(buffer.Length, 8192);
      Assert.AreEqual(buffer.Data.Count, 8192);
      buffer.Dispose();
      Assert.IsNull(buffer.Raw);
      Assert.IsNull(buffer.Data.Array);
      Assert.AreEqual(buffer.Offset, 0);
      Assert.AreEqual(buffer.Data.Offset, 0);
      Assert.AreEqual(buffer.Length, 0);
      Assert.AreEqual(buffer.Data.Count, 0);
      // buffer constructor
      buffer = new ManagedBuffer(manager, new ArraySegment<Byte>(manager.TakeBuffer(8192)));
      Assert.IsNotNull(buffer.Raw);
      Assert.IsNotNull(buffer.Data.Array);
      Assert.AreEqual(buffer.Offset, 0);
      Assert.AreEqual(buffer.Data.Offset, 0);
      Assert.AreEqual(buffer.Length, 8192);
      Assert.AreEqual(buffer.Data.Count, 8192);
      buffer.Dispose();
      Assert.IsNull(buffer.Raw);
      Assert.IsNull(buffer.Data.Array);
      Assert.AreEqual(buffer.Offset, 0);
      Assert.AreEqual(buffer.Data.Offset, 0);
      Assert.AreEqual(buffer.Length, 0);
      Assert.AreEqual(buffer.Data.Count, 0);
      // multiple disposal
      buffer.Dispose();
      buffer.Dispose();
 }
示例#22
0
 public ManagedBufferTextureLightMap(ManagedBuffer buffer)
     : base(buffer)
 {
 }
示例#23
0
 public ManagedBufferCol3b(ManagedBuffer buffer)
     : base(buffer)
 {
 }
示例#24
0
        public void Validate(bool write, int dataSize)
        {
            bool valid = false;
            if (write)
            {
                if (this.Size < dataSize && this.autoGrow)
                {
                    if (this.references != 1)
                    {
                        throw new InvalidOperationException("Cannot grow the current buffer because it has more than one references");
                    }

                    int newSize = Math.Max(this.Capacity * 2, this.Capacity + dataSize);
                    ManagedBuffer newBuffer;
                    if (this.bufferManager != null)
                    {
                        newBuffer = ByteBuffer.AllocateBuffer(newSize, this.bufferManager);
                    }
                    else
                    {
                        newBuffer = new ManagedBuffer(new byte[newSize], null);
                    }
                    
                    System.Buffer.BlockCopy(this.buffer, this.start, newBuffer.Buffer, 0, this.Capacity);

                    int consumed = this.read - this.start;
                    int written = this.write - this.start;

                    this.start = 0;
                    this.read = consumed;
                    this.write = written;
                    this.end = newSize;

                    if (this.bufferManager != null)
                    {
                        this.bufferManager.ReturnBuffer(this.buffer);
                    }
                    this.buffer = newBuffer.Buffer;
                    this.bufferManager = newBuffer.BufferManager;
                }

                valid = this.Size >= dataSize;
            }
            else
            {
                valid = this.Length >= dataSize;
            }

            if (!valid)
            {
                throw new AmqpException(AmqpErrorCode.DecodeError, AmqpResources.GetString(AmqpResources.AmqpInsufficientBufferSize, dataSize, write ? this.Size : this.Length));
            }
        }
示例#25
0
 ByteBuffer(ManagedBuffer bufferReference, bool autoGrow, int size)
     : this(bufferReference.Buffer, 0, 0, size, autoGrow, bufferReference.BufferManager)
 {
 }
示例#26
0
        public void GetSegmentsTest5()
        {
            //Single slab test:
            {
                MemorySlab    slab   = new MemorySlab(blockSize * 3, null);
                ManagedBuffer target = GetNewBuffer(slab);
                int           Length = (int)(blockSize - 1);
                var           actual = target.GetSegments(Length);
                Assert.AreEqual(1, actual.Count);
                Assert.AreEqual <long>(target.MemoryBlocks[0].StartLocation, actual[0].Offset);
                Assert.AreEqual <long>(Length, actual[0].Count);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[0].Slab.Array, actual[0].Array);

                //Test again for full blocksize
                Length = (int)(blockSize);
                actual = target.GetSegments(Length);
                Assert.AreEqual(1, actual.Count);
                Assert.AreEqual <long>(actual[0].Offset, target.MemoryBlocks[0].StartLocation);
                Assert.AreEqual <long>(actual[0].Count, Length);
                Assert.AreEqual <byte[]>(actual[0].Array, target.MemoryBlocks[0].Slab.Array);
            }

            //Multi slab test:
            {
                MemorySlab[]  slabs  = { new MemorySlab(blockSize * 2, null), new MemorySlab(blockSize * 2, null), new MemorySlab(blockSize * 2, null) };
                ManagedBuffer target = GetNewBuffer(slabs);
                int           Length = (int)(blockSize - 1);
                var           actual = target.GetSegments(Length);
                Assert.AreEqual(1, actual.Count);
                Assert.AreEqual <long>(target.MemoryBlocks[0].StartLocation, actual[0].Offset);
                Assert.AreEqual <long>(Length, actual[0].Count);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[0].Slab.Array, actual[0].Array);

                //Test for full blocksize
                Length = (int)blockSize;
                actual = target.GetSegments(Length);
                Assert.AreEqual(1, actual.Count);
                Assert.AreEqual <long>(target.MemoryBlocks[0].StartLocation, actual[0].Offset);
                Assert.AreEqual <long>(Length, actual[0].Count);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[0].Slab.Array, actual[0].Array);

                //Test that blocksize plus 1 flows into another segment
                Length = (int)(blockSize + 1);
                actual = target.GetSegments(Length);
                Assert.AreEqual(2, actual.Count);
                Assert.AreEqual <long>(target.MemoryBlocks[0].StartLocation, actual[0].Offset);
                Assert.AreEqual <long>(target.MemoryBlocks[1].StartLocation, actual[1].Offset);
                Assert.AreEqual(1, actual[1].Count);
                Assert.AreEqual <long>(Length, actual[0].Count + actual[1].Count);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[0].Slab.Array, actual[0].Array);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[1].Slab.Array, actual[1].Array);

                //Test that (blocksize * 2) - 1 stays in two segments
                Length = (int)(blockSize * 2 - 1);
                actual = target.GetSegments(Length);
                Assert.AreEqual(2, actual.Count);
                Assert.AreEqual <long>(target.MemoryBlocks[0].StartLocation, actual[0].Offset);
                Assert.AreEqual <long>(target.MemoryBlocks[1].StartLocation, actual[1].Offset);
                Assert.AreEqual(blockSize - 1, actual[1].Count);
                Assert.AreEqual <long>(Length, actual[0].Count + actual[1].Count);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[0].Slab.Array, actual[0].Array);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[1].Slab.Array, actual[1].Array);

                //Test that (blocksize * 2) stays in two segments
                Length = (int)(blockSize * 2);
                actual = target.GetSegments(Length);
                Assert.AreEqual(2, actual.Count);
                Assert.AreEqual <long>(target.MemoryBlocks[0].StartLocation, actual[0].Offset);
                Assert.AreEqual <long>(target.MemoryBlocks[1].StartLocation, actual[1].Offset);
                Assert.AreEqual(blockSize, actual[1].Count);
                Assert.AreEqual <long>(Length, actual[0].Count + actual[1].Count);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[0].Slab.Array, actual[0].Array);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[1].Slab.Array, actual[1].Array);

                //Test that (blocksize * 2) + 1 flows into the third segment
                Length = (int)(blockSize * 2 + 1);
                actual = target.GetSegments(Length);
                Assert.AreEqual(3, actual.Count);
                Assert.AreEqual <long>(target.MemoryBlocks[0].StartLocation, actual[0].Offset);
                Assert.AreEqual <long>(target.MemoryBlocks[1].StartLocation, actual[1].Offset);
                Assert.AreEqual <long>(target.MemoryBlocks[2].StartLocation, actual[2].Offset);
                Assert.AreEqual(1, actual[2].Count);
                Assert.AreEqual <long>(Length, actual[0].Count + actual[1].Count + actual[2].Count);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[0].Slab.Array, actual[0].Array);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[1].Slab.Array, actual[1].Array);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[2].Slab.Array, actual[2].Array);
            }
        }
示例#27
0
        public void GetSegmentsTest()
        {
            //Single Slab tests:

            {
                MemorySlab          slab   = new MemorySlab(blockSize * 3, null);
                ManagedBuffer       target = GetNewBuffer(slab);
                int                 Offset = 0;
                int                 Length = (int)(blockSize - 1);
                ArraySegment <byte> actual;
                actual = target.GetSegments(Offset, Length)[0];
                Assert.AreEqual <long>(actual.Offset, Offset + target.MemoryBlocks[0].StartLocation);
                Assert.AreEqual <long>(actual.Count, Length);
                Assert.AreEqual <byte[]>(actual.Array, target.MemoryBlocks[0].Slab.Array);

                //Test for full blocksize
                Offset = 0;
                Length = (int)blockSize;
                actual = target.GetSegments(Offset, Length)[0];
                Assert.AreEqual <long>(actual.Offset, Offset + target.MemoryBlocks[0].StartLocation);
                Assert.AreEqual <long>(actual.Count, Length);
                Assert.AreEqual <byte[]>(actual.Array, target.MemoryBlocks[0].Slab.Array);


                //Test for offset of 1
                Offset = 1;
                Length = (int)(blockSize - 1);
                actual = target.GetSegments(Offset, Length)[0];
                Assert.AreEqual <long>(actual.Offset, Offset + target.MemoryBlocks[0].StartLocation);
                Assert.AreEqual <long>(actual.Count, Length);
                Assert.AreEqual <byte[]>(actual.Array, target.MemoryBlocks[0].Slab.Array);
            }

            //Multi-Slab tests:

            {
                MemorySlab[]  slabs  = { new MemorySlab(blockSize * 2, null), new MemorySlab(blockSize * 2, null), new MemorySlab(blockSize * 2, null) };
                ManagedBuffer target = GetNewBuffer(slabs);
                int           Offset = 0;
                int           Length = (int)(blockSize - 1);
                var           actual = target.GetSegments(Offset, Length);
                Assert.AreEqual(1, actual.Count);
                Assert.AreEqual <long>(Offset + target.MemoryBlocks[0].StartLocation, actual[0].Offset);
                Assert.AreEqual <long>(Length, actual[0].Count);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[0].Slab.Array, actual[0].Array);

                //Test for full blocksize
                Offset = 0;
                Length = (int)blockSize;
                actual = target.GetSegments(Offset, Length);
                Assert.AreEqual(1, actual.Count);
                Assert.AreEqual <long>(Offset + target.MemoryBlocks[0].StartLocation, actual[0].Offset);
                Assert.AreEqual <long>(Length, actual[0].Count);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[0].Slab.Array, actual[0].Array);


                //Test for offset of 1
                Offset = 1;
                Length = (int)(blockSize - 1);
                actual = target.GetSegments(Offset, Length);
                Assert.AreEqual(1, actual.Count);
                Assert.AreEqual <long>(Offset + target.MemoryBlocks[0].StartLocation, actual[0].Offset);
                Assert.AreEqual <long>(Length, actual[0].Count);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[0].Slab.Array, actual[0].Array);

                //Test that offset of 1 plus blocksize flows into another segment
                Offset = 1;
                Length = (int)(blockSize);
                actual = target.GetSegments(Offset, Length);
                Assert.AreEqual(2, actual.Count);
                Assert.AreEqual <long>(Offset + target.MemoryBlocks[0].StartLocation, actual[0].Offset);
                Assert.AreEqual <long>(target.MemoryBlocks[1].StartLocation, actual[1].Offset);
                Assert.AreEqual(1, actual[1].Count);
                Assert.AreEqual <long>(Length, actual[0].Count + actual[1].Count);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[0].Slab.Array, actual[0].Array);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[1].Slab.Array, actual[1].Array);

                //Test that offset of 1 plus (blocksize * 2) - 2 stays in two segments
                Offset = 1;
                Length = (int)(blockSize * 2 - 2);
                actual = target.GetSegments(Offset, Length);
                Assert.AreEqual(2, actual.Count);
                Assert.AreEqual <long>(Offset + target.MemoryBlocks[0].StartLocation, actual[0].Offset);
                Assert.AreEqual <long>(target.MemoryBlocks[1].StartLocation, actual[1].Offset);
                Assert.AreEqual(blockSize - 1, actual[1].Count);
                Assert.AreEqual <long>(Length, actual[0].Count + actual[1].Count);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[0].Slab.Array, actual[0].Array);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[1].Slab.Array, actual[1].Array);

                //Test that offset of 1 plus (blocksize * 2) - 1 stays in two segments
                Offset = 1;
                Length = (int)(blockSize * 2 - 1);
                actual = target.GetSegments(Offset, Length);
                Assert.AreEqual(2, actual.Count);
                Assert.AreEqual <long>(Offset + target.MemoryBlocks[0].StartLocation, actual[0].Offset);
                Assert.AreEqual <long>(target.MemoryBlocks[1].StartLocation, actual[1].Offset);
                Assert.AreEqual(blockSize, actual[1].Count);
                Assert.AreEqual <long>(Length, actual[0].Count + actual[1].Count);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[0].Slab.Array, actual[0].Array);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[1].Slab.Array, actual[1].Array);

                //Test that offset of 0 plus (blocksize * 2) stays in two segments
                Offset = 0;
                Length = (int)(blockSize * 2);
                actual = target.GetSegments(Offset, Length);
                Assert.AreEqual(2, actual.Count);
                Assert.AreEqual <long>(Offset + target.MemoryBlocks[0].StartLocation, actual[0].Offset);
                Assert.AreEqual <long>(target.MemoryBlocks[1].StartLocation, actual[1].Offset);
                Assert.AreEqual(blockSize, actual[1].Count);
                Assert.AreEqual <long>(Length, actual[0].Count + actual[1].Count);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[0].Slab.Array, actual[0].Array);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[1].Slab.Array, actual[1].Array);


                //Test that offset of 1 plus (blocksize * 2) flows into the third segment
                Offset = 1;
                Length = (int)(blockSize * 2);
                actual = target.GetSegments(Offset, Length);
                Assert.AreEqual(3, actual.Count);
                Assert.AreEqual <long>(Offset + target.MemoryBlocks[0].StartLocation, actual[0].Offset);
                Assert.AreEqual <long>(target.MemoryBlocks[1].StartLocation, actual[1].Offset);
                Assert.AreEqual <long>(target.MemoryBlocks[2].StartLocation, actual[2].Offset);
                Assert.AreEqual(1, actual[2].Count);
                Assert.AreEqual <long>(Length, actual[0].Count + actual[1].Count + actual[2].Count);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[0].Slab.Array, actual[0].Array);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[1].Slab.Array, actual[1].Array);
                Assert.AreEqual <byte[]>(target.MemoryBlocks[2].Slab.Array, actual[2].Array);
            }
        }
示例#28
0
 public void BufferConstructorTest()
 {
     IMemoryBlock  nullSlab = null;
     ManagedBuffer target   = new ManagedBuffer(nullSlab);
 }
示例#29
0
 public void BufferConstructorTest()
 {
     IMemoryBlock[] nullBlocks = null;
     ManagedBuffer  target     = new ManagedBuffer(nullBlocks);
 }
示例#30
0
 public void BufferConstructorTest2()
 {
     IMemoryBlock[] emptyBlocks = new IMemoryBlock[0];
     ManagedBuffer  target      = new ManagedBuffer(emptyBlocks);
 }
示例#31
0
 public EntityIterator(ManagedBuffer <T> array, uint count) : this()
 {
     _array = array.ToManagedArray();
     _count = count;
     _index = -1;
 }
示例#32
0
 ByteBuffer(ManagedBuffer bufferReference, bool autoGrow, int size)
     : this(bufferReference.Buffer, 0, 0, size, autoGrow, bufferReference.BufferManager)
 {
 }
示例#33
0
 public EntityCollection(ManagedBuffer <T> buffer, uint count)
 {
     _buffer = buffer;
     _count  = count;
 }