예제 #1
0
        public void DisposedTest()
        {
            var ms = new NativeMemoryStream(20);

            ms.Dispose();
            Assert.That(ms.CanRead, Is.False);
            Assert.That(ms.CanSeek, Is.False);
            Assert.That(ms.CanWrite, Is.False);
            Assert.That(() => ms.Capacity > 0, Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.Read <int>(), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.Read(typeof(int)), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.Read(new byte[3], 0, 3), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.ReadByte(), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.ReadArray(typeof(byte), 1, false), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.ReadReference <int>(), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.ReadReference <string>(), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.Seek(0, SeekOrigin.Begin), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.SetLength(20), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.Write("X"), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.Write("X", CharSet.Ansi), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.Write(new[] { "X", "Y" }), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.Write(new byte[3], 0, 3), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.Write(new[] { 1, 2 }), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.Write(256), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.WriteByte(1), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.WriteObject(Guid.NewGuid()), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.WriteReference("X"), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.WriteReference(256), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.WriteReferenceObject(Guid.NewGuid()), Throws.InstanceOf <ObjectDisposedException>());
        }
예제 #2
0
        public void ReadWriteOnlyTest()
        {
            using (var m = new SafeHGlobalHandle(10))
                using (var ms = new NativeMemoryStream((IntPtr)m, m.Size))
                {
                    Assert.That(ms.CanWrite, Is.False);
                    Assert.That(ms.CanSeek, Is.True);
                    Assert.That(ms.CanRead, Is.True);

                    Assert.That(() => ms.Write("X"), Throws.InstanceOf <NotSupportedException>());
                    Assert.That(() => ms.Write("X", CharSet.Ansi), Throws.InstanceOf <NotSupportedException>());
                    Assert.That(() => ms.Write(new[] { "X", "Y" }), Throws.InstanceOf <NotSupportedException>());
                    Assert.That(() => ms.Write(new byte[3], 0, 3), Throws.InstanceOf <NotSupportedException>());
                    Assert.That(() => ms.Write(new[] { 1, 2 }), Throws.InstanceOf <NotSupportedException>());
                    Assert.That(() => ms.Write(256), Throws.InstanceOf <NotSupportedException>());
                    Assert.That(() => ms.WriteByte(1), Throws.InstanceOf <NotSupportedException>());
                    Assert.That(() => ms.WriteObject(Guid.NewGuid()), Throws.InstanceOf <NotSupportedException>());
                    Assert.That(() => ms.WriteReference("X"), Throws.InstanceOf <NotSupportedException>());
                    Assert.That(() => ms.WriteReference(256), Throws.InstanceOf <NotSupportedException>());
                    Assert.That(() => ms.WriteReferenceObject(Guid.NewGuid()), Throws.InstanceOf <NotSupportedException>());
                }

            using (var ms = new NativeMemoryStream(10, 10, 20, FileAccess.Write))
            {
                Assert.That(ms.CanWrite, Is.True);
                Assert.That(ms.CanSeek, Is.True);
                Assert.That(ms.CanRead, Is.False);

                Assert.That(() => ms.Read <int>(), Throws.InstanceOf <NotSupportedException>());
                Assert.That(() => ms.Read(typeof(int)), Throws.InstanceOf <NotSupportedException>());
                Assert.That(() => ms.Read(new byte[3], 0, 3), Throws.InstanceOf <NotSupportedException>());
                Assert.That(() => ms.ReadByte(), Throws.InstanceOf <NotSupportedException>());
                Assert.That(() => ms.ReadArray(typeof(byte), 1, false), Throws.InstanceOf <NotSupportedException>());
                Assert.That(() => ms.ReadReference <int>(), Throws.InstanceOf <NotSupportedException>());
                Assert.That(() => ms.ReadReference <string>(), Throws.InstanceOf <NotSupportedException>());
            }
        }