Пример #1
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Running performance tests...");
            Lookup_ScopeManager.RegisterAll();

            Thread.Sleep(1500);     // why?

            run <Entity_AddRemove_CompData_Class>();
            run <Entity_AddRemove_CompData_Struct>();
            run <Entity_AddRemove_CompData_Class_WithGroups>();
            run <Entity_AddRemove_CompData_Struct_WithGroups>();
            run <Entity_Replace_CompData_Class_WithGroups>();
            run <Entity_Replace_CompData_Struct_WithGroups>();

            run <Entity_Get_CompData_Class>(  );
            run <Entity_Get_CompData_Struct>(  );

            run <Entity_Has_CompData_Class>(  );
            run <Entity_Has_CompData_Struct>(  );

            run <Entity_Flag_CompFlag>(  );
            run <Entity_Is_CompFlag>(  );

            // Console.WriteLine("\nPress any key...");
            // Console.Read();
        }
Пример #2
0
        private void                                    InitContexts(  )
        {
            Lookup_ScopeManager.RegisterAll(  );

            Hub.Contexts = new Contexts(  );
            Hub.Contexts.AddScopedContexts(  );
        }
Пример #3
0
        public static void Main(string[] args)
        {
            Lookup_ScopeManager.RegisterAll();

            Thread.Sleep(500);     // 1500

            MemoryHelper.MemoryBegin();

            R.Log("CompData");
            // 1 exclude component-data-init time from test
            R.Run <Entity_AddRemove_CompData_Class>();
            // 2 include component-data-init into test
            // (results can be compared with tests 3 4 5 and 11)
            R.Run <Entity_AddRemove_CompData_Class_AddNewInstance>();
            // 3 replace component data init by Cache.I.Set
            // (results can be compared with tests 2 4 5 and 11)
            R.Run <Entity_AddRemove_CompData_Class_SetUsingCache>();
            // 4 Create-Apply workflow
            // (results can be compared with tests 2, 3 5  and 11)
            R.Run <Entity_AddRemove_CompData_Class_CreateSetApply>();
            // 3.2 repeat test#3 to double check stats accuracy
            R.Run <Entity_AddRemove_CompData_Class_SetUsingCache>();

            // 10 base struct Add test
            R.Run <Entity_AddRemove_CompData_Struct>();
            // 11 is like 10 but component initialization is included into test
            // so results can be compared with tests 2-5
            R.Run <Entity_AddRemove_CompData_Struct_IncludeInit>();
            R.Run <Entity_AddRemove_CompData_Class_WithGroups>();
            R.Run <Entity_AddRemove_CompData_Struct_WithGroups>();
            R.Run <Entity_Replace_CompData_Class_WithGroups>();
            R.Run <Entity_Replace_CompData_Struct_WithGroups>();

            R.Log("\nCompData.Get");
            R.Run <Entity_Get_CompData_Class>(  );
            R.Run <Entity_Get_CompData_Struct>(  );

            R.Log("\nCompData.Has");
            R.Run <Entity_Has_CompData_Class>(  );
            R.Run <Entity_Has_CompData_Struct>(  );

            R.Log("\nFlag");
            R.Run <Entity_Flag_CompFlag>(  );
            R.Run <Entity_Is_CompFlag>(  );

            R.Log("\nMisc");
            R.Run <Struct_ToString_Implemented>(  );
            R.Run <Struct_ToString_NotImplemented>(  );
            R.Run <Struct_ToString_ToGenericTypeString>(  );

            MemoryHelper.MemoryEnd(  );
        }
