public void TestPofSerialization()
        {
            ISerializer serializer = new SimplePofContext();

            IDictionary original = InstantiateDictionary();

            original.Add("A", "A");
            original.Add("B", "B");
            original.Add("C", "C");

            var stream = new BinaryMemoryStream();

            serializer.Serialize(new DataWriter(stream), original);
            Binary     bin    = stream.ToBinary();
            IPofReader reader = new PofStreamReader(bin.GetReader(), (IPofContext)serializer);
            Object     copy   = reader.ReadDictionary(-1, InstantiateDictionary());

            Assert.AreEqual(original, copy);
            Assert.AreEqual(original.GetHashCode(), copy.GetHashCode());

            original.Add(null, null);
            stream = new BinaryMemoryStream();
            serializer.Serialize(new DataWriter(stream), original);
            bin    = stream.ToBinary();
            reader = new PofStreamReader(bin.GetReader(), (IPofContext)serializer);
            copy   = reader.ReadDictionary(-1, InstantiateDictionary());

            Assert.AreEqual(original, copy);
            Assert.AreEqual(original.GetHashCode(), copy.GetHashCode());
            Assert.AreEqual(original.ToString(), copy.ToString());
        }
        public void TestReferencesInUniformMap()
        {
            var localCtx = new SimplePofContext();

            localCtx.RegisterUserType(101, typeof(PortablePerson),
                                      new PortableObjectSerializer(101));
            localCtx.RegisterUserType(102, typeof(PortablePersonReference),
                                      new PortableObjectSerializer(102));
            localCtx.RegisterUserType(201, typeof(CompositeKey),
                                      new PortableObjectSerializer(201));

            IDictionary  mapPerson = new Dictionary <CompositeKey, IPortableObject>();
            const String lastName  = "Smith";
            CompositeKey key1      = new CompositeKey(lastName, "ivan"),
                         key2      = new CompositeKey(lastName, "goran");
            var ivan = new PortablePersonReference("Ivan", new DateTime(78, 4, 25));

            ivan.Children = null;
            mapPerson.Add(key1, ivan);
            mapPerson.Add(key2, ivan);

            initPOFWriter();
            m_pofWriter = new PofStreamWriter(m_writer, localCtx);
            m_pofWriter.EnableReference();
            m_pofWriter.WriteDictionary(0, mapPerson, typeof(CompositeKey),
                                        typeof(PortablePersonReference));

            var mapPersonR = new Dictionary <CompositeKey, IPortableObject>();

            initPOFReader();
            m_pofReader = new PofStreamReader(m_reader, localCtx);
            m_pofReader.ReadDictionary(0, (IDictionary)mapPersonR);

            // compare mapPerson with result
            Assert.IsTrue(mapPersonR[key1].Equals(mapPerson[key1]));
            Assert.IsTrue(mapPersonR[key2].Equals(mapPerson[key2]));

            ICollection colValR = mapPersonR.Values;
            IEnumerator iter    = colValR.GetEnumerator();

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

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

            Assert.IsTrue(val1 == val2);
        }
        public void TestReferencesInUniformArray()
        {
            var localCtx = new SimplePofContext();

            localCtx.RegisterUserType(101, typeof(PortablePerson),
                                      new PortableObjectSerializer(101));
            localCtx.RegisterUserType(102, typeof(PortablePersonReference),
                                      new PortableObjectSerializer(102));
            localCtx.RegisterUserType(201, typeof(CompositeKey),
                                      new PortableObjectSerializer(201));
            localCtx.IsReferenceEnabled = true;

            var ivan  = new PortablePersonReference("Ivan", new DateTime(78, 4, 25));
            var goran = new PortablePersonReference("Goran", new DateTime(82, 3, 3));

            ivan.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     = new PortablePersonReference[1];
            ivan.Siblings[0]  = goran;
            goran.Siblings    = new PortablePersonReference[1];
            goran.Siblings[0] = ivan;

            IDictionary  mapPerson = new Dictionary <CompositeKey, IPortableObject>();
            const String lastName  = "Smith";
            CompositeKey key1      = new CompositeKey(lastName, "ivan"),
                         key2      = new CompositeKey(lastName, "goran");

            mapPerson.Add(key1, ivan);
            mapPerson.Add(key2, goran);

            initPOFWriter();
            m_pofWriter = new PofStreamWriter(m_writer, localCtx);
            if (localCtx.IsReferenceEnabled)
            {
                m_pofWriter.EnableReference();
            }
            m_pofWriter.WriteDictionary(0, mapPerson);

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

            IDictionary mapResult = m_pofReader.ReadDictionary(0, null);

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

            var         ivanR  = (PortablePersonReference)mapResult[key1];
            var         goranR = (PortablePersonReference)mapResult[key2];
            ICollection keySet = mapResult.Keys;
            IEnumerator iter   = keySet.GetEnumerator();

            iter.MoveNext();
            var key1R = (CompositeKey)iter.Current;

            iter.MoveNext();
            var key2R = (CompositeKey)iter.Current;

            Assert.IsFalse(key1R.PrimaryKey == key2R.PrimaryKey);
            Assert.IsTrue(ivanR.Siblings[0] == goranR);
            Assert.IsTrue(goran.Name.Equals(goranR.Name));
            Assert.IsNull(ivanR.Children);
        }
