Ejemplo n.º 1
0
        private static void TestHazImplicitMap(TypeModel model)
        {
            var obj = new HazImplicitMap
            {
                Lookup =
                {
                    { 123, "abc" },
                    { 456, "def" },
                }
            };
            var clone = (HazImplicitMap)model.DeepClone(obj);

            Assert.NotNull(clone);
            Assert.NotSame(obj, clone);
            Assert.NotSame(obj.Lookup, clone.Lookup);
            Assert.Equal(2, clone.Lookup.Count);
            Assert.True(clone.Lookup.TryGetValue(123, out var val));
            Assert.Equal("abc", val);
            Assert.True(clone.Lookup.TryGetValue(456, out val));
            Assert.Equal("def", val);
        }
        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.Equal(3, clone.Points.Count); //, caption);
            Assert.Equal(1, clone.Points[0].X);  //, caption);
            Assert.Equal(2, clone.Points[0].Y);  //, caption);
            Assert.Equal(3, clone.Points[1].X);  //, caption);
            Assert.Equal(4, clone.Points[1].Y);  //, caption);
            Assert.Equal(5, clone.Points[2].X);  //, caption);
            Assert.Equal(6, clone.Points[2].Y);  //, caption);
        }
Ejemplo n.º 3
0
        private void ExecSystemDrawing(TypeModel typeModel, string caption)
        {
            var obj = new Foo
            {
                Points = new List <System.Drawing.Point>
                {
                    new System.Drawing.Point(1, 2),
                    new System.Drawing.Point(3, 4),
                    new System.Drawing.Point(5, 6),
                }
            };
            var clone = (Foo)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);
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        static void CheckSuccessNonGeneric <T>(TypeModel model, T obj, string expectedHex)
            where T : class, IHazFoo
        {
            // via serialize/deserialize
            using var ms = new MemoryStream();
            model.Serialize(ms, (object)obj);
            ms.Position = 0;
            var clone = (T)model.Deserialize(ms, null, typeof(T));

            Assert.NotSame(obj, clone);
            Assert.IsType <T>(clone);
            Assert.Equal(obj.Foo, clone.Foo);
            var hex = BitConverter.ToString(ms.GetBuffer(), 0, (int)ms.Length);

            Assert.Equal(expectedHex, hex, ignoreCase: true);

            // via clone
            clone = (T)model.DeepClone((object)obj);
            Assert.NotSame(obj, clone);
            Assert.IsType <T>(clone);
            Assert.Equal(obj.Foo, clone.Foo);
        }
Ejemplo n.º 6
0
        private static void TestSortedDictionaryImpl <T>(TypeModel model, string caption)
            where T : class, IImmutableCollectionWrapper, new()
        {
            var dict = new Dictionary <int, string>
            {
                { 1, "a" },
                { 3, "c" },
                { 2, "b" }
            };
            var obj = new T {
                SortedDictionary = ImmutableSortedDictionary.Create <int, string>().AddRange(dict)
            };

            model.ForceSerializationDuringClone = true;
            var clone = model.DeepClone(obj);

            Assert.AreEqual(3, clone.SortedDictionary.Count, caption);
            Assert.AreEqual("a", clone.SortedDictionary[1], caption);
            Assert.AreEqual("b", clone.SortedDictionary[2], caption);
            Assert.AreEqual("c", clone.SortedDictionary[3], caption);
            AssertSequence(new[] { 1, 2, 3 }, clone.SortedDictionary.Keys, caption);
        }
Ejemplo n.º 7
0
        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.Equal(2, clone.Length);                //, caption + ":length");
            Assert.Equal(1, clone[0].Order);              //, caption + ":order");
            Assert.Equal(2, clone[1].Order);              //, caption + ":order");
            Assert.IsType <Foo>(clone[0].Value);          //, caption + ":type");
            Assert.IsType <Bar>(clone[1].Value);          //, caption + ":type");
            Assert.Equal(123, ((Foo)clone[0].Value).A);   //, caption + ":value");
            Assert.Equal("abc", ((Bar)clone[1].Value).B); //, caption + ":value");
        }
Ejemplo n.º 8
0
        private static void TestBoxedValueType(TypeModel model)
        {
            var obj = new Container
            {
                Value = new Point {
                    X = 42, Y = 9001
                }
            };

            var clone = model.DeepClone(obj);

            Assert.NotNull(clone);
            Assert.IsType <Container>(clone);

            var container = (Container)clone;

            Assert.NotNull(container.Value);
            Assert.IsType <Point>(container.Value);

            var point = (Point)container.Value;

            Assert.Equal(42, point.X);
            Assert.Equal(9001, point.Y);
        }
Ejemplo n.º 9
0
        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);
            }
        }
