示例#1
1
        private void Check(TypeModel model, SerializationContext ctx, int magicNumber, string caption)
        {
            try
            {
                CanHazFactory orig = new CanHazFactory {Foo = 123, Bar = 456}, clone;
                using(var ms = new MemoryStream())
                {
                    model.Serialize(ms, orig, ctx);
                    ms.Position = 0;
                    clone = (CanHazFactory) model.Deserialize(ms, null, typeof(CanHazFactory), ctx);
                }

                Assert.AreNotSame(orig, clone);

                Assert.AreEqual(123, orig.Foo, caption);
                Assert.AreEqual(456, orig.Bar, caption);
                Assert.AreEqual(0, orig.MagicNumber, caption);

                Assert.AreEqual(123, clone.Foo, caption);
                Assert.AreEqual(456, clone.Bar, caption);
                Assert.AreEqual(magicNumber, clone.MagicNumber, caption);

            } catch
            {
                Debug.WriteLine(caption);
                throw;
            }

        }
示例#2
0
        void ExecuteTest(TypeModel model, string test)
        {
            A a = new A { flags = new List<string> { "abc", "def" } }, c;

            Assert.IsNotNull(a.flags.Count, test);
            Assert.AreEqual(2, a.flags.Count, test);
            Assert.AreEqual("abc", a.flags[0], test);
            Assert.AreEqual("def", a.flags[1], test);

            B b;
            using (var ms = new MemoryStream())
            {
                model.Serialize(ms, a);
                ms.Position = 0;
                b = (B)model.Deserialize(ms, null, typeof(B));
            }
            Assert.IsNotNull(b.flags.Count, test);
            Assert.AreEqual(2, b.flags.Count, test);
            Assert.AreEqual("abc", b.flags[0], test);
            Assert.AreEqual("def", b.flags[1], test);

            using (var ms = new MemoryStream())
            {
                model.Serialize(ms, b);
                ms.Position = 0;
                c = (A)model.Deserialize(ms, null, typeof(A));
            }
            Assert.IsNotNull(c.flags.Count, test);
            Assert.AreEqual(2, c.flags.Count, test);
            Assert.AreEqual("abc", c.flags[0], test);
            Assert.AreEqual("def", c.flags[1], test);
        }
示例#3
0
        private static void RunTestIssue103(int loop, TypeA typeA, TypeB typeB, TypeModel model, string caption)
        {
            // for JIT and preallocation
            MemoryStream ms = new MemoryStream();
            ms.SetLength(0);
            model.Serialize(ms, typeA);
            ms.Position = 0;
            model.Deserialize(ms, null, typeof(TypeA));

            Stopwatch typeASer = Stopwatch.StartNew();
            for (int i = 0; i < loop; i++)
            {
                ms.SetLength(0);
                model.Serialize(ms, typeA);
            }
            typeASer.Stop();
            Stopwatch typeADeser = Stopwatch.StartNew();
            for (int i = 0; i < loop; i++)
            {
                ms.Position = 0;
                model.Deserialize(ms, null, typeof(TypeA));
            }
            typeADeser.Stop();

            ms.SetLength(0);
            model.Serialize(ms, typeB);
            ms.Position = 0;
            TypeB clone = (TypeB)model.Deserialize(ms, null, typeof(TypeB));
            Assert.AreEqual(typeB.containedType.Count, clone.containedType.Count);

            Stopwatch typeBSer = Stopwatch.StartNew();
            for (int i = 0; i < loop; i++)
            {
                ms.SetLength(0);
                model.Serialize(ms, typeB);
            }
            typeBSer.Stop();
            Stopwatch typeBDeser = Stopwatch.StartNew();
            for (int i = 0; i < loop; i++)
            {
                ms.Position = 0;
                model.Deserialize(ms, null, typeof(TypeB));
            }
            typeBDeser.Stop();

            Console.WriteLine(caption + " A/ser\t" + (typeASer.ElapsedMilliseconds * 1000 / loop) + " μs/item");
            Console.WriteLine(caption + " A/deser\t" + (typeADeser.ElapsedMilliseconds * 1000 / loop) + " μs/item");
            Console.WriteLine(caption + " B/ser\t" + (typeBSer.ElapsedMilliseconds * 1000 / loop) + " μs/item");
            Console.WriteLine(caption + " B/deser\t" + (typeBDeser.ElapsedMilliseconds * 1000 / loop) + " μs/item");
        }
 private void Execute(TypeModel model, string caption)
 {
     var large = new LargeType { Foo = 1, Bar = "abc" };
     SmallType small;
     using(var ms = new MemoryStream())
     {
         model.Serialize(ms, large);
         ms.Position = 0;
         small = (SmallType) model.Deserialize(ms, null, typeof(SmallType));
     }
     Assert.IsNotNull(small, caption);
 }
