Example #1
0
        public void Test_Basic_Parsing(
            [Values] uMessages.DatatypeVersion
            aVersion,
            [Values(ByteOrdering.BigEndian, ByteOrdering.LittleEndian)]
            ByteOrdering aByteOrdering,
            [Values((uint)0, (uint)0xffffffff), Random(1)] uint
            aSize,
            [Values((uint)0, (uint)0xffffffff), Random(1)] uint
            aBitPrecision)
        {
            //Arrange
            using (Stream fTestSource = new MemoryStream())
                using (BinaryWriter fWriter = new BinaryWriter(fTestSource))
                {
                    fWriter.Write((byte)(((byte)aVersion << 4) | 0x02));
                    fWriter.Write((byte)(
                                      (aByteOrdering == ByteOrdering.BigEndian ? 1 : 0)));
                    fWriter.Write((byte)0);
                    fWriter.Write((byte)0);

                    fWriter.Write(aSize);
                    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,
                            aBitPrecision),
                            fResult = ndf5.Messages.Message.Read(
                            fReader,
                            uMessages.MessageType.Datatype,
                            uMessages.MessageAttributeFlag.None,
                            null,
                            out fReadBytes) as uTest;

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

                        Assert.That(
                            fResult,
                            Is.Not.EqualTo(new uTest(
                                               aSize,
                                               aByteOrdering,
                                               (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(
                            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.Time),
                            "Incorrect Data class");
                        Assert.That(
                            fResult.ByteOrdering,
                            Is.EqualTo(aByteOrdering),
                            "Incorrect byte ordering");
                        Assert.That(
                            fResult.Size,
                            Is.EqualTo(aSize),
                            "Incorrect Data Element Size");
                        Assert.That(
                            fResult.BitPrecision,
                            Is.EqualTo(aBitPrecision),
                            "Incorrect bit precision");
                    }
                }
        }
Example #2
0
        public void Test_Basic_Parse(
            [Values] uMessages.DatatypeVersion
            aVersion,
            [Values] StringPadding
            aPadding,
            [Values] StringEncoding
            aEncoding,
            [Values((uint)0, (uint)0xffffffff), Random(1)] uint
            aSize)
        {
            using (Stream fTestSource = new MemoryStream())
                using (BinaryWriter fWriter = new BinaryWriter(fTestSource))
                {
                    fWriter.Write((byte)(((byte)aVersion << 4) | 0x03));
                    fWriter.Write((byte)((byte)aPadding | (((byte)aEncoding << 4))));
                    fWriter.Write((byte)0);
                    fWriter.Write((byte)0);
                    fWriter.Write(aSize);

                    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, aPadding, aEncoding),
                            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,
                                               aPadding,
                                               aEncoding == StringEncoding.ASCII
                                ? StringEncoding.UTF8
                                : StringEncoding.ASCII)),
                            "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(8),
                            "Wrong number of bytes read");
                        Assert.That(
                            fTestSource.Position,
                            Is.EqualTo(8),
                            "Wrong number of bytes read");

                        Assert.That(
                            fResult.Class,
                            Is.EqualTo(uMessages.DatatypeClass.String),
                            "Incorrect Data class");

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

                        Assert.That(
                            fResult.StringPadding,
                            Is.EqualTo(aPadding),
                            "Incorrect StringPadding");

                        Assert.That(
                            fResult.StringEncoding,
                            Is.EqualTo(aEncoding),
                            "Incorrect StringEncoding");
                    }
                }
        }
Example #3
0
        public void Test_Basic_Parsing(
            [Values] uMessages.DatatypeVersion
            aVersion,
            [Values((uint)0, (uint)0xffffffff), Random(1)] uint
            aSize,
            [Values("", "a", "A", ".", "abc123", "!@#$1234%^&*()67890")] string
            aName)
        {
            using (Stream fTestSource = new MemoryStream())
                using (BinaryWriter fWriter = new BinaryWriter(fTestSource))
                {
                    byte[]
                    fTagBytes = Encoding.ASCII.GetBytes(aName);

                    int
                        fPadding = 8 - (fTagBytes.Length & 0x7),
                        fLength  = fTagBytes.Length + fPadding;

                    //Verifying this test
                    Assert.That(fLength % 8, Is.EqualTo(0));

                    fWriter.Write((byte)(((byte)aVersion << 4) | (byte)0x05));
                    fWriter.Write((byte)fLength);
                    fWriter.Write((byte)0);
                    fWriter.Write((byte)0);
                    fWriter.Write(aSize);
                    fWriter.Write(fTagBytes);
                    fWriter.Write(Enumerable.Repeat((byte)0, fPadding).ToArray());

                    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,
                            aName),
                            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,
                                               aName + "Fail")),
                            "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(fLength + 8),
                            "Wrong number of bytes read");
                        Assert.That(
                            fTestSource.Position,
                            Is.EqualTo(fLength + 8),
                            "Wrong number of bytes read");

                        Assert.That(
                            fReadBytes % 8,
                            Is.EqualTo(0),
                            "Read should be a mutiple of 8");

                        Assert.That(
                            fResult.Size,
                            Is.EqualTo(aSize),
                            "Incorrect Data Element Size");
                        Assert.That(
                            fResult.AsciiTag,
                            Is.EqualTo(aName),
                            "Incorrect Ascii Tag");
                    }
                }
        }