Ejemplo n.º 10
0
        static void TestEnumModel(TypeModel model)
        {
            var obj = model.GetSerializer <Foo>();

            Assert.NotNull(obj);

            Assert.True(obj.Features.GetCategory() == SerializerFeatures.CategoryScalar, "should be a scalar serializer; is " + obj.GetType().NormalizeName());

            using var ms = new MemoryStream();
            Assert.False(model.CanSerializeContractType(typeof(Foo)), "should not be a contract type");
            Assert.True(model.CanSerializeBasicType(typeof(Foo)), "should be a basic type");
            model.Serialize(ms, Foo.B);

            var hex = BitConverter.ToString(ms.GetBuffer(), 0, (int)ms.Length);

            Assert.Equal("08-01", hex);
            ms.Position = 0;
            var val = model.Deserialize <Foo>(ms);

            Assert.Equal(Foo.B, val);

            val = model.DeepClone(Foo.B);
            Assert.Equal(Foo.B, val);
        }
Ejemplo n.º 11
0
        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");
        }
Ejemplo n.º 12
0
 public TData DeepClone <TData>(TData data)
 {
     return((TData)model.DeepClone(data));
 }
Ejemplo n.º 13
0
        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.Equal(123, clone.Item1);
            Assert.Equal(2, clone.Item2.Length);
            Assert.Equal(1, clone.Item2[0].Item1);
            Assert.Equal(2, clone.Item2[0].Item2);
            Assert.Equal(3, clone.Item2[0].Item3);
            Assert.Equal(4, clone.Item2[0].Item4);
            Assert.Equal(5, clone.Item2[0].Item5);
            Assert.Equal(6, clone.Item2[0].Item6);
            Assert.Equal(7, clone.Item2[0].Item7);
            Assert.Equal(Tuple.Create(1F, 2F), clone.Item2[0].Rest.Item1.Single());
            Assert.Equal(9, clone.Item2[1].Item1);
            Assert.Equal(10, clone.Item2[1].Item2);
            Assert.Equal(11, clone.Item2[1].Item3);
            Assert.Equal(12, clone.Item2[1].Item4);
            Assert.Equal(13, clone.Item2[1].Item5);
            Assert.Equal(14, clone.Item2[1].Item6);
            Assert.Equal(15, clone.Item2[1].Item7);
            Assert.Equal(Tuple.Create(3F, 4F), clone.Item2[1].Rest.Item1.Single());
            Assert.True(clone.Item3);
        }
