Esempio n. 1
0
        public static unsafe void PointerCtor()
        {
            int   someInt32 = 42;
            int * pInt32    = &someInt32;
            byte *pByte     = (byte *)pInt32;

            using (var stream = new UnmanagedMemoryStream(pByte, 0))
            {
                Assert.True(stream.CanRead);
                Assert.True(stream.CanSeek);
                Assert.False(stream.CanWrite);
                Assert.False(stream.CanTimeout);

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

                Assert.Throws <InvalidOperationException>(() => stream.ReadTimeout);
                Assert.Throws <InvalidOperationException>(() => stream.ReadTimeout = 42);
                Assert.Throws <InvalidOperationException>(() => stream.WriteTimeout);
                Assert.Throws <InvalidOperationException>(() => stream.WriteTimeout = 42);

                Assert.True(stream.PositionPointer == pByte);
            }

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

                Assert.Throws <ArgumentOutOfRangeException>(() => stream.Initialize(pByte, -1, 4, FileAccess.Read));
                Assert.Throws <ArgumentOutOfRangeException>(() => stream.Initialize(pByte, 1, -4, FileAccess.Read));
                Assert.Throws <ArgumentOutOfRangeException>(() => stream.Initialize(pByte, 5, 4, FileAccess.Read));
                Assert.Throws <ArgumentOutOfRangeException>(() => stream.Initialize(pByte, 1, 4, (FileAccess)12345));
                stream.Initialize(pByte, 1, 4, FileAccess.ReadWrite);
                Assert.Throws <InvalidOperationException>(() => stream.Initialize(pByte, 1, 4, FileAccess.ReadWrite));

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

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

                Assert.Throws <InvalidOperationException>(() => stream.ReadTimeout);
                Assert.Throws <InvalidOperationException>(() => stream.ReadTimeout = 42);
                Assert.Throws <InvalidOperationException>(() => stream.WriteTimeout);
                Assert.Throws <InvalidOperationException>(() => stream.WriteTimeout = 42);

                Assert.True(stream.PositionPointer == pByte);
            }
        }
Esempio n. 2
0
        public static unsafe void PointerCtor()
        {
            int someInt32 = 42;
            int* pInt32 = &someInt32;
            byte* pByte = (byte*)pInt32;

            using (var stream = new UnmanagedMemoryStream(pByte, 0))
            {
                Assert.True(stream.CanRead);
                Assert.True(stream.CanSeek);
                Assert.False(stream.CanWrite);
                Assert.False(stream.CanTimeout);

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

                Assert.Throws<InvalidOperationException>(() => stream.ReadTimeout);
                Assert.Throws<InvalidOperationException>(() => stream.ReadTimeout = 42);
                Assert.Throws<InvalidOperationException>(() => stream.WriteTimeout);
                Assert.Throws<InvalidOperationException>(() => stream.WriteTimeout = 42);

                Assert.True(stream.PositionPointer == pByte);
            }

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

                Assert.Throws<ArgumentOutOfRangeException>(() => stream.Initialize(pByte, -1, 4, FileAccess.Read));
                Assert.Throws<ArgumentOutOfRangeException>(() => stream.Initialize(pByte, 1, -4, FileAccess.Read));
                Assert.Throws<ArgumentOutOfRangeException>(() => stream.Initialize(pByte, 5, 4, FileAccess.Read));
                Assert.Throws<ArgumentOutOfRangeException>(() => stream.Initialize(pByte, 1, 4, (FileAccess)12345));
                stream.Initialize(pByte, 1, 4, FileAccess.ReadWrite);
                Assert.Throws<InvalidOperationException>(() => stream.Initialize(pByte, 1, 4, FileAccess.ReadWrite));

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

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

                Assert.Throws<InvalidOperationException>(() => stream.ReadTimeout);
                Assert.Throws<InvalidOperationException>(() => stream.ReadTimeout = 42);
                Assert.Throws<InvalidOperationException>(() => stream.WriteTimeout);
                Assert.Throws<InvalidOperationException>(() => stream.WriteTimeout = 42);

                Assert.True(stream.PositionPointer == pByte);
            }
        }
Esempio n. 3
0
        private unsafe Task <Stream> CreateStream(byte[] initialData, FileAccess access)
        {
            Stream stream = null;

            if (initialData is not null)
            {
                IntPtr ptr = Marshal.AllocHGlobal(initialData.Length);
                _pointers.Add(ptr);
                Marshal.Copy(initialData, 0, ptr, initialData.Length);
                stream = new DerivedUnmanagedMemoryStream((byte *)ptr, initialData.Length, initialData.Length, access);
            }
            return(Task.FromResult(stream));
        }
Esempio n. 4
0
        public static void Flush()
        {
            const int length = 1000;
            using (var manager = new UmsManager(FileAccess.Write, length))
            {
                UnmanagedMemoryStream stream = manager.Stream;

                stream.Flush();
                Assert.True(stream.FlushAsync(new CancellationToken(true)).IsCanceled);
                Assert.True(stream.FlushAsync().Status == TaskStatus.RanToCompletion);
            }

            using (var stream = new DerivedUnmanagedMemoryStream())
            {
                Assert.Throws<ObjectDisposedException>(() => stream.Flush());

                Task t = stream.FlushAsync();
                Assert.True(t.IsFaulted);
                Assert.IsType<ObjectDisposedException>(t.Exception.InnerException);
            }
        }
Esempio n. 5
0
        public static void Flush()
        {
            const int length = 1000;

            using (var manager = new UmsManager(FileAccess.Write, length))
            {
                UnmanagedMemoryStream stream = manager.Stream;

                stream.Flush();
                Assert.True(stream.FlushAsync(new CancellationToken(true)).IsCanceled);
                Assert.True(stream.FlushAsync().Status == TaskStatus.RanToCompletion);
            }

            using (var stream = new DerivedUnmanagedMemoryStream())
            {
                Assert.Throws <ObjectDisposedException>(() => stream.Flush());

                Task t = stream.FlushAsync();
                Assert.True(t.IsFaulted);
                Assert.IsType <ObjectDisposedException>(t.Exception.InnerException);
            }
        }
Esempio n. 6
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));
                }
        }
Esempio n. 7
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));
            }
        }