public static VertexLayout ReadVertexLayout(this UnmanagedMemoryAccessor reader, ref int index) { var layout = new VertexLayout(); layout.Begin(); var attributeCount = reader.ReadByte(ref index); var stride = reader.ReadUInt16(ref index); for (int i = 0; i < attributeCount; i++) { var offset = reader.ReadUInt16(ref index); var attrib = reader.ReadUInt16(ref index); var count = reader.ReadByte(ref index); var attribType = reader.ReadUInt16(ref index); var normalized = reader.ReadBool(ref index); var asInt = reader.ReadBool(ref index); var usage = attributeUsageMap[attrib]; layout.Add(usage, count, attributeTypeMap[attribType], normalized, asInt); if (layout.GetOffset(usage) != offset) { throw new InvalidOperationException("Invalid mesh data; vertex attribute offset mismatch."); } } layout.End(); if (layout.Stride != stride) { throw new InvalidOperationException("Invalid mesh data; vertex layout stride mismatch."); } return(layout); }
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)); } }
public static bool ReadBool(this UnmanagedMemoryAccessor reader, ref int index) { var result = reader.ReadByte(index); index++; return(result != 0); }
private static void CheckIfManaged( FileInspectionResponse response, UnmanagedMemoryAccessor viewOfFile, IReadOnlyList <IMAGE_DATA_DIRECTORY> dataDirectories, IEnumerable <IMAGE_SECTION_HEADER> sectionHeaders) { var managedDataDirectory = dataDirectories[PeConstants.IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR]; if (managedDataDirectory.VirtualAddress == 0 || managedDataDirectory.Size == 0) { return; } var managedSectionHeader = sectionHeaders.First(section => managedDataDirectory.VirtualAddress >= section.VirtualAddress && managedDataDirectory.VirtualAddress < section.VirtualAddress + section.VirtualSize); var managedDirectoryOffset = managedSectionHeader.ToFileOffset(managedDataDirectory.VirtualAddress); viewOfFile.Read(managedDirectoryOffset, out IMAGE_COR20_HEADER managedHeader); var sizeOfManagedHeader = Marshal.SizeOf <IMAGE_COR20_HEADER>(); if (managedHeader.cb != sizeOfManagedHeader) { return; } response.IsManaged = true; response.IsStrongNameSigned = managedHeader.Flags.HasFlag(ComImageFlags.StrongNameSigned); }
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)); } }
public static void WriteBuffer(this UnmanagedMemoryAccessor memoryAccessor, Byte[] buffer) { Verify.NotNull(memoryAccessor, "memoryAccessor"); Verify.NotNull(buffer, "buffer"); memoryAccessor.WriteArray(0, buffer, 0, Math.Min(buffer.Length, (Int32)memoryAccessor.Capacity)); }
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 uint ReadUInt32(this UnmanagedMemoryAccessor reader, ref int index) { var result = reader.ReadUInt32(index); index += sizeof(uint); return(result); }
public static T Read <T>( this UnmanagedMemoryAccessor accessor, ref long position) where T : struct { accessor.Read(position, out T value); position += Marshal.SizeOf <T>(); return(value); }
public static T[] ReadArray <T>(this UnmanagedMemoryAccessor reader, int count, ref int index) where T : struct { var result = new T[count]; reader.ReadArray(index, result, 0, count); index += Marshal.SizeOf(typeof(T)) * count; return(result); }
public static T Read <T>(this UnmanagedMemoryAccessor reader, ref int index) where T : struct { T result; reader.Read(index, out result); index += Marshal.SizeOf(typeof(T)); return(result); }
private static CarState ReadCarState(UnmanagedMemoryAccessor memoryAccessor) { CarState carState; memoryAccessor.Read(CarStatePosition, out carState); return(carState); }
private static string ReadKey(UnmanagedMemoryAccessor reader, int size) { var buffer = new byte[size]; reader.ReadArray(0, buffer, 0, size); return(Encoding.UTF8.GetString(buffer)); }
private static GameStates ReadGameStates(UnmanagedMemoryAccessor memoryAccessor) { GameStates gameStates; memoryAccessor.Read(GameStatesPosition, out gameStates); return(gameStates); }
private static VehicleInformation ReadVehicleInformation(UnmanagedMemoryAccessor memoryAccessor) { var carName = memoryAccessor.ReadString(VehicleInformationPosition, StringLengthMax); var carClassName = memoryAccessor.ReadString(VehicleInformationPosition + StringLengthMax, StringLengthMax); return(new VehicleInformation { CarName = carName, CarClassName = carClassName }); }
public ProjectCarsGameData ReadData(UnmanagedMemoryAccessor memoryAccessor) { return(new ProjectCarsGameData { GameStates = ReadGameStates(memoryAccessor), VehicleInformation = ReadVehicleInformation(memoryAccessor), CarState = ReadCarState(memoryAccessor), }); }
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 ReferenceAccessor(UnmanagedMemoryAccessor accessor, ulong @ref) { if ((@ref & 1) != 0) { throw new ArgumentException("ref の最下位ビットは 0 である必要があります。"); } this.Accessor = accessor; this.Ref = @ref; }
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); } }
internal static SafeBuffer GetSafeBuffer(this UnmanagedMemoryAccessor accessor) { var buffer = _GetSafeBuffer(accessor); if (buffer == null) { throw new InvalidDataException(); } return(buffer); }
public static Byte[] ReadBuffer(this UnmanagedMemoryAccessor memoryAccessor) { Verify.NotNull(memoryAccessor, "memoryAccessor"); var result = new Byte[memoryAccessor.Capacity]; memoryAccessor.ReadArray(0, result, 0, result.Length); return(result); }
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_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_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)); } }
/// <summary></summary> /// <param name="accessor"></param> /// <param name="position"></param> /// <param name="count"></param> /// <returns></returns> public static Byte[] ReadArray(this UnmanagedMemoryAccessor accessor, Int32 position, Int32 count) { var buf = new Byte[count]; var n = accessor.ReadArray(position, buf, 0, buf.Length); if (n <= buf.Length) { buf = buf.ReadBytes(0, n); } return(buf); }
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 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 T[] ReadArray <T>( this UnmanagedMemoryAccessor accessor, long position, int availableLength) where T : struct { var count = availableLength / Marshal.SizeOf <T>(); var values = new T[count]; if (accessor.ReadArray(position, values, 0, count) != count) { throw new EndOfStreamException(); } return(values); }
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 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 UmaReadWriteStructWithReferenceType_ThrowsArgumentException() { const int capacity = 100; UmaTestStruct_ContainsReferenceType inStruct = new UmaTestStruct_ContainsReferenceType { referenceType = new object() }; using (var buffer = new TestSafeBuffer(capacity)) using (var uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { AssertExtensions.Throws <ArgumentException>(null, "type", () => uma.Write <UmaTestStruct_ContainsReferenceType>(0, ref inStruct)); AssertExtensions.Throws <ArgumentException>(null, "type", () => uma.Read <UmaTestStruct_ContainsReferenceType>(0, 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)) { 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)); } }
/// <summary> /// Extends WriteArray<T> so that buffer offset of 0 and call to Array.Length are not needed. /// <example> /// unmanagedmemoryaccessor.WriteArray<T>(position, array); /// </example> /// </summary> public static void WriteArray <T>(this UnmanagedMemoryAccessor unmanagedmemoryaccessor, Int64 position, T[] array) where T : struct { if (unmanagedmemoryaccessor == null) { throw new ArgumentNullException("unmanagedmemoryaccessor"); } if (array == null) { throw new ArgumentNullException("array"); } unmanagedmemoryaccessor.WriteArray(position, array, 0, array.Length); }
public static void UmaReadWriteGenericStringStruct_ThrowsArgumentException() { const int capacity = 100; UmaTestStruct_Generic <string> inStruct = new UmaTestStruct_Generic <string>() { ofT = "Cats!" }; using (var buffer = new TestSafeBuffer(capacity)) using (var uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { AssertExtensions.Throws <ArgumentException>(null, "type", () => uma.Write <UmaTestStruct_Generic <string> >(0, ref inStruct)); AssertExtensions.Throws <ArgumentException>(null, "type", () => uma.Read <UmaTestStruct_Generic <string> >(0, out inStruct)); } }
public static void UmaInvalidReadDecimal() { const int capacity = 16; // sizeof(decimal) using (var buffer = new TestSafeBuffer(capacity)) using (var uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { // UMA should throw when reading bad decimal values (some bits of flags are reserved and must be 0) uma.Write(0, 0); // lo uma.Write(4, 0); // mid uma.Write(8, 0); // hi uma.Write(12, -1); // flags (all bits are set, so this should raise an exception) Assert.Throws<ArgumentException>(() => uma.ReadDecimal(0)); // Should throw same exception as decimal(int[]) ctor for compat } }
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)); } }
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 UmaReadWriteStructWithReferenceType_ThrowsArgumentException() { const int capacity = 100; UmaTestStruct_ContainsReferenceType inStruct = new UmaTestStruct_ContainsReferenceType { referenceType = new object() }; using (var buffer = new TestSafeBuffer(capacity)) using (var uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { Assert.Throws<ArgumentException>("type", () => uma.Write<UmaTestStruct_ContainsReferenceType>(0, ref inStruct)); Assert.Throws<ArgumentException>("type", () => uma.Read<UmaTestStruct_ContainsReferenceType>(0, out inStruct)); } }
public static void UmaReadWrite() { var capacity = 99; Boolean v1 = true; // 1 Byte v2 = (Byte)123; // 1 Char v3 = (Char)255; // 1 Int16 v4 = Int16.MaxValue - 1; // 2 Int32 v5 = Int32.MinValue; // 4 Int64 v6 = 12345; // 8 Decimal v7 = 1.1m; //123.456m; // 16 Single v8 = 0.000123f; // 4 Double v9 = 1234567.890; // 8 SByte v10 = (SByte)(-128); // 1 UInt16 v11 = UInt16.MinValue; // 2 UInt32 v12 = 67890; // 4 UInt64 v13 = UInt32.MaxValue - 12345; // 8 using (var buffer = new TestSafeBuffer(capacity)) using (var uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { long pos = 0; uma.Write(pos, v1); pos += sizeof(Boolean); uma.Write(pos, v2); pos += sizeof(Byte); uma.Write(pos, v3); pos += sizeof(Char); uma.Write(pos, v4); pos += sizeof(Int16); uma.Write(pos, v5); pos += sizeof(Int32); uma.Write(pos, v6); pos += sizeof(Int64); long pp = pos; uma.Write(pos, v7); pos += sizeof(Decimal); uma.Write(pos, v8); pos += sizeof(Single); uma.Write(pos, v9); pos += sizeof(Double); uma.Write(pos, v10); pos += sizeof(SByte); uma.Write(pos, v11); pos += sizeof(UInt16); uma.Write(pos, v12); pos += sizeof(UInt32); uma.Write(pos, v13); pos = 0; var v01 = uma.ReadBoolean(pos); Assert.Equal(v1, v01); pos += sizeof(Boolean); var v02 = uma.ReadByte(pos); Assert.Equal(v2, v02); pos += sizeof(Byte); var v03 = uma.ReadChar(pos); Assert.Equal(v3, v03); pos += sizeof(Char); var v04 = uma.ReadInt16(pos); Assert.Equal(v4, v04); pos += sizeof(Int16); var v05 = uma.ReadInt32(pos); Assert.Equal(v5, v05); pos += sizeof(Int32); var v06 = uma.ReadInt64(pos); Assert.Equal(v6, v06); pos += sizeof(Int64); var v07 = uma.ReadDecimal(pos); Assert.Equal(v7, v07); pos += sizeof(Decimal); var v08 = uma.ReadSingle(pos); Assert.Equal(v8, v08); pos += sizeof(Single); var v09 = uma.ReadDouble(pos); Assert.Equal(v9, v09); pos += sizeof(Double); var v010 = uma.ReadSByte(pos); Assert.Equal(v10, v010); pos += sizeof(SByte); var v011 = uma.ReadUInt16(pos); Assert.Equal(v11, v011); pos += sizeof(UInt16); var v012 = uma.ReadUInt32(pos); Assert.Equal(v12, v012); pos += sizeof(UInt32); var v013 = uma.ReadUInt64(pos); Assert.Equal(v13, v013); } }
public static void UmaReadWriteStructArrayGenericIntStruct_Valid() { const int capacity = 100; UmaTestStruct_Generic<int>[] inStructArr = new UmaTestStruct_Generic<int>[1] { new UmaTestStruct_Generic<int>() { ofT = 190 } }; UmaTestStruct_Generic<int>[] outStructArr = new UmaTestStruct_Generic<int>[1]; using (var buffer = new TestSafeBuffer(capacity)) using (var uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { uma.WriteArray<UmaTestStruct_Generic<int>>(0, inStructArr, 0, 1); Assert.Equal(1, uma.ReadArray<UmaTestStruct_Generic<int>>(0, outStructArr, 0, 1)); Assert.Equal(inStructArr[0].ofT, outStructArr[0].ofT); } }
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 UmaReadWriteGenericIntStruct_Valid() { const int capacity = 100; UmaTestStruct_Generic<int> inStruct = new UmaTestStruct_Generic<int> () { ofT = 158 }; UmaTestStruct_Generic<int> outStruct; using (var buffer = new TestSafeBuffer(capacity)) using (var uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { uma.Write<UmaTestStruct_Generic<int>>(0, ref inStruct); uma.Read<UmaTestStruct_Generic<int>>(0, out outStruct); Assert.Equal(inStruct.ofT, outStruct.ofT); } }
public static void UmaReadWriteGenericStringStruct_ThrowsArgumentException() { const int capacity = 100; UmaTestStruct_Generic<string> inStruct = new UmaTestStruct_Generic<string>() { ofT = "Cats!" }; using (var buffer = new TestSafeBuffer(capacity)) using (var uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { Assert.Throws<ArgumentException>("type", () => uma.Write<UmaTestStruct_Generic<string>>(0, ref inStruct)); Assert.Throws<ArgumentException>("type", () => uma.Read<UmaTestStruct_Generic<string>>(0, out inStruct)); } }
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_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 UmaReadWrite() { const int capacity = 199; const bool expectedBool = true; // 1 const byte expectedByte = 123; // 1 const sbyte expectedSByte = -128; // 1 const char expectedChar = (char)255; // 2 const ushort expectedUInt16 = ushort.MinValue; // 2 const short expectedInt16 = short.MaxValue - 1; // 2 const uint expectedUInt32 = 67890; // 4 const int expectedInt32 = int.MinValue; // 4 const float expectedSingle = 0.000123f; // 4 const ulong expectedUInt64 = ulong.MaxValue - 12345; // 8 const long expectedInt64 = 12345; // 8 const double expectedDouble = 1234567.890; // 8 const decimal expectedDecimal = 1.1m; //123.456m; // 16 using (var buffer = new TestSafeBuffer(capacity)) using (var uma = new UnmanagedMemoryAccessor(buffer, 0, capacity, FileAccess.ReadWrite)) { long pos = 0; uma.Write(pos, expectedBool); pos += sizeof(bool); uma.Write(pos, expectedByte); pos += sizeof(byte); uma.Write(pos, expectedSByte); pos += sizeof(sbyte); pos = EnsureAligned(pos, sizeof(char)); uma.Write(pos, expectedChar); pos += sizeof(char); pos = EnsureNotAligned(pos, sizeof(char)); uma.Write(pos, expectedChar); pos += sizeof(char); pos = EnsureAligned(pos, sizeof(ushort)); uma.Write(pos, expectedUInt16); pos += sizeof(ushort); pos = EnsureNotAligned(pos, sizeof(ushort)); uma.Write(pos, expectedUInt16); pos += sizeof(ushort); pos = EnsureAligned(pos, sizeof(short)); uma.Write(pos, expectedInt16); pos += sizeof(short); pos = EnsureNotAligned(pos, sizeof(short)); uma.Write(pos, expectedInt16); pos += sizeof(short); pos = EnsureAligned(pos, sizeof(uint)); uma.Write(pos, expectedUInt32); pos += sizeof(uint); pos = EnsureNotAligned(pos, sizeof(uint)); uma.Write(pos, expectedUInt32); pos += sizeof(uint); pos = EnsureAligned(pos, sizeof(int)); uma.Write(pos, expectedInt32); pos += sizeof(int); pos = EnsureNotAligned(pos, sizeof(int)); uma.Write(pos, expectedInt32); pos += sizeof(int); pos = EnsureAligned(pos, sizeof(float)); uma.Write(pos, expectedSingle); pos += sizeof(float); pos = EnsureNotAligned(pos, sizeof(float)); uma.Write(pos, expectedSingle); pos += sizeof(float); pos = EnsureAligned(pos, sizeof(ulong)); uma.Write(pos, expectedUInt64); pos += sizeof(ulong); pos = EnsureNotAligned(pos, sizeof(ulong)); uma.Write(pos, expectedUInt64); pos += sizeof(ulong); pos = EnsureAligned(pos, sizeof(long)); uma.Write(pos, expectedInt64); pos += sizeof(long); pos = EnsureNotAligned(pos, sizeof(long)); uma.Write(pos, expectedInt64); pos += sizeof(long); pos = EnsureAligned(pos, sizeof(double)); uma.Write(pos, expectedDouble); pos += sizeof(double); pos = EnsureNotAligned(pos, sizeof(double)); uma.Write(pos, expectedDouble); pos += sizeof(double); pos = EnsureAligned(pos, sizeof(decimal)); uma.Write(pos, expectedDecimal); pos += sizeof(decimal); pos = EnsureNotAligned(pos, sizeof(decimal)); uma.Write(pos, expectedDecimal); pos = 0; Assert.Equal(expectedBool, uma.ReadBoolean(pos)); pos += sizeof(bool); Assert.Equal(expectedByte, uma.ReadByte(pos)); pos += sizeof(byte); Assert.Equal(expectedSByte, uma.ReadSByte(pos)); pos += sizeof(sbyte); pos = EnsureAligned(pos, sizeof(char)); Assert.Equal(expectedChar, uma.ReadChar(pos)); pos += sizeof(char); pos = EnsureNotAligned(pos, sizeof(char)); Assert.Equal(expectedChar, uma.ReadChar(pos)); pos += sizeof(char); pos = EnsureAligned(pos, sizeof(ushort)); Assert.Equal(expectedUInt16, uma.ReadUInt16(pos)); pos += sizeof(ushort); pos = EnsureNotAligned(pos, sizeof(ushort)); Assert.Equal(expectedUInt16, uma.ReadUInt16(pos)); pos += sizeof(ushort); pos = EnsureAligned(pos, sizeof(short)); Assert.Equal(expectedInt16, uma.ReadInt16(pos)); pos += sizeof(short); pos = EnsureNotAligned(pos, sizeof(short)); Assert.Equal(expectedInt16, uma.ReadInt16(pos)); pos += sizeof(short); pos = EnsureAligned(pos, sizeof(uint)); Assert.Equal(expectedUInt32, uma.ReadUInt32(pos)); pos += sizeof(uint); pos = EnsureNotAligned(pos, sizeof(uint)); Assert.Equal(expectedUInt32, uma.ReadUInt32(pos)); pos += sizeof(uint); pos = EnsureAligned(pos, sizeof(int)); Assert.Equal(expectedInt32, uma.ReadInt32(pos)); pos += sizeof(int); pos = EnsureNotAligned(pos, sizeof(int)); Assert.Equal(expectedInt32, uma.ReadInt32(pos)); pos += sizeof(int); pos = EnsureAligned(pos, sizeof(float)); Assert.Equal(expectedSingle, uma.ReadSingle(pos)); pos += sizeof(float); pos = EnsureNotAligned(pos, sizeof(float)); Assert.Equal(expectedSingle, uma.ReadSingle(pos)); pos += sizeof(float); pos = EnsureAligned(pos, sizeof(ulong)); Assert.Equal(expectedUInt64, uma.ReadUInt64(pos)); pos += sizeof(ulong); pos = EnsureNotAligned(pos, sizeof(ulong)); Assert.Equal(expectedUInt64, uma.ReadUInt64(pos)); pos += sizeof(ulong); pos = EnsureAligned(pos, sizeof(long)); Assert.Equal(expectedInt64, uma.ReadInt64(pos)); pos += sizeof(long); pos = EnsureNotAligned(pos, sizeof(long)); Assert.Equal(expectedInt64, uma.ReadInt64(pos)); pos += sizeof(long); pos = EnsureAligned(pos, sizeof(double)); Assert.Equal(expectedDouble, uma.ReadDouble(pos)); pos += sizeof(double); pos = EnsureNotAligned(pos, sizeof(double)); Assert.Equal(expectedDouble, uma.ReadDouble(pos)); pos += sizeof(double); pos = EnsureAligned(pos, sizeof(decimal)); Assert.Equal(expectedDecimal, uma.ReadDecimal(pos)); pos += sizeof(decimal); pos = EnsureNotAligned(pos, sizeof(decimal)); Assert.Equal(expectedDecimal, uma.ReadDecimal(pos)); } }
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); } }