示例#1
0
文件: UmaTests.cs 项目: jnm2/corefx
        public static void UmaInvalidReadWrite()
        {
            const int      capacity = 99;
            FakeSafeBuffer sbuf     = new FakeSafeBuffer((ulong)capacity);

            using (var uma = new UnmanagedMemoryAccessor(sbuf, 0, capacity, FileAccess.ReadWrite))
            {
                Assert.Throws <ArgumentOutOfRangeException>(() => uma.ReadChar(-1));
                Assert.Throws <ArgumentOutOfRangeException>(() => uma.ReadDecimal(capacity));
                Assert.Throws <ArgumentException>(() => uma.ReadSingle(capacity - 1));

                Assert.Throws <ArgumentOutOfRangeException>(() => uma.Write(-1, true));
                Assert.Throws <ArgumentOutOfRangeException>(() => uma.Write(capacity, 12345));
                Assert.Throws <ArgumentException>(() => uma.Write(capacity - 1, 0.123));

                uma.Dispose();
                Assert.Throws <ObjectDisposedException>(() => uma.ReadByte(0));
                Assert.Throws <ObjectDisposedException>(() => uma.Write(0, (byte)123));
            }

            using (var uma = new UnmanagedMemoryAccessor(sbuf, 0, capacity, FileAccess.Write))
            {
                Assert.Throws <NotSupportedException>(() => uma.ReadInt16(0));
            }

            using (var uma = new UnmanagedMemoryAccessor(sbuf, 0, capacity, FileAccess.Read))
            {
                Assert.Throws <NotSupportedException>(() => uma.Write(0, (int)123));
            }
        }
示例#2
0
        public static void UmaInvalidReadWrite()
        {
            const int capacity = 99;
            FakeSafeBuffer sbuf = new FakeSafeBuffer((ulong)capacity);

            using (var uma = new UnmanagedMemoryAccessor(sbuf, 0, capacity, FileAccess.ReadWrite))
            {
                Assert.Throws<ArgumentOutOfRangeException>(() => uma.ReadChar(-1));
                Assert.Throws<ArgumentOutOfRangeException>(() => uma.ReadDecimal(capacity));
                Assert.Throws<ArgumentException>(() => uma.ReadSingle(capacity - 1));

                Assert.Throws<ArgumentOutOfRangeException>(() => uma.Write(-1, true));
                Assert.Throws<ArgumentOutOfRangeException>(() => uma.Write(capacity, 12345));
                Assert.Throws<ArgumentException>(() => uma.Write(capacity - 1, 0.123));

                uma.Dispose();
                Assert.Throws<ObjectDisposedException>(() => uma.ReadByte(0));
                Assert.Throws<ObjectDisposedException>(() => uma.Write(0, (byte)123));
            }

            using (var uma = new UnmanagedMemoryAccessor(sbuf, 0, capacity, FileAccess.Write))
            {
                Assert.Throws<NotSupportedException>(() => uma.ReadInt16(0));
            }

            using (var uma = new UnmanagedMemoryAccessor(sbuf, 0, capacity, FileAccess.Read))
            {
                Assert.Throws<NotSupportedException>(() => uma.Write(0, (int)123));
            }
        }
示例#3
0
 public static void UmaCtors()
 {
     using (FakeSafeBuffer fakeBuffer = new FakeSafeBuffer(99))
         using (var uma = new UnmanagedMemoryAccessor(fakeBuffer, 0, 0))
         {
             Assert.True(uma.CanRead);
             Assert.False(uma.CanWrite);
             Assert.Equal(0, uma.Capacity);
         }
 }
示例#4
0
 public static void UmaCtors()
 {
     using (FakeSafeBuffer fakeBuffer = new FakeSafeBuffer(99))
     using (var uma = new UnmanagedMemoryAccessor(fakeBuffer, 0, 0))
     {
         Assert.True(uma.CanRead);
         Assert.False(uma.CanWrite);
         Assert.Equal(0, uma.Capacity);
     }
 }
示例#5
0
        public static void UmaCtorsThatFail()
        {
            FakeSafeBuffer fakeBuffer = new FakeSafeBuffer(99);
            FakeSafeBuffer nullBuffer = null;

            Assert.Throws <ArgumentNullException>(() => new UnmanagedMemoryAccessor(nullBuffer, 0, 0));

            Assert.Throws <ArgumentOutOfRangeException>(() => new UnmanagedMemoryAccessor(fakeBuffer, 2, -1));
            Assert.Throws <ArgumentOutOfRangeException>(() => new UnmanagedMemoryAccessor(fakeBuffer, -1, 1));

            Assert.Throws <ArgumentException>(() => new UnmanagedMemoryAccessor(fakeBuffer, 2, 999));
            Assert.Throws <ArgumentException>(() => new UnmanagedMemoryAccessor(fakeBuffer, 999, 9));
            Assert.Throws <ArgumentException>(() => new UnmanagedMemoryAccessor(fakeBuffer, 1, 100));

            Assert.Throws <ArgumentException>(() => new UnmanagedMemoryAccessor(fakeBuffer, Int32.MaxValue, 1));
        }
