public FixedPointDataType( uint aSize, ByteOrdering aByteOrdering, byte aHighPaddingBit, byte aLowPaddingBit, bool aIsSigned, ushort aBitOffset, ushort aBitPrecision) : base(DatatypeClass.FixedPoint, aSize) { if (!(aHighPaddingBit == 0 || aHighPaddingBit == 1)) { throw new ArgumentException( "Padding bits must be 0 or 1", nameof(aHighPaddingBit)); } if (!(aLowPaddingBit == 0 || aLowPaddingBit == 1)) { throw new ArgumentException( "Padding bits must be 0 or 1", nameof(aLowPaddingBit)); } ByteOrdering = aByteOrdering; HighPaddingBit = aHighPaddingBit; LowPaddingBit = aLowPaddingBit; IsSigned = aIsSigned; BitOffset = aBitOffset; BitPrecision = aBitPrecision; }
internal static Datatype ReadMessage( DatatypeHeader aHeader, Hdf5Reader aReader, long?aLocalMessageSize, out long aBytes) { if (aHeader.Class != DatatypeClass.Time) { throw new ArgumentException( $"aHeader must be for type {nameof(DatatypeClass.Time)}"); } if (aLocalMessageSize.HasValue && aLocalMessageSize < mcAddionalSize) { throw new ArgumentException("Specified Local Message Size not long enough"); } Flags fFlags = (Flags)aHeader.Flags; ByteOrdering fByteOrdering = fFlags.HasFlag(Flags.BigEndian) ? ByteOrdering.BigEndian : ByteOrdering.LittleEndian; uint fBitPrecision = aReader.ReadUInt32(); aBytes = mcAddionalSize; return(new TimeDataType( aHeader, fByteOrdering, fBitPrecision)); }
private FloatingPointDataType( IDatatypeHeader aHeader, ByteOrdering aByteOrdering, byte aHighPaddingBit, byte aLowPaddingBit, byte aInternalPaddingBit, ushort aSignLocation, ushort aBitOffset, ushort aBitPrecision, byte aExponentLocation, byte aExponentSize, byte aMantissaLocation, byte aMantissaSize, uint aExponentBias) : this( aHeader.Size, aByteOrdering, aHighPaddingBit, aLowPaddingBit, aInternalPaddingBit, aSignLocation, aBitOffset, aBitPrecision, aExponentLocation, aExponentSize, aMantissaLocation, aMantissaSize, aExponentBias) { if (aHeader.Class != DatatypeClass.FloatingPoint) { throw new ArgumentException( $"Header Class must be {nameof(DatatypeClass.FixedPoint)}", nameof(aHeader)); } }
public TimeDataType( uint aSize, ByteOrdering aByteOrdering, uint aBitPrecision) : base(DatatypeClass.Time, aSize) { ByteOrdering = aByteOrdering; BitPrecision = aBitPrecision; }
/// <summary> /// Initializes a new instance of the <see cref="BitSequence"/> class with given bytes. /// </summary> /// <param name="bytes">The bytes.</param> /// <param name="byteOrdering">A <see cref="ByteOrdering"/> value specifying whether the bytes are big-endian or little-endian (endianness).</param> public BitSequence(byte[] bytes, ByteOrdering byteOrdering = ByteOrdering.LittleEndian) { list = new List<bool>(bytes.Length * 8); for (var index = 0; index < bytes.Length * 8; index++) { var byteIndex = index / 8; var bitIndex = index % 8; var mask = (byte)(1 << bitIndex); list.Add((bytes[byteIndex] & mask) != 0); } ByteOrdering = byteOrdering; }
private TimeDataType( IDatatypeHeader aHeader, ByteOrdering aByteOrdering, uint aBitPrecision) : this( aHeader.Size, aByteOrdering, aBitPrecision) { if (aHeader.Class != DatatypeClass.Time) { throw new ArgumentException( $"Header Class must be {nameof(DatatypeClass.Time)}", nameof(aHeader)); } }
public FloatingPointDataType( uint aSize, ByteOrdering aByteOrdering, byte aHighPaddingBit, byte aLowPaddingBit, byte aInternalPaddingBit, ushort aSignLocation, ushort aBitOffset, ushort aBitPrecision, byte aExponentLocation, byte aExponentSize, byte aMantissaLocation, byte aMantissaSize, uint aExponentBias) : base(DatatypeClass.FloatingPoint, aSize) { if (!(aHighPaddingBit == 0 || aHighPaddingBit == 1)) { throw new ArgumentException( "Padding bits must be 0 or 1", nameof(aHighPaddingBit)); } if (!(aLowPaddingBit == 0 || aLowPaddingBit == 1)) { throw new ArgumentException( "Padding bits must be 0 or 1", nameof(aLowPaddingBit)); } if (!(aInternalPaddingBit == 0 || aInternalPaddingBit == 1)) { throw new ArgumentException( "Padding bits must be 0 or 1", nameof(aLowPaddingBit)); } ByteOrdering = aByteOrdering; HighPaddingBit = aHighPaddingBit; LowPaddingBit = aLowPaddingBit; InternalPaddingBit = aInternalPaddingBit; SignLocation = aSignLocation; BitOffset = aBitOffset; BitPrecision = aBitPrecision; ExponentLocation = aExponentLocation; ExponentSize = aExponentSize; MantissaLocation = aMantissaLocation; MantissaSize = aMantissaSize; ExponentBias = aExponentBias; }
internal static Datatype ReadMessage( DatatypeHeader aHeader, Hdf5Reader aReader, long?aLocalMessageSize, out long aBytes) { if (aHeader.Class != DatatypeClass.FixedPoint) { throw new ArgumentException( $"aHeader must be for type {nameof(DatatypeClass.FixedPoint)}"); } if (aLocalMessageSize.HasValue && aLocalMessageSize < mcAddionalSize) { throw new ArgumentException("Specified Local Message Size not long enough"); } Flags fFlags = (Flags)aHeader.Flags; ByteOrdering fByteOrdering = fFlags.HasFlag(Flags.BigEndian) ? ByteOrdering.BigEndian : ByteOrdering.LittleEndian; byte fLowPad = (byte)(fFlags.HasFlag(Flags.LowPad) ? 1 : 0), fHighPad = (byte)(fFlags.HasFlag(Flags.HighPad) ? 1 : 0); bool fIsSigned = fFlags.HasFlag(Flags.Signed); ushort fBitOffset = aReader.ReadUInt16(), fBitPrecision = aReader.ReadUInt16(); aBytes = mcAddionalSize; return(new FixedPointDataType( aHeader, fByteOrdering, fHighPad, fLowPad, fIsSigned, fBitOffset, fBitPrecision)); }
private BitFieldDataType( IDatatypeHeader aHeader, ByteOrdering aByteOrdering, byte aHighPaddingBit, byte aLowPaddingBit, ushort aBitOffset, ushort aBitPrecision) : this( aHeader.Size, aByteOrdering, aHighPaddingBit, aLowPaddingBit, aBitOffset, aBitPrecision) { if (aHeader.Class != DatatypeClass.BitField) { throw new ArgumentException( $"Header Class must be {nameof(DatatypeClass.BitField)}", nameof(aHeader)); } }
private FixedPointDataType( IDatatypeHeader aHeader, ByteOrdering aByteOrdering, byte aHighPaddingBit, byte aLowPaddingBit, bool aIsSigned, ushort aBitOffset, ushort aBitPrecision) : this( aHeader.Size, aByteOrdering, aHighPaddingBit, aLowPaddingBit, aIsSigned, aBitOffset, aBitPrecision) { if (aHeader.Class != DatatypeClass.FixedPoint) { throw new ArgumentException( $"Header Class must be {nameof(DatatypeClass.FixedPoint)}", nameof(aHeader)); } }
public void Test_Basic_Parsing( [Values] uMessages.DatatypeVersion aVersion, [Values(ByteOrdering.BigEndian, ByteOrdering.LittleEndian)] ByteOrdering aByteOrdering, [Values(0, 1)] byte aLowPaddingBit, [Values(0, 1)] byte aHighPaddingBit, [Values] bool aIsSigned, [Values((uint)0, (uint)0xffffffff), Random(1)] uint aSize, [Values((ushort)0, (ushort)0xffff), Random(1)] ushort aBitOffset, [Values((ushort)0, (ushort)0xffff), Random(1)] ushort aBitPrecision) { //Arrange using (Stream fTestSource = new MemoryStream()) using (BinaryWriter fWriter = new BinaryWriter(fTestSource)) { fWriter.Write((byte)((byte)aVersion << 4)); fWriter.Write((byte)( (aByteOrdering == ByteOrdering.BigEndian ? 1 : 0) | (aLowPaddingBit << 1) | (aHighPaddingBit << 2) | (aIsSigned ? 0x8 : 0))); fWriter.Write((byte)0); fWriter.Write((byte)0); fWriter.Write(aSize); fWriter.Write(aBitOffset); fWriter.Write(aBitPrecision); fTestSource.Seek(0, SeekOrigin.Begin); Moq.Mock <ndf5.Metadata.ISuperBlock> fSuperblock = new Moq.Mock <ndf5.Metadata.ISuperBlock>( Moq.MockBehavior.Loose); using (Hdf5Reader fReader = new Hdf5Reader( fTestSource, fSuperblock.Object)) { long fReadBytes; uTest fExpected = new uTest( aSize, aByteOrdering, aHighPaddingBit, aLowPaddingBit, aIsSigned, aBitOffset, aBitPrecision), fResult = ndf5.Messages.Message.Read( fReader, uMessages.MessageType.Datatype, uMessages.MessageAttributeFlag.None, null, out fReadBytes) as uTest; Assert.That( fResult, Is.Not.Null, "Incorrect Message Type returned"); Assert.That( fResult, Is.EqualTo(fExpected), "Equality check failed"); Assert.That( fResult, Is.Not.EqualTo(new uTest( aSize, aByteOrdering, aHighPaddingBit, aLowPaddingBit, aIsSigned, aBitOffset, (ushort)(aBitPrecision + 1))), "Inequality Check Failed"); Assert.That( fResult, Is.Not.EqualTo(null), "Null Inequality Check Failed"); Assert.That( fResult.GetHashCode(), Is.EqualTo(fExpected.GetHashCode()), "Hash Code Equality check failed"); Assert.That( fReadBytes, Is.EqualTo(12), "Wrong number of bytes read"); Assert.That( fTestSource.Position, Is.EqualTo(12), "Wrong number of bytes read"); Assert.That( fResult.Class, Is.EqualTo(uMessages.DatatypeClass.FixedPoint), "Incorrect Data class"); Assert.That( fResult.ByteOrdering, Is.EqualTo(aByteOrdering), "Incorrect byte ordering"); Assert.That( fResult.LowPaddingBit, Is.EqualTo(aLowPaddingBit), "Incorrect low bit padding"); Assert.That( fResult.HighPaddingBit, Is.EqualTo(aHighPaddingBit), "Incorrect high bit padding"); Assert.That( fResult.IsSigned, Is.EqualTo(aIsSigned), "Incorrect value for IsSigned"); Assert.That( fResult.Size, Is.EqualTo(aSize), "Incorrect Data Element Size"); Assert.That( fResult.BitOffset, Is.EqualTo(aBitOffset), "Incorrect bit offset"); Assert.That( fResult.BitPrecision, Is.EqualTo(aBitPrecision), "Incorrect bit precision"); } } }
/// <summary> /// Appends an array of bytes with specified endianness. /// </summary> /// <param name="bytes">The bytes to append.</param> /// <param name="byteOrdering">A value that specifies the byte ordering (endianness) of the array.</param> public void AppendBytes(byte[] bytes, ByteOrdering byteOrdering) { if (byteOrdering != ByteOrdering.BigEndian && byteOrdering != ByteOrdering.LittleEndian) { throw new ArgumentException("Only big-endian and little-endian byte ordering is supported.", nameof(byteOrdering)); } foreach (var @byte in (byteOrdering == ByteOrdering.LittleEndian ? bytes : bytes.Reverse())) { AppendByte(@byte); } }