public void TestSerialization()
        {
            SimplePofContext cxt = new SimplePofContext();

            cxt.RegisterUserType(1001, typeof(PersonV1), new PofAnnotationSerializer(1001, typeof(PersonV1)));
            cxt.RegisterUserType(1002, typeof(Child), new PofAnnotationSerializer(1002, typeof(Child), true));

            PersonV1 value = new PersonV1("Frank", "Spencer", 57);
            Child    child = new Child("Betty", "Spencer", 55);

            Binary binValue = EH.ToBinary(child, cxt);
            var    reader   = new DataReader(binValue.GetStream());
            int    typeId   = reader.ReadByte() == 21 ? reader.ReadPackedInt32() : -1;

            PersonV1 teleported  = EH.FromBinary(SerializationHelper.ToBinary(value, cxt), cxt) as PersonV1;
            Child    teleported2 = EH.FromBinary(binValue, cxt) as Child;

            Assert.AreEqual(1002, typeId);
            Assert.IsNotNull(teleported);
            Assert.IsNotNull(teleported2);
            Assert.AreEqual("Frank", teleported.m_firstName);
            Assert.AreEqual("Spencer", teleported.m_lastName);
            Assert.AreEqual(57, teleported.m_age);
            Assert.AreEqual("Betty", teleported2.m_firstName);
            Assert.AreEqual("Spencer", teleported2.m_lastName);
            Assert.AreEqual(55, teleported2.m_age);
        }
        public void TestEvolvable()
        {
            var cxt1 = new SimplePofContext();
            var cxt2 = new SimplePofContext();

            cxt1.RegisterUserType(1001, typeof(PersonV1), new PofAnnotationSerializer(1001, typeof(PersonV1)));
            cxt2.RegisterUserType(1001, typeof(PersonV2), new PofAnnotationSerializer(1001, typeof(PersonV2)));

            var personV1 = new PersonV1("Frank", "Spencer", 57);
            // verify we can go forward 1 => 2
            var teleportedV2 = EH.FromBinary(EH.ToBinary(personV1, cxt1), cxt2) as PersonV2;

            // verify we can go back 2 => 1
            teleportedV2.m_fMale = true;
            var teleportedV1       = EH.FromBinary(EH.ToBinary(teleportedV2, cxt2), cxt1) as PersonV1;
            var teleportedV2FromV1 = EH.FromBinary(EH.ToBinary(teleportedV1, cxt1), cxt2) as PersonV2;

            // v1 => v2
            Assert.IsNotNull(teleportedV2);
            Assert.AreEqual("Frank", teleportedV2.m_firstName);
            Assert.AreEqual("Spencer", teleportedV2.m_lastName);
            Assert.AreEqual(57, teleportedV2.m_age);
            // v2 => v1
            Assert.IsNotNull(teleportedV1);
            Assert.IsNotNull(teleportedV1.FutureData);
            Assert.AreEqual("Frank", teleportedV1.m_firstName);
            Assert.AreEqual("Spencer", teleportedV1.m_lastName);
            Assert.AreEqual(57, teleportedV1.m_age);
            Assert.IsNotNull(teleportedV2FromV1);
            Assert.AreEqual("Frank", teleportedV2FromV1.m_firstName);
            Assert.AreEqual("Spencer", teleportedV2FromV1.m_lastName);
            Assert.AreEqual(57, teleportedV2FromV1.m_age);
            Assert.IsTrue(teleportedV2FromV1.m_fMale);
        }
        public void TestDecoration()
        {
            ISerializer serializer = new BinarySerializer();
            string      original   = "hello";
            Binary      bin        = SerializationHelper.ToBinary(original, serializer);

            Assert.IsFalse(SerializationHelper.IsIntDecorated(bin));
            bin = SerializationHelper.DecorateBinary(bin, 6);
            Assert.IsTrue(SerializationHelper.IsIntDecorated(bin));
            Assert.AreEqual(SerializationHelper.ExtractIntDecoration(bin), 6);

            // verify adding a decoration does not impact the deserialized Object
            string copy = (string) SerializationHelper.FromBinary(bin, serializer);
            Assert.AreEqual(original, copy);

            // handle empty Binary appropriately
            bin = new Binary();
            Assert.IsFalse(SerializationHelper.IsIntDecorated(bin));
            try
            {
                SerializationHelper.ExtractIntDecoration(bin);
                Assert.Fail();
            }
            catch (System.ArgumentException)
            {
            }
        }
        public void TestGenerics()
        {
            SimplePofContext cxt = new SimplePofContext();

            cxt.RegisterUserType(1001, typeof(PersonV1), new PofAnnotationSerializer(1001, typeof(PersonV1)));

            PersonV1 value = new PersonV1("Frank", "Spencer", 57);

            PersonV1 teleported = EH.FromBinary(EH.ToBinary(value, cxt), cxt) as PersonV1;

            Assert.IsNotNull(teleported);
            Assert.AreEqual("Frank", teleported.m_firstName);
            Assert.AreEqual("Spencer", teleported.m_lastName);
            Assert.AreEqual(57, teleported.m_age);
        }
        public void TestInheritance()
        {
            var cxt = new SimplePofContext();

            cxt.RegisterUserType(1001, typeof(PersonV1), new PofAnnotationSerializer(1001, typeof(PersonV1)));
            cxt.RegisterUserType(1005, typeof(BewilderedPerson), new PofAnnotationSerializer(1005, typeof(BewilderedPerson), true));

            var value      = new BewilderedPerson("Frank", "Spencer", 57, "dizzy");
            var teleported = EH.FromBinary(EH.ToBinary(value, cxt), cxt) as BewilderedPerson;

            Assert.IsNotNull(teleported);
            Assert.AreEqual("Frank", teleported.m_firstName);
            Assert.AreEqual("Spencer", teleported.m_lastName);
            Assert.AreEqual(57, teleported.m_age);
            Assert.AreEqual("dizzy", teleported.m_state);
        }
        public void TestToAndFromBinary()
        {
            ISerializer serializer = new BinarySerializer();
            string      original   = "hello";
            Binary      bin        = SerializationHelper.ToBinary(original, serializer);
            string      copy       = (string) SerializationHelper.FromBinary(bin, serializer);
            Assert.AreEqual(original, copy);

            var stream = new BinaryMemoryStream();
            serializer.Serialize(new DataWriter(stream), original);
            try
            {
                // throws IOException as type (fmt_ext) information is missing
                SerializationHelper.FromBinary(stream.ToBinary(), serializer);
                Assert.Fail();
            }
            catch (IOException)
            {
            }
        }
        public void TestAncestry()
        {
            SimplePofContext cxt = new SimplePofContext();

            cxt.RegisterUserType(1002, typeof(GrandFather), new PofAnnotationSerializer(1002, typeof(GrandFather), true));
            cxt.RegisterUserType(1003, typeof(Father), new PofAnnotationSerializer(1003, typeof(Father), true));
            cxt.RegisterUserType(1004, typeof(Child), new PofAnnotationSerializer(1004, typeof(Child), true));

            var son = new Child("Bart", "Simpson", 10);
            var dad = new Father("Homer", "Simpson", 50, son);
            var gf  = new GrandFather("Abe", "Simpson", 100, dad);

            var teleported = (GrandFather)EH.FromBinary(EH.ToBinary(gf, cxt), cxt);

            Assert.AreEqual("Abe", teleported.m_firstName);
            Assert.AreEqual("Simpson", teleported.m_lastName);
            Assert.AreEqual(100, teleported.m_age);
            Assert.AreEqual("Homer", teleported.m_father.m_firstName);
            Assert.AreEqual("Simpson", teleported.m_father.m_lastName);
            Assert.AreEqual(50, teleported.m_father.m_age);
            Assert.AreEqual("Bart", teleported.m_father.m_child.m_firstName);
            Assert.AreEqual("Simpson", teleported.m_father.m_child.m_lastName);
            Assert.AreEqual(10, teleported.m_father.m_child.m_age);
        }