Skip to content

Commit

Permalink
- refactor the method of restoring IEnumerable with Add method (zachs…
Browse files Browse the repository at this point in the history
…aw#31)

- support of nullable in collections and as properties (zachsaw#34, zachsaw#35, zachsaw#36)
  • Loading branch information
nikolaygekht committed Feb 12, 2021
1 parent 5b96b5c commit 577e1a5
Show file tree
Hide file tree
Showing 10 changed files with 591 additions and 160 deletions.
216 changes: 209 additions & 7 deletions src/Binaron.Serializer.Debug/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
Expand All @@ -25,6 +26,28 @@ public TestObject(int a)
}
}

class TestObject1
{
public int? A { get; set; }

public TestObject1()
{

}

public TestObject1(int? a)
{
A = a;
}
}

enum E1
{
E11,
E12,
E13
};

class Collector<T> : IEnumerable<T>
{
private List<T> mData = new List<T>();
Expand All @@ -41,18 +64,197 @@ class Collector<T> : IEnumerable<T>

public static void Main(string[] args)
{
Collector<TestObject> c = new Collector<TestObject>() { new TestObject(1), new TestObject(2) };
using (var ms1 = new MemoryStream())
if (false)
Benchmark();

if (false)
{
BinaronConvert.Serialize(c, ms1);
using (var ms2 = new MemoryStream(ms1.ToArray()))
Collector<TestObject> c = new Collector<TestObject>() { new TestObject(1), new TestObject(2) };
using (var ms1 = new MemoryStream())
{
var c2 = BinaronConvert.Deserialize<Collector<TestObject>>(ms2);
;
BinaronConvert.Serialize(c, ms1);
using (var ms2 = new MemoryStream(ms1.ToArray()))
{
var cr = BinaronConvert.Deserialize<Collector<TestObject>>(ms2);
;
}
}
}


if (true)
{
Collector<TestObject1> c = new Collector<TestObject1>() { new TestObject1(1), null, new TestObject1(null), new TestObject1(2) };
using (var ms1 = new MemoryStream())
{
BinaronConvert.Serialize(c, ms1);
using (var ms2 = new MemoryStream(ms1.ToArray()))
{
var cr = BinaronConvert.Deserialize<Collector<TestObject1>>(ms2);
;
}
}
}

if (false)
{
Collector<E1> c1 = new Collector<E1>() { E1.E11, E1.E12, E1.E13 };
using (var ms1 = new MemoryStream())
{
BinaronConvert.Serialize(c1, ms1);
using (var ms2 = new MemoryStream(ms1.ToArray()))
{
var cr1 = BinaronConvert.Deserialize<Collector<E1>>(ms2);
;
}
}
}

if (false)
{
List<TestObject> l = new List<TestObject>() { new TestObject(1), null, new TestObject(2) };
using (var ms1 = new MemoryStream())
{
BinaronConvert.Serialize(l, ms1);
using (var ms2 = new MemoryStream(ms1.ToArray()))
{
var l1 = BinaronConvert.Deserialize<List<TestObject>>(ms2);
;
}
}
}

if (false)
{
List<int?> c2 = new List<int?>() { 1, null, 2 };
using (var ms1 = new MemoryStream())
{
BinaronConvert.Serialize(c2, ms1);
using (var ms2 = new MemoryStream(ms1.ToArray()))
{
var cr2 = BinaronConvert.Deserialize<List<int?>>(ms2);
;
}
}
}

if (true)
{
Collector<int?> c2 = new Collector<int?>() { 1, null, 2 };
using (var ms1 = new MemoryStream())
{
BinaronConvert.Serialize(c2, ms1);
using (var ms2 = new MemoryStream(ms1.ToArray()))
{
var cr2 = BinaronConvert.Deserialize<Collector<int?>>(ms2);
;
}

using (var ms2 = new MemoryStream(ms1.ToArray()))
{
var cr2 = BinaronConvert.Deserialize(ms2);
;
}
}
}

if (false)
{
using (var ms1 = new MemoryStream())
{
BinaronConvert.Serialize<int?>(1, ms1);
using (var ms2 = new MemoryStream(ms1.ToArray()))
{
var cr2 = BinaronConvert.Deserialize<int?>(ms2);
;
}
}

using (var ms1 = new MemoryStream())
{
BinaronConvert.Serialize<int?>(null, ms1);
using (var ms2 = new MemoryStream(ms1.ToArray()))
{
var cr2 = BinaronConvert.Deserialize<int?>(ms2);
;
}
}
}




}

private static void Benchmark()
{
Stopwatch sw = new Stopwatch();

{
sw.Reset();
Collector<int> c = new Collector<int>();
for (int i = 0; i < 1000; i++)
c.Add(i);
sw.Start();
for (int i = 0; i < 1000; i++)
{
using (var ms1 = new MemoryStream())
{
BinaronConvert.Serialize(c, ms1);
using (var ms2 = new MemoryStream(ms1.ToArray()))
{
var c2 = BinaronConvert.Deserialize<Collector<int>>(ms2);
}
}
}
sw.Stop();
Console.WriteLine("IEnumerable/invoke {0}", sw.ElapsedMilliseconds);
}

{
sw.Reset();
Collector<short> c = new Collector<short>();
for (int i = 0; i < 1000; i++)
c.Add((short)i);
sw.Start();
for (int i = 0; i < 1000; i++)
{
using (var ms1 = new MemoryStream())
{
BinaronConvert.Serialize(c, ms1);
using (var ms2 = new MemoryStream(ms1.ToArray()))
{
var c2 = BinaronConvert.Deserialize<Collector<short>>(ms2);
}
}
}
sw.Stop();
Console.WriteLine("IEnumerable/reflection {0}", sw.ElapsedMilliseconds);
}

{
sw.Reset();
List<int> c = new List<int>();
for (int i = 0; i < 1000; i++)
c.Add(i);
sw.Start();
for (int i = 0; i < 1000; i++)
{
using (var ms1 = new MemoryStream())
{
BinaronConvert.Serialize(c, ms1);
using (var ms2 = new MemoryStream(ms1.ToArray()))
{
var c2 = BinaronConvert.Deserialize<List<int>>(ms2);
}
}
}
sw.Stop();
Console.WriteLine("List {0}", sw.ElapsedMilliseconds);
}




}
}
}
12 changes: 6 additions & 6 deletions src/Binaron.Serializer.Tests/CustomTestCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ namespace Binaron.Serializer.Tests
{
public class CustomTestCollection<T> : IEnumerable<T>
{
private List<T> mList = new List<T>();
private List<T> List = new List<T>();

public T this[int index] => mList[index];
public T this[int index] => List[index];

public int Count => mList.Count;
public int Count => List.Count;

public void Add(T value) => mList.Add(value);
public void Add(T value) => List.Add(value);


public IEnumerator<T> GetEnumerator() => mList.GetEnumerator();
public IEnumerator<T> GetEnumerator() => List.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => mList.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => List.GetEnumerator();

public class TestCollectionObject
{
Expand Down
77 changes: 66 additions & 11 deletions src/Binaron.Serializer.Tests/ListSerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,23 +283,20 @@ public void CustomCollectionIntTest()
Assert.AreEqual(4, dest[4]);
}

[Test]
public void CustomCollectionStringTest()
[TestCaseSource(typeof(AllTestCases), nameof(AllTestCases.TestCaseOfValues))]
public void CustomCollectionOfTypeTest<TSource>(TSource v)
{
CustomTestCollection<string> collection = new CustomTestCollection<string>()
CustomTestCollection<TSource> collection = new CustomTestCollection<TSource>()
{
"0", "1", "2", "3", "4"
v
};

var dest = Tester.TestRoundTrip<CustomTestCollection<string>>(collection);
Assert.AreEqual(5, dest.Count);
Assert.AreEqual("0", dest[0]);
Assert.AreEqual("1", dest[1]);
Assert.AreEqual("2", dest[2]);
Assert.AreEqual("3", dest[3]);
Assert.AreEqual("4", dest[4]);
var dest = Tester.TestRoundTrip<CustomTestCollection<TSource>>(collection);
Assert.AreEqual(1, dest.Count);
Assert.AreEqual(v, dest[0]);
}


[Test]
public void CustomCollectionObjectTest()
{
Expand All @@ -320,5 +317,63 @@ public void CustomCollectionObjectTest()
Assert.AreEqual(3, dest[3].A);
Assert.AreEqual(4, dest[4].A);
}

[Test]
public void TestListWithNullInside()
{
CustomTestCollection<CustomTestCollection<int>.TestCollectionObject> collection = new CustomTestCollection<CustomTestCollection<int>.TestCollectionObject>()
{
new CustomTestCollection<int>.TestCollectionObject(0),
new CustomTestCollection<int>.TestCollectionObject(1),
null,
new CustomTestCollection<int>.TestCollectionObject(3),
new CustomTestCollection<int>.TestCollectionObject(4),
};

var dest = Tester.TestRoundTrip(collection);
Assert.AreEqual(5, dest.Count);
Assert.AreEqual(0, dest[0].A);
Assert.AreEqual(1, dest[1].A);
Assert.AreEqual(null, dest[2]);
Assert.AreEqual(3, dest[3].A);
Assert.AreEqual(4, dest[4].A);
}

[Test]
public void TestListOfNullables1()
{
var collection = new CustomTestCollection<int?>() { 1, null, 2 };
using (var ms1 = new MemoryStream())
{
var so = new SerializerOptions();
BinaronConvert.Serialize(collection, ms1, so);
using (var ms2 = new MemoryStream(ms1.ToArray()))
{
var cr2 = BinaronConvert.Deserialize<CustomTestCollection<int?>>(ms2);
Assert.AreEqual(3, cr2.Count);
Assert.AreEqual(1, cr2[0]);
Assert.AreEqual(null, cr2[1]);
Assert.AreEqual(2, cr2[2]);
}
}
}
[Test]
public void TestListOfNullables2()
{
var collection = new List<int?>() { 1, null, 2 };
using (var ms1 = new MemoryStream())
{
var so = new SerializerOptions();
BinaronConvert.Serialize(collection, ms1, so);
using (var ms2 = new MemoryStream(ms1.ToArray()))
{
var cr2 = BinaronConvert.Deserialize<List<int?>>(ms2);
Assert.AreEqual(3, cr2.Count);
Assert.AreEqual(1, cr2[0]);
Assert.AreEqual(null, cr2[1]);
Assert.AreEqual(2, cr2[2]);
}
}
}
}
}
1 change: 1 addition & 0 deletions src/Binaron.Serializer.Tests/MemberGetSetterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ public int NoRead
set { }
}
}

}
}
Loading

0 comments on commit 577e1a5

Please sign in to comment.