Ejemplo n.º 14
0
        public static RuntimeTypeModel BuildMeta()
        {
            RuntimeTypeModel model;

#if !FX11
            model = TypeModel.Create();
            model.Add(typeof(Order), false)
            .Add(1, "OrderID")
            .Add(2, "CustomerID")
            .Add(3, "EmployeeID")
            .Add(4, "OrderDate")
            .Add(5, "RequiredDate")
            .Add(6, "ShippedDate")
            .Add(7, "ShipVia")
            .Add(8, "Freight")
            .Add(9, "ShipName")
            .Add(10, "ShipAddress")
            .Add(11, "ShipCity")
            .Add(12, "ShipRegion")
            .Add(13, "ShipPostalCode")
            .Add(14, "ShipCountry");
            model.Add(typeof(Product), false)
            .Add(1, "ProductID")
            .Add(2, "ProductName")
            .Add(3, "SupplierID")
            .Add(4, "CategoryID")
            .Add(5, "QuantityPerUnit")
            .Add(6, "UnitPrice")
            .Add(7, "UnitsInStock")
            .Add(8, "UnitsOnOrder")
            .Add(9, "ReorderLevel")
            .Add(10, "Discontinued")
            .Add(11, "LastEditDate")
            .Add(12, "CreationDate");

            TypeModel      compiled = model.Compile();
            Type           type     = typeof(Product);
            PropertyInfo[] props    = type.GetProperties();

            Product prod = new Product();
            prod.ProductID       = 123;
            prod.ProductName     = "abc devil";
            prod.SupplierID      = 456;
            prod.CategoryID      = 13;
            prod.QuantityPerUnit = "1";
            prod.UnitPrice       = 12.99M;
            prod.UnitsInStock    = 96;
            prod.UnitsOnOrder    = 12;
            prod.ReorderLevel    = 30;
            prod.Discontinued    = false;
            prod.LastEditDate    = new DateTime(2009, 5, 7);
            prod.CreationDate    = new DateTime(2009, 1, 3);

            DumpObject("Original", props, prod);

            const int loop = 100000;
            Console.WriteLine("Iterations: " + loop);
            Stopwatch    watch;
            MemoryStream reuseDump = new MemoryStream(100 * 1024);
#if FX30
            System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(type);

            using (MemoryStream ms = new MemoryStream()) {
                dcs.WriteObject(ms, prod);
                Console.WriteLine("DataContractSerializer: {0} bytes", ms.Length);
            }

            watch = Stopwatch.StartNew();
            for (int i = 0; i < loop; i++)
            {
                reuseDump.SetLength(0);
                dcs.WriteObject(reuseDump, prod);
            }
            watch.Stop();
            Console.WriteLine("DataContractSerializer serialize: {0} ms", watch.ElapsedMilliseconds);
            watch = Stopwatch.StartNew();
            for (int i = 0; i < loop; i++)
            {
                reuseDump.Position = 0;
                dcs.ReadObject(reuseDump);
            }
            watch.Stop();
            Console.WriteLine("DataContractSerializer deserialize: {0} ms", watch.ElapsedMilliseconds);

            {
                reuseDump.Position = 0;
                Product p1 = (Product)dcs.ReadObject(reuseDump);
                DumpObject("DataContractSerializer", props, p1);
            }

            System.Runtime.Serialization.NetDataContractSerializer ndcs = new System.Runtime.Serialization.NetDataContractSerializer();

            using (MemoryStream ms = new MemoryStream()) {
                ndcs.Serialize(ms, prod);
                Console.WriteLine("NetDataContractSerializer: {0} bytes", ms.Length);
            }

            watch = Stopwatch.StartNew();
            for (int i = 0; i < loop; i++)
            {
                reuseDump.SetLength(0);
                ndcs.Serialize(reuseDump, prod);
            }
            watch.Stop();
            Console.WriteLine("NetDataContractSerializer serialize: {0} ms", watch.ElapsedMilliseconds);
            watch = Stopwatch.StartNew();
            for (int i = 0; i < loop; i++)
            {
                reuseDump.Position = 0;
                ndcs.Deserialize(reuseDump);
            }
            watch.Stop();
            Console.WriteLine("NetDataContractSerializer deserialize: {0} ms", watch.ElapsedMilliseconds);

            {
                reuseDump.Position = 0;
                Product p1 = (Product)ndcs.Deserialize(reuseDump);
                DumpObject("NetDataContractSerializer", props, p1);
            }
#endif

            using (MemoryStream ms = new MemoryStream())
            {
                compiled.Serialize(ms, prod);
#if COREFX
                ArraySegment <byte> tmp;
                if (!ms.TryGetBuffer(out tmp))
                {
                    throw new Exception("oops");
                }
                byte[] buffer = tmp.Array;
#else
                byte[] buffer = ms.GetBuffer();
#endif
                int len = (int)ms.Length;
                Console.WriteLine("protobuf-net v2: {0} bytes", len);
                for (int i = 0; i < len; i++)
                {
                    Console.Write(buffer[i].ToString("x2"));
                }
                Console.WriteLine();
            }
            watch = Stopwatch.StartNew();
            for (int i = 0; i < loop; i++)
            {
                reuseDump.SetLength(0);
                compiled.Serialize(reuseDump, prod);
            }
            watch.Stop();
            Console.WriteLine("protobuf-net v2 serialize: {0} ms", watch.ElapsedMilliseconds);

            watch = Stopwatch.StartNew();
            for (int i = 0; i < loop; i++)
            {
                reuseDump.Position = 0;
                compiled.Deserialize(reuseDump, null, type);
            }
            watch.Stop();

            Console.WriteLine("protobuf-net v2 deserialize: {0} ms", watch.ElapsedMilliseconds);
            {
                reuseDump.Position = 0;
                Product p1 = (Product)compiled.Deserialize(reuseDump, null, type);
                DumpObject("protobuf-net v2", props, p1);
            }


            // 080d 1203(616263) 207b
            // 3205(08b9601804)
            // 5000 6204(08cede01)

            // 00   08 = 1|000 = field 1, variant
            // 01   0d = 13

            // 02   12 = 10|010 = field 2, string
            // 03   03 = length 3
            // 04   616263 = "abc"

            // 07   20 = 100|000 = field 4, variant
            // 08   7B = 123

            // 09   32 = 110|010 = field 6, string
            // 10   05 = length 5
            // 11     08 = 1|000 = field 1, variant
            // 12     b960 (le) = 1100000:0111001 = 12345
            // 14     18 = 11|000 = field 3, variant
            // 15     04 = 4 (signScale = scale 2, +ve)

            // 16   50 = 1010|000 = field 10, variant
            // 17   00 = false

            // 18   62 = 1100|010 = field 12, string
            // 19   04 = length 4
            // 20    08 = 1|000 = field 1, variant
            // 21    cede01 = 1:1011110:1001110 = 28494 (days, signed) = 14247 = 03/01/2009

            Product clone = (Product)compiled.DeepClone(prod);
            Console.WriteLine(clone.CategoryID);
            Console.WriteLine(clone.ProductName);
            Console.WriteLine(clone.CreationDate);
            Console.WriteLine(clone.ProductID);
            Console.WriteLine(clone.UnitPrice);
#endif
            model = TypeModel.Create();
            model.Add(typeof(Customer), false)
            .Add(1, "Id")
            .Add(3, "Name")
#if !FX11
            .Add(5, "HowMuch")
            .Add(6, "HasValue")
#endif
            ;
            ;
            model.Add(typeof(CustomerStruct), false)
            .Add(1, "Id")
            .Add(3, "Name")
#if !FX11
            .Add(5, "HowMuch")
            .Add(6, "HasValue")
#endif
            ;
            return(model);
        }
Ejemplo n.º 15
0
 private static void TestIndividual(TypeModel model)
 {
     var value = E.V1;
     Assert.True(Program.CheckBytes(value, model, 0x08, 0x04));
     Assert.Equal(value, model.DeepClone(value));
 }
Ejemplo n.º 16
0
 protected override bool TryDeepClone(TypeModel model, ref object value)
 {
     value = model.DeepClone <T>(TypeHelper <T> .FromObject(value));
     return(true);
 }