예제 #1
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();
        }
예제 #2
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();
        }
예제 #3
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);
            }
        }
예제 #4
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);
        }
예제 #5
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));
        }
예제 #6
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();
            }
        }