Esempio n. 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;
            }

        }
Esempio n. 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);
        }
Esempio n. 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");
        }
Esempio n. 4
0
 public void CompiledTypeModelSerializeInt()
 {
     using (var stream = new MemoryStream())
     {
         _compiledTypeModel.Serialize(stream, 42);
     }
 }
Esempio n. 5
0
 void CheckBytes(TypeModel model, object obj, string expected, string message)
 {
     using(var ms = new MemoryStream())
     {
         model.Serialize(ms, obj);
         Assert.AreEqual(expected, Program.GetByteString(ms.ToArray()), message);
     }
 }
Esempio n. 6
0
    public static Stream SerializeInfo(CDataGCCStrike15_v2_MatchInfo matchInfo)
    {
        MemoryStream memoryStream = new MemoryStream();

        ProtoBuf.Meta.TypeModel model = (ProtoBuf.Meta.TypeModel)Activator.CreateInstance(Type.GetType("MyProtoModel, MyProtoModel"));
        model.Serialize(memoryStream, matchInfo);
        //Serializer.Serialize(memoryStream, matchInfo);
        memoryStream.Position = 0;
        return(memoryStream);
    }
Esempio n. 7
0
        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");
        }
Esempio n. 8
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());
     }
 }
Esempio n. 9
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);
     }
 }
 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);
 }
Esempio n. 11
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);
     }
 }
Esempio n. 12
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);
     }
 }
Esempio n. 13
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);
            }
        }
Esempio n. 14
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);
     }
 }
Esempio n. 15
0
 static public int Serialize(IntPtr l)
 {
     try {
         int argc = LuaDLL.lua_gettop(l);
         if (matchType(l, argc, 2, typeof(ProtoBuf.ProtoWriter), typeof(System.Object)))
         {
             ProtoBuf.Meta.TypeModel self = (ProtoBuf.Meta.TypeModel)checkSelf(l);
             ProtoBuf.ProtoWriter    a1;
             checkType(l, 2, out a1);
             System.Object a2;
             checkType(l, 3, out a2);
             self.Serialize(a1, a2);
             pushValue(l, true);
             return(1);
         }
         else if (matchType(l, argc, 2, typeof(System.IO.Stream), typeof(System.Object)))
         {
             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);
             self.Serialize(a1, a2);
             pushValue(l, true);
             return(1);
         }
         else if (argc == 4)
         {
             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);
             ProtoBuf.SerializationContext a3;
             checkType(l, 4, out a3);
             self.Serialize(a1, a2, a3);
             pushValue(l, true);
             return(1);
         }
         pushValue(l, false);
         LuaDLL.lua_pushstring(l, "No matched override function to call");
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Esempio n. 16
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));
        }
Esempio n. 17
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);
            }
        }
Esempio n. 18
0
        private byte[] GetItemBuffer(TItem current)
        {
            using (var ms = new MemoryStream())
            {
                _typeModel.Serialize(ms, current);
                ms.Seek(0, SeekOrigin.Begin);
                var result = new byte[ms.Length + 4];
                var read   = ms.Read(result, 4, result.Length - 4);
                if (read != result.Length - 4)
                {
                    throw new NotSupportedException("this should not happen");
                }

                var length = BitConverter.GetBytes(result.Length - 4);

                Buffer.BlockCopy(length, 0, result, 0, 4);

                return(result);
            }
        }
Esempio n. 19
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");
        }
Esempio n. 20
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);
            }
        }
Esempio n. 21
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);
     }
 }
Esempio n. 23
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();
            
        }