示例#1
0
        /// <summary>
        /// Helps test binary arrays.
        /// </summary>
        /// <param name="values">The values.</param>
        /// <param name="nullable">If set to <c>true</c> [nullable].</param>
        public static void BinaryArrayHelper(List <byte[]> values, bool nullable)
        {
            System.IO.Stream stream = new SerializedStreamBuilder()
                                      .Array <byte>(values, nullable)
                                      .ToStream();

            BinaryReader reader = new BinaryReader(stream, DeserializationHelper.CannedVersion);

            List <byte[]> actual = (List <byte[]>)reader.ReadBinaryArray(string.Empty, nullable, 1);

            Assert.IsNotNull(actual);

            using (IEnumerator <byte[]> valuesEnumerator = values.GetEnumerator())
            {
                using (IEnumerator <byte[]> actualEnumerator = actual.GetEnumerator())
                {
                    Assert.AreEqual <int>(values.Count, actual.Count);

                    while (valuesEnumerator.MoveNext() && actualEnumerator.MoveNext())
                    {
                        TestHelper.AssertCollectionEquals <byte>(valuesEnumerator.Current, actualEnumerator.Current);
                    }
                }
            }
        }
示例#2
0
        /// <summary>
        /// Helps test 2D arrays.
        /// </summary>
        /// <typeparam name="T">Type of the underlying value stored in the array.</typeparam>
        /// <param name="values">The values.</param>
        /// <param name="nullable">If set to <c>true</c> [nullable].</param>
        public static void Array2DHelper <T>(List <List <T> > values, bool nullable)
        {
            System.IO.Stream stream = new SerializedStreamBuilder()
                                      .Array <T>(values, nullable)
                                      .ToStream();

            BinaryReader reader = new BinaryReader(stream, DeserializationHelper.CannedVersion);

            string methodName = DeserializationHelper.GetReaderMethodName(typeof(T));

            MethodInfo methodInfo = GetReaderMethodInfoFromName <T>(methodName, reader);

            List <List <T> > actual = (List <List <T> >)methodInfo.Invoke(reader, new object[] { string.Empty, nullable, 2U });

            Assert.IsNotNull(actual);

            using (IEnumerator <List <T> > valuesEnumerator = values.GetEnumerator())
            {
                using (IEnumerator <List <T> > actualEnumerator = actual.GetEnumerator())
                {
                    Assert.AreEqual <int>(values.Count, actual.Count);

                    while (valuesEnumerator.MoveNext() && actualEnumerator.MoveNext())
                    {
                        TestHelper.AssertCollectionEquals <T>(valuesEnumerator.Current, actualEnumerator.Current);
                    }
                }
            }
        }
示例#3
0
        /// <summary>
        /// Helps test empty arrays.
        /// </summary>
        /// <typeparam name="T">Type of the underlying value stored in the array.</typeparam>
        /// <param name="nullable">If set to <c>true</c> [nullable].</param>
        public static void EmptyArrayHelper <T>(bool nullable)
        {
            SerializedStreamBuilder builder = new SerializedStreamBuilder();

            if (nullable)
            {
                builder.NullableFlag(false);
            }

            System.IO.Stream stream = builder
                                      .Size(0)
                                      .ToStream();

            BinaryReader reader = new BinaryReader(stream, DeserializationHelper.CannedVersion);

            for (uint rank = 1; rank <= 4; rank++)
            {
                string methodName = DeserializationHelper.GetReaderMethodName(typeof(T));

                MethodInfo methodInfo = GetReaderMethodInfoFromName <T>(methodName, reader);

                ICollection obj = (ICollection)methodInfo.Invoke(reader, new object[] { string.Empty, nullable, rank });
                Assert.AreEqual <int>(obj.Count, 0);

                stream.Seek(0L, System.IO.SeekOrigin.Begin);
            }
        }
