예제 #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 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);
            }
        }
예제 #3
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);
        }
예제 #4
0
        public void CopyToTest3()
        {
            MemorySlab    slab   = new MemorySlab(blockSize * 3, null);
            ManagedBuffer target = GetNewBuffer(slab);

            target.FillWith(GetRandomizedByteArray(blockSize));
            byte[] DestArray = new byte[blockSize];
            target.CopyTo(DestArray, 1, blockSize - 2);
            byte[] copyOfSource      = new byte[blockSize - 2];
            byte[] copyOfDestination = new byte[blockSize - 2];
            Array.Copy(DestArray, 1, copyOfDestination, 0, copyOfDestination.LongLength);
            Array.Copy(target.GetSegments()[0].Array, target.GetSegments()[0].Offset, copyOfSource, 0, copyOfSource.LongLength);
            Assert.IsTrue(ArraysMatch(copyOfSource, copyOfDestination));
        }
예제 #5
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();
            }
        }