Example #1
0
        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");
                    }
                }
        }
Example #2
0
        internal static Datatype Read(
            Hdf5Reader aReader,
            long?aLocalMessageSize,
            out long aBytes)
        {
            if (aLocalMessageSize.HasValue && aLocalMessageSize.Value < DatatypeHeader.HeaderSize)
            {
                throw new ArgumentException("Specified Local Message Size not long enough");
            }
            DatatypeHeader
                fHeader = DatatypeHeader.Read(aReader);
            Datatype
                fMessage;
            long
                fAdditionalBytes;

            long?
                fBodySize = aLocalMessageSize.HasValue
                    ? (long?)aLocalMessageSize.Value - DatatypeHeader.HeaderSize
                    : null;

            switch (fHeader.Class)
            {
            case DatatypeClass.FixedPoint:
                fMessage = FixedPointDataType.ReadMessage(
                    fHeader,
                    aReader,
                    fBodySize,
                    out fAdditionalBytes);
                break;

            case DatatypeClass.FloatingPoint:
                fMessage = FloatingPointDataType.ReadMessage(
                    fHeader,
                    aReader,
                    fBodySize,
                    out fAdditionalBytes);
                break;

            case DatatypeClass.Time:
                fMessage = TimeDataType.ReadMessage(
                    fHeader,
                    aReader,
                    fBodySize,
                    out fAdditionalBytes);
                break;

            case DatatypeClass.String:
                fMessage = StringDataType.ReadMessage(
                    fHeader,
                    aReader,
                    fBodySize,
                    out fAdditionalBytes);
                break;

            case DatatypeClass.BitField:
                fMessage = BitFieldDataType.ReadMessage(
                    fHeader,
                    aReader,
                    fBodySize,
                    out fAdditionalBytes);
                break;

            case DatatypeClass.Opaque:
                fMessage = OpaqueDataType.ReadMessage(
                    fHeader,
                    aReader,
                    fBodySize,
                    out fAdditionalBytes);
                break;



            case DatatypeClass.Array:
                fMessage = ArrayDataType.ReadMessage(
                    fHeader,
                    aReader,
                    fBodySize,
                    out fAdditionalBytes);
                break;

            default:
                // We shoudl never git her, as header parsing should check
                // for known versions of the header
                throw new Exception("Unexpected Version Type");
            }
            aBytes = DatatypeHeader.HeaderSize + fAdditionalBytes;
            return(fMessage);
        }
Example #3
0
        public void Test_SixteenBit_Example()
        {
            //Arrange
            using (Stream fTestSource = new MemoryStream())
                using (BinaryWriter fWriter = new BinaryWriter(fTestSource))
                {
                    fWriter.Write(new byte[] {
                        0x10, 0x08, 0x00, 0x00,
                        0x02, 0x00, 0x00, 0x00,
                        0x00, 0x00, 0x10, 0x00,
                    });

                    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 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(
                            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(ByteOrdering.LittleEndian),
                            "Incorrect byte ordering");
                        Assert.That(
                            fResult.LowPaddingBit,
                            Is.EqualTo(0),
                            "Incorrect low bit padding");
                        Assert.That(
                            fResult.HighPaddingBit,
                            Is.EqualTo(0),
                            "Incorrect high bit padding");
                        Assert.That(
                            fResult.IsSigned,
                            Is.EqualTo(true),
                            "Incorrect value for IsSigned");
                        Assert.That(
                            fResult.Size,
                            Is.EqualTo(2),
                            "Incorrect Data Element Size");
                        Assert.That(
                            fResult.BitOffset,
                            Is.EqualTo(0),
                            "Incorrect bit offset");
                        Assert.That(
                            fResult.BitPrecision,
                            Is.EqualTo(16),
                            "Incorrect bit precision");
                    }
                }
        }
Example #4
0
        public static void Test_V2_Parse(
            [Random(2, Distinct = true)] int aSize,
            [Values(1, 2, 12, 37, 42)] int aDimCount)
        {
            uint
                fSize = (uint)aSize;

            using (Stream fTestSource = new MemoryStream())
                using (BinaryWriter fWriter = new BinaryWriter(fTestSource))
                {
                    fWriter.Write((byte)(((byte)DatatypeVersion.Version2 << 4) | 0x0A));
                    fWriter.Write((byte)0);
                    fWriter.Write((byte)0);
                    fWriter.Write((byte)0);

                    fWriter.Write(fSize);

                    fWriter.Write((byte)aDimCount);
                    fWriter.Write((byte)0);
                    fWriter.Write((byte)0);
                    fWriter.Write((byte)0);

                    System.Random
                        fRandom = new Random();
                    List <uint>
                    fDimensions = new List <uint>();
                    for (int fIndex = 0; fIndex < aDimCount; ++fIndex)
                    {
                        uint
                            fRnd = (uint)fRandom.Next();
                        fWriter.Write(fRnd);
                        fDimensions.Add(fRnd);
                    }
                    for (int fIndex = 0; fIndex < aDimCount; ++fIndex)
                    {
                        fWriter.Write((uint)fIndex);
                    }
                    //BaseType is a 16 bit fixed point
                    fWriter.Write(new byte[] {
                        0x10, 0x08, 0x00, 0x00,
                        0x02, 0x00, 0x00, 0x00,
                        0x00, 0x00, 0x10, 0x00,
                    });

                    ndf5.Messages.Datatype
                        fBaseType = new ndf5.Messages.FixedPointDataType(
                        2,
                        Objects.ByteOrdering.LittleEndian,
                        0,
                        0,
                        true,
                        0,
                        16),
                        fNonEqualBaseType = new ndf5.Messages.FixedPointDataType(
                        4,
                        Objects.ByteOrdering.LittleEndian,
                        0,
                        0,
                        true,
                        0,
                        32);

                    fTestSource.Seek(0L, 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(
                            fDimensions.ToArray(),
                            fBaseType,
                            fSize),
                            fResult = ndf5.Messages.Message.Read(
                            fReader,
                            MessageType.Datatype,
                            MessageAttributeFlag.None,
                            null,
                            out fReadBytes) as uTest;

                        Assert.That(
                            fResult,
                            Is.EqualTo(fExpected),
                            "Equality check failed");

                        Assert.That(
                            fResult.BaseType,
                            Is.EqualTo(fBaseType),
                            "Incorrect Base Type");

                        Assert.That(
                            fResult.DimensionSizes.ToArray(),
                            Is.EquivalentTo(fDimensions.ToArray()),
                            "Incorrect Dimensions");

                        Assert.That(
                            fResult.Size,
                            Is.EqualTo(fSize),
                            "Incorrect Size");

                        Assert.That(
                            fResult,
                            Is.Not.EqualTo(
                                new uTest(
                                    fDimensions.Concat(new uint[] { 1 }).ToArray(),
                                    fBaseType,
                                    fSize)),
                            "Inequality check failed (Dimensions)");

                        Assert.That(
                            fResult,
                            Is.Not.EqualTo(
                                new uTest(
                                    fDimensions.ToArray(),
                                    fNonEqualBaseType,
                                    fSize)),
                            "Inequality check failed (Base Type)");

                        Assert.That(
                            fResult,
                            Is.Not.EqualTo(
                                new uTest(
                                    fDimensions.ToArray(),
                                    fBaseType,
                                    fSize + 1)),
                            "Inequality check failed (Size)");
                    }
                }
        }