示例#4
0
        public void ReadObject1DArrayTest()
        {
            List <InnerClass> values = new List <InnerClass>()
            {
                new InnerClass()
                {
                    m_bool = true, m_double = 0.1, m_float = 150
                },
                new InnerClass()
                {
                    m_bool = false, m_double = 2.1, m_float = 150e5F
                },
            };

            SerializedStreamBuilder builder = new SerializedStreamBuilder()
                                              .Size((byte)2);

            foreach (InnerClass innerClass in values)
            {
                builder
                .Float(innerClass.m_float)
                .Double(innerClass.m_double)
                .Boolean(innerClass.m_bool);
            }

            BinaryReader reader = new BinaryReader(builder.ToStream(), DeserializationHelper.CannedVersion);

            List <InnerClass> actual = (List <InnerClass>)reader.ReadObjectArray <InnerClass>(string.Empty, false, 1);

            Assert.IsNotNull(actual);

            TestHelper.AssertCollectionEquals <InnerClass>(values, actual);
        }
示例#5
0
        /// <summary>
        /// Helps test 1D arrays.
        /// </summary>
        /// <typeparam name="T">Type of the underlying value stored in the array.</typeparam>
        /// <param name="values">The values.</param>
        /// <param name="nullable">If set to <c>true</c> [nullable].</param>
        public static void Array1DHelper <T>(List <T> values, bool nullable)
        {
            System.IO.Stream stream = new SerializedStreamBuilder()
                                      .Array <T>(values, nullable)
                                      .ToStream();

            BinaryReader reader = new BinaryReader(stream, DeserializationHelper.CannedVersion);

            string methodName = DeserializationHelper.GetReaderMethodName(typeof(T));

            MethodInfo methodInfo = GetReaderMethodInfoFromName <T>(methodName, reader);

            List <T> actual = (List <T>)methodInfo.Invoke(reader, new object[] { string.Empty, nullable, 1U });

            if (values != null)
            {
                Assert.IsNotNull(actual);
            }
            else
            {
                Assert.IsNull(actual);
            }

            TestHelper.AssertCollectionEquals <T>(values, actual);
        }
示例#6
0
        public void WriteObject1DArrayTest()
        {
            List <InnerClass> values = new List <InnerClass>()
            {
                new InnerClass()
                {
                    m_bool = true, m_double = 0.1, m_float = 150
                },
                new InnerClass()
                {
                    m_bool = false, m_double = 2.1, m_float = 150e5F
                },
            };

            SerializedStreamBuilder builder = new SerializedStreamBuilder()
                                              .Size((byte)2);

            foreach (InnerClass innerClass in values)
            {
                builder
                .Float(innerClass.m_float)
                .Double(innerClass.m_double)
                .Boolean(innerClass.m_bool);
            }

            System.IO.MemoryStream actual = new System.IO.MemoryStream();

            BinaryWriter writer = new BinaryWriter(actual, BinaryWriterTest.CannedVersion);

            writer.WriteObjectArray <InnerClass>(string.Empty, values, false, 1);

            TestHelper.AssertCollectionEquals <byte>(builder.ToArray(), actual.ToArray());
        }
示例#7
0
        public void VersionTest()
        {
            SchemaVersion expected = TestHelper.GetEexpectedVersion();

            SerializedStreamBuilder builder = new SerializedStreamBuilder();

            BinaryReader reader = new BinaryReader(builder.ToStream(), expected);

            Assert.AreEqual <short>(expected.getVersionNumber(), reader.Version.getVersionNumber());
        }
示例#8
0
        public void ReadTooLargeFloatTest()
        {
            System.IO.Stream stream = new SerializedStreamBuilder()
                                      .Double(double.MaxValue)
                                      .ToStream();

            BinaryReader reader = new BinaryReader(stream, DeserializationHelper.CannedVersion);

            float actual = reader.ReadFloat(string.Empty);

            Assert.AreEqual <float>(float.NaN, actual);
        }
