public void DeepClone_ClassWithFields(TypeModel model) { var classWithFields = new ClassWithFields( 12, "lalala", new Guid("e0947b41-437a-4bda-8a47-16451417f307"), new ItemClass() { Message = "hello" }, new List<int>() { 23, 45 }, EnumTest.Item2, 0.00000m, 1.2345678m); var clone = (ClassWithFields)model.DeepClone(classWithFields); Assert.AreEqual(classWithFields.Guid, clone.Guid); Assert.AreEqual(classWithFields.Integer, clone.Integer); Assert.AreEqual(classWithFields.ItemClass.Message, clone.ItemClass.Message); Assert.AreEqual(classWithFields.List.Count, clone.List.Count); Assert.AreEqual(classWithFields.List[0], clone.List[0]); Assert.AreEqual(classWithFields.List[1], clone.List[1]); Assert.AreEqual(classWithFields.S, clone.S); Assert.AreEqual(classWithFields.EnumTest, clone.EnumTest); Assert.AreEqual(classWithFields._dec1, clone._dec1); Assert.AreEqual(classWithFields._dec2, clone._dec2); }
public void DeepClone_FruitBag(TypeModel model) { var fruitBag = new FruitBag() { Apple = new Apple() { Age = 12, StickerMessage = "13", Taste = 14 }, Color = 15, Pear = new Pear() { Age = 16, StickerMessage = "17", Hello = 18, Taste = 19 } }; var cloneFruitBag = (FruitBag)model.DeepClone(fruitBag); Assert.AreEqual(fruitBag.Apple.Age, cloneFruitBag.Apple.Age); Assert.AreEqual(fruitBag.Apple.StickerMessage, cloneFruitBag.Apple.StickerMessage); Assert.AreEqual(fruitBag.Apple.Taste, cloneFruitBag.Apple.Taste); Assert.AreEqual(fruitBag.Color, cloneFruitBag.Color); Assert.AreEqual(fruitBag.Pear.Age, cloneFruitBag.Pear.Age); Assert.AreEqual(fruitBag.Pear.StickerMessage, cloneFruitBag.Pear.StickerMessage); Assert.AreEqual(fruitBag.Pear.Hello, cloneFruitBag.Pear.Hello); Assert.AreEqual(fruitBag.Pear.Taste, cloneFruitBag.Pear.Taste); }
public void DeepClone_DictionaryStringToObjectContainer(TypeModel model) { var dictionary = new Dictionary<string, YellowPage>() { { "A", new YellowPage() { Message = "Ahahaha" } }, { "B", new YellowPage() { Message = "Blahblah" } }, { "C", new YellowPage() { Message = "Caca" } }, }; var yellowPages = new YellowPages() { Collection = dictionary, RefLink = dictionary }; yellowPages.Collection.Add("D", new YellowPage() { Message = "Dannnnnnng!" }); var cloneYellowPages = (YellowPages)model.DeepClone(yellowPages); Assert.AreEqual(yellowPages.Collection.Count, yellowPages.RefLink.Count); foreach (var key in yellowPages.Collection.Keys) { Assert.AreEqual(yellowPages.Collection[key].Message, cloneYellowPages.Collection[key].Message); } cloneYellowPages.Collection.Add("E", new YellowPage() { Message = "Ello without H" }); Assert.AreEqual(cloneYellowPages.Collection.Count, cloneYellowPages.RefLink.Count); }
public void DeepClone_ClassWithFieldsRef_AsReference(TypeModel model) { var classWithFields = new ClassWithFieldsRef(); var item = new ItemClass() { Message = "Hi there!" }; classWithFields._item1 = item; classWithFields._item2 = item; var list = new List<int> { 2, 4, 5 }; classWithFields._list1 = list; classWithFields._list2 = list; var clone = (ClassWithFieldsRef)model.DeepClone(classWithFields); Assert.AreEqual(classWithFields._item1.Message, clone._item1.Message); Assert.AreEqual(classWithFields._item2.Message, clone._item2.Message); Assert.AreEqual(classWithFields._list1.Count, clone._list1.Count); for (int i = 0; i < classWithFields._list1.Count; i++) { Assert.AreEqual(classWithFields._list1[i], clone._list1[i]); } Assert.AreEqual(classWithFields._list2.Count, clone._list2.Count); for (int i = 0; i < classWithFields._list2.Count; i++) { Assert.AreEqual(classWithFields._list2[i], clone._list2[i]); } Assert.IsTrue(object.ReferenceEquals(classWithFields._item1, classWithFields._item2)); Assert.IsTrue(object.ReferenceEquals(classWithFields._list1, classWithFields._list2)); Assert.IsTrue(object.ReferenceEquals(clone._item1, clone._item2)); Assert.IsTrue(object.ReferenceEquals(clone._list1, clone._list2)); }
public void DeepClone_DictionaryOfClassRef_AsReference(TypeModel model) { var itemClass = new ItemClass() { Message = "hello"}; var dictionary = new DictionaryOfClass() { Collection = new Dictionary<int, ItemClass> { { 1, itemClass }, { 2, itemClass } } }; var clone = (DictionaryOfClass)model.DeepClone(dictionary); Assert.AreEqual(dictionary.Collection.Count, clone.Collection.Count); foreach (var key in dictionary.Collection.Keys) { Assert.AreEqual(dictionary.Collection[key].Message, clone.Collection[key].Message); } Assert.IsTrue(object.ReferenceEquals(dictionary.Collection[1], dictionary.Collection[2]), "Original reference failed"); Assert.IsTrue(object.ReferenceEquals(clone.Collection[1], clone.Collection[2]), "Clone reference not maintained"); }
public void DeepCloneMaintainReference(TypeModel model) { var collection = new List<ItemClass>() { new ItemClass() { Message = "Hello" } }; var itemClass = new ItemClass() { Message = "Haha" }; var obj = new ComplexTestOfMaintainedReference() { Collection1 = collection, Collection2 = collection, Item1 = itemClass, Item2 = itemClass, Primitive1 = 1, Primitive2 = 2, String1 = "Test" }; var clone = (ComplexTestOfMaintainedReference)model.DeepClone(obj); Assert.AreEqual(obj.Collection1.Count, clone.Collection1.Count); Assert.AreEqual(obj.Collection1[0].Message, clone.Collection1[0].Message); Assert.AreEqual(obj.Collection2.Count, clone.Collection2.Count); Assert.AreEqual(obj.Collection2[0].Message, clone.Collection2[0].Message); Assert.IsTrue(object.ReferenceEquals(clone.Collection1, clone.Collection2), "Collection reference"); Assert.AreEqual(obj.Item1.Message, clone.Item1.Message); Assert.AreEqual(obj.Item2.Message, clone.Item2.Message); Assert.IsTrue(object.ReferenceEquals(clone.Item1, clone.Item2), "Item reference"); Assert.AreEqual(obj.Primitive1, clone.Primitive1); Assert.AreEqual(obj.Primitive2, clone.Primitive2); Assert.AreEqual(obj.String1, clone.String1); }
void Check(TypeModel model) { var obj = Tuple.Create( 123, new[] { Tuple.Create(1, 2, 3, 4, 5, 6, 7, new List<Tuple<float, float>> { Tuple.Create(1F,2F) }), Tuple.Create(9, 10, 11, 12, 13, 14, 15, new List<Tuple<float, float>> { Tuple.Create(3F,4F) }) }, true); var clone = (Tuple<int, Tuple<int, int, int, int, int, int, int, Tuple<List<Tuple<float, float>>>>[], bool>)model.DeepClone(obj); Assert.AreEqual(123, clone.Item1); Assert.AreEqual(2, clone.Item2.Length); Assert.AreEqual(1, clone.Item2[0].Item1); Assert.AreEqual(2, clone.Item2[0].Item2); Assert.AreEqual(3, clone.Item2[0].Item3); Assert.AreEqual(4, clone.Item2[0].Item4); Assert.AreEqual(5, clone.Item2[0].Item5); Assert.AreEqual(6, clone.Item2[0].Item6); Assert.AreEqual(7, clone.Item2[0].Item7); Assert.AreEqual(Tuple.Create(1F,2F), clone.Item2[0].Rest.Item1.Single()); Assert.AreEqual(9, clone.Item2[1].Item1); Assert.AreEqual(10, clone.Item2[1].Item2); Assert.AreEqual(11, clone.Item2[1].Item3); Assert.AreEqual(12, clone.Item2[1].Item4); Assert.AreEqual(13, clone.Item2[1].Item5); Assert.AreEqual(14, clone.Item2[1].Item6); Assert.AreEqual(15, clone.Item2[1].Item7); Assert.AreEqual(Tuple.Create(3F, 4F), clone.Item2[1].Rest.Item1.Single()); Assert.AreEqual(true, clone.Item3); }
void CheckClone(TypeModel model, A original) { int sum = original.B.Data.Sum(x => x.Sum(b => (int)b)); var clone = (A)model.DeepClone(original); Assert.IsInstanceOfType(typeof(A), clone); Assert.IsInstanceOfType(typeof(B), clone.B); Assert.AreEqual(sum, clone.B.Data.Sum(x => x.Sum(b => (int)b))); }
public void DeepClone_NestedWithMultipleVisibility(TypeModel model) { var obj = new Level1("1", 2, "3", "4", "5", new DateTime(2012, 05, 04), new ItemClass() { Message = "7" }, 8, 9, 10); var clone = (Level1)model.DeepClone(obj); Assert.AreEqual(obj.ToString(), clone.ToString()); }
private static void TestMember(TypeModel model) { var value = new Foo {Bar = new E[] {E.V0, E.V1, E.V2}}; Assert.IsTrue(Program.CheckBytes(value, model, 0x18, 0x03, 0x18, 0x04, 0x18, 0x05)); var clone = (Foo) model.DeepClone(value); Assert.AreEqual("V0,V1,V2", string.Join(",", clone.Bar), "clone"); }
static void Execute(TypeModel model, string caption) { var orig = CreateData(); var clone = (CanHazConcurrent)model.DeepClone(orig); Assert.AreNotEqual(clone, orig); TestData(clone, caption); }
private void Test(TypeModel model, string p) { var obj = Foo.Create(123); int oldCount = Count; var clone = (Foo)model.DeepClone(obj); int newCount = Count; Assert.AreEqual(oldCount + 1, newCount); Assert.AreEqual(123, clone.X); }
public void DeepClone_NestedGenericLevel1(TypeModel model) { var obj = new NestedClassTestsWrapper() { NestedGenericLevel1 = new NestedGenericLevel1<IItemClass>(new ItemClass() { Message = "Hello !" }) }; var clone = (NestedClassTestsWrapper)model.DeepClone(obj); Assert.AreEqual(obj.NestedGenericLevel1.ToString(), clone.NestedGenericLevel1.ToString()); }
private static void Execute(TestUser obj, TypeModel model, string caption) { var ms = new MemoryStream(); model.Serialize(ms, obj); Assert.Greater(2, 0, caption + ": I always get this wrong"); Assert.Greater(ms.Length, 0, caption + ": Nothing was serialized"); var clone = (TestUser) model.DeepClone(obj); Assert.AreEqual(0, clone.uid, caption + ": uid wasn't zero"); Assert.IsTrue(clone.uidSpecified, caption + ": uid wasn't specified"); }
public void DeepClone_KeyValuePairAsField(TypeModel model) { var obj = new KeyValuePairWrapper() { Field = new KeyValuePair<int, ItemClass>(12, new ItemClass() { Message = "ABC" }) }; var clone = (KeyValuePairWrapper)model.DeepClone(obj); Assert.AreEqual(obj.Field.Key, clone.Field.Key); Assert.AreEqual(obj.Field.Value.Message, clone.Field.Value.Message); }
public void DeepClone_KeyValuePairWithNullItem(TypeModel model) { var obj = new KeyValuePairWrapper() { Property = new KeyValuePair<int, ItemClass>(12, null) }; var clone = (KeyValuePairWrapper)model.DeepClone(obj); Assert.AreEqual(obj.Field.Key, clone.Field.Key); Assert.IsNull(clone.Property.Value); }
public void DeepClone_NestedGenericLevelDictionaryBaseType(TypeModel model) { var dictionary = new Dictionary<int, DateTime>() { { 12, new DateTime(2033, 2, 5) }, { 45, new DateTime(2000, 1, 4) } }; var obj = new NestedClassTestsWrapper() { NestedGenericLevelDictionaryBaseType = new NestedGenericLevelDictionaryBaseType(dictionary) }; var clone = (NestedClassTestsWrapper)model.DeepClone(obj); Assert.AreEqual(obj.NestedGenericLevelDictionaryBaseType.ToString(), clone.NestedGenericLevelDictionaryBaseType.ToString()); }
public void DeepClone_Albert(TypeModel model) { var albert = new Albert { Age = 23, NbChildren = 20 }; var cloneAlbert = (Albert)model.DeepClone(albert); Assert.AreEqual(albert.Age, cloneAlbert.Age); Assert.AreEqual(albert.NbChildren, cloneAlbert.NbChildren); }
private void RunTestNonNull(TypeModel model, string caption) { NullableSequences l = new NullableSequences(); l.Int32List = new List<int?>(new int?[] { 2, 3 }); l.StringList = new List<string> {"a", "b", ""}; l.Int32Array = new int?[] {4, 5}; l.StringArray = new string[] { "c", "", "d" }; NullableSequences clone = (NullableSequences) model.DeepClone(l); Assert.AreEqual("2,3", string.Join(",", clone.Int32List), caption); Assert.IsTrue(clone.StringList.SequenceEqual(new[] { "a", "b", "" })); Assert.AreEqual("4,5", string.Join(",", clone.Int32Array), caption); Assert.IsTrue(clone.StringArray.SequenceEqual(new[] { "c", "", "d" })); }
static void Execute(TypeModel model, int size, int offset, int count, string caption) { byte[] data = new byte[size]; new Random(1234).NextBytes(data); var obj = new Foo { Data = new ArraySegment<byte>(data, offset, count) }; var clone = (Foo) model.DeepClone(obj); var seg2 = clone.Data; var data2 = seg2.Array; Assert.AreEqual(offset, seg2.Offset, caption); Assert.AreEqual(count, seg2.Count, caption); Assert.AreEqual(data.Length, data2.Length, caption); Assert.AreEqual(BitConverter.ToString(data), BitConverter.ToString(data2), caption); }
public void Execute(TypeModel model, string caption) { BinaryNode head = new BinaryNode(); BinaryNode node = head; // 13 is the magic limit that triggers recursion check for (int i = 0; i < 13; ++i) { node.Left = new BinaryNode(); node = (BinaryNode)node.Left; } var clone = (Node)model.DeepClone(head); Assert.AreEqual(head.Count(), clone.Count(), caption); }
public void DeepClone_KeyValuePairWithRef(TypeModel model) { var itemClass = new ItemClass() { Message = "ABC" }; var obj = new KeyValuePairWrapper() { Reference = new KeyValuePair<IItemClass, IItemClass>(itemClass, itemClass) }; var clone = (KeyValuePairWrapper)model.DeepClone(obj); Assert.AreEqual(obj.Reference.Key.Message, clone.Reference.Key.Message); Assert.IsTrue(object.ReferenceEquals(obj.Reference.Key, obj.Reference.Value), "Original reference failed"); Assert.IsTrue(object.ReferenceEquals(clone.Reference.Key, clone.Reference.Value), "Clone reference not maintained"); }
public void DeepClone_MultiLayerInterface(TypeModel model) { var cleaner = new Cleaner { OdorCode = 10, NbBubbles = 12, IsDirty = true }; var cloneFarter = (Cleaner)model.DeepClone(cleaner); Assert.AreEqual(cleaner.OdorCode, cloneFarter.OdorCode); Assert.AreEqual(cleaner.NbBubbles, cloneFarter.NbBubbles); Assert.AreEqual(cleaner.IsDirty, cloneFarter.IsDirty); }
public void DeepClone_MultiplePathInterface(TypeModel model) { var lolz = new DiamondProblem() { A = 1, B = 2, C = 3 }; var cloneLolz = (DiamondProblem)model.DeepClone(lolz); Assert.AreEqual(lolz.A, cloneLolz.A); Assert.AreEqual(lolz.B, cloneLolz.B); Assert.AreEqual(lolz.C, cloneLolz.C); }
public void RoundtripEmptyDictionaryShouldNotNullThem(TypeModel model, string scenario) { var orig = new Test(); Assert.IsNotNull(orig.dict, scenario); Assert.AreEqual(0, orig.dict.Count, scenario); Assert.IsNotNull(orig.dict2, scenario); Assert.AreEqual(0, orig.dict2.Count, scenario); var clone = (Test)model.DeepClone(orig); Assert.IsNotNull(clone.dict, scenario); Assert.AreEqual(0, clone.dict.Count, scenario); Assert.IsNotNull(clone.dict2, scenario); Assert.AreEqual(0, clone.dict2.Count, scenario); }
static public int DeepClone(IntPtr l) { try { ProtoBuf.Meta.TypeModel self = (ProtoBuf.Meta.TypeModel)checkSelf(l); System.Object a1; checkType(l, 2, out a1); var ret = self.DeepClone(a1); pushValue(l, true); pushValue(l, ret); return(2); } catch (Exception e) { return(error(l, e)); } }
private void Execute(TypeModel model, string caption) { var args = new[] { new ProtoObjectDTO { Order = 1, Value = new Foo { A = 123 }}, new ProtoObjectDTO { Order = 2, Value = new Bar { B = "abc" }}, }; var clone = (ProtoObjectDTO[])model.DeepClone(args); Assert.AreEqual(2, clone.Length, caption + ":length"); Assert.AreEqual(1, clone[0].Order, caption + ":order"); Assert.AreEqual(2, clone[1].Order, caption + ":order"); Assert.IsInstanceOfType(typeof(Foo), clone[0].Value, caption + ":type"); Assert.IsInstanceOfType(typeof(Bar), clone[1].Value, caption + ":type"); Assert.AreEqual(123, ((Foo)clone[0].Value).A, caption + ":value"); Assert.AreEqual("abc", ((Bar)clone[1].Value).B, caption + ":value"); }
static void TestGuids(TypeModel model, int count) { Random rand = new Random(); byte[] buffer = new byte[16]; Stopwatch watch = Stopwatch.StartNew(); for (int i = 0; i < count; i++) { rand.NextBytes(buffer); Data data = new Data { Value = new Guid(buffer), SomeTailData = (char)rand.Next(1, ushort.MaxValue) }; Data clone = (Data)model.DeepClone(data); Assert.IsNotNull(clone); Assert.AreEqual(data.Value, clone.Value); Assert.AreEqual(data.SomeTailData, clone.SomeTailData); } watch.Stop(); Trace.WriteLine(watch.ElapsedMilliseconds); }
private static void Execute(int count, TypeModel model, string caption) { const int InnerLoop = 1000; object lockObj = new object(); var average = 0d; var min = double.MaxValue; var max = double.MinValue; int complete = 0; model.DeepClone(Create()); // warm-up Parallel.For(0, count, i => { var classThree = Create(); var counter = Stopwatch.StartNew(); using (var ms = new MemoryStream()) { for (int j = 0; j < InnerLoop; j++) { ms.SetLength(0); model.Serialize(ms, classThree); ms.Position = 0; var des = model.Deserialize(ms, null, typeof(ClassThree)); var aaa = des; } counter.Stop(); } var elapsed = counter.Elapsed.TotalMilliseconds; double currentAverage; lock (lockObj) { complete++; average += elapsed; var oldMin = min; min = Math.Min(min, elapsed); max = Math.Max(max, elapsed); currentAverage = average / complete; if (min != oldMin || (complete % 500) == 0) { Trace.WriteLine(string.Format("{5}\tCycle {0}: {1:N2} ms - avg: {2:N2} ms - min: {3:N2} - max: {4:N2}", i, elapsed, currentAverage, min, max, caption)); } } }); Trace.WriteLine(string.Format("{5}\tComplete {0}: avg: {2:N2} ms - min: {3:N2} - max: {4:N2}", complete, 0, average / complete, min, max, caption)); }
private void ExecSystemWindows(TypeModel typeModel, string caption) { var obj = new Bar { Points = new List<System.Windows.Point> { new System.Windows.Point(1,2), new System.Windows.Point(3,4), new System.Windows.Point(5,6), } }; var clone = (Bar)typeModel.DeepClone(obj); Assert.AreEqual(3, clone.Points.Count, caption); Assert.AreEqual(1, clone.Points[0].X, caption); Assert.AreEqual(2, clone.Points[0].Y, caption); Assert.AreEqual(3, clone.Points[1].X, caption); Assert.AreEqual(4, clone.Points[1].Y, caption); Assert.AreEqual(5, clone.Points[2].X, caption); Assert.AreEqual(6, clone.Points[2].Y, caption); }
static void Test(TypeModel with, TypeModel without, string message) { var obj = new DodgyDefault { Value = false }; DodgyDefault c1 = (DodgyDefault)with.DeepClone(obj); Assert.IsTrue(c1.Value, message); DodgyDefault c2 = (DodgyDefault)without.DeepClone(obj); Assert.IsFalse(c2.Value, message); using (var ms = new MemoryStream()) { with.Serialize(ms, obj); Assert.AreEqual(0, ms.Length, message); } using (var ms = new MemoryStream()) { without.Serialize(ms, obj); Assert.AreEqual(2, ms.Length, message); } }