Пример #4
0
 static Contexts()
 {
     Lookup_ScopeManager.RegisterAll();
 }
        private void                                    test_CoreMethods(  )
        {
            Lookup_ScopeManager.RegisterAll(  );

            before = () =>
            {
                _contexts = new Contexts(  );
                _contexts.AddScopedContexts(  );
            };

            it["OnAny listens other"] = () =>
            {
                // given
                var system   = new EventSystem_Any <ScopeA, TestCompA>(_contexts);
                var listener = Substitute.For <IOnAny <ScopeA, TestCompA> >(  );
                {
                    var ent = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                    ent.Add_OnAny(listener);
                }
                listener.DidNotReceiveWithAnyArgs(  ).OnAny(null, null, null);

                // when
                var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                entity.Add(new TestCompA(  ));

                system.Execute(  );

                // then
                listener.ReceivedWithAnyArgs(  ).OnAny(null, null, null);
            };

            it["OnAnyRemoved listens other"] = () =>
            {
                // given
                var system   = new EventSystem_AnyRemoved <ScopeA, TestCompA>(_contexts);
                var listener = Substitute.For <IOnAnyRemoved <ScopeA, TestCompA> >(  );
                {
                    var ent = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                    ent.Add_OnAnyRemoved(listener);
                }
                listener.DidNotReceiveWithAnyArgs(  ).OnAnyRemoved(null, null, null);

                // when
                var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                entity.Add(new TestCompA(  ));
                entity.Remove <TestCompA>(  );

                system.Execute(  );

                // then
                listener.ReceivedWithAnyArgs(  ).OnAnyRemoved(null, null, null);
            };

            it["OnSelf listens self"] = () =>
            {
                // given
                var system   = new EventSystem_Self <ScopeA, TestCompA>(_contexts);
                var listener = Substitute.For <IOnSelf <ScopeA, TestCompA> >(  );
                listener.DidNotReceiveWithAnyArgs(  ).OnSelf(null, null, null);

                // when
                {
                    var ent = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                    ent.Add(new TestCompA(  ));
                    ent.Add_OnSelf(listener);
                }

                system.Execute(  );

                // then
                listener.ReceivedWithAnyArgs(  ).OnSelf(null, null, null);
            };

            it["OnSelf doesn't listen other"] = () =>
            {
                // given
                var system   = new EventSystem_Self <ScopeA, TestCompA>(_contexts);
                var listener = Substitute.For <IOnSelf <ScopeA, TestCompA> >(  );
                listener.DidNotReceiveWithAnyArgs(  ).OnSelf(null, null, null);

                // when
                {
                    var ent = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                    ent.Add_OnSelf(listener);
                }
                {
                    var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                    entity.Add(new TestCompA(  ));
                }

                system.Execute(  );

                // then
                listener.DidNotReceiveWithAnyArgs(  ).OnSelf(null, null, null);
            };

            it["OnSelfRemoved listens self"] = () =>
            {
                // given
                var system   = new EventSystem_SelfRemoved <ScopeA, TestCompA>(_contexts);
                var listener = Substitute.For <IOnSelfRemoved <ScopeA, TestCompA> >(  );
                listener.DidNotReceiveWithAnyArgs(  ).OnSelfRemoved(null, null, null);

                // when
                {
                    var ent = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                    ent.Add(new TestCompA(  ));
                    ent.Add_OnSelfRemoved(listener);
                    ent.Remove <TestCompA>(  );
                }

                system.Execute(  );

                // then
                listener.ReceivedWithAnyArgs(  ).OnSelfRemoved(null, null, null);
            };

            it["OnSelfRemoved doesn't listen other"] = () =>
            {
                // given
                var system   = new EventSystem_SelfRemoved <ScopeA, TestCompA>(_contexts);
                var listener = Substitute.For <IOnSelfRemoved <ScopeA, TestCompA> >(  );
                listener.DidNotReceiveWithAnyArgs(  ).OnSelfRemoved(null, null, null);

                // when
                {
                    var ent = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                    ent.Add_OnSelfRemoved(listener);
                }
                {
                    var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                    entity.Add(new TestCompA(  ));
                }

                system.Execute(  );

                // then
                listener.DidNotReceiveWithAnyArgs(  ).OnSelfRemoved(null, null, null);
            };

            it["OnSelfFlag listens self Added"] = () =>
            {
                // given
                var ent      = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                var listener = Substitute.For <IOnSelf <ScopeA, TestFlagA> >(  );
                ent.Add_OnSelf(listener);

                var system = new EventSystem_SelfFlag <ScopeA, TestFlagA>(_contexts);

                // when
                ent.Flag <TestFlagA>(false);
                system.Execute(  );
                // then
                listener.DidNotReceiveWithAnyArgs(  ).OnSelf(null, null, null);

                // when
                ent.Flag <TestFlagA>(true);
                system.Execute(  );
                // then
                listener.Received(  ).OnSelf(null, ent, _contexts);
            };

            it["OnSelfFlag listens self Removed"] = () =>
            {
                // given
                var ent      = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                var listener = Substitute.For <IOnSelf <ScopeA, TestFlagA> >(  );
                ent.Add_OnSelf(listener);
                ent.Flag <TestFlagA>(true);

                var system = new EventSystem_SelfFlag <ScopeA, TestFlagA>(_contexts);

                // when
                ent.Flag <TestFlagA>(true);
                system.Execute(  );
                // then
                listener.DidNotReceiveWithAnyArgs(  ).OnSelf(null, null, null);

                // when
                ent.Flag <TestFlagA>(false);
                system.Execute(  );
                // then
                listener.Received(  ).OnSelf(null, ent, _contexts);
            };
        }
        private void                                    test_Entity_ClassMethods(  )
        {
            Lookup_ScopeManager.RegisterAll();

            before = () =>
            {
                _contexts = new Contexts(  );
                _contexts.AddScopedContexts(  );
            };

            it["add component"] = () =>
            {
                // given
                var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                entity.Has <TestCompA>(  ).should_be_false(  );

                // when
                entity.Add(new TestCompA(  ));

                // then
                entity.Has <TestCompA>(  ).should_be_true(  );
            };

            it["add twice throws"] = () =>
            {
                // given
                var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                entity.Has <TestCompA>(  ).should_be_false(  );

                // when
                entity.Add(new TestCompA(  ));

                // then
                Action act = () =>
                {
                    entity.Add(new TestCompA(  ));
                };
                act.ShouldThrow <EntityAlreadyHasComponentException>(  );
            };

            it["remove component"] = () =>
            {
                // given
                var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                entity.Add(new TestCompA(  ));
                entity.Has <TestCompA>(  ).should_be_true(  );

                // when
                entity.Remove <TestCompA>();

                // then
                entity.Has <TestCompA>(  ).should_be_false(  );
            };

            it["remove inexistent throws"] = () =>
            {
                // given
                var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );

                // when
                Action act = () =>
                {
                    entity.Remove <TestCompA>(  );
                };

                // then
                act.ShouldThrow <EntityDoesNotHaveComponentException>(  );
            };

            it["replace component"] = () =>
            {
                // given
                var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                entity.Add(Cache <TestCompA> .I.Set(1f));
                entity.Get <TestCompA>(  ).Value.should_be(1f);

                // when
                entity.Replace(Cache <TestCompA> .I.Set(2f));
                Cache <TestCompA> .I.Set(3f);

                // then
                entity.Get <TestCompA>(  ).Value.should_be(2f);
            };

            it["ICreateApply. Create component"] = () =>
            {
                // given
                var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );

                // when
                var comp = entity.Create <TestComp_CreateApply_A>(  );

                // then
                entity.Has <TestComp_CreateApply_A>(  ).should_be_false(  );
            };

            it["ICreateApply. Apply component"] = () =>
            {
                // given
                var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );

                // when
                var comp = entity.Create <TestComp_CreateApply_A>(  );
                comp.Value.should_be(0f);
                comp.Value = 1f;
                entity.Has <TestComp_CreateApply_A>(  ).should_be_false(  );
                entity.Apply(comp);

                // then
                entity.Has <TestComp_CreateApply_A>(  ).should_be_true(  );
                entity.Get <TestComp_CreateApply_A>(  ).should_be_same(comp);
            };

            it["Init (Create or Replace) component"] = () =>
            {
                // given
                var entity = _contexts.Get <ScopeA>().CreateEntity();

                // when
                var comp1 = entity.Init <TestComp_InitSet>();

                // then - entity has component at once after Init
                comp1.Value.should_be(0f);
                entity.Has <TestComp_InitSet>().should_be_true();

                // when Remove updated component and Init it again
                comp1.Value = 1f;
                entity.Remove <TestComp_InitSet>();
                var comp2 = entity.Init <TestComp_InitSet>();

                // then
                entity.Has <TestComp_InitSet>().should_be_true();
                // ... it is the same component as comp1, it was taken from components pool
                entity.Get <TestComp_InitSet>().should_be_same(comp1);
                entity.Get <TestComp_InitSet>().should_be_same(comp2);
                // ... and its value is not reset after Init !
                // users have to remember to call .Set method
                comp2.Value.should_be(1);
            };

            it["Init-Set (Create or Replace + Set) component data"] = () =>
            {
                // given
                var entity = _contexts.Get <ScopeA>().CreateEntity();

                // when
                var comp1 = entity.Init <TestComp_InitSet>().Set(1);

                // then
                entity.Has <TestComp_InitSet>().should_be_true();
                comp1.Value.should_be(1);
            };

            it["flag, is"] = () =>
            {
                // given
                var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                entity.Is <TestFlagA>(  ).should_be(false);

                // when
                entity.Flag <TestFlagA>(true);

                // then
                entity.Is <TestFlagA>(  ).should_be(true);

                // when
                entity.Flag <TestFlagA>(false);

                // then
                entity.Is <TestFlagA>(  ).should_be(false);
            };
        }
        private void                                    test_Entity_ClassMethods(  )
        {
            Lookup_ScopeManager.RegisterAll();

            before = () =>
            {
                _contexts = new Contexts(  );
                _contexts.AddScopedContexts(  );
            };

            it["add component"] = () =>
            {
                // given
                var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                entity.Has <TestCompA>(  ).should_be_false(  );

                // when
                entity.Add(new TestCompA(  ));

                // then
                entity.Has <TestCompA>(  ).should_be_true(  );
            };

            it["add twice throws"] = () =>
            {
                // given
                var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                entity.Has <TestCompA>(  ).should_be_false(  );

                // when
                entity.Add(new TestCompA(  ));

                // then
                Action act = () =>
                {
                    entity.Add(new TestCompA(  ));
                };
                act.ShouldThrow <EntityAlreadyHasComponentException>(  );
            };

            it["remove component"] = () =>
            {
                // given
                var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                entity.Add(new TestCompA(  ));
                entity.Has <TestCompA>(  ).should_be_true(  );

                // when
                entity.Remove <TestCompA>();

                // then
                entity.Has <TestCompA>(  ).should_be_false(  );
            };

            it["remove inexistent throws"] = () =>
            {
                // given
                var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );

                // when
                Action act = () =>
                {
                    entity.Remove <TestCompA>(  );
                };

                // then
                act.ShouldThrow <EntityDoesNotHaveComponentException>(  );
            };

            it["replace component"] = () =>
            {
                // given
                var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                entity.Add(Cache <TestCompA> .I.Set(1));
                entity.Get <TestCompA>(  ).Value.should_be(1);

                // when
                entity.Replace(Cache <TestCompA> .I.Set(2));
                Cache <TestCompA> .I.Set(3);

                // then
                entity.Get <TestCompA>(  ).Value.should_be(2);
            };

            it["ICreateApply. Create component"] = () =>
            {
                // given
                var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );

                // when
                var comp = entity.Create <TestComp_CreateApply_A>(  );

                // then
                entity.Has <TestComp_CreateApply_A>(  ).should_be_false(  );
            };

            it["ICreateApply. Apply component"] = () =>
            {
                // given
                var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );

                // when
                var comp = entity.Create <TestComp_CreateApply_A>(  );
                comp.Value.should_be(0f);
                comp.Value = 1f;
                entity.Has <TestComp_CreateApply_A>(  ).should_be_false(  );
                entity.Apply(comp);

                // then
                entity.Has <TestComp_CreateApply_A>(  ).should_be_true(  );
                entity.Get <TestComp_CreateApply_A>(  ).should_be_same(comp);
            };

            it["flag, is"] = () =>
            {
                // given
                var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                entity.Is <TestFlagA>(  ).should_be(false);

                // when
                entity.Flag <TestFlagA>(true);

                // then
                entity.Is <TestFlagA>(  ).should_be(true);

                // when
                entity.Flag <TestFlagA>(false);

                // then
                entity.Is <TestFlagA>(  ).should_be(false);
            };
        }
        private void                                    test_OnSelf_Flag(  )
        {
            Lookup_ScopeManager.RegisterAll(  );

            before = () =>
            {
                _db = new Contexts(  );
                _db.AddScopedContexts(  );
            };

            after = () =>
            {
                // Important
                OnSelf_Flag <ScopeA, TestFlagA> .I = null;
            };

            it["OnSelf_Flag.Invoke is called for each entity in context with Flag class component"] = () =>
            {
                // given
                OnSelf_Flag <ScopeA, TestFlagA> .I = Substitute.For <IEventsFeature2_OnSelf_Flag <ScopeA, TestFlagA> >(  );
                var system = new EventSystem_Self_Flag2 <ScopeA, TestFlagA>(_db);

                // when
                var context = _db.Get <ScopeA>(  );
                var ent     = context.CreateEntity(  );
                ent.Flag <TestFlagA>(true);
                ent.Flag <TestFlagA>(false);

                system.Execute(  );

                // then
                OnSelf_Flag <ScopeA, TestFlagA> .I
                .Received(1)
                .Invoke(ent.creationIndex, ent, context);
            };

            it["OnSelf_Flag.Invoke is not called if value is same as before"] = () =>
            {
                // given
                OnSelf_Flag <ScopeA, TestFlagA> .I = Substitute.For <IEventsFeature2_OnSelf_Flag <ScopeA, TestFlagA> >(  );
                var system = new EventSystem_Self_Flag2 <ScopeA, TestFlagA>(_db);

                // when
                var context = _db.Get <ScopeA>(  );
                var ent     = context.CreateEntity(  );
                ent.Flag <TestFlagA>(false);

                system.Execute(  );

                // then
                OnSelf_Flag <ScopeA, TestFlagA> .I
                .DidNotReceiveWithAnyArgs(  )
                .Invoke(0, null, null);
            };

            it["OnSelf_Flag listens once self ent with matching Flag class component"] = () =>
            {
                // given
                var system   = new EventSystem_Self_Flag2 <ScopeA, TestFlagA>(_db);
                var listener = Substitute.For <IEventsFeature2_OnSelfSubscriber <ScopeA, TestFlagA> >(  );

                // when
                var ent = _db.Get <ScopeA>(  ).CreateEntity(  );
                ent.Flag <TestFlagA>(true);
                ent.Flag <TestFlagA>(false);
                OnSelf_Flag <ScopeA, TestFlagA> .I.Sub(ent.creationIndex, listener.OnSelf);

                system.Execute(  );
                system.Execute(  );

                // then
                listener
                .Received(1)
                .OnSelf(ent);
            };

            it["OnSelf_Flag doesn't listen after OnSelf.Unsub"] = () =>
            {
                // given
                var system   = new EventSystem_Self_Flag2 <ScopeA, TestFlagA>(_db);
                var listener = Substitute.For <IEventsFeature2_OnSelfSubscriber <ScopeA, TestFlagA> >(  );

                var ent = _db.Get <ScopeA>(  ).CreateEntity(  );
                ent.Flag <TestFlagA>(true);
                OnSelf_Flag <ScopeA, TestFlagA> .I.Sub(ent.creationIndex, listener.OnSelf);

                // when
                OnSelf_Flag <ScopeA, TestFlagA> .I.Unsub(ent.creationIndex, listener.OnSelf);

                system.Execute(  );

                // then
                listener
                .DidNotReceiveWithAnyArgs(  )
                .OnSelf(null);
            };

            it["OnSelf_Flag doesn't listen other ent with matching class component"] = () =>
            {
                // given
                var system   = new EventSystem_Self_Flag2 <ScopeA, TestFlagA>(_db);
                var listener = Substitute.For <IEventsFeature2_OnSelfSubscriber <ScopeA, TestFlagA> >(  );

                // when
                {
                    var ent = _db.Get <ScopeA>(  ).CreateEntity(  );
                    OnSelf_Flag <ScopeA, TestFlagA> .I.Sub(ent.creationIndex, listener.OnSelf);
                }
                {
                    var ent = _db.Get <ScopeA>(  ).CreateEntity(  );
                    ent.Flag <TestFlagA>(true);
                    ent.Flag <TestFlagA>(false);
                }

                system.Execute(  );

                // then
                listener
                .DidNotReceiveWithAnyArgs(  )
                .OnSelf(null);
            };
        }
        private void                                    test_OnSelf_Removed(  )
        {
            Lookup_ScopeManager.RegisterAll(  );

            before = () =>
            {
                _db = new Contexts(  );
                _db.AddScopedContexts(  );
            };

            after = () =>
            {
                // Important
                OnSelf <ScopeA, TestCompA> .I         = null;
                OnSelf <ScopeA, TestDataA> .I         = null;
                OnSelf_Removed <ScopeA, TestCompA> .I = null;
                OnSelf_Removed <ScopeA, TestDataA> .I = null;
            };

            it["OnSelf_Removed.Invoke is called for each entity in context with removed class component"] = () =>
            {
                // given
                OnSelf_Removed <ScopeA, TestCompA> .I = Substitute.For <IEventsFeature2_OnSelf_Removed <ScopeA, TestCompA> >(  );
                var system = new EventSystem_Self_Removed2 <ScopeA, TestCompA>(_db);

                // when
                var context = _db.Get <ScopeA>(  );
                var ent     = context.CreateEntity(  );
                ent.Add(new TestCompA(  ));
                ent.Remove <TestCompA>();

                system.Execute(  );

                // then
                OnSelf_Removed <ScopeA, TestCompA> .I
                .Received(1)
                .Invoke(ent.creationIndex, ent, context);
            };

            it["OnSelf_Removed.Invoke is not called for entities in context with class component"] = () =>
            {
                // given
                OnSelf_Removed <ScopeA, TestCompA> .I = Substitute.For <IEventsFeature2_OnSelf_Removed <ScopeA, TestCompA> >(  );
                var system = new EventSystem_Self_Removed2 <ScopeA, TestCompA>(_db);

                // when
                var context = _db.Get <ScopeA>(  );
                var ent     = context.CreateEntity(  );
                ent.Add(new TestCompA(  ));
                ent.Remove <TestCompA>();
                ent.Add(new TestCompA(  ));

                system.Execute(  );

                // then
                OnSelf_Removed <ScopeA, TestCompA> .I
                .DidNotReceiveWithAnyArgs(  )
                .Invoke(0, null, null);
            };

            it["OnSelf_Removed listens once self ent with matching removed class component"] = () =>
            {
                // given
                var system   = new EventSystem_Self_Removed2 <ScopeA, TestCompA>(_db);
                var listener = Substitute.For <IEventsFeature2_OnSelfSubscriber <ScopeA, TestCompA> >(  );

                // when
                var ent = _db.Get <ScopeA>(  ).CreateEntity(  );
                ent.Add(new TestCompA(  ));
                ent.Remove <TestCompA>(  );
                OnSelf_Removed <ScopeA, TestCompA> .I.Sub(ent.creationIndex, listener.OnSelf);

                system.Execute(  );
                system.Execute(  );

                // then
                listener
                .Received(1)
                .OnSelf(ent);
            };

            it["OnSelf_Removed listens once self ent with matching structComp"] = () =>
            {
                // given
                var system   = new EventSystem_Self_Removed2 <ScopeA, TestDataA>(_db);
                var listener = Substitute.For <IEventsFeature2_OnSelfSubscriber <ScopeA, TestDataA> >(  );

                // when
                var ent = _db.Get <ScopeA>(  ).CreateEntity(  );
                ent.Add_(new TestDataA(  ));
                ent.Remove_ <TestDataA>();
                OnSelf_Removed <ScopeA, TestDataA> .I.Sub(ent.creationIndex, listener.OnSelf);

                system.Execute(  );
                system.Execute(  );

                // then
                listener
                .Received(1)
                .OnSelf(ent);
            };

            it["OnSelf_Removed doesn't listen after OnSelf.Unsub"] = () =>
            {
                // given
                var system   = new EventSystem_Self_Removed2 <ScopeA, TestCompA>(_db);
                var listener = Substitute.For <IEventsFeature2_OnSelfSubscriber <ScopeA, TestCompA> >(  );

                var ent = _db.Get <ScopeA>(  ).CreateEntity(  );
                ent.Add(new TestCompA(  ));
                ent.Remove <TestCompA>(  );
                OnSelf_Removed <ScopeA, TestCompA> .I.Sub(ent.creationIndex, listener.OnSelf);

                // when
                OnSelf_Removed <ScopeA, TestCompA> .I.Unsub(ent.creationIndex, listener.OnSelf);

                system.Execute(  );

                // then
                listener
                .DidNotReceiveWithAnyArgs(  )
                .OnSelf(null);
            };

            it["OnSelf_Removed doesn't listen other ent with matching class component"] = () =>
            {
                // given
                var system   = new EventSystem_Self_Removed2 <ScopeA, TestCompA>(_db);
                var listener = Substitute.For <IEventsFeature2_OnSelfSubscriber <ScopeA, TestCompA> >(  );

                // when
                {
                    var ent = _db.Get <ScopeA>(  ).CreateEntity(  );
                    OnSelf_Removed <ScopeA, TestCompA> .I.Sub(ent.creationIndex, listener.OnSelf);
                }
                {
                    var ent = _db.Get <ScopeA>(  ).CreateEntity(  );
                    ent.Add(new TestCompA(  ));
                    ent.Remove <TestCompA>(  );
                }

                system.Execute(  );

                // then
                listener
                .DidNotReceiveWithAnyArgs(  )
                .OnSelf(null);
            };
        }
