public async Task Test() { var resolver = new MockResolveProxyIds(); var serializer = new DefaultSerializer(); Assert.IsTrue(serializer.CanSerialize(typeof(DateTime))); var values = new[] { DateTime.MinValue, DateTime.Today, DateTime.Now, DateTime.UtcNow, DateTime.MaxValue }; foreach (var value in values) { var stream = new MemoryStream(); using (var writer = new BinaryWriter(stream, Encoding.UTF8, true)) { await serializer.Serialize(writer, typeof(DateTime), value, resolver).ConfigureAwait(false); } Console.WriteLine($"Size of {value}: {stream.Length}"); stream.Position = 0; using (var reader = new BinaryReader(stream, Encoding.UTF8, true)) { var deserializedValue = await serializer.Deserialize(reader, typeof(DateTime), resolver, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(value, deserializedValue); } } }
public async Task Test() { var resolver = new MockResolveProxyIds(); var serializer = new DefaultSerializer(); //single dimension arrays (primitive, nullable, structs, and objects) await test(serializer, resolver, new[] { int.MinValue, -1, 0, 1, int.MaxValue }).ConfigureAwait(false); await test(serializer, resolver, new int?[] { int.MinValue, -1, 0, null, 1, int.MaxValue }).ConfigureAwait(false); await test(serializer, resolver, new[] { DateTime.MinValue, DateTime.Today, DateTime.Now, DateTime.UtcNow, DateTime.MaxValue }).ConfigureAwait(false); await test(serializer, resolver, new object[] { int.MinValue, DateTime.UtcNow, Guid.NewGuid(), TimeSpan.MinValue }).ConfigureAwait(false); //jagged array await test(serializer, resolver, new[] { new[] { int.MinValue, int.MaxValue }, new[] { -1, 1 }, new[] { 0 } }).ConfigureAwait(false); //multi dimensional arrays await test(serializer, resolver, new[, ] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }).ConfigureAwait(false); //array with lower bounds var array = Array.CreateInstance(typeof(Guid), new [] { 1, 2 }, //1 columns, 2 rows new [] { 10, 11 }); //10 lower bound column, 11 lower bound row array.SetValue(Guid.NewGuid(), 10, 11); array.SetValue(Guid.NewGuid(), 10, 12); await test(serializer, resolver, array).ConfigureAwait(false); }
public async Task Test() { var resolver = new MockResolveProxyIds(); var serializer = new DefaultSerializer(); //boolean await test(serializer, resolver, true, false).ConfigureAwait(false); //char await test(serializer, resolver, char.MinValue, char.MaxValue).ConfigureAwait(false); //signed integer types await test <sbyte>(serializer, resolver, sbyte.MinValue, -1, 0, 1, sbyte.MaxValue).ConfigureAwait(false); await test(serializer, resolver, int.MinValue, -1, 0, 1, int.MaxValue).ConfigureAwait(false); await test(serializer, resolver, long.MinValue, -1, 0, 1, long.MaxValue).ConfigureAwait(false); await test <short>(serializer, resolver, short.MinValue, -1, 0, 1, short.MaxValue).ConfigureAwait(false); //unsigned integer types await test <byte>(serializer, resolver, byte.MinValue, 1, byte.MaxValue).ConfigureAwait(false); await test(serializer, resolver, uint.MinValue, (uint)1, uint.MaxValue).ConfigureAwait(false); await test(serializer, resolver, ulong.MinValue, (ulong)1, ulong.MaxValue).ConfigureAwait(false); await test <ushort>(serializer, resolver, ushort.MinValue, 1, ushort.MaxValue).ConfigureAwait(false); //float point types await test(serializer, resolver, decimal.MinValue, -1, 0, 1, decimal.MaxValue).ConfigureAwait(false); await test(serializer, resolver, double.MinValue, -1, 0, 1, double.MaxValue).ConfigureAwait(false); await test(serializer, resolver, float.MinValue, -1, 0, 1, float.MaxValue).ConfigureAwait(false); }
private async Task test <T>(DefaultSerializer serializer, MockResolveProxyIds resolver, params T[] values) { Assert.IsTrue(serializer.CanSerialize(typeof(T))); foreach (var value in values) { var stream = new MemoryStream(); using (var writer = new BinaryWriter(stream, Encoding.UTF8, true)) { await serializer.Serialize(writer, typeof(T), value, resolver).ConfigureAwait(false); } Console.WriteLine($"Size of {value}: {stream.Length}"); stream.Position = 0; using (var reader = new BinaryReader(stream, Encoding.UTF8, true)) { var deserializedValue = await serializer.Deserialize(reader, typeof(T), resolver, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(value, deserializedValue); } } }
public async Task Test() { var resolver = new MockResolveProxyIds(); var serializer = new DefaultSerializer(); foreach (var value in Enum.GetValues(typeof(SampleEnum)).Cast <SampleEnum>()) { await test(serializer, resolver, value).ConfigureAwait(false); } }
public async Task Test() { var resolver = new MockResolveProxyIds(); var serializer = new DefaultSerializer(); var stream = new MemoryStream(); var str = new string(new[] { char.MinValue, //standard ASCII 'A', 'Z', 'a', 'z', //extended ASCII 'À', 'Ï', //unicode 'Ā', 'ď', char.MaxValue }); using (var writer = new BinaryWriter(stream, Encoding.UTF8, true)) { await serializer.Serialize(writer, typeof(string), str, resolver).ConfigureAwait(false); } Console.WriteLine($"Size of \"{str}\": {stream.Length}"); stream.Position = 0; using (var reader = new BinaryReader(stream, Encoding.UTF8, true)) { var deserializedValue = await serializer.Deserialize(reader, typeof(string), resolver, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(deserializedValue, str); } }
private async Task test(DefaultSerializer serializer, MockResolveProxyIds resolver, object value) { var stream = new MemoryStream(); using (var writer = new BinaryWriter(stream, Encoding.UTF8, true)) { await serializer.Serialize(writer, typeof(object), value, resolver).ConfigureAwait(false); } Console.WriteLine($"Size of {value}: {stream.Length}"); stream.Position = 0; using (var reader = new BinaryReader(stream, Encoding.UTF8, true)) { var deserializedValue = await serializer.Deserialize(reader, typeof(object), resolver, CancellationToken.None).ConfigureAwait(false); Assert.AreEqual(deserializedValue, value); } }
private async Task test(DefaultSerializer serializer, MockResolveProxyIds resolver, Array value) { var arrayType = value.GetType(); Assert.IsTrue(serializer.CanSerialize(arrayType)); var stream = new MemoryStream(); using (var writer = new BinaryWriter(stream, Encoding.UTF8, true)) { await serializer.Serialize(writer, arrayType, value, resolver).ConfigureAwait(false); } Console.WriteLine($"Size of {value}: {stream.Length}"); stream.Position = 0; using (var reader = new BinaryReader(stream, Encoding.UTF8, true)) { var deserializedValue = await serializer.Deserialize(reader, arrayType, resolver, CancellationToken.None).ConfigureAwait(false) as Array; Assert.IsNotNull(deserializedValue); Assert.AreEqual(value.Rank, deserializedValue.Rank, "Ranks don't match."); for (var dimension = 0; dimension < value.Rank; dimension++) { Assert.AreEqual(value.GetLength(dimension), deserializedValue.GetLength(dimension), $"Lengths ({dimension}) don't match."); Assert.AreEqual(value.GetLowerBound(dimension), deserializedValue.GetLowerBound(dimension), $"Lower bounds ({dimension}) don't match."); Assert.AreEqual(value.GetUpperBound(dimension), deserializedValue.GetUpperBound(dimension), $"Upper bounds ({dimension}) don't match."); } var enumerator = value.GetEnumerator(); var deserializedEnumerator = deserializedValue.GetEnumerator(); while (enumerator.MoveNext()) { Assert.IsTrue(deserializedEnumerator.MoveNext()); Assert.AreEqual(enumerator.Current, deserializedEnumerator.Current); } Assert.IsFalse(deserializedEnumerator.MoveNext()); } }
public async Task Test() { var resolver = new MockResolveProxyIds(); var serializer = new DefaultSerializer(); var min = new object(); resolver.Instances[long.MinValue] = min; var zero = new object(); resolver.Instances[0] = zero; var max = new object(); resolver.Instances[long.MaxValue] = max; await test(serializer, resolver, min).ConfigureAwait(false); await test(serializer, resolver, zero).ConfigureAwait(false); await test(serializer, resolver, max).ConfigureAwait(false); }
public async Task Test() { var resolver = new MockResolveProxyIds(); var serializer = new DefaultSerializer(); var stream = new MemoryStream(); using (var writer = new BinaryWriter(stream, Encoding.UTF8, true)) { await serializer.Serialize(writer, typeof(object), null, resolver).ConfigureAwait(false); } Console.WriteLine($"Size: {stream.Length}"); stream.Position = 0; using (var reader = new BinaryReader(stream, Encoding.UTF8, true)) { var deserializedValue = await serializer.Deserialize(reader, typeof(object), resolver, CancellationToken.None).ConfigureAwait(false); Assert.IsNull(deserializedValue); } }
public async Task Test() { var resolver = new MockResolveProxyIds(); var serializer = new DefaultSerializer(); await test(serializer, resolver, typeof(DateTime)).ConfigureAwait(false); await test(serializer, resolver, typeof(DateTime?)).ConfigureAwait(false); await test(serializer, resolver, typeof(DateTime[])).ConfigureAwait(false); await test(serializer, resolver, typeof(Guid)).ConfigureAwait(false); await test(serializer, resolver, typeof(Guid?)).ConfigureAwait(false); await test(serializer, resolver, typeof(Guid[])).ConfigureAwait(false); await test(serializer, resolver, typeof(SampleClassWithÑƑ)).ConfigureAwait(false); await test(serializer, resolver, typeof(SampleClassWithÑƑ[])).ConfigureAwait(false); await test(serializer, resolver, typeof(TimeSpan)).ConfigureAwait(false); await test(serializer, resolver, typeof(TimeSpan?)).ConfigureAwait(false); await test(serializer, resolver, typeof(TimeSpan[])).ConfigureAwait(false); await test(serializer, resolver, typeof(Type)).ConfigureAwait(false); await test(serializer, resolver, typeof(Type[])).ConfigureAwait(false); await test(serializer, resolver, typeof(bool)).ConfigureAwait(false); await test(serializer, resolver, typeof(bool?)).ConfigureAwait(false); await test(serializer, resolver, typeof(bool[])).ConfigureAwait(false); await test(serializer, resolver, typeof(byte)).ConfigureAwait(false); await test(serializer, resolver, typeof(byte?)).ConfigureAwait(false); await test(serializer, resolver, typeof(byte[])).ConfigureAwait(false); await test(serializer, resolver, typeof(char)).ConfigureAwait(false); await test(serializer, resolver, typeof(char?)).ConfigureAwait(false); await test(serializer, resolver, typeof(char[])).ConfigureAwait(false); await test(serializer, resolver, typeof(decimal)).ConfigureAwait(false); await test(serializer, resolver, typeof(decimal?)).ConfigureAwait(false); await test(serializer, resolver, typeof(decimal[])).ConfigureAwait(false); await test(serializer, resolver, typeof(double)).ConfigureAwait(false); await test(serializer, resolver, typeof(double?)).ConfigureAwait(false); await test(serializer, resolver, typeof(double[])).ConfigureAwait(false); await test(serializer, resolver, typeof(float)).ConfigureAwait(false); await test(serializer, resolver, typeof(float?)).ConfigureAwait(false); await test(serializer, resolver, typeof(float[])).ConfigureAwait(false); await test(serializer, resolver, typeof(int)).ConfigureAwait(false); await test(serializer, resolver, typeof(int?)).ConfigureAwait(false); await test(serializer, resolver, typeof(int[])).ConfigureAwait(false); await test(serializer, resolver, typeof(long)).ConfigureAwait(false); await test(serializer, resolver, typeof(long?)).ConfigureAwait(false); await test(serializer, resolver, typeof(long[])).ConfigureAwait(false); await test(serializer, resolver, typeof(object)).ConfigureAwait(false); await test(serializer, resolver, typeof(object[])).ConfigureAwait(false); await test(serializer, resolver, typeof(sbyte)).ConfigureAwait(false); await test(serializer, resolver, typeof(sbyte?)).ConfigureAwait(false); await test(serializer, resolver, typeof(sbyte[])).ConfigureAwait(false); await test(serializer, resolver, typeof(short)).ConfigureAwait(false); await test(serializer, resolver, typeof(short?)).ConfigureAwait(false); await test(serializer, resolver, typeof(short[])).ConfigureAwait(false); await test(serializer, resolver, typeof(string)).ConfigureAwait(false); await test(serializer, resolver, typeof(string[])).ConfigureAwait(false); await test(serializer, resolver, typeof(uint)).ConfigureAwait(false); await test(serializer, resolver, typeof(uint?)).ConfigureAwait(false); await test(serializer, resolver, typeof(uint[])).ConfigureAwait(false); await test(serializer, resolver, typeof(ulong)).ConfigureAwait(false); await test(serializer, resolver, typeof(ulong?)).ConfigureAwait(false); await test(serializer, resolver, typeof(ulong[])).ConfigureAwait(false); await test(serializer, resolver, typeof(ushort)).ConfigureAwait(false); await test(serializer, resolver, typeof(ushort?)).ConfigureAwait(false); await test(serializer, resolver, typeof(ushort[])).ConfigureAwait(false); }