示例#5
0
 public void Execute(TypeModel model, string caption)
 {
     var obj = new Foo { Status = Status.All };
     using(var ms = new MemoryStream())
     {
         model.Serialize(ms, obj);
         ms.Position = 0;
         Assert.AreEqual(3, ms.Length);
         var clone = (Foo)model.Deserialize(ms, null, typeof(Foo));
         Assert.AreEqual(Status.All, clone.Status);
     }
 }
示例#6
0
 private static void Test(TypeModel m)
 {
     var list = new List<object> { new A { Id = "Abracadabra" }, new B { Id = "Focuspocus" }, new A { Id = "Abracadabra" }, };
     using (var ms = new MemoryStream())
     {
         m.Serialize(ms, list);
         ms.Position = 0;
         var list2 = (List<object>)m.Deserialize(ms, null, typeof(List<object>));
         Debug.Assert(list.SequenceEqual(list2));
         File.WriteAllBytes(@"output.dump", ms.ToArray());
     }
 }
示例#7
0
    public static CDataGCCStrike15_v2_MatchInfo ReadInfoFile(string location)
    {
        CDataGCCStrike15_v2_MatchInfo matchInfoData;

        using (FileStream infoFileStream = new FileStream(location, FileMode.Open))
        {
            ProtoBuf.Meta.TypeModel model = (ProtoBuf.Meta.TypeModel)Activator.CreateInstance(Type.GetType("MyProtoModel, MyProtoModel"));
            matchInfoData = (CDataGCCStrike15_v2_MatchInfo)model.Deserialize(infoFileStream, null, typeof(CDataGCCStrike15_v2_MatchInfo));
            //matchInfoData = Serializer.Deserialize<CDataGCCStrike15_v2_MatchInfo>(infoFileStream);
        }

        return(matchInfoData);
    }
示例#8
0
 static void Test(TypeModel model, C c, string caption)
 {
     Assert.AreEqual(43, c.Unknown.N, "braindead");
     using (var ms = new MemoryStream())
     {
         model.Serialize(ms, c);
         Assert.Greater(1, 0, "args fail");
         Assert.Greater(ms.Length, 0, "Nothing written");
         ms.Position = 0;
         var c2 = (C)model.Deserialize(ms, null, typeof(C));
         Assert.AreEqual(c.Unknown.N, c2.Unknown.N, caption);
     }
 }
示例#9
0
 private static void TestGeneratedModel(TypeModel model, string message)
 {
     var a = new A_generated() { Age = 10, b = new B_generated { Balls = 23 } };
     using (var ms = new MemoryStream())
     {
         model.Serialize(ms, a);
         Assert.IsTrue(ms.ToArray().SequenceEqual(new byte[] { 08, 10, 82, 2, 16, 23 }), message);
         ms.Position = 0;
         var clone = (A_generated)model.Deserialize(ms, null, typeof(A_generated));
         Assert.AreEqual(10, clone.Age, message);
         Assert.AreEqual(23, clone.b.Balls, message);
     }
 }
示例#10
0
        void Execute_Vanilla(TypeModel model, string caption)
        {
            const Test original = Test.test2;
            using (MemoryStream ms = new MemoryStream())
            {
                model.Serialize(ms, original);
                ms.Position = 0;
                Assert.AreEqual("08-01", BitConverter.ToString(ms.GetBuffer(), 0, (int)ms.Length));
                Test obj;
                obj = (Test)model.Deserialize(ms, null, typeof(Test));

                Assert.AreEqual(original, obj);
            }
        }
