示例#1
0
        public void TestWriteGenericList()
        {
            var ctx = new SimplePofContext();

            ctx.RegisterUserType(101, typeof(PortablePersonLite), new PortableObjectSerializer(101));
            ctx.RegisterUserType(102, typeof(PortablePerson), new PortableObjectSerializer(102));
            ctx.RegisterUserType(103, typeof(EvolvablePortablePerson), new PortableObjectSerializer(103));

            //initPOFWriter();
            stream    = new MemoryStream();
            writer    = new DataWriter(stream);
            pofwriter = new PofStreamWriter(writer, ctx);

            ICollection <string> list1 = new List <string>();
            ICollection <object> list2 = new List <object>();

            list1.Add("A"); list1.Add(null); list1.Add("7");
            list2.Add("A"); list2.Add(null); list2.Add(7);

            ICollection <IPortableObject> persons  = new List <IPortableObject>();
            ICollection <PortablePerson>  persons2 = new List <PortablePerson>();
            var ivan = new PortablePerson("Ivan", new DateTime(1978, 4, 25));

            ivan.Children = null;
            var goran = new PortablePerson("Goran", new DateTime(1982, 3, 3));

            goran.Children = null;
            var aleks = new EvolvablePortablePerson("Aleks", new DateTime(1974, 8, 24));

            aleks.Children    = new EvolvablePortablePerson[1];
            aleks.Children[0] = new EvolvablePortablePerson("Ana Maria", new DateTime(2004, 8, 14));
            aleks.DataVersion = 2;

            persons.Add(ivan);
            persons.Add(aleks);
            persons.Add(goran);
            persons.Add(null);
            persons2.Add(ivan);
            persons2.Add(null);
            persons2.Add(goran);


            pofwriter.WriteCollection(0, list1);
            pofwriter.WriteCollection(0, list2);
            pofwriter.WriteCollection(0, persons);
            pofwriter.WriteCollection(0, persons);
            pofwriter.WriteCollection(0, (ICollection)persons2, typeof(PortablePerson));

            //initPOFReader();
            stream.Position = 0;
            reader          = new DataReader(stream);
            pofreader       = new PofStreamReader(reader, ctx);

            ICollection <string> result1 = new List <string>();

            pofreader.ReadCollection(0, result1);
            Assert.AreEqual(3, result1.Count);
            for (int i = 0; i < result1.Count; i++)
            {
                Assert.AreEqual(((List <string>)list1)[i], ((List <string>)result1)[i]);
            }

            ICollection <object> result2 = new List <object>();

            pofreader.ReadCollection(0, result2);
            Assert.AreEqual(3, result2.Count);
            for (int i = 0; i < result2.Count; i++)
            {
                Assert.AreEqual(((List <object>)list2)[i], ((List <object>)result2)[i]);
            }

            ICollection <IPortableObject> result3 = new List <IPortableObject>();

            pofreader.ReadCollection(0, result3);
            Assert.AreEqual(4, result3.Count);
            Assert.IsFalse(((List <IPortableObject>)result3)[0] is EvolvablePortablePerson);
            Assert.IsTrue(((List <IPortableObject>)result3)[1] is EvolvablePortablePerson);
            Assert.IsFalse(((List <IPortableObject>)result3)[2] is EvolvablePortablePerson);
            Assert.AreEqual(((List <IPortableObject>)result3)[3], null);
            EvolvablePortablePerson epp = (EvolvablePortablePerson)((List <IPortableObject>)result3)[1];

            Assert.AreEqual(aleks.Name, epp.Name);
            Assert.AreEqual(aleks.Children[0].Name, epp.Children[0].Name);

            PortablePerson pp = (PortablePerson)((List <IPortableObject>)result3)[0];

            Assert.AreEqual(ivan.Name, pp.Name);
            Assert.IsNull(pp.Children);

            List <IPortableObject> result4 = (List <IPortableObject>)pofreader.ReadCollection <IPortableObject>(0, null);

            Assert.AreEqual(4, result4.Count);
            Assert.IsFalse(result4[0] is EvolvablePortablePerson);
            Assert.IsTrue(result4[1] is EvolvablePortablePerson);
            Assert.IsFalse(result4[2] is EvolvablePortablePerson);
            Assert.AreEqual(result4[3], null);
            epp = (EvolvablePortablePerson)result4[1];
            Assert.AreEqual(aleks.Name, epp.Name);
            Assert.AreEqual(aleks.Children[0].Name, epp.Children[0].Name);

            pp = (PortablePerson)result4[0];
            Assert.AreEqual(ivan.Name, pp.Name);
            Assert.IsNull(pp.Children);

            List <PortablePerson> result5 = (List <PortablePerson>)pofreader.ReadCollection <PortablePerson>(0, null);

            for (int i = 0; i < persons2.Count; i++)
            {
                Assert.AreEqual(((List <PortablePerson>)persons2)[i], result5[i]);
            }
        }
        public void TestReferencesInArray()
        {
            var ctx = new SimplePofContext();

            ctx.RegisterUserType(101, typeof(PortablePerson),
                                 new PortableObjectSerializer(101));
            ctx.RegisterUserType(102, typeof(PortablePersonReference),
                                 new PortableObjectSerializer(102));

            var ivan     = new PortablePersonReference("Ivan", new DateTime(78, 4, 25));
            var goran    = new PortablePersonReference("Goran", new DateTime(82, 3, 3));
            var jack     = new PortablePersonReference("Jack", new DateTime(80, 5, 25));
            var jim      = new PortablePersonReference("Jim", new DateTime(80, 5, 25));
            var siblings = new PortablePersonReference[2];

            siblings[0] = jack;
            siblings[1] = jim;

            ivan.Children     = null;
            jack.Children     = null;
            jim.Children      = null;
            goran.Children    = new PortablePerson[2];
            goran.Children[0] = new PortablePerson("Tom", new DateTime(103, 7, 5));
            goran.Children[1] = new PortablePerson("Ellen", new DateTime(105, 3, 15));
            ivan.Siblings     = siblings;
            goran.Siblings    = siblings;
            Assert.IsTrue(ivan.Siblings == goran.Siblings);

            var col1 = new Collection <IPortableObject>();

            col1.Add(ivan);
            col1.Add(goran);

            initPOFWriter();
            m_pofWriter = new PofStreamWriter(m_writer, ctx);
            m_pofWriter.EnableReference();
            m_pofWriter.WriteCollection(0, (ICollection)col1);
            m_pofWriter.WriteCollection(0, col1, typeof(PortablePersonReference));

            initPOFReader();
            m_pofReader = new PofStreamReader(m_reader, ctx);

            var result  = m_pofReader.ReadCollection(0, null);
            var result2 = m_pofReader.ReadCollection(0, null);

            Assert.IsTrue(2 == result.Count);

            IEnumerator iter = result.GetEnumerator();

            iter.MoveNext();
            var ivanR = (PortablePersonReference)iter.Current;

            iter.MoveNext();
            var goranR = (PortablePersonReference)iter.Current;

            Assert.IsFalse(ivanR.Siblings == goranR.Siblings);
            Assert.IsTrue(ivanR.Siblings[0] == goranR.Siblings[0]);
            Assert.IsTrue(ivanR.Siblings[1] == goranR.Siblings[1]);
            Assert.IsNull(ivanR.Children);

            iter = result2.GetEnumerator();
            iter.MoveNext();
            var ivanR2 = (PortablePersonReference)iter.Current;

            iter.MoveNext();
            var goranR2 = (PortablePersonReference)iter.Current;

            Assert.IsFalse(ivanR2.Siblings == goranR2.Siblings);
            Assert.IsTrue(ivanR2.Siblings[0] == goranR2.Siblings[0]);
            Assert.IsTrue(ivanR2.Siblings[1] == goranR2.Siblings[1]);
            Assert.IsNull(ivanR2.Children);
        }
示例#3
0
        public void TestPofStreamWriteWriteCollection()
        {
            initPOFWriter();
            var col1 = new ArrayList();
            var col2 = new ArrayList();

            col1.Add("A"); col1.Add("Z"); col1.Add("7");
            col2.Add(32); col2.Add(Int32.MinValue); col2.Add(Int32.MaxValue);

            pofwriter.WriteCollection(0, null);
            pofwriter.WriteCollection(0, col1);
            pofwriter.WriteCollection(0, col2);
            pofwriter.WriteCollection(0, col1);
            pofwriter.WriteDate(0, new DateTime(2006, 8, 8));

            initPOFReader();
            Assert.AreEqual(null, pofreader.ReadCollection(0, null));
            Assert.AreEqual(col1.ToArray(), ((ArrayList)pofreader.ReadCollection(0, new ArrayList(3))).ToArray());
            Assert.AreEqual(col2.ToArray(), ((ArrayList)pofreader.ReadCollection(0, new ArrayList(3))).ToArray());
            Assert.AreEqual(col1.ToArray(), pofreader.ReadCollection(0, null));
            Assert.AreEqual(col1.ToArray(), pofreader.ReadCollection(0, null)); // exception
        }