/// <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); } } } }
/// <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); } } } }
/// <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); } }
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); }
/// <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); }
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()); }
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()); }
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); }
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."); }
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."); }
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."); }
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"); }
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."); }
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."); }
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); }
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()); }
/// <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); }
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); }
/// <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); }
/// <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; }