Esempio n. 1
0
        public void AgentInfo_CheckEncodeAndDecode()
        {
            EndPoint  ep    = new EndPoint("129.123.7.24:1345");
            AgentInfo info1 = new AgentInfo(20, AgentInfo.PossibleAgentType.WhiningSpinner, ep)
            {
                ANumber   = "A00001",
                FirstName = "Joe",
                LastName  = "Jones",
                Location  = new FieldLocation(10, 20, false),
                Strength  = 1200.5,
                Speed     = 1500.0
            };

            ByteList bytes = new ByteList();

            info1.Encode(bytes);
            AgentInfo info2 = AgentInfo.Create(bytes);

            Assert.AreEqual(info1.Id, info2.Id);
            Assert.AreEqual(info1.AgentType, info2.AgentType);
            Assert.AreEqual(info1.ANumber, info2.ANumber);
            Assert.AreEqual(info1.FirstName, info2.FirstName);
            Assert.AreEqual(info1.LastName, info2.LastName);
            Assert.AreEqual(info1.Strength, info2.Strength);
            Assert.AreEqual(info1.Speed, info2.Speed);
            Assert.AreEqual(info1.Points, info2.Points);
            Assert.AreEqual(info1.Location.X, info2.Location.X);
            Assert.AreEqual(info1.Location.Y, info2.Location.Y);
            Assert.AreEqual(info1.CommunicationEndPoint.Address, info2.CommunicationEndPoint.Address);
            Assert.AreEqual(info1.CommunicationEndPoint.Port, info2.CommunicationEndPoint.Port);

            bytes.Clear();
            info1.Encode(bytes);
            bytes.GetByte();            // Read one byte, which will throw the length off
            try
            {
                info2 = AgentInfo.Create(bytes);
                Assert.Fail("Expected an exception to be thrown");
            }
            catch (ApplicationException)
            {
            }

            bytes.Clear();
            info1.Encode(bytes);
            bytes.Add((byte)100);       // Add a byte
            bytes.GetByte();            // Read one byte, which will make the ID wrong
            try
            {
                info2 = AgentInfo.Create(bytes);
                Assert.Fail("Expected an exception to be thrown");
            }
            catch (ApplicationException)
            {
            }
        }