Пример #10
0
        private void                                    test_PrimaryEntityIndex_GetSingleEntBy(  )
        {
            Lookup_ScopeManager.RegisterAll();

            before = () =>
            {
                _contexts = new Contexts(  );
                _contexts.AddScopedContexts(  );
            };

            it["gets 0 entity by non-matching index"] = () =>
            {
                // given
                var indexKey = "indexKey";
                var contextB = _contexts.Get <ScopeB>(  );
                contextB.AddPrimaryEntityIndex(
                    indexKey
                    , contextB.GetGroup(Matcher <ScopeB, TestCompB> .I)
                    , (e, c) => ((TestCompB )c).Value);

                var entity = contextB.CreateEntity(  );
                entity.Add(new TestCompB(  ).Set(1));

                // when
                var ent = contextB.GetSingleEntBy <ScopeB, TestCompB, Int32>(indexKey, 1);

                // then
                ent.should_be_same(entity);
            };

            it["gets 1 entity by matching index"] = () =>
            {
                // given
                var indexKey = "indexKey";
                var contextB = _contexts.Get <ScopeB>(  );
                contextB.AddPrimaryEntityIndex(
                    indexKey
                    , contextB.GetGroup(Matcher <ScopeB, TestCompB> .I)
                    , (e, c) => ((TestCompB )c).Value);

                var entity = contextB.CreateEntity(  );
                entity.Add(new TestCompB(  ).Set(1));

                // when
                var ent = contextB.GetSingleEntBy <ScopeB, TestCompB, Int32>(indexKey, 1);

                // then
                ent.should_be_same(entity);
            };

            it["add same value twice throws"] = () =>
            {
                // given
                var indexKey = "indexKey";
                var contextB = _contexts.Get <ScopeB>(  );
                contextB.AddPrimaryEntityIndex(
                    indexKey
                    , contextB.GetGroup(Matcher <ScopeB, TestCompB> .I)
                    , (e, c) => ((TestCompB )c).Value);

                {
                    var entity = contextB.CreateEntity(  );
                    entity.Add(new TestCompB(  ).Set(1));
                }

                // when
                Action act = () =>
                {
                    var entity = contextB.CreateEntity(  );
                    entity.Add(new TestCompB(  ).Set(1));
                };

                // then
                act.ShouldThrow <EntityIndexException>(  );
            };
        }