示例#11
0
 public void Execute(TypeModel model, string caption)
 {
     try
     {
         var ms = new MemoryStream();
         model.Serialize(ms, new EncapsulatedOuter { X = 123, Inner = new EncapsulatedInner { Y = 456 } });
         ms.Position = 0;
         var obj = (InheritedChild)model.Deserialize(ms, null, typeof(InheritedBase));
         Assert.AreEqual(123, obj.X, caption);
         Assert.AreEqual(456, obj.Y, caption);
     }
     catch (Exception ex)
     {
         Assert.Fail(caption + ":" + ex.Message);
     }
 }
示例#12
0
        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));
        }
示例#13
0
        static void TestModel(TypeModel model, string caption)
        {
            var b = new B { A = new A(117) };
            ASurrogate.HackyFlags = 0;
            using (var ms = new MemoryStream())
            {
                model.Serialize(ms, b);
                Assert.AreEqual(12, ASurrogate.HackyFlags, caption);

                ms.Position = 0;
                ASurrogate.HackyFlags = 0;
                var b2 = (B)model.Deserialize(ms, null, typeof(B));
                Assert.AreEqual(3, ASurrogate.HackyFlags, caption);
                Assert.AreEqual(117, b2.A.X, caption);
            }
        }
示例#14
0
 static public int Deserialize(IntPtr l)
 {
     try {
         int argc = LuaDLL.lua_gettop(l);
         if (matchType(l, argc, 2, typeof(ProtoBuf.ProtoReader), typeof(System.Object), typeof(System.Type)))
         {
             ProtoBuf.Meta.TypeModel self = (ProtoBuf.Meta.TypeModel)checkSelf(l);
             ProtoBuf.ProtoReader    a1;
             checkType(l, 2, out a1);
             System.Object a2;
             checkType(l, 3, out a2);
             System.Type a3;
             checkType(l, 4, out a3);
             var ret = self.Deserialize(a1, a2, a3);
             pushValue(l, true);
             pushValue(l, ret);
             return(2);
         }
         else if (matchType(l, argc, 2, typeof(System.IO.Stream), typeof(System.Object), typeof(System.Type)))
         {
             ProtoBuf.Meta.TypeModel self = (ProtoBuf.Meta.TypeModel)checkSelf(l);
             System.IO.Stream        a1;
             checkType(l, 2, out a1);
             System.Object a2;
             checkType(l, 3, out a2);
             System.Type a3;
             checkType(l, 4, out a3);
             var ret = self.Deserialize(a1, a2, a3);
             pushValue(l, true);
             pushValue(l, ret);
             return(2);
         }
         else if (matchType(l, argc, 2, typeof(System.IO.Stream), typeof(System.Object), typeof(System.Type), typeof(int)))
         {
             ProtoBuf.Meta.TypeModel self = (ProtoBuf.Meta.TypeModel)checkSelf(l);
             System.IO.Stream        a1;
             checkType(l, 2, out a1);
             System.Object a2;
             checkType(l, 3, out a2);
             System.Type a3;
             checkType(l, 4, out a3);
             System.Int32 a4;
             checkType(l, 5, out a4);
             var ret = self.Deserialize(a1, a2, a3, a4);
             pushValue(l, true);
             pushValue(l, ret);
             return(2);
         }
         else if (matchType(l, argc, 2, typeof(System.IO.Stream), typeof(System.Object), typeof(System.Type), typeof(ProtoBuf.SerializationContext)))
         {
             ProtoBuf.Meta.TypeModel self = (ProtoBuf.Meta.TypeModel)checkSelf(l);
             System.IO.Stream        a1;
             checkType(l, 2, out a1);
             System.Object a2;
             checkType(l, 3, out a2);
             System.Type a3;
             checkType(l, 4, out a3);
             ProtoBuf.SerializationContext a4;
             checkType(l, 5, out a4);
             var ret = self.Deserialize(a1, a2, a3, a4);
             pushValue(l, true);
             pushValue(l, ret);
             return(2);
         }
         else if (argc == 6)
         {
             ProtoBuf.Meta.TypeModel self = (ProtoBuf.Meta.TypeModel)checkSelf(l);
             System.IO.Stream        a1;
             checkType(l, 2, out a1);
             System.Object a2;
             checkType(l, 3, out a2);
             System.Type a3;
             checkType(l, 4, out a3);
             System.Int32 a4;
             checkType(l, 5, out a4);
             ProtoBuf.SerializationContext a5;
             checkType(l, 6, out a5);
             var ret = self.Deserialize(a1, a2, a3, a4, a5);
             pushValue(l, true);
             pushValue(l, ret);
             return(2);
         }
         pushValue(l, false);
         LuaDLL.lua_pushstring(l, "No matched override function to call");
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
示例#15
0
        private static void CheckEntityDtoWithItems(TypeModel model, string message) {
            var entity = new EntityDTO() { Id = 1 };
            var healthComponent = new HealthDTO() { CurrentHealth = 100, Owner = entity, Name = "Health", Id = 2 };
            entity.Components.Add(healthComponent);
            var locationComponent = new PhysicalLocationDTO() { X = 1, Y = 2, Owner = entity, Name = "PhysicalLocation", Id = 3 };
            entity.Components.Add(locationComponent);

            MemoryStream memstream2 = new MemoryStream();
            model.Serialize(memstream2, entity);
            memstream2.Seek(0, SeekOrigin.Begin);
            var result2 = (EntityDTO)model.Deserialize(memstream2, null, typeof(EntityDTO));

            Assert.AreEqual(1, result2.Id, message + ":Id");
            Assert.AreEqual(2, result2.Components.Count, message + ":Count");
            // These two tests are lame and will not be used for long
            Assert.AreEqual(typeof(HealthDTO), result2.Components.First().GetType(), message + ":First");
            Assert.AreEqual(typeof(PhysicalLocationDTO), result2.Components.Last().GetType(), message + ":Last");
        }
示例#16
0
        private static void CheckEmptyEntityDto(TypeModel model, string message) {
            // Test 1 - simple case, EntityDTO only
            var memstream = new MemoryStream();
            model.Serialize(memstream, new EntityDTO() { Id = 1 });
            memstream.Seek(0, SeekOrigin.Begin);
            var result = (EntityDTO)model.Deserialize(memstream, null, typeof(EntityDTO));

            Assert.AreEqual(typeof(EntityDTO), result.GetType(), message + ":type");
            Assert.AreEqual(1, result.Id, message + ":Id");
            Assert.AreEqual(0, result.Components.Count, message + ":Count");
        }
 static PackedData RoundTrip(TypeModel model, PackedData orig, string scenario, out int length)
 {
     try
     {
         using (MemoryStream ms = new MemoryStream())
         {
             model.Serialize(ms, orig);
             length = (int)ms.Length;
             ms.Position = 0;
             return (PackedData)model.Deserialize(ms, null, typeof(PackedData));
         }
     }
     catch (Exception ex)
     {
         throw new InvalidOperationException(scenario + ": " + ex.Message, ex);
     }
 }
示例#18
0
        private static void WriteCustomer(TypeModel model, string caption, object obj)
        {
            WriteHeading(caption);
            byte[] blob;
            using (MemoryStream ms = new MemoryStream())
            {
                model.Serialize(ms, obj);
                blob = ms.ToArray();
            }
            foreach (byte b in blob)
            {
                Console.Write(b.ToString("x2"));
            }
            Console.WriteLine();
            
            using (MemoryStream ms = new MemoryStream(blob))
            {
                object clone = model.Deserialize(ms, null, obj.GetType());
                string oldS = Convert.ToString(obj), newS = Convert.ToString(clone);
                Console.WriteLine(oldS == newS ? ("match: " + newS) : ("delta" + oldS + " vs " + newS));

            }
            Console.WriteLine();
            
        }
示例#19
0
 private static void TestInheritanceModel(TypeModel model, string message)
 {
     using (var ms = new MemoryStream(new byte[] {08, 10, 82, 2, 16, 23}))
     {
         var clone = (A)model.Deserialize(ms, null, typeof(A));
         Assert.AreEqual(10, clone.Age, message);
         B b = (B) clone;
         Assert.AreEqual(23, b.Balls, message);
     }
 }