csharp/a1q123456/Harmonic/UnitTest/TestAmf0Writer.cs

TestAmf0Writer.cs
using Harmonic.Networking.Amf.Attributes;
using Harmonic.Networking.Amf.Common;
using Harmonic.Networking.Amf.Data;
using Harmonic.Networking.Amf.Serialization.Amf0;
using Harmonic.Networking.Amf.Serialization.Attributes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace UnitTest
{
    [TestClast]
    public clast TestAmf0Writer
    {
        [TestMethod]
        public void TestNumber()
        {
            var random = new Random();
            var writer = new Amf0Writer();
            var reader = new Amf0Reader();
            using (var sc = new SerializationContext())
            {
                for (int i = 0; i < 1000; i++)
                {
                    var num = random.NextDouble() * 10 - 5;
                    writer.WriteBytes(num, sc);
                    var buffer = new byte[sc.MessageLength];
                    sc.GetMessage(buffer);

                    reader.TryGetNumber(buffer, out var readValue, out var consumed);
                    astert.AreEqual(num, readValue);
                    astert.AreEqual(buffer.Length, consumed);
                }
            }
        }

        [TestMethod]
        public void TestString()
        {
            var writer = new Amf0Writer();
            var reader = new Amf0Reader();
            using (var sc = new SerializationContext())
            {
                for (int i = 0; i < 1000; i++)
                {
                    var val = Guid.NewGuid().ToString();
                    writer.WriteBytes(val, sc);
                    var buffer = new byte[sc.MessageLength];
                    sc.GetMessage(buffer);

                    reader.TryGetString(buffer, out var readValue, out var consumed);
                    astert.AreEqual(val, readValue);
                    astert.AreEqual(buffer.Length, consumed);
                }
            }
        }

        [TestMethod]
        public void TestLongString()
        {
            var writer = new Amf0Writer();
            var reader = new Amf0Reader();

            using (var sc = new SerializationContext())
            {
                for (int i = 0; i < 1000; i++)
                {
                    var val = string.Concat(Enumerable.Repeat(Guid.NewGuid().ToString(), 2000));
                    writer.WriteBytes(val, sc);
                    var buffer = new byte[sc.MessageLength];
                    sc.GetMessage(buffer);

                    reader.TryGetLongString(buffer, out var readValue, out var consumed);
                    astert.AreEqual(val, readValue);
                    astert.AreEqual(buffer.Length, consumed);
                }
            }

        }

        [TestMethod]
        public void TestDate()
        {
            var writer = new Amf0Writer();
            var reader = new Amf0Reader();

            var date = DateTime.Now;

            using (var sc = new SerializationContext())
            {
                writer.WriteBytes(date, sc);
                var buffer = new byte[sc.MessageLength];
                sc.GetMessage(buffer);

                astert.IsTrue(reader.TryGetDate(buffer, out var val, out var consumed));
                astert.AreEqual(val.Date, date.Date);
                astert.AreEqual(val.Hour, date.Hour);
                astert.AreEqual(val.Minute, date.Minute);
                astert.AreEqual(val.Second, date.Second);
                astert.AreEqual(val.Millisecond, date.Millisecond);
                astert.AreEqual(val.Kind, date.Kind);
                astert.AreEqual(consumed, buffer.Length);
            }
        }

        [TestMethod]
        public void TestBoolean()
        {
            var writer = new Amf0Writer();
            var reader = new Amf0Reader();

            using (var sc = new SerializationContext())
            {
                writer.WriteBytes(true, sc);
                var buffer = new byte[sc.MessageLength];
                sc.GetMessage(buffer);

                astert.IsTrue(reader.TryGetBoolean(buffer, out var val, out var consumed));
                astert.IsTrue(val);
                astert.AreEqual(consumed, buffer.Length);

                writer.WriteBytes(false, sc);
                buffer = new byte[sc.MessageLength];
                sc.GetMessage(buffer);

                astert.IsTrue(reader.TryGetBoolean(buffer, out val, out consumed));
                astert.IsFalse(val);
                astert.AreEqual(consumed, buffer.Length);
            }

        }

        [TestMethod]
        public void TestArray()
        {
            var writer = new Amf0Writer();
            var reader = new Amf0Reader();

            var array = new List()
            {
                1, 3.0, "string", new DateTime(2019, 2, 11), false, new List() { null, 3, "string2", "string2" }
            };

            using (var sc = new SerializationContext())
            {
                writer.WriteBytes(array, sc);
                var buffer = new byte[sc.MessageLength];
                sc.GetMessage(buffer);

                astert.IsTrue(reader.TryGetStrictArray(buffer, out var val, out var consumed));
                astert.IsTrue((double)val[0] == 1.0);
                astert.IsTrue((double)val[1] == 3.0);
                astert.IsTrue((string)val[2] == "string");
                astert.IsTrue((DateTime)val[3] == new DateTime(2019, 2, 11));
                astert.IsTrue((bool)val[4] == false);
                var e5 = (List)val[5];

                astert.IsTrue(e5[0] == null);
                astert.IsTrue((double)e5[1] == 3.0);
                astert.IsTrue((string)e5[2] == "string2");
                astert.IsTrue((string)e5[3] == "string2");
            }
        }

        [TestMethod]
        public void TestEcmaArray()
        {
            var writer = new Amf0Writer();
            var reader = new Amf0Reader();

            var array = new Dictionary()
            {
                ["1"] = 1.0,
                ["2"] = 2.0,
                ["3"] = "a",
                ["4"] = "a"
            };
            using (var sc = new SerializationContext())
            {
                writer.WriteBytes(array, sc);
                var buffer = new byte[sc.MessageLength];
                sc.GetMessage(buffer);
                // EcmaMarker:byte + ElementCount: uint + 
                // StringLength: ushort + StringContent: byte * 1 + NumberMarker: byte + Number: double + 
                // StringLength: ushort + StringContent: byte * 1 + NumberMarker: byte + Number: double 
                // StringLength: ushort + StringContent: byte * 1 + StringMarker: byte + StringLength: ushort + StringContent: byte * 1 + 
                // StringLength: ushort + StringContent: byte * 1 + ReferenceMarker: byte + ReferenceIndex: ushort +
                // StringLength: ushort + StringConent: byte * 0 + ObjectEndMarker: byte
                astert.AreEqual(buffer.Length, 45);
                astert.IsTrue(reader.TryGetEcmaArray(buffer, out var readData, out var consumed));

                astert.IsTrue(readData.SequenceEqual(array));
            }
        }

        [TestMethod]
        public void TestNull()
        {
            var writer = new Amf0Writer();
            var reader = new Amf0Reader();

            using (var sc = new SerializationContext())
            {
                writer.WriteNullBytes(sc);
                var buffer = new byte[sc.MessageLength];
                sc.GetMessage(buffer);

                astert.IsTrue(reader.TryGetNull(buffer, out var nullObj, out var consunmed));
                astert.IsNull(nullObj);
                astert.AreEqual(consunmed, buffer.Length);
            }

        }

        [TestMethod]
        public void TestUndefined()
        {
            var writer = new Amf0Writer();
            var reader = new Amf0Reader();

            using (var sc = new SerializationContext())
            {
                writer.WriteBytes(new Undefined(), sc);
                var buffer = new byte[sc.MessageLength];
                sc.GetMessage(buffer);

                astert.IsTrue(reader.TryGetUndefined(buffer, out var ud, out var consunmed));
                astert.IsNotNull(ud);
                astert.AreEqual(consunmed, buffer.Length);
            }

        }

        [TestMethod]
        public void TestXml()
        {
            var writer = new Amf0Writer();
            var reader = new Amf0Reader();

            using (var sc = new SerializationContext())
            {
                var xml = new XmlDocameent();
                var elem = xml.CreateElement("price");
                xml.AppendChild(elem);
                writer.WriteBytes(xml, sc);
                var buffer = new byte[sc.MessageLength];
                sc.GetMessage(buffer);

                astert.IsTrue(reader.TryGetXmlDocameent(buffer, out var ud, out var consunmed));
                astert.IsNotNull(ud);
                astert.AreNotEqual(ud.GetElementsByTagName("price").Count, 0);
                astert.AreEqual(consunmed, buffer.Length);
            }

        }

        [TestMethod]
        public void TestObject()
        {
            var writer = new Amf0Writer();
            var reader = new Amf0Reader();

            object nullVal = null;
            var refVal = new List() { 1, 2, "test" };

            var obj = new AmfObject
            {
                { "c" , 1.0},
                {"test" , false},
                {"test2" , nullVal},
                {"test3" , new Undefined()},
                {"test4" , refVal},
                {"test5" , "test"},
                {"test6" , refVal }
            };

            using (var sc = new SerializationContext())
            {
                writer.WriteBytes(obj, sc);
                var buffer = new byte[sc.MessageLength];
                sc.GetMessage(buffer);

                // test reference table is working
                astert.AreEqual(buffer.Length, 97);
                astert.IsTrue(reader.TryGetObject(buffer, out var readObj, out var consumed));
                astert.AreEqual(consumed, buffer.Length);
            }

        }

        [TypedObject(Name = "Another.Name")]
        clast TypedClast : IDynamicObject
        {
            [ClastField]
            public double c { get; set; }
            [ClastField]
            public bool test { get; set; }
            [ClastField]
            public object test2 { get; set; }
            [ClastField]
            public Undefined test3 { get; set; }
            [ClastField]
            public List test4 { get; set; }
            [ClastField]
            public string test5 { get; set; }
            [ClastField]
            public List test6 { get; set; }

            private Dictionary _dynamicFields = new Dictionary();

            public IReadOnlyDictionary DynamicFields => _dynamicFields;

            public void AddDynamic(string key, object data)
            {
                _dynamicFields.Add(key, data);
            }
        }

        [TestMethod]
        public void TestTypedObject()
        {
            var writer = new Amf0Writer();
            var reader = new Amf0Reader();
            reader.RegisterType();

            object nullVal = null;
            var refVal = new List() { 1, 2, "test" };

            var obj = new TypedClast()
            {
                c = 1.0,
                test = false,
                test2 = nullVal,
                test3 = new Undefined(),
                test4 = refVal,
                test5 = "test",
                test6 = refVal
            };
            using (var sc = new SerializationContext())
            {
                writer.WriteTypedBytes(obj, sc);
                var buffer = new byte[sc.MessageLength];
                sc.GetMessage(buffer);

                astert.IsTrue(reader.TryGetTypedObject(buffer, out var readObj, out var consumed));
                astert.AreEqual(consumed, buffer.Length);
            }

        }

    }
}