Пример #11
0
        private void                                    test_OnAny(  )
        {
            Lookup_ScopeManager.RegisterAll(  );

            before = () =>
            {
                _db = new Contexts(  );
                _db.AddScopedContexts(  );
            };

            after = () =>
            {
                // Important
                OnAny <ScopeA, TestCompA> .I = null;
                OnAny <ScopeA, TestDataA> .I = null;
            };

            it["OnAny.Invoke is called for each entity in context with class component match"] = () =>
            {
                // given
                OnAny <ScopeA, TestCompA> .I = Substitute.For <IEventsFeature2_OnAny <ScopeA, TestCompA> >(  );
                var system = new EventSystem_Any2 <ScopeA, TestCompA>(_db);

                // when
                var context = _db.Get <ScopeA>(  );
                var ent     = context.CreateEntity(  );
                ent.Add(new TestCompA(  ));

                system.Execute(  );

                // then
                OnAny <ScopeA, TestCompA> .I
                .Received(1)
                .Invoke(ent, context);

                OnAny <ScopeA, TestCompA> .I = null;
            };

            it["OnAny.Invoke is not called for entities in context without class component"] = () =>
            {
                // given
                OnAny <ScopeA, TestCompA> .I = Substitute.For <IEventsFeature2_OnAny <ScopeA, TestCompA> >(  );
                var system = new EventSystem_Any2 <ScopeA, TestCompA>(_db);

                // when
                var context = _db.Get <ScopeA>(  );
                var ent     = context.CreateEntity(  );
                ent.Add(new TestCompA(  ));
                ent.Remove <TestCompA>();

                system.Execute(  );

                // then
                OnAny <ScopeA, TestCompA> .I
                .DidNotReceiveWithAnyArgs(  )
                .Invoke(null, null);
            };

            it["OnAny listens once Any ent with matching class component"] = () =>
            {
                // given
                var system   = new EventSystem_Any2 <ScopeA, TestCompA>(_db);
                var listener = Substitute.For <IEventsFeature2_OnAnySubscriber <ScopeA, TestCompA> >(  );

                // when
                var ent = _db.Get <ScopeA>(  ).CreateEntity(  );
                ent.Add(new TestCompA(  ));
                OnAny <ScopeA, TestCompA> .I.Sub(listener.OnAny);

                system.Execute(  );
                system.Execute(  );

                // then
                listener
                .Received(1)
                .OnAny(ent);
            };

            it["OnAny listens once Any ent with matching structComp"] = () =>
            {
                // given
                var system   = new EventSystem_Any2 <ScopeA, TestDataA>(_db);
                var listener = Substitute.For <IEventsFeature2_OnAnySubscriber <ScopeA, TestDataA> >(  );

                // when
                var ent = _db.Get <ScopeA>(  ).CreateEntity(  );
                ent.Add_(new TestDataA(  ));
                OnAny <ScopeA, TestDataA> .I.Sub(listener.OnAny);

                system.Execute(  );
                system.Execute(  );

                // then
                listener
                .Received(1)
                .OnAny(ent);
            };

            it["OnAny doesn't listen after OnAny.Unsub"] = () =>
            {
                // given
                var system   = new EventSystem_Any2 <ScopeA, TestCompA>(_db);
                var listener = Substitute.For <IEventsFeature2_OnAnySubscriber <ScopeA, TestCompA> >(  );

                var ent = _db.Get <ScopeA>(  ).CreateEntity(  );
                ent.Add(new TestCompA(  ));
                OnAny <ScopeA, TestCompA> .I.Sub(listener.OnAny);

                // when
                OnAny <ScopeA, TestCompA> .I.Unsub(listener.OnAny);

                system.Execute(  );

                // then
                listener
                .DidNotReceiveWithAnyArgs(  )
                .OnAny(null);
            };

            it["OnAny listens other ent with matching class component"] = () =>
            {
                // given
                var system   = new EventSystem_Any2 <ScopeA, TestCompA>(_db);
                var listener = Substitute.For <IEventsFeature2_OnAnySubscriber <ScopeA, TestCompA> >(  );

                // when
                {
                    var ent = _db.Get <ScopeA>(  ).CreateEntity(  );
                    OnAny <ScopeA, TestCompA> .I.Sub(listener.OnAny);
                }
                var entity = _db.Get <ScopeA>(  ).CreateEntity(  );
                entity.Add(new TestCompA(  ));

                system.Execute(  );

                // then
                listener
                .Received(  )
                .OnAny(entity);
            };
        }