示例#6
0
        public static void UmaCtorsThatFail()
        {
            FakeSafeBuffer fakeBuffer = new FakeSafeBuffer(99);
            FakeSafeBuffer nullBuffer = null;

            Assert.Throws<ArgumentNullException>(() => new UnmanagedMemoryAccessor(nullBuffer, 0, 0));

            Assert.Throws<ArgumentOutOfRangeException>(() => new UnmanagedMemoryAccessor(fakeBuffer, 2, -1));
            Assert.Throws<ArgumentOutOfRangeException>(() => new UnmanagedMemoryAccessor(fakeBuffer, -1, 1));

            Assert.Throws<ArgumentException>(() => new UnmanagedMemoryAccessor(fakeBuffer, 2, 999));
            Assert.Throws<ArgumentException>(() => new UnmanagedMemoryAccessor(fakeBuffer, 999, 9));
            Assert.Throws<ArgumentException>(() => new UnmanagedMemoryAccessor(fakeBuffer, 1, 100));

            Assert.Throws<ArgumentException>(() => new UnmanagedMemoryAccessor(fakeBuffer, Int32.MaxValue, 1));
        }
示例#7
0
        public static unsafe void CtorsThatFail()
        {
            Assert.Throws<ArgumentNullException>(() => { var ums = new UnmanagedMemoryStream(null, 0); });

            TestSafeBuffer nullBuffer = null;
            FakeSafeBuffer fakeBuffer = new FakeSafeBuffer(99);
            Assert.Throws<ArgumentNullException>(() => new UnmanagedMemoryStream(nullBuffer, 0, 1));

            Assert.Throws<ArgumentOutOfRangeException>(() => new UnmanagedMemoryStream(fakeBuffer, 2, -1));
            Assert.Throws<ArgumentOutOfRangeException>(() => new UnmanagedMemoryStream(fakeBuffer, -1, 1));
            Assert.Throws<ArgumentOutOfRangeException>(() => new UnmanagedMemoryStream(fakeBuffer, 1, 2, (FileAccess)(-1)));
            Assert.Throws<ArgumentOutOfRangeException>(() => new UnmanagedMemoryStream(fakeBuffer, 1, 2, (FileAccess)42));

            Assert.Throws<ArgumentException>(() => new UnmanagedMemoryStream(fakeBuffer, 2, 999));
            Assert.Throws<ArgumentException>(() => new UnmanagedMemoryStream(fakeBuffer, 999, 9));
            Assert.Throws<ArgumentException>(() => new UnmanagedMemoryStream(fakeBuffer, 1, 100));

            Assert.Throws<ArgumentException>(() => new UnmanagedMemoryStream(fakeBuffer, Int32.MaxValue, 1));
        }
示例#8
0
        public static unsafe void CtorsThatFail()
        {
            Assert.Throws <ArgumentNullException>(() => { var ums = new UnmanagedMemoryStream(null, 0); });

            TestSafeBuffer nullBuffer = null;
            FakeSafeBuffer fakeBuffer = new FakeSafeBuffer(99);

            Assert.Throws <ArgumentNullException>(() => new UnmanagedMemoryStream(nullBuffer, 0, 1));

            Assert.Throws <ArgumentOutOfRangeException>(() => new UnmanagedMemoryStream(fakeBuffer, 2, -1));
            Assert.Throws <ArgumentOutOfRangeException>(() => new UnmanagedMemoryStream(fakeBuffer, -1, 1));
            Assert.Throws <ArgumentOutOfRangeException>(() => new UnmanagedMemoryStream(fakeBuffer, 1, 2, (FileAccess)(-1)));
            Assert.Throws <ArgumentOutOfRangeException>(() => new UnmanagedMemoryStream(fakeBuffer, 1, 2, (FileAccess)42));

            Assert.Throws <ArgumentException>(() => new UnmanagedMemoryStream(fakeBuffer, 2, 999));
            Assert.Throws <ArgumentException>(() => new UnmanagedMemoryStream(fakeBuffer, 999, 9));
            Assert.Throws <ArgumentException>(() => new UnmanagedMemoryStream(fakeBuffer, 1, 100));

            Assert.Throws <ArgumentException>(() => new UnmanagedMemoryStream(fakeBuffer, Int32.MaxValue, 1));
        }
示例#9
0
        public static void UmaCtors()
        {
            using (FakeSafeBuffer fakeBuffer = new FakeSafeBuffer(99))
                using (var uma = new UnmanagedMemoryAccessor(fakeBuffer, 0, 0))
                {
                    Assert.True(uma.CanRead);
                    Assert.False(uma.CanWrite);
                    Assert.Equal(0, uma.Capacity);
                }

            using (FakeSafeBuffer fakeBuffer = new FakeSafeBuffer(99))
                using (var duma = new DerivedUnmanagedMemoryAccessor())
                {
                    Assert.False(duma.CanRead);
                    Assert.False(duma.CanWrite);
                    Assert.Equal(0, duma.Capacity);
                    Assert.False(duma.IsOpen);
                    duma.Initialize(fakeBuffer, 0, (long)fakeBuffer.ByteLength, FileAccess.ReadWrite);
                    Assert.True(duma.IsOpen);
                    Assert.Throws <InvalidOperationException>(() => duma.Initialize(fakeBuffer, 0, (long)fakeBuffer.ByteLength, FileAccess.ReadWrite));
                }
        }
