public static void UmaReadWriteStructArray_Multiples() { const int numberOfStructs = 12; const int capacity = numberOfStructs * UmaTestStruct_AlignedSize; UmaTestStruct[] inStructArr = new UmaTestStruct[numberOfStructs]; UmaTestStruct[] outStructArr = new UmaTestStruct[numberOfStructs]; for (int i = 0; i < numberOfStructs; i++) { inStructArr[i] = new UmaTestStruct() { bool1 = false, bool2 = true, int1 = i, int2 = i + 1, char1 = (char)i }; } using (TestSafeBuffer buffer = new TestSafeBuffer(capacity)) using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { uma.WriteArray <UmaTestStruct>(0, inStructArr, 0, numberOfStructs); Assert.Equal(numberOfStructs, uma.ReadArray <UmaTestStruct>(0, outStructArr, 0, numberOfStructs)); for (int i = 0; i < numberOfStructs; i++) { Assert.Equal(i, outStructArr[i].int1); Assert.Equal(i + 1, outStructArr[i].int2); Assert.Equal(false, outStructArr[i].bool1); Assert.Equal((char)i, outStructArr[i].char1); Assert.Equal(true, outStructArr[i].bool2); } } }
public static void UmaReadStructArray_WriteMode() { const int capacity = 100; UmaTestStruct[] structArr = new UmaTestStruct[1]; using (TestSafeBuffer buffer = new TestSafeBuffer(capacity)) using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.Write)) { Assert.Throws<NotSupportedException>(() => uma.ReadArray<UmaTestStruct>(0, structArr, 0, 1)); } }
public static void UmaWriteStruct_ReadMode() { const int capacity = 100; UmaTestStruct inStruct = new UmaTestStruct(); using (TestSafeBuffer buffer = new TestSafeBuffer(capacity)) using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.Read)) { Assert.Throws<NotSupportedException>(() => uma.Write<UmaTestStruct>(0, ref inStruct)); } }
public static void UmaReadStructArray_WriteMode() { const int capacity = 100; UmaTestStruct[] structArr = new UmaTestStruct[1]; using (TestSafeBuffer buffer = new TestSafeBuffer(capacity)) using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.Write)) { Assert.Throws <NotSupportedException>(() => uma.ReadArray <UmaTestStruct>(0, structArr, 0, 1)); } }
public static void UmaReadWriteStruct_PositionGreaterThanCapacity() { const int capacity = 100; UmaTestStruct inStruct = new UmaTestStruct(); using (TestSafeBuffer buffer = new TestSafeBuffer(capacity)) using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { Assert.Throws<ArgumentOutOfRangeException>("position", () => uma.Write<UmaTestStruct>(capacity, ref inStruct)); Assert.Throws<ArgumentOutOfRangeException>("position", () => uma.Read<UmaTestStruct>(capacity, out inStruct)); } }
public static void UmaWriteStruct_ReadMode() { const int capacity = 100; UmaTestStruct inStruct = new UmaTestStruct(); using (TestSafeBuffer buffer = new TestSafeBuffer(capacity)) using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.Read)) { Assert.Throws <NotSupportedException>(() => uma.Write <UmaTestStruct>(0, ref inStruct)); } }
public static void UmaReadWriteStruct_Closed() { const int capacity = 100; UmaTestStruct inStruct = new UmaTestStruct(); using (var buffer = new TestSafeBuffer(capacity)) { UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite); uma.Dispose(); Assert.Throws<ObjectDisposedException>(() => uma.Write<UmaTestStruct>(0, ref inStruct)); Assert.Throws<ObjectDisposedException>(() => uma.Read<UmaTestStruct>(0, out inStruct)); } }
public static void UmaReadWriteStruct_PositionGreaterThanCapacity() { const int capacity = 100; UmaTestStruct inStruct = new UmaTestStruct(); using (TestSafeBuffer buffer = new TestSafeBuffer(capacity)) using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => uma.Write <UmaTestStruct>(capacity, ref inStruct)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => uma.Read <UmaTestStruct>(capacity, out inStruct)); } }
public static void UmaReadWriteStruct_InsufficientSpace() { const int capacity = 100; UmaTestStruct inStruct = new UmaTestStruct(); using (TestSafeBuffer buffer = new TestSafeBuffer(capacity)) using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { Assert.Throws<ArgumentException>(() => uma.Write<UmaTestStruct>(capacity - UmaTestStruct_UnalignedSize + 1, ref inStruct)); Assert.Throws<ArgumentException>(() => uma.Write<UmaTestStruct>(capacity - UmaTestStruct_AlignedSize + 1, ref inStruct)); Assert.Throws<ArgumentException>(() => uma.Read<UmaTestStruct>(capacity - UmaTestStruct_UnalignedSize + 1, out inStruct)); Assert.Throws<ArgumentException>(() => uma.Read<UmaTestStruct>(capacity - UmaTestStruct_AlignedSize + 1, out inStruct)); } }
public static void UmaReadWriteStruct_Closed() { const int capacity = 100; UmaTestStruct inStruct = new UmaTestStruct(); using (var buffer = new TestSafeBuffer(capacity)) { UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite); uma.Dispose(); Assert.Throws <ObjectDisposedException>(() => uma.Write <UmaTestStruct>(0, ref inStruct)); Assert.Throws <ObjectDisposedException>(() => uma.Read <UmaTestStruct>(0, out inStruct)); } }
public static void UmaReadWriteStructArray_InsufficientSpace() { const int capacity = 100; UmaTestStruct[] structArr = new UmaTestStruct[1]; using (TestSafeBuffer buffer = new TestSafeBuffer(capacity)) using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { Assert.Throws<ArgumentException>(() => uma.WriteArray<UmaTestStruct>(capacity - UmaTestStruct_UnalignedSize + 1, structArr, 0, 1)); Assert.Throws<ArgumentException>(() => uma.WriteArray<UmaTestStruct>(capacity - UmaTestStruct_AlignedSize + 1, structArr, 0, 1)); Assert.Equal(0, uma.ReadArray<UmaTestStruct>(capacity - UmaTestStruct_UnalignedSize + 1, structArr, 0, 1)); Assert.Equal(0, uma.ReadArray<UmaTestStruct>(capacity - UmaTestStruct_AlignedSize + 1, structArr, 0, 1)); } }
public static void UmaReadWriteStructArray_InsufficientSpace() { const int capacity = 100; UmaTestStruct[] structArr = new UmaTestStruct[1]; using (TestSafeBuffer buffer = new TestSafeBuffer(capacity)) using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { AssertExtensions.Throws <ArgumentException>(null, () => uma.WriteArray <UmaTestStruct>(capacity - UmaTestStruct_UnalignedSize + 1, structArr, 0, 1)); AssertExtensions.Throws <ArgumentException>(null, () => uma.WriteArray <UmaTestStruct>(capacity - UmaTestStruct_AlignedSize + 1, structArr, 0, 1)); Assert.Equal(0, uma.ReadArray <UmaTestStruct>(capacity - UmaTestStruct_UnalignedSize + 1, structArr, 0, 1)); Assert.Equal(0, uma.ReadArray <UmaTestStruct>(capacity - UmaTestStruct_AlignedSize + 1, structArr, 0, 1)); } }
public static void UmaReadWriteStruct_InsufficientSpace() { const int capacity = 100; UmaTestStruct inStruct = new UmaTestStruct(); using (TestSafeBuffer buffer = new TestSafeBuffer(capacity)) using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { AssertExtensions.Throws <ArgumentException>("position", () => uma.Write <UmaTestStruct>(capacity - UmaTestStruct_UnalignedSize + 1, ref inStruct)); AssertExtensions.Throws <ArgumentException>("position", () => uma.Write <UmaTestStruct>(capacity - UmaTestStruct_AlignedSize + 1, ref inStruct)); AssertExtensions.Throws <ArgumentException>("position", () => uma.Read <UmaTestStruct>(capacity - UmaTestStruct_UnalignedSize + 1, out inStruct)); AssertExtensions.Throws <ArgumentException>("position", () => uma.Read <UmaTestStruct>(capacity - UmaTestStruct_AlignedSize + 1, out inStruct)); } }
public static void UmaReadWriteStructArray_TryToReadMoreThanAvailable() { const int capacity = 100; UmaTestStruct[] inStructArr = new UmaTestStruct[1] { new UmaTestStruct() { int1 = 1, int2 = 2, bool1 = false, char1 = 'p', bool2 = true } }; UmaTestStruct[] outStructArr = new UmaTestStruct[5000]; using (TestSafeBuffer buffer = new TestSafeBuffer(capacity)) using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { uma.WriteArray <UmaTestStruct>(0, inStructArr, 0, 1); int readCount = uma.ReadArray <UmaTestStruct>(0, outStructArr, 0, 5000); Assert.Equal(capacity / UmaTestStruct_AlignedSize, readCount); } }
public static void UmaReadWriteStruct_Valid() { const int capacity = 100; UmaTestStruct inStruct = new UmaTestStruct() { int1 = 1, int2 = 2, bool1 = false, char1 = 'p', bool2 = true }; UmaTestStruct outStruct; using (TestSafeBuffer buffer = new TestSafeBuffer(capacity)) using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { uma.Write <UmaTestStruct>(capacity - UmaTestStruct_AlignedSize, ref inStruct); uma.Read <UmaTestStruct>(capacity - UmaTestStruct_AlignedSize, out outStruct); Assert.Equal(inStruct.int1, outStruct.int1); Assert.Equal(inStruct.int2, outStruct.int2); Assert.Equal(inStruct.bool1, outStruct.bool1); Assert.Equal(inStruct.char1, outStruct.char1); Assert.Equal(inStruct.bool2, outStruct.bool2); } }
public static void UmaReadWriteStructArray_OneItem() { const int capacity = 100; UmaTestStruct[] inStructArr = new UmaTestStruct[1] { new UmaTestStruct() { int1 = 1, int2 = 2, bool1 = false, char1 = 'p', bool2 = true } }; UmaTestStruct[] outStructArr = new UmaTestStruct[1]; using (TestSafeBuffer buffer = new TestSafeBuffer(capacity)) using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { uma.WriteArray <UmaTestStruct>(capacity - UmaTestStruct_AlignedSize, inStructArr, 0, 1); Assert.Equal(1, uma.ReadArray <UmaTestStruct>(capacity - UmaTestStruct_AlignedSize, outStructArr, 0, 1)); Assert.Equal(inStructArr[0].int1, outStructArr[0].int1); Assert.Equal(inStructArr[0].int2, outStructArr[0].int2); Assert.Equal(inStructArr[0].bool1, outStructArr[0].bool1); Assert.Equal(inStructArr[0].char1, outStructArr[0].char1); Assert.Equal(inStructArr[0].bool2, outStructArr[0].bool2); } }
public static void UmaReadWriteStructArray_InvalidParameters() { const int capacity = 100; UmaTestStruct[] structArr = new UmaTestStruct[1]; using (TestSafeBuffer buffer = new TestSafeBuffer(capacity)) using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { Assert.Throws<ArgumentNullException>("array", () => uma.WriteArray<UmaTestStruct>(0, null, 0, 1)); Assert.Throws<ArgumentNullException>("array", () => uma.ReadArray<UmaTestStruct>(0, null, 0, 1)); Assert.Throws<ArgumentOutOfRangeException>("offset", () => uma.WriteArray<UmaTestStruct>(0, structArr, -1, 1)); Assert.Throws<ArgumentOutOfRangeException>("offset", () => uma.ReadArray<UmaTestStruct>(0, structArr, -1, 1)); Assert.Throws<ArgumentOutOfRangeException>("count", () => uma.WriteArray<UmaTestStruct>(0, structArr, 0, -1)); Assert.Throws<ArgumentOutOfRangeException>("count", () => uma.ReadArray<UmaTestStruct>(0, structArr, 0, -1)); Assert.Throws<ArgumentException>(() => uma.WriteArray<UmaTestStruct>(0, structArr, 2, 0)); Assert.Throws<ArgumentException>(() => uma.ReadArray<UmaTestStruct>(0, structArr, 2, 0)); Assert.Throws<ArgumentException>(() => uma.WriteArray<UmaTestStruct>(0, structArr, 0, 2)); Assert.Throws<ArgumentException>(() => uma.ReadArray<UmaTestStruct>(0, structArr, 0, 2)); Assert.Throws<ArgumentOutOfRangeException>("position", () => uma.WriteArray<UmaTestStruct>(-1, structArr, 0, 1)); Assert.Throws<ArgumentOutOfRangeException>("position", () => uma.ReadArray<UmaTestStruct>(-1, structArr, 0, 1)); Assert.Throws<ArgumentOutOfRangeException>("position", () => uma.WriteArray<UmaTestStruct>(capacity, structArr, 0, 1)); Assert.Throws<ArgumentOutOfRangeException>("position", () => uma.ReadArray<UmaTestStruct>(capacity, structArr, 0, 1)); } }
public static void UmaReadWriteStructArray_InvalidParameters() { const int capacity = 100; UmaTestStruct[] structArr = new UmaTestStruct[1]; using (TestSafeBuffer buffer = new TestSafeBuffer(capacity)) using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { AssertExtensions.Throws <ArgumentNullException>("array", () => uma.WriteArray <UmaTestStruct>(0, null, 0, 1)); AssertExtensions.Throws <ArgumentNullException>("array", () => uma.ReadArray <UmaTestStruct>(0, null, 0, 1)); AssertExtensions.Throws <ArgumentOutOfRangeException>("offset", () => uma.WriteArray <UmaTestStruct>(0, structArr, -1, 1)); AssertExtensions.Throws <ArgumentOutOfRangeException>("offset", () => uma.ReadArray <UmaTestStruct>(0, structArr, -1, 1)); AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => uma.WriteArray <UmaTestStruct>(0, structArr, 0, -1)); AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => uma.ReadArray <UmaTestStruct>(0, structArr, 0, -1)); AssertExtensions.Throws <ArgumentException>(null, () => uma.WriteArray <UmaTestStruct>(0, structArr, 2, 0)); AssertExtensions.Throws <ArgumentException>(null, () => uma.ReadArray <UmaTestStruct>(0, structArr, 2, 0)); AssertExtensions.Throws <ArgumentException>(null, () => uma.WriteArray <UmaTestStruct>(0, structArr, 0, 2)); AssertExtensions.Throws <ArgumentException>(null, () => uma.ReadArray <UmaTestStruct>(0, structArr, 0, 2)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => uma.WriteArray <UmaTestStruct>(-1, structArr, 0, 1)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => uma.ReadArray <UmaTestStruct>(-1, structArr, 0, 1)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => uma.WriteArray <UmaTestStruct>(capacity, structArr, 0, 1)); AssertExtensions.Throws <ArgumentOutOfRangeException>("position", () => uma.ReadArray <UmaTestStruct>(capacity, structArr, 0, 1)); } }
public static void UmaReadWriteStructArray_Multiples() { const int numberOfStructs = 12; const int capacity = numberOfStructs * UmaTestStruct_AlignedSize; UmaTestStruct[] inStructArr = new UmaTestStruct[numberOfStructs]; UmaTestStruct[] outStructArr = new UmaTestStruct[numberOfStructs]; for (int i = 0; i < numberOfStructs; i++) { inStructArr[i] = new UmaTestStruct() { bool1 = false, bool2 = true, int1 = i, int2 = i + 1, char1 = (char)i }; } using (TestSafeBuffer buffer = new TestSafeBuffer(capacity)) using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { uma.WriteArray<UmaTestStruct>(0, inStructArr, 0, numberOfStructs); Assert.Equal(numberOfStructs, uma.ReadArray<UmaTestStruct>(0, outStructArr, 0, numberOfStructs)); for (int i = 0; i < numberOfStructs; i++) { Assert.Equal(i, outStructArr[i].int1); Assert.Equal(i+1, outStructArr[i].int2); Assert.Equal(false, outStructArr[i].bool1); Assert.Equal((char)i, outStructArr[i].char1); Assert.Equal(true, outStructArr[i].bool2); } } }
public static void UmaReadWriteStruct_Valid() { const int capacity = 100; UmaTestStruct inStruct = new UmaTestStruct(){ int1 = 1, int2 = 2, bool1 = false, char1 = 'p', bool2 = true}; UmaTestStruct outStruct; using (TestSafeBuffer buffer = new TestSafeBuffer(capacity)) using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { uma.Write<UmaTestStruct>(capacity - UmaTestStruct_AlignedSize, ref inStruct); uma.Read<UmaTestStruct>(capacity - UmaTestStruct_AlignedSize, out outStruct); Assert.Equal(inStruct.int1, outStruct.int1); Assert.Equal(inStruct.int2, outStruct.int2); Assert.Equal(inStruct.bool1, outStruct.bool1); Assert.Equal(inStruct.char1, outStruct.char1); Assert.Equal(inStruct.bool2, outStruct.bool2); } }
public static void UmaReadWriteStructArray_TryToReadMoreThanAvailable() { const int capacity = 100; UmaTestStruct[] inStructArr = new UmaTestStruct[1] { new UmaTestStruct() { int1 = 1, int2 = 2, bool1 = false, char1 = 'p', bool2 = true } }; UmaTestStruct[] outStructArr = new UmaTestStruct[5000]; using (TestSafeBuffer buffer = new TestSafeBuffer(capacity)) using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { uma.WriteArray<UmaTestStruct>(0, inStructArr, 0, 1); int readCount = uma.ReadArray<UmaTestStruct>(0, outStructArr, 0, 5000); Assert.Equal(capacity / UmaTestStruct_AlignedSize, readCount); } }
public static void UmaReadWriteStructArray_OneItem() { const int capacity = 100; UmaTestStruct[] inStructArr = new UmaTestStruct[1] { new UmaTestStruct() { int1 = 1, int2 = 2, bool1 = false, char1 = 'p', bool2 = true } }; UmaTestStruct[] outStructArr = new UmaTestStruct[1]; using (TestSafeBuffer buffer = new TestSafeBuffer(capacity)) using (UnmanagedMemoryAccessor uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { uma.WriteArray<UmaTestStruct>(capacity - UmaTestStruct_AlignedSize, inStructArr, 0, 1); Assert.Equal(1, uma.ReadArray<UmaTestStruct>(capacity - UmaTestStruct_AlignedSize, outStructArr, 0, 1)); Assert.Equal(inStructArr[0].int1, outStructArr[0].int1); Assert.Equal(inStructArr[0].int2, outStructArr[0].int2); Assert.Equal(inStructArr[0].bool1, outStructArr[0].bool1); Assert.Equal(inStructArr[0].char1, outStructArr[0].char1); Assert.Equal(inStructArr[0].bool2, outStructArr[0].bool2); } }