Пример #12
0
        private void                                    test_EntityIndex_GetAllEntsBy(  )
        {
            Lookup_ScopeManager.RegisterAll();

            before = () =>
            {
                _contexts = new Contexts(  );
                _contexts.AddScopedContexts(  );
            };

            it["gets 0 entity by non-matching index"] = () =>
            {
                // given
                var indexKey = "indexKey";
                var contextA = _contexts.Get <ScopeA>(  );
                contextA.AddEntityIndex(
                    indexKey
                    , contextA.GetGroup(Matcher <ScopeA, TestCompA> .I)
                    , (e, c) => ((TestCompA )c).Value);

                {
                    var entity = contextA.CreateEntity(  );
                    entity.Add(new TestCompA(  ).Set(1));
                }

                {
                    var entity = contextA.CreateEntity(  );
                    entity.Add(new TestCompA(  ).Set(2));
                }

                {
                    var entity = contextA.CreateEntity(  );
                    entity.Add(new TestCompA(  ).Set(2));
                }

                // when
                var ents = contextA.GetAllEntsBy <ScopeA, TestCompA, Int32>(indexKey, 100);

                // then
                ents.Count.should_be(0);
            };

            it["gets correct amount of entities by matching index"] = () =>
            {
                // given
                var indexKey = "indexKey";
                var contextA = _contexts.Get <ScopeA>(  );
                contextA.AddEntityIndex(
                    indexKey
                    , contextA.GetGroup(Matcher <ScopeA, TestCompA> .I)
                    , (e, c) => ((TestCompA )c).Value);

                {
                    var entity = contextA.CreateEntity(  );
                    entity.Add(new TestCompA(  ).Set(1));
                }

                {
                    var entity = contextA.CreateEntity(  );
                    entity.Add(new TestCompA(  ).Set(2));
                }

                {
                    var entity = contextA.CreateEntity(  );
                    entity.Add(new TestCompA(  ).Set(2));
                }

                {
                    // when
                    var ents = contextA.GetAllEntsBy <ScopeA, TestCompA, Int32>(indexKey, 1);

                    // then
                    ents.Count.should_be(1);
                }

                {
                    // when
                    var ents = contextA.GetAllEntsBy <ScopeA, TestCompA, Int32>(indexKey, 2);

                    // then
                    ents.Count.should_be(2);
                }
            };
        }
        private void                                    test_Entity_StructMethods(  )
        {
            Lookup_ScopeManager.RegisterAll();

            before = () =>
            {
                _contexts = new Contexts(  );
                _contexts.AddScopedContexts(  );
            };

            it["add component"] = () =>
            {
                // given
                var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                entity.Has_ <TestDataA>(  ).should_be_false(  );

                // when
                entity.Add_(new TestDataA(21f));

                // then
                entity.Has_ <TestDataA>(  ).should_be_true(  );
            };

            it["add twice throws"] = () =>
            {
                // given
                var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                entity.Has_ <TestDataA>(  ).should_be_false(  );

                // when
                entity.Add_(new TestDataA(21f));

                // then
                Action act = () =>
                {
                    entity.Add_(new TestDataA(32f));
                };
                act.ShouldThrow <EntityAlreadyHasComponentException>(  );
            };

            it["remove component"] = () =>
            {
                // given
                var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                entity.Add_(new TestDataA(  ));
                entity.Has_ <TestDataA>(  ).should_be_true(  );

                // when
                entity.Remove_ <TestDataA>(  );

                // then
                entity.Has_ <TestDataA>(  ).should_be_false(  );
            };

            it["remove inexistent throws"] = () =>
            {
                // given
                var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );

                // when
                Action act = () =>
                {
                    entity.Remove_ <TestDataA>(  );
                };

                // then
                act.ShouldThrow <EntityDoesNotHaveComponentException>(  );
            };

            it["replace component"] = () =>
            {
                // given
                var entity = _contexts.Get <ScopeA>(  ).CreateEntity(  );
                entity.Add_(new TestDataA(1f));
                entity.Get_ <TestDataA>(  ).Value.should_be(1f);

                // when
                entity.Replace_(new TestDataA(2f));

                // then
                entity.Get_ <TestDataA>(  ).Value.should_be(2f);
            };
        }
