1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
7 namespace System.Runtime.InteropServices.Tests
9 public class SafeBufferTests
14 public void Ctor_Bool(bool ownsHandle)
16 var buffer = new SubBuffer(ownsHandle);
17 Assert.True(buffer.IsInvalid);
21 public void Initialize_InvalidNumBytes_ThrowsArgumentOutOfRangeException()
23 var buffer = new SubBuffer(true);
24 AssertExtensions.Throws<ArgumentOutOfRangeException>("numBytes", () => buffer.Initialize(ulong.MaxValue));
28 public void Initialize_NumBytesTimesSizeOfEachElement_ThrowsArgumentOutOfRangeExceptionIfNot64Bit()
30 var buffer = new SubBuffer(true);
31 AssertExtensions.ThrowsIf<ArgumentOutOfRangeException>(!Environment.Is64BitProcess, () => buffer.Initialize(uint.MaxValue, uint.MaxValue));
32 AssertExtensions.ThrowsIf<ArgumentOutOfRangeException>(!Environment.Is64BitProcess, () => buffer.Initialize<int>(uint.MaxValue));
36 public unsafe void AcquirePointer_NotInitialized_ThrowsInvalidOperationException()
38 var wrapper = new SubBuffer(true);
40 Assert.Throws<InvalidOperationException>(() => wrapper.AcquirePointer(ref pointer));
44 public void ReleasePointer_NotInitialized_ThrowsInvalidOperationException()
46 var wrapper = new SubBuffer(true);
47 Assert.Throws<InvalidOperationException>(() => wrapper.ReleasePointer());
51 public void ReadWrite_NotInitialized_ThrowsInvalidOperationException()
53 var wrapper = new SubBuffer(true);
55 Assert.Throws<InvalidOperationException>(() => wrapper.Read<int>(0));
56 Assert.Throws<InvalidOperationException>(() => wrapper.Write(0, 2));
62 [InlineData(ulong.MaxValue)]
63 public void ReadWrite_NotEnoughSpaceInBuffer_ThrowsArgumentException(ulong byteOffset)
65 var buffer = new SubBuffer(true);
68 Assert.Throws<ArgumentException>(null, () => buffer.Read<int>(byteOffset));
69 Assert.Throws<ArgumentException>(null, () => buffer.Write<int>(byteOffset, 2));
73 public void ReadArray_NullArray_ThrowsArgumentNullException()
75 var wrapper = new SubBuffer(true);
76 AssertExtensions.Throws<ArgumentNullException>("array", () => wrapper.ReadArray<int>(0, null, 0, 0));
77 AssertExtensions.Throws<ArgumentNullException>("array", () => wrapper.WriteArray<int>(0, null, 0, 0));
81 public void ReadArray_NegativeIndex_ThrowsArgumentOutOfRangeException()
83 var wrapper = new SubBuffer(true);
84 AssertExtensions.Throws<ArgumentOutOfRangeException>("index", () => wrapper.ReadArray(0, new int[0], -1, 0));
85 AssertExtensions.Throws<ArgumentOutOfRangeException>("index", () => wrapper.WriteArray(0, new int[0], -1, 0));
89 public void ReadWriteArray_NegativeCount_ThrowsArgumentOutOfRangeException()
91 var wrapper = new SubBuffer(true);
92 AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => wrapper.ReadArray(0, new int[0], 0, -1));
93 AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => wrapper.WriteArray(0, new int[0], 0, -1));
100 [InlineData(2, 2, 1)]
101 [InlineData(2, 1, 2)]
102 [InlineData(2, 0, 3)]
103 public void ReadWriteArray_NegativeCount_ThrowsArgumentOutOfRangeException(int arrayLength, int index, int count)
105 var wrapper = new SubBuffer(true);
106 AssertExtensions.Throws<ArgumentException>(null, () => wrapper.ReadArray(0, new int[arrayLength], index, count));
107 AssertExtensions.Throws<ArgumentException>(null, () => wrapper.WriteArray(0, new int[arrayLength], index, count));
111 public void ReadWriteArray_NotInitialized_ThrowsInvalidOperationException()
113 var wrapper = new SubBuffer(true);
114 Assert.Throws<InvalidOperationException>(() => wrapper.ReadArray(0, new int[0], 0, 0));
115 Assert.Throws<InvalidOperationException>(() => wrapper.WriteArray(0, new int[0], 0, 0));
119 public void ByteLength_GetNotInitialized_ThrowsInvalidOperationException()
121 var wrapper = new SubBuffer(true);
122 Assert.Throws<InvalidOperationException>(() => wrapper.ByteLength);
125 public class SubBuffer : SafeBuffer
127 public SubBuffer(bool ownsHandle) : base(ownsHandle) { }
129 protected override bool ReleaseHandle()
131 throw new NotImplementedException();