示例#9
0
        public void ReadEmptyByteTest()
        {
            System.IO.Stream stream = new SerializedStreamBuilder()
                                      .ToStream();

            BinaryReader reader = new BinaryReader(stream, DeserializationHelper.CannedVersion);

            TestHelper.ExpectException <TeslaDeserializationException>(
                () =>
            {
                reader.ReadByte(string.Empty);
            },
                "The end of the stream is reached during deserialization.");
        }
示例#10
0
        public void ReadInvalidBinarySize()
        {
            System.IO.Stream stream = new SerializedStreamBuilder()
                                      .Int64(long.MaxValue)
                                      .ToStream();

            BinaryReader reader = new BinaryReader(stream, DeserializationHelper.CannedVersion);

            TestHelper.ExpectException <TeslaDeserializationException>(
                () =>
            {
                reader.ReadBinary(string.Empty, false);
            },
                "Invalid data was encountered in the stream. The stream is corrupted.");
        }
示例#11
0
        public void ReadTooLargeInt64Test()
        {
            System.IO.Stream stream = new SerializedStreamBuilder()
                                      .RawByte(0xFF, 10)
                                      .ToStream();

            BinaryReader reader = new BinaryReader(stream, DeserializationHelper.CannedVersion);

            TestHelper.ExpectException <TeslaDeserializationException>(
                () =>
            {
                reader.ReadInt64(string.Empty);
            },
                "Bad format integer was encounted. The data may be corrupted.");
        }
示例#12
0
        public void ReadZeroRankArray()
        {
            System.IO.Stream stream = new SerializedStreamBuilder()
                                      .Size(0)
                                      .ToStream();

            BinaryReader reader = new BinaryReader(stream, DeserializationHelper.CannedVersion);

            TestHelper.ExpectArgumentException(
                () =>
            {
                reader.ReadBinaryArray(string.Empty, false, 0);
            },
                "rank");
        }
示例#13
0
        public void ReadInvalidBoolTest()
        {
            System.IO.Stream stream = new SerializedStreamBuilder()
                                      .RawByte(0xFF)
                                      .ToStream();

            BinaryReader reader = new BinaryReader(stream, DeserializationHelper.CannedVersion);

            TestHelper.ExpectException <TeslaDeserializationException>(
                () =>
            {
                reader.ReadBoolean(string.Empty);
            },
                "Invalid boolean value: 255.");
        }
示例#14
0
        public void ReadTooLargeUInt16Test()
        {
            System.IO.Stream stream = new SerializedStreamBuilder()
                                      .Int64(long.MaxValue)
                                      .ToStream();

            BinaryReader reader = new BinaryReader(stream, DeserializationHelper.CannedVersion);

            TestHelper.ExpectException <TeslaDeserializationException>(
                () =>
            {
                reader.ReadUInt16(string.Empty);
            },
                "Bad format UInt16 value. The value was read is outside the range of the UInt16.");
        }
示例#15
0
        public void ReadBinaryTest()
        {
            // Test binary object of length 10:
            // { 0x01, 0x02, 0x32, 0x00, 0x23, 0x90, 0xFE, 0xA3, 0xB8, 0x99 }
            byte[] expected = new byte[] { 0x02, 0x02, 0x32, 0x00, 0x23, 0x90, 0xFE, 0xA3, 0xB8, 0x99 };

            System.IO.Stream stream = new SerializedStreamBuilder()
                                      .Binary(expected)
                                      .ToStream();

            BinaryReader reader = new BinaryReader(stream, DeserializationHelper.CannedVersion);

            byte[] actual = reader.ReadBinary(string.Empty, false);

            TestHelper.AssertCollectionEquals <byte>(expected, actual);
        }
