public void TestRoundTripBooleanArray() { for (var i = 0; i < 1000; i++) { var inputBool = new bool[i]; for (var j = 0; j < i; j++) { inputBool[j] = j % 2 == 0; } var stream = new MemoryStream(); var writer = new ObjectWriter(stream); writer.WriteValue(inputBool); writer.Dispose(); stream.Position = 0; var reader = new ObjectReader(stream); Assert.True(Enumerable.SequenceEqual(inputBool, (bool[])reader.ReadValue())); reader.Dispose(); } }
public void TestRoundTripPrimitiveArray() { var inputBool = new bool[] { true, false }; var inputByte = new byte[] { 1, 2, 3, 4, 5 }; var inputChar = new char[] { 'h', 'e', 'l', 'l', 'o' }; var inputDecimal = new decimal[] { 1.0M, 2.0M, 3.0M, 4.0M, 5.0M }; var inputDouble = new double[] { 1.0, 2.0, 3.0, 4.0, 5.0 }; var inputFloat = new float[] { 1.0F, 2.0F, 3.0F, 4.0F, 5.0F }; var inputInt = new int[] { -1, -2, -3, -4, -5 }; var inputLong = new long[] { 1, 2, 3, 4, 5 }; var inputSByte = new sbyte[] { -1, -2, -3, -4, -5 }; var inputShort = new short[] { -1, -2, -3, -4, -5 }; var inputUInt = new uint[] { 1, 2, 3, 4, 5 }; var inputULong = new ulong[] { 1, 2, 3, 4, 5 }; var inputUShort = new ushort[] { 1, 2, 3, 4, 5 }; var inputString = new string[] { "h", "e", "l", "l", "o" }; var stream = new MemoryStream(); var writer = new ObjectWriter(stream); writer.WriteValue(inputBool); writer.WriteValue(inputByte); writer.WriteValue(inputChar); writer.WriteValue(inputDecimal); writer.WriteValue(inputDouble); writer.WriteValue(inputFloat); writer.WriteValue(inputInt); writer.WriteValue(inputLong); writer.WriteValue(inputSByte); writer.WriteValue(inputShort); writer.WriteValue(inputUInt); writer.WriteValue(inputULong); writer.WriteValue(inputUShort); writer.WriteValue(inputString); writer.Dispose(); stream.Position = 0; var reader = new ObjectReader(stream); Assert.True(Enumerable.SequenceEqual(inputBool, (bool[])reader.ReadValue())); Assert.True(Enumerable.SequenceEqual(inputByte, (byte[])reader.ReadValue())); Assert.True(Enumerable.SequenceEqual(inputChar, (char[])reader.ReadValue())); Assert.True(Enumerable.SequenceEqual(inputDecimal, (decimal[])reader.ReadValue())); Assert.True(Enumerable.SequenceEqual(inputDouble, (double[])reader.ReadValue())); Assert.True(Enumerable.SequenceEqual(inputFloat, (float[])reader.ReadValue())); Assert.True(Enumerable.SequenceEqual(inputInt, (int[])reader.ReadValue())); Assert.True(Enumerable.SequenceEqual(inputLong, (long[])reader.ReadValue())); Assert.True(Enumerable.SequenceEqual(inputSByte, (sbyte[])reader.ReadValue())); Assert.True(Enumerable.SequenceEqual(inputShort, (short[])reader.ReadValue())); Assert.True(Enumerable.SequenceEqual(inputUInt, (uint[])reader.ReadValue())); Assert.True(Enumerable.SequenceEqual(inputULong, (ulong[])reader.ReadValue())); Assert.True(Enumerable.SequenceEqual(inputUShort, (ushort[])reader.ReadValue())); Assert.True(Enumerable.SequenceEqual(inputString, (string[])reader.ReadValue())); reader.Dispose(); }
public void TestRoundTripPrimitives() { var stream = new MemoryStream(); var writer = new ObjectWriter(stream); writer.WriteBoolean(true); writer.WriteBoolean(false); writer.WriteByte(Byte.MaxValue); writer.WriteSByte(SByte.MaxValue); writer.WriteInt16(Int16.MaxValue); writer.WriteInt32(Int32.MaxValue); writer.WriteInt32(Byte.MaxValue); writer.WriteInt32(Int16.MaxValue); writer.WriteInt64(Int64.MaxValue); writer.WriteUInt16(UInt16.MaxValue); writer.WriteUInt32(UInt32.MaxValue); writer.WriteUInt64(UInt64.MaxValue); writer.WriteDecimal(Decimal.MaxValue); writer.WriteDouble(Double.MaxValue); writer.WriteSingle(Single.MaxValue); writer.WriteChar('X'); writer.WriteString("YYY"); writer.WriteCompressedUInt(Byte.MaxValue >> 2); // 6 bits writer.WriteCompressedUInt(UInt16.MaxValue >> 2); // 14 bits writer.WriteCompressedUInt(UInt32.MaxValue >> 2); // 30 bits var dt = DateTime.Now; writer.WriteDateTime(dt); writer.Dispose(); stream.Position = 0; var reader = new ObjectReader(stream); Assert.Equal(true, reader.ReadBoolean()); Assert.Equal(false, reader.ReadBoolean()); Assert.Equal(Byte.MaxValue, reader.ReadByte()); Assert.Equal(SByte.MaxValue, reader.ReadSByte()); Assert.Equal(Int16.MaxValue, reader.ReadInt16()); Assert.Equal(Int32.MaxValue, reader.ReadInt32()); Assert.Equal(Byte.MaxValue, reader.ReadInt32()); Assert.Equal(Int16.MaxValue, reader.ReadInt32()); Assert.Equal(Int64.MaxValue, reader.ReadInt64()); Assert.Equal(UInt16.MaxValue, reader.ReadUInt16()); Assert.Equal(UInt32.MaxValue, reader.ReadUInt32()); Assert.Equal(UInt64.MaxValue, reader.ReadUInt64()); Assert.Equal(Decimal.MaxValue, reader.ReadDecimal()); Assert.Equal(Double.MaxValue, reader.ReadDouble()); Assert.Equal(Single.MaxValue, reader.ReadSingle()); Assert.Equal('X', reader.ReadChar()); Assert.Equal("YYY", reader.ReadString()); Assert.Equal((UInt32)(Byte.MaxValue >> 2), reader.ReadCompressedUInt()); Assert.Equal((UInt32)(UInt16.MaxValue >> 2), reader.ReadCompressedUInt()); Assert.Equal(UInt32.MaxValue >> 2, reader.ReadCompressedUInt()); Assert.Equal(dt, reader.ReadDateTime()); reader.Dispose(); }
private void TestRoundTripArray <T>(T[] values) { var stream = new MemoryStream(); var writer = new ObjectWriter(stream); writer.WriteValue(values); writer.Dispose(); stream.Position = 0; var reader = new ObjectReader(stream, binder: writer.Binder); var readValues = (T[])reader.ReadValue(); reader.Dispose(); Assert.Equal(true, values.SequenceEqual(readValues)); }
private void RoundTripString(string text) { var stream = new MemoryStream(); var writer = new ObjectWriter(stream); writer.WriteString(text); writer.Dispose(); stream.Position = 0; var reader = new ObjectReader(stream); var readText = reader.ReadString(); reader.Dispose(); Assert.Equal(text, readText); }
private void RoundTripCharacter(Char ch) { var stream = new MemoryStream(); var writer = new ObjectWriter(stream); writer.WriteChar(ch); writer.Dispose(); stream.Position = 0; var reader = new ObjectReader(stream); var readch = reader.ReadChar(); reader.Dispose(); Assert.Equal(ch, readch); }
private void TestRoundTripGraphCore(Node graph) { var stream = new MemoryStream(); var writer = new ObjectWriter(stream); writer.WriteValue(graph); writer.Dispose(); stream.Position = 0; var reader = new ObjectReader(stream, binder: writer.Binder); var newGraph = (Node)reader.ReadValue(); reader.Dispose(); Assert.NotNull(newGraph); Assert.Equal(true, graph.IsEquivalentTo(newGraph)); }
public void TestRoundTripSingletonClass() { var instance = SingletonClass.Singleton; var stream = new MemoryStream(); var writer = new ObjectWriter(stream); writer.WriteValue(instance); writer.Dispose(); stream.Position = 0; var reader = new ObjectReader(stream); var instance2 = (SingletonClass)reader.ReadValue(); reader.Dispose(); Assert.Same(instance, instance2); }
public void TestRoundTripFalseBooleanArray() { var inputBool = Enumerable.Repeat <bool>(false, 1000).ToArray(); var stream = new MemoryStream(); var writer = new ObjectWriter(stream); writer.WriteValue(inputBool); writer.Dispose(); stream.Position = 0; var reader = new ObjectReader(stream); Assert.True(Enumerable.SequenceEqual(inputBool, (bool[])reader.ReadValue())); reader.Dispose(); }
public void TestRoundTripRawSerializableObject() { var instance = new RawSerializableClass(123, "456"); var stream = new MemoryStream(); var writer = new ObjectWriter(stream); writer.WriteValue(instance); writer.Dispose(); stream.Position = 0; var reader = new ObjectReader(stream); var instance2 = (RawSerializableClass)reader.ReadValue(); reader.Dispose(); Assert.NotNull(instance2); Assert.Equal(instance.X, instance2.X); Assert.Equal(instance.Y, instance2.Y); }
public void TestRoundTripLocation() { var instance = new SpecialLocation(); var stream = new MemoryStream(); var writer = new ObjectWriter(stream); writer.WriteValue(instance); writer.Dispose(); stream.Position = 0; var reader = new ObjectReader(stream); var instance2 = (Location)reader.ReadValue(); reader.Dispose(); Assert.NotNull(instance2); Assert.Equal(instance2.GetType(), typeof(SerializedLocation)); Assert.Equal(instance.Kind, instance2.Kind); Assert.Equal(instance.IsInSource, instance2.IsInSource); Assert.Equal(instance.SourceSpan, instance2.SourceSpan); Assert.Equal(instance.Kind, instance2.Kind); }
public void TestRoundTripPrimitivesAsValues() { var stream = new MemoryStream(); var writer = new ObjectWriter(stream); writer.WriteValue(true); writer.WriteValue(false); writer.WriteValue(Byte.MaxValue); writer.WriteValue(SByte.MaxValue); writer.WriteValue(Int16.MaxValue); writer.WriteValue(Int32.MaxValue); writer.WriteValue((Int32)Byte.MaxValue); writer.WriteValue((Int32)Int16.MaxValue); writer.WriteValue(Int64.MaxValue); writer.WriteValue(UInt16.MaxValue); writer.WriteValue(UInt32.MaxValue); writer.WriteValue(UInt64.MaxValue); writer.WriteValue(Decimal.MaxValue); writer.WriteValue(Double.MaxValue); writer.WriteValue(Single.MaxValue); writer.WriteValue('X'); writer.WriteValue("YYY"); writer.WriteString("\uD800\uDC00"); // valid surrogate pair writer.WriteString("\uDC00\uD800"); // invalid surrogate pair writer.WriteString("\uD800"); // incomplete surrogate pair writer.WriteValue(null); writer.WriteValue(ConsoleColor.Cyan); writer.WriteValue(EByte.Value); writer.WriteValue(ESByte.Value); writer.WriteValue(EShort.Value); writer.WriteValue(EUShort.Value); writer.WriteValue(EInt.Value); writer.WriteValue(EUInt.Value); writer.WriteValue(ELong.Value); writer.WriteValue(EULong.Value); writer.WriteValue(typeof(object)); var date = DateTime.Now; writer.WriteValue(date); writer.Dispose(); stream.Position = 0; var reader = new ObjectReader(stream, binder: writer.Binder); Assert.Equal(true, (bool)reader.ReadValue()); Assert.Equal(false, (bool)reader.ReadValue()); Assert.Equal(Byte.MaxValue, (Byte)reader.ReadValue()); Assert.Equal(SByte.MaxValue, (SByte)reader.ReadValue()); Assert.Equal(Int16.MaxValue, (Int16)reader.ReadValue()); Assert.Equal(Int32.MaxValue, (Int32)reader.ReadValue()); Assert.Equal(Byte.MaxValue, (Int32)reader.ReadValue()); Assert.Equal(Int16.MaxValue, (Int32)reader.ReadValue()); Assert.Equal(Int64.MaxValue, (Int64)reader.ReadValue()); Assert.Equal(UInt16.MaxValue, (UInt16)reader.ReadValue()); Assert.Equal(UInt32.MaxValue, (UInt32)reader.ReadValue()); Assert.Equal(UInt64.MaxValue, (UInt64)reader.ReadValue()); Assert.Equal(Decimal.MaxValue, (Decimal)reader.ReadValue()); Assert.Equal(Double.MaxValue, (Double)reader.ReadValue()); Assert.Equal(Single.MaxValue, (Single)reader.ReadValue()); Assert.Equal('X', (Char)reader.ReadValue()); Assert.Equal("YYY", (String)reader.ReadValue()); Assert.Equal("\uD800\uDC00", (String)reader.ReadValue()); // valid surrogate pair Assert.Equal("\uDC00\uD800", (String)reader.ReadValue()); // invalid surrogate pair Assert.Equal("\uD800", (String)reader.ReadValue()); // incomplete surrogate pair Assert.Equal(null, reader.ReadValue()); Assert.Equal(ConsoleColor.Cyan, reader.ReadValue()); Assert.Equal(EByte.Value, reader.ReadValue()); Assert.Equal(ESByte.Value, reader.ReadValue()); Assert.Equal(EShort.Value, reader.ReadValue()); Assert.Equal(EUShort.Value, reader.ReadValue()); Assert.Equal(EInt.Value, reader.ReadValue()); Assert.Equal(EUInt.Value, reader.ReadValue()); Assert.Equal(ELong.Value, reader.ReadValue()); Assert.Equal(EULong.Value, reader.ReadValue()); Assert.Equal(typeof(object), (Type)reader.ReadValue()); Assert.Equal(date, (DateTime)reader.ReadValue()); reader.Dispose(); }