Exemplo n.º 4
0
        public void TestWriteGenericDictionary()
        {
            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);


            IDictionary <string, double> dict = new Dictionary <string, double>();

            dict.Add("A", 11.11); dict.Add("Z", 88.88); dict.Add("7", 100.1);
            IDictionary <string, string> dict2 = new Dictionary <string, string>();

            dict2.Add("ABC", "value"); dict2.Add("N", null);

            IDictionary <string, IPortableObject> persons = new Dictionary <string, IPortableObject>();
            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("key1", ivan);
            persons.Add("key2", aleks);
            persons.Add("key3", goran);

            pofwriter.WriteDictionary(0, dict);
            pofwriter.WriteDictionary(0, dict2);
            pofwriter.WriteDictionary(0, persons);
            pofwriter.WriteDictionary(0, persons);

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

            IDictionary <string, double> result = new Dictionary <string, double>();

            pofreader.ReadDictionary(0, result);
            Assert.AreEqual(3, result.Count);
            foreach (string key in dict.Keys)
            {
                Assert.AreEqual(dict[key], result[key]);
            }

            IDictionary <string, string> result2 = new Dictionary <string, string>();

            pofreader.ReadDictionary(0, result2);
            Assert.AreEqual(2, result2.Count);
            foreach (string key in dict.Keys)
            {
                Assert.AreEqual(dict[key], result[key]);
            }

            IDictionary <string, IPortableObject> result3 = new Dictionary <string, IPortableObject>();

            pofreader.ReadDictionary(0, result3);
            Assert.AreEqual(3, result3.Count);
            Assert.IsFalse(result3["key1"] is EvolvablePortablePerson);
            Assert.IsTrue(result3["key2"] is EvolvablePortablePerson);
            Assert.IsFalse(result3["key3"] is EvolvablePortablePerson);
            EvolvablePortablePerson epp = (EvolvablePortablePerson)result3["key2"];

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

            var pp = (PortablePerson)result3["key3"];

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

            IDictionary <string, IPortableObject> result4 = pofreader.ReadDictionary <string, IPortableObject>(0, null);

            Assert.AreEqual(3, result4.Count);
            Assert.IsFalse(result4["key1"] is EvolvablePortablePerson);
            Assert.IsTrue(result4["key2"] is EvolvablePortablePerson);
            Assert.IsFalse(result4["key3"] is EvolvablePortablePerson);
            epp = (EvolvablePortablePerson)result4["key2"];
            Assert.AreEqual(aleks.Name, epp.Name);
            Assert.AreEqual(aleks.Children[0].Name, epp.Children[0].Name);

            pp = (PortablePerson)result4["key3"];
            Assert.AreEqual(goran.Name, pp.Name);
            Assert.IsNull(pp.Children);
        }
Exemplo n.º 5
0
        public void TestPofStreamWriteWriteDictionary()
        {
            initPOFWriter();
            var col1 = new Hashtable();
            var col2 = new Hashtable();

            col1.Add(0, "A"); col1.Add(1, "Z"); col1.Add(2, "7");
            col2.Add(5, 32); col2.Add(10, Int32.MinValue); col2.Add(15, Int32.MaxValue);

            pofwriter.WriteDictionary(0, col1);
            pofwriter.WriteDictionary(0, col2);
            pofwriter.WriteDictionary(0, null);
            pofwriter.WriteDictionary(0, col1);

            initPOFReader();
            IDictionary rcol1 = pofreader.ReadDictionary(0, new Hashtable(3));
            IDictionary rcol2 = pofreader.ReadDictionary(0, new Hashtable(3));

            #region Compare entries in rcol1 with col1

            IDictionaryEnumerator denum = col1.GetEnumerator();
            denum.Reset();
            foreach (DictionaryEntry entryr in rcol1)
            {
                denum.MoveNext();
                DictionaryEntry entry = denum.Entry;
                Assert.AreEqual(entryr.Key, entry.Key);
                Assert.AreEqual(entryr.Value, entry.Value);
            }

            #endregion

            #region Compare entries in rcol2 with col2

            denum = col2.GetEnumerator();
            denum.Reset();
            foreach (DictionaryEntry entryr in rcol2)
            {
                denum.MoveNext();
                DictionaryEntry entry = denum.Entry;
                Assert.AreEqual(entryr.Key, entry.Key);
                Assert.AreEqual(entryr.Value, entry.Value);
            }

            #endregion

            Assert.AreEqual(null, pofreader.ReadDictionary(0, null));

            IDictionary rcol3 = pofreader.ReadDictionary(0, null);

            #region Compare entries in rcol3 with col1

            denum = col1.GetEnumerator();
            denum.Reset();
            foreach (DictionaryEntry entryr in rcol3)
            {
                denum.MoveNext();
                DictionaryEntry entry = denum.Entry;
                Assert.AreEqual(entryr.Key, entry.Key);
                Assert.AreEqual(entryr.Value, entry.Value);
            }

            #endregion
        }