Beispiel #1
0
        public void StateAdvertisementTest()
        {
            var        ctx  = new SimulationContext();
            EntityPool pool = RandomDefaultPool(100, ctx);

            for (int i = 0; i < 100; i++)
            {
                EntityChangeSet set = new EntityChangeSet();
                ctx.SetGeneration(i);
                var errors = pool.TestEvolve(set, InconsistencyCoverage.NewCommon(), i, TimeSpan.FromSeconds(5));
                Assert.IsNull(errors, errors != null ? errors[0].ToString() : "");
                Assert.AreEqual(0, set.Execute(pool, ctx));

                HashSet <Guid> env   = new HashSet <Guid>();
                var            state = pool.ToArray();
                int            it    = 0;
                foreach (var e in state)
                {
                    env.Clear();
                    foreach (var e1 in state)
                    {
                        float dist = ctx.GetDistance(e.ID.Position, e1.ID.Position);
                        if (e1.ID.Guid != e.ID.Guid && dist <= Simulation.SensorRange)
                        {
                            Console.WriteLine(dist + "/" + Simulation.SensorRange);
                            env.Add(e1.ID.Guid);
                        }
                    }
                    Assert.AreEqual(env.Count, e.Contacts.Length, i + "." + it + ": " + e);
                    if (env.Count > 0)
                    {
                        it++;
                    }

                    foreach (var c in e.Contacts)
                    {
                        float dist = ctx.GetDistance(c.ID.Position, e.ID.Position);
                        Assert.IsTrue(dist <= Simulation.SensorRange, dist + " <= " + Simulation.SensorRange);
                        Assert.AreNotEqual(c.ID.Guid, e.ID.Guid);
                        Assert.IsTrue(env.Contains(c.ID.Guid));
                    }

                    var app = e.Appearances.Get <ConsistencyAppearance>();
                    if (i > 0)
                    {
                        Assert.IsNotNull(e.Appearances);
                        Assert.IsNotNull(app);
                    }
                    if (app != null)
                    {
                        Assert.IsTrue(app.IsConsistent);
                    }
                }
            }
        }
Beispiel #2
0
        public void ExtensiveEntityFaultTest()
        {
            int mismatched = 0;

            var ctx = new SimulationContext(true);

            for (int k = 0; k < 20; k++)
            {
                InconsistencyCoverage ic     = InconsistencyCoverage.NewCommon(),
                                      nextIC = ic;

                EntityPool pool = new EntityPool(EntityPoolTests.CreateEntities(ctx.LocalSpace, 100, i => i > 0 ? new ConsistentLogic() : (EntityLogic)(new FaultLogic())), ctx);

                int    faultyCount = 0;
                Entity faulty      = new Entity();
                foreach (var e in pool.ToArray())
                {
                    if (Helper.Deserialize(e.SerialLogicState) is FaultLogic)
                    {
                        faultyCount++;
                        faulty = e;
                    }
                }
                Assert.AreEqual(faultyCount, 1);

                bool doGrow = k % 2 != 0;
                ConsistentLogic.GrowingIC = doGrow;

                for (int i = 0; i < InconsistencyCoverage.CommonResolution; i++)                        //no point going further than current resolution
                {
                    EntityChangeSet set = new EntityChangeSet();
                    ctx.SetGeneration(i);

                    var errors = pool.TestEvolve(set, ic, i, TimeSpan.FromSeconds(1));
                    Assert.AreEqual(1, errors.Count, Helper.Concat(",", errors));
                    Assert.AreEqual(0, set.Execute(pool, ic, ctx));
                    //Assert.AreEqual(ic.OneCount, 1);
                    if (doGrow)
                    {
                        ic = ic.Grow(true);
                    }

                    if (!doGrow)
                    {
                        Assert.AreEqual(ic.OneCount, 1);
                    }


                    Entity[] entities  = pool.ToArray();
                    bool     hasFaulty = false;
                    foreach (var e in entities)
                    {
                        bool isFaultOrigin = (Helper.Deserialize(e.SerialLogicState) is FaultLogic);
                        if (isFaultOrigin)
                        {
                            faulty = e;
                            Assert.IsFalse(hasFaulty);
                            hasFaulty = true;
                        }
                    }
                    Assert.IsTrue(hasFaulty);

                    foreach (var e in entities)
                    {
                        bool isFaultOrigin = (Helper.Deserialize(e.SerialLogicState) is FaultLogic);
#if STATE_ADV
                        if (!isFaultOrigin)
                        {
                            if (ctx.GetDistance(e.ID.Position, faulty.ID.Position) <= Simulation.SensorRange)
                            {
                                Assert.IsTrue(e.HasContact(faulty.ID.Guid));
                                Assert.IsTrue(e.HasContact(faulty.ID));
                            }

                            var adv = set.FindAdvertisementFor(e.ID);
                            Assert.IsNotNull(e.Appearances);
                        }
                        bool consistent = ConsistentLogic.IsConsistent(e.Appearances);
#else
                        ConsistentLogic c          = e.MyLogic as ConsistentLogic;
                        bool            consistent = c != null && c.isConsistent;
#endif
                        bool icIsInc = ic.IsInconsistentR(Simulation.MySpace.Relativate(e.ID.Position));

                        if (!consistent && !icIsInc)
                        {
                            if (doGrow)
                            {
                                Assert.Fail("Inconsistent entity located outside IC area: " + e + ", " + e.MyLogic);
                            }
                            else
                            {
                                mismatched++;
                                break;
                            }
                        }
                    }

                    //Assert.AreEqual(ic.OneCount, 1);
                }
            }
            Assert.AreNotEqual(mismatched, 0);
        }