Пример #1
0
        public void CopyToTest()
        {
            MemorySlab    slab = new MemorySlab(blockSize * 3, null);
            ManagedBuffer target1, target2;

            {
                ManagedBuffer target = GetNewBuffer(slab);
                target1 = target;
                target.CopyFrom(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.CopyFrom(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.CopyFrom(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.CopyFrom(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 CopyFromTest()
        {
            MemorySlab    slab = new MemorySlab(blockSize * 3, null);
            ManagedBuffer target1, target2;

            {
                ManagedBuffer target = GetNewBuffer(slab);
                target1 = target;
                byte[] SourceArray = GetRandomizedByteArray(blockSize);
                target.CopyFrom(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.CopyFrom(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.CopyFrom(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.CopyFrom(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.CopyFrom(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.CopyFrom(SourceArray);
        }
Пример #5
0
        public void CopyFromTest3()
        {
            MemorySlab    slab   = new MemorySlab(blockSize * 3, null);
            ManagedBuffer target = GetNewBuffer(slab);

            byte[] SourceArray = GetRandomizedByteArray(blockSize);
            target.CopyFrom(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));
        }