示例#16
0
        public void WriteBinaryTest()
        {
            // Test binary object of length 10:
            // { 0x01, 0x02, 0x32, 0x00, 0x23, 0x90, 0xFE, 0xA3, 0xB8, 0x99 }
            byte[] binary = new byte[] { 0x01, 0x02, 0x32, 0x00, 0x23, 0x90, 0xFE, 0xA3, 0xB8, 0x99 };

            byte[] expected = new SerializedStreamBuilder()
                              .Binary(binary)
                              .ToArray();

            System.IO.MemoryStream actual = new System.IO.MemoryStream();

            BinaryWriter writer = new BinaryWriter(actual, BinaryWriterTest.CannedVersion);

            writer.Write(string.Empty, binary, false);

            TestHelper.AssertCollectionEquals <byte>(expected, actual.ToArray());
        }
示例#17
0
        /// <summary>
        /// Helps test n-dimensional arrays.
        /// </summary>
        /// <typeparam name="T">Type of the underlying value stored in the array.</typeparam>
        /// <param name="values">The values.</param>
        /// <param name="nullable">If set to <c>true</c> [nullable].</param>
        /// <param name="rank">The rank.</param>
        private static void ArrayHelper <T>(ICollection values, bool nullable, uint rank)
        {
            System.IO.MemoryStream output = new System.IO.MemoryStream();

            BinaryWriter writer = new BinaryWriter(output, BinaryWriterTest.CannedVersion);

            string methodName = BinaryWriterTest.GetWriterMethodName(typeof(T));

            MethodInfo methodInfo = GetWriterMethodInfoFromName <T>(methodName, writer);

            methodInfo.Invoke(writer, new object[] { string.Empty, values, nullable, rank });

            byte[] expected = new SerializedStreamBuilder()
                              .Array <T>(values, nullable)
                              .ToArray();

            TestHelper.AssertCollectionEquals <byte>(expected, output.ToArray());
        }
        public void ReadNullableStringTest()
        {
            const string Expected = "foo";

            System.IO.Stream stream = new SerializedStreamBuilder()
                                      .String(Expected, false)
                                      .String(null, true)
                                      .ToStream();

            BinaryReader reader = new BinaryReader(stream, DeserializationHelper.CannedVersion);

            string actual;

            actual = reader.ReadString(string.Empty, true);
            Assert.AreEqual <string>(Expected, actual);

            actual = reader.ReadString(string.Empty, true);
            Assert.IsNull(actual);
        }
示例#19
0
        public void ReadNullableBinaryTest()
        {
            byte[] expected = new byte[] { 0x12, 0x66, 0x3 };

            System.IO.Stream stream = new SerializedStreamBuilder()
                                      .NullableFlag(false)
                                      .Size((ulong)expected.Length)
                                      .RawBytes(expected)
                                      .NullableFlag(true)
                                      .ToStream();

            BinaryReader reader = new BinaryReader(stream, DeserializationHelper.CannedVersion);

            byte[] actual;

            actual = reader.ReadBinary(string.Empty, true);
            TestHelper.AssertCollectionEquals <byte>(expected, actual);

            actual = reader.ReadBinary(string.Empty, true);
            Assert.IsNull(actual);
        }
示例#20
0
        /// <summary>
        /// Helps test binary data.
        /// </summary>
        /// <param name="values">The values.</param>
        /// <param name="nullable">If set to <c>true</c> [nullable].</param>
        public static void BinaryHelper(byte[] values, bool nullable)
        {
            System.IO.Stream stream = new SerializedStreamBuilder()
                                      .Array <byte>(values, nullable)
                                      .ToStream();

            BinaryReader reader = new BinaryReader(stream, DeserializationHelper.CannedVersion);

            byte[] actual = reader.ReadBinary(string.Empty, nullable);

            if (values != null)
            {
                Assert.IsNotNull(actual);
            }
            else
            {
                Assert.IsNull(actual);
            }

            TestHelper.AssertCollectionEquals <byte>(values, actual);
        }
示例#21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SerializedStreamBuilder"/> class. This is
 /// the copy constructor used during building of the object.
 /// </summary>
 /// <param name="otherBuilder">The other builder.</param>
 private SerializedStreamBuilder(SerializedStreamBuilder otherBuilder)
 {
     this.stream = otherBuilder.stream;
 }