public void TestStack()
        {
            var q = new Stack <CAddress>();

            for (var i = 1; i < 11; i++)
            {
                q.Push(CAddress.Get());
            }

            var s   = new CSerializer();
            var doc = s.Serialize(q);

            Print(doc);

            var d  = new CDeserializer();
            var q2 = (Stack <CAddress>)d.Deserialize(doc);

            Assert.AreEqual(q.Count, q2.Count, "Number of resulting elements is wrong.");
            Assert.AreEqual(q.Count,
                            doc.DocumentElement.ChildNodes.Count,
                            "The number of child nodes does not equal the number of elements in the Collection.");

            while (q.Count > 0)
            {
                var a1 = q.Pop();
                var a2 = q2.Pop();
                CompareCAddresses(a1, a2);
            }
        }
        public void TestSortedDictionary()
        {
            var names = GenerateRandomNames();
            var dict  = new SortedDictionary <string, CAddress>();

            for (var i = 0; i < 26; i++)
            {
                dict[names[i]] = CAddress.Get();
            }

            var s   = new CSerializer();
            var doc = s.Serialize(dict);

            Print(doc);

            var d  = new CDeserializer();
            var d2 = (SortedDictionary <string, CAddress>)d.Deserialize(doc);

            Assert.AreEqual(dict.Count, d2.Count, "Size of resulting dictionary is wrong");
            Assert.AreEqual(dict.Count, doc.DocumentElement.ChildNodes.Count, "The number of XmlNodes for the collection is wrong");

            foreach (var key in dict.Keys)
            {
                CompareCAddresses(dict[key], d2[key]);
            }
        }
Esempio n. 3
0
        public void TestFieldRenamer()
        {
            var c = new CSerializationContext
            {
                FieldRenamer = new CFieldRenamer()
            };

            var s   = new CSerializer(c);
            var add = new CAddress();

            var doc = s.Serialize(add);

            Print(doc);

            var root = doc.DocumentElement;

            TestSingleRenamedField(add.m_zip, root["INT_Zip"]);
            TestSingleRenamedField(add.m_city, root["STRING_City"]);
            TestSingleRenamedField(add.m_street, root["STRING_Street"]);


            var d   = new CDeserializer(c);
            var ad2 = d.Deserialize <CAddress>(doc);

            Assert.AreEqual(add.m_city, ad2.m_city, "City");
            Assert.AreEqual(add.m_street, ad2.m_street, "Street");
            Assert.AreEqual(add.m_zip, ad2.m_zip, "Zip");
        }
Esempio n. 4
0
        public static CAddress Get()
        {
            var a = new CAddress
            {
                m_street = (sm_rng.Next(1900) * 5).ToString() + " Evergreen Terrace",
                m_city   = "Springfield " + (sm_rng.Next(10) + 1),
                m_zip    = 10000 + sm_rng.Next(90000)
            };

            return(a);
        }
 private static void CompareCAddresses(CAddress _a1, CAddress _a2)
 {
     Assert.AreEqual(_a1.m_street, _a2.m_street, "Street wrong");
     Assert.AreEqual(_a1.m_city, _a2.m_city, "City wrong");
     Assert.AreEqual(_a1.m_zip, _a2.m_zip, "Zip wrong");
 }
        public void TestBothInterfaceAndFullSerialization()
        {
            var ca = new CClassWithBothTypesOfCollections();

            for (var i = 0; i < 8; i++)
            {
                var name = "Address:" + i;
                ca.ByName[name] = CAddress.Get();
            }
            for (var i = 0; i < 6; i++)
            {
                ca.AsStack.Push(CAddress.Get());
            }


            var ctx = new CSerializationContext();

            ctx.SetConcise();
            ctx.RemoveNullValuesFromXml = false; // required for one test that checks the # of child elements where some of them are null

            var s   = new CSerializer(ctx);
            var doc = s.Serialize(ca);

            Print(doc);

            var dict = doc.DocumentElement["ByName"];

            Assert.AreEqual(ca.ByName.Count,
                            dict.ChildNodes.Count,
                            "Child Node Count should match Dictionary Count when using an Interface surrogate");

            var stak = doc.DocumentElement["AsStack"];
            //var stakArr = stak.ChildNodes[0];
            //Assert.AreEqual( ca.AsStack.Count, stakArr.ChildNodes.Count, $"The Stack size is incorrect" );

            var stakCount = stak["_size"];

            Assert.AreEqual(int.Parse(stakCount.InnerText), ca.AsStack.Count, "The number of stack elements is wrong");

            var stakElems = stak["_array"];
            var c         = int.Parse(XmlExtensions.GetAttributeValue(stakElems, ctx.ArrayAttributeName));

            Assert.IsTrue(c >= ca.AsStack.Count, "Not enough child elements for Stack");

            var d  = new CDeserializer(ctx);
            var cb = (CClassWithBothTypesOfCollections)d.Deserialize(doc);

            foreach (var key in ca.ByName.Keys)
            {
                Assert.AreEqual(ca.ByName[key].m_street, cb.ByName[key].m_street, "Incorrect Address for key: " + key);
                Assert.AreEqual(ca.ByName[key].m_city, cb.ByName[key].m_city, "Incorrect Address for key: " + key);
                Assert.AreEqual(ca.ByName[key].m_zip, cb.ByName[key].m_zip, "Incorrect Address for key: " + key);
            }

            var s_a = ca.AsStack.ToArray();
            var s_b = cb.AsStack.ToArray();

            Assert.AreEqual(s_a.Length, s_b.Length, "Lengths of Stacks are wrong");

            for (var i = 0; i < s_a.Length; i++)
            {
                var aa = s_a[i] as CAddress;
                var ab = s_b[i] as CAddress;

                CompareCAddresses(aa, ab);
            }
        }