コード例 #1
0
ファイル: EntityTest.cs プロジェクト: IronFox/Shard
        public void EntityMotionTest()
        {
            int        numEntities = 100;
            var        ctx         = new SimulationContext(true);
            EntityPool pool        = new EntityPool(EntityPoolTests.CreateEntities(ctx.LocalSpace, numEntities, i => new RandomMotion()), ctx);

            InconsistencyCoverage ic = InconsistencyCoverage.NewCommon();

            for (int i = 0; i < 100; i++)
            {
                var             old = pool.ToArray();
                EntityChangeSet set = new EntityChangeSet();
                ctx.SetGeneration(i);
                var errors = pool.TestEvolve(set, InconsistencyCoverage.NewCommon(), i, TimeSpan.FromSeconds(1));
                Assert.IsNull(errors, errors != null ? errors[0].ToString() : "");
                Assert.AreEqual(numEntities, set.FindNamedSet("motions").Size);
                foreach (var e in old)
                {
                    var m = set.FindMotionOf(e.ID.Guid);
                    Assert.IsNotNull(m);
                    Assert.AreNotEqual(m.TargetLocation, m.Origin.Position, i.ToString());
                    Assert.IsTrue(Simulation.FullSimulationSpace.Contains(m.TargetLocation));
                    Assert.IsTrue(Simulation.MySpace.Contains(m.TargetLocation));
                }

                Assert.AreEqual(0, set.Execute(pool, ic, ctx));
                Assert.AreEqual(numEntities, pool.Count);
                Entity e1;
                foreach (var e in old)
                {
                    Assert.IsTrue(pool.Find(e.ID.Guid, out e1));
                    Assert.AreNotEqual(e.ID.Position, e1.ID.Position);
                }
            }
        }
コード例 #2
0
ファイル: EntityTest.cs プロジェクト: IronFox/Shard
        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);
                    }
                }
            }
        }
コード例 #3
0
        public void MotionConflictOrderingTest()
        {
            var ctx = new SimulationContext(true);

            for (int i = 0; i < 10; i++)
            {
                var  entities = CreateEntities(ctx.LocalSpace, 1);
                var  e        = entities[0];
                Vec3 original = e.ID.Position;

                EntityChange.Motion[] motions = new EntityChange.Motion[10];
                for (int j = 0; j < motions.Length; j++)
                {
                    motions[j] = new EntityChange.Motion(new EntityID(e.ID.Guid, random.NextVec3(0, 1)), ctx.LocalSpace.Clamp(original + random.NextVec3(-ctx.Ranges.R, ctx.Ranges.R)), e.MyLogic, null);
                }

                Entity e0 = null;

                for (int j = 0; j < motions.Length; j++)
                {
                    var             motions2 = motions.OrderBy(x => random.Next()).ToArray();
                    EntityPool      pool     = new EntityPool(entities, ctx);
                    EntityChangeSet set      = new EntityChangeSet();
                    foreach (var m in motions2)
                    {
                        set.Add(m);
                    }

                    int errors = set.Execute(pool, InconsistencyCoverage.NewAllOne(), ctx);
                    Assert.AreEqual(motions.Length - 1, errors);                        //motions-1 get rejected, one is accepted. must always be the same

                    var e1 = pool.First();
                    Assert.AreNotEqual(e1, e);                          //must have moved
                    if (e0 == null)
                    {
                        e0 = e1;
                    }
                    else
                    {
                        Assert.AreEqual(e0, e1);                         //must have moved to the same location
                    }
                }
            }
        }
コード例 #4
0
ファイル: EntityTest.cs プロジェクト: IronFox/Shard
        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);
        }