示例#10
0
        public static void UmaCtors()
        {
            using (FakeSafeBuffer fakeBuffer = new FakeSafeBuffer(99))
            using (var uma = new UnmanagedMemoryAccessor(fakeBuffer, 0, 0))
            {
                Assert.True(uma.CanRead);
                Assert.False(uma.CanWrite);
                Assert.Equal(0, uma.Capacity);
            }

            using (FakeSafeBuffer fakeBuffer = new FakeSafeBuffer(99))
            using (var duma = new DerivedUnmanagedMemoryAccessor())
            {
                Assert.False(duma.CanRead);
                Assert.False(duma.CanWrite);
                Assert.Equal(0, duma.Capacity);
                Assert.False(duma.IsOpen);
                duma.Initialize(fakeBuffer, 0, (long)fakeBuffer.ByteLength, FileAccess.ReadWrite);
                Assert.True(duma.IsOpen);
                Assert.Throws<InvalidOperationException>(() => duma.Initialize(fakeBuffer, 0, (long)fakeBuffer.ByteLength, FileAccess.ReadWrite));
            }
        }
示例#11
0
        public static unsafe void BufferCtor()
        {
            const int length = 99;

            using (FakeSafeBuffer buffer = new FakeSafeBuffer(length))
                using (var stream = new DerivedUnmanagedMemoryStream(buffer, length, FileAccess.Read))
                {
                    Assert.True(stream.CanRead);
                    Assert.True(stream.CanSeek);
                    Assert.False(stream.CanWrite);

                    Assert.Equal(length, stream.Length);
                    Assert.Equal(length, stream.Capacity);
                    Assert.Equal(0, stream.Position);
                }

            using (FakeSafeBuffer buffer = new FakeSafeBuffer(length))
                using (var stream = new DerivedUnmanagedMemoryStream())
                {
                    Assert.False(stream.CanRead);
                    Assert.False(stream.CanSeek);
                    Assert.False(stream.CanWrite);

                    stream.Initialize(buffer, 0, length, FileAccess.Write);
                    Assert.Throws <InvalidOperationException>(() => stream.Initialize(buffer, 0, length, FileAccess.Write));

                    Assert.False(stream.CanRead);
                    Assert.True(stream.CanSeek);
                    Assert.True(stream.CanWrite);

                    Assert.Equal(length, stream.Length);
                    Assert.Equal(length, stream.Capacity);
                    Assert.Equal(0, stream.Position);

                    Assert.Throws <NotSupportedException>(() => stream.SetLength(1));
                }
        }
示例#12
0
 internal void Initialize(FakeSafeBuffer buffer, long offset, long capacity, FileAccess access)
 {
     base.Initialize(buffer, offset, capacity, access);
 }
示例#13
0
 internal DerivedUnmanagedMemoryStream(FakeSafeBuffer buffer, long length, FileAccess access) : base(buffer, 0, length, access)
 {
 }
示例#14
0
        public static unsafe void BufferCtor()
        {
            const int length = 99;
            using (FakeSafeBuffer buffer = new FakeSafeBuffer(length))
            using (var stream = new DerivedUnmanagedMemoryStream(buffer, length, FileAccess.Read))
            {
                Assert.True(stream.CanRead);
                Assert.True(stream.CanSeek);
                Assert.False(stream.CanWrite);

                Assert.Equal(length, stream.Length);
                Assert.Equal(length, stream.Capacity);
                Assert.Equal(0, stream.Position);
            }

            using (FakeSafeBuffer buffer = new FakeSafeBuffer(length))
            using (var stream = new DerivedUnmanagedMemoryStream())
            {
                Assert.False(stream.CanRead);
                Assert.False(stream.CanSeek);
                Assert.False(stream.CanWrite);

                stream.Initialize(buffer, 0, length, FileAccess.Write);
                Assert.Throws<InvalidOperationException>(() => stream.Initialize(buffer, 0, length, FileAccess.Write));

                Assert.False(stream.CanRead);
                Assert.True(stream.CanSeek);
                Assert.True(stream.CanWrite);

                Assert.Equal(length, stream.Length);
                Assert.Equal(length, stream.Capacity);
                Assert.Equal(0, stream.Position);

                Assert.Throws<NotSupportedException>(() => stream.SetLength(1));
            }
        }
示例#15
0
 internal void Initialize(FakeSafeBuffer buffer, long offset, long capacity, FileAccess access)
 {
     base.Initialize(buffer, offset, capacity, access);
 }
示例#16
0
 internal DerivedUnmanagedMemoryStream(FakeSafeBuffer buffer, long length, FileAccess access) : base(buffer, 0, length, access)
 {
 }