Пример #14
0
        private void                                    test_StructMethods(  )
        {
            Lookup_ScopeManager.RegisterAll();
            before = () =>
            {
                _contexts = new Contexts(  );
                _contexts.AddScopedContexts(  );
            };

            it["Set component"] = () =>
            {
                // given
                var context = _contexts.Get <ScopeA>(  );
                context.Has_ <TestDataAUnique>(  ).should_be_false(  );

                // when
                var entity = context.Set_(new TestDataAUnique(  ));

                // then
                context.Has_ <TestDataAUnique>(  ).should_be_true(  );
            };

            it["Set twice throws"] = () =>
            {
                // given
                var context = _contexts.Get <ScopeA>(  );
                context.Has_ <TestDataAUnique>(  ).should_be_false(  );

                // when
                var entity = context.Set_(new TestDataAUnique(  ));

                // then
                context.Has_ <TestDataAUnique>(  ).should_be_true(  );

                // then
                Action act = () =>
                {
                    context.Set_(new TestDataAUnique(  ));
                };
                act.ShouldThrow <Exception>(  );
            };

            it["Remove component"] = () =>
            {
                // given
                var context = _contexts.Get <ScopeA>(  );
                context.Has_ <TestDataAUnique>(  ).should_be_false(  );

                var entity = context.Set_(new TestDataAUnique(  ));
                context.Has_ <TestDataAUnique>(  ).should_be_true(  );

                // when
                context.Remove_ <TestDataAUnique>(  );

                // then
                context.Has_ <TestDataAUnique>(  ).should_be_false(  );
            };

            it["Remove inexistent throws"] = () =>
            {
                // given
                var context = _contexts.Get <ScopeA>(  );
                context.Has_ <TestDataAUnique>(  ).should_be_false(  );

                // then
                Action act = () =>
                {
                    context.Remove_ <TestDataAUnique>(  );
                };
                act.ShouldThrow <Exception>(  );
            };

            it["Replace component"] = () =>
            {
                // given
                var context = _contexts.Get <ScopeA>(  );
                context.Has_ <TestDataAUnique>(  ).should_be_false(  );

                context.Set_(new TestDataAUnique(1f));
                context.Get_ <TestDataAUnique>(  ).Value.should_be(1f);

                // when
                context.Replace_(new TestDataAUnique(2f));

                // then
                context.Get_ <TestDataAUnique>(  ).Value.should_be(2f);
            };
        }
        private void                                    test_CoreMethods(  )
        {
            Lookup_ScopeManager.RegisterAll();
            before = () =>
            {
                _contexts = new Contexts(  );
                _contexts.AddScopedContexts(  );
            };

            it["Set component"] = () =>
            {
                // given
                var context = _contexts.Get <ScopeA>(  );
                context.Has <TestCompAUnique>(  ).should_be_false(  );

                // when
                var entity = context.Set(new TestCompAUnique(  ));

                // then
                context.Has <TestCompAUnique>(  ).should_be_true(  );
            };

            it["Set twice throws"] = () =>
            {
                // given
                var context = _contexts.Get <ScopeA>(  );
                context.Has <TestCompAUnique>(  ).should_be_false(  );

                // when
                var entity = context.Set(new TestCompAUnique(  ));

                // then
                context.Has <TestCompAUnique>(  ).should_be_true(  );

                // then
                Action act = () =>
                {
                    context.Set(new TestCompAUnique(  ));
                };
                act.ShouldThrow <Exception>(  );
            };

            it["Remove component"] = () =>
            {
                // given
                var context = _contexts.Get <ScopeA>(  );
                context.Has <TestCompAUnique>(  ).should_be_false(  );

                var entity = context.Set(new TestCompAUnique(  ));
                context.Has <TestCompAUnique>(  ).should_be_true(  );

                // when
                context.Remove <TestCompAUnique>(  );

                // then
                context.Has <TestCompAUnique>(  ).should_be_false(  );
            };

            it["Remove inexistent throws"] = () =>
            {
                // given
                var context = _contexts.Get <ScopeA>(  );
                context.Has <TestCompAUnique>(  ).should_be_false(  );

                // then
                Action act = () =>
                {
                    context.Remove <TestCompAUnique>(  );
                };
                act.ShouldThrow <Exception>(  );
            };

            it["Replace component"] = () =>
            {
                // given
                var context = _contexts.Get <ScopeA>(  );
                context.Has <TestCompAUnique>(  ).should_be_false(  );

                context.Set(Cache <TestCompAUnique> .I.Set(1f));
                context.Get <TestCompAUnique>(  ).Value.should_be(1f);

                // when
                context.Replace(Cache <TestCompAUnique> .I.Set(2f));
                Cache <TestCompAUnique> .I.Set(3f);

                // then
                context.Get <TestCompAUnique>(  ).Value.should_be(2f);
            };

            it["Flag, Is"] = () =>
            {
                // given
                var context = _contexts.Get <ScopeA>(  );
                context.Is <TestFlagAUnique>(  ).should_be_false(  );

                // when
                context.Flag <TestFlagAUnique>(true);

                // then
                context.Is <TestFlagAUnique>(  ).should_be_true(  );
                context.GetEntity <TestFlagAUnique>(  ).should_not_be_null(  );

                // when
                context.Flag <TestFlagAUnique>(false);

                // then
                context.Is <TestFlagAUnique>(  ).should_be_false(  );
                context.GetEntity <TestFlagAUnique>(  ).should_be_null(  );
            };
        }
Пример #16
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Running performance tests...");
            currentProcess = Process.GetCurrentProcess();
            Console.WriteLine($"memory at process start: {MemoryHelper.GetMemoryAllStatsString(currentProcess)}");

            Lookup_ScopeManager.RegisterAll();

            Thread.Sleep(1500);     // why?

            GCSettings.LatencyMode = GCLatencyMode.LowLatency;
            if (!GC.TryStartNoGCRegion(0))
            {
                throw new Exception("can't start NoGCRegion");
            }

            Console.WriteLine($"memory before tests: {MemoryHelper.GetMemoryAllStatsString(currentProcess)}");

            // 1 exclude component-data-init time from test
            run <Entity_AddRemove_CompData_Class>();
            // 2 include component-data-init into test
            // (results can be compared with tests 3 4 5 and 11)
            run <Entity_AddRemove_CompData_Class_AddNewInstance>();
            // 3 replace component data init by Cache.I.Set
            // (results can be compared with tests 2 4 5 and 11)
            run <Entity_AddRemove_CompData_Class_SetUsingCache>();
            // 4 Create-Apply workflow
            // (results can be compared with tests 2, 3 5  and 11)
            run <Entity_AddRemove_CompData_Class_CreateSetApply>();
            // 5 InitSet is a syntax-variant of Create-Apply
            // (results can be compared with tests 2, 3 4 and 11 but especially with 4)
            run <Entity_AddRemove_CompData_Class_InitSet>();
            // 3.2 repeat test#3 to double check stats accuracy
            run <Entity_AddRemove_CompData_Class_SetUsingCache>();

            // 10 base struct Add test
            run <Entity_AddRemove_CompData_Struct>();
            // 11 is like 10 but component initialization is included into test
            // so results can be compared with tests 2-5
            run <Entity_AddRemove_CompData_Struct_IncludeInit>();
            run <Entity_AddRemove_CompData_Class_WithGroups>();
            run <Entity_AddRemove_CompData_Struct_WithGroups>();
            run <Entity_Replace_CompData_Class_WithGroups>();
            run <Entity_Replace_CompData_Struct_WithGroups>();

            run <Entity_Get_CompData_Class>(  );
            run <Entity_Get_CompData_Struct>(  );

            run <Entity_Has_CompData_Class>(  );
            run <Entity_Has_CompData_Struct>(  );

            run <Entity_Flag_CompFlag>(  );
            run <Entity_Is_CompFlag>(  );

            run <Struct_ToString_Implemented>(  );
            run <Struct_ToString_NotImplemented>(  );
            run <Struct_ToString_ToGenericTypeString>(  );    Console.WriteLine($"memory after tests: {MemoryHelper.GetMemoryAllStatsString(currentProcess)}");

            try {
                GC.EndNoGCRegion();
            } catch (System.InvalidOperationException e) {
                Console.WriteLine("\nMemory validation needs to be improved... " +
                                  "\nProcess had to clean memory while in NoGC mode" +
                                  "\nNegative memory diffs indicate that");
                throw;
            }

            // Console.WriteLine("\nPress any key...");
            // Console.Read();
        }