Example #1
0
        public void TestNetworkMessageCanBeSerializedAndDeserialized()
        {
            var kernel = new StandardKernel();

            kernel.Load <ProtogameNetworkModule>();
            kernel.Bind <INetworkMessage>().To <TestNetworkMessage>();

            var serializer = kernel.Get <INetworkMessageSerialization>();
            var message    = new TestNetworkMessage
            {
                TestInteger = 5,
                TestString  = "hello world"
            };
            var  data = serializer.Serialize(message);
            Type messageType;
            var  deserialized = serializer.Deserialize(data, out messageType);

            _assert.Same(messageType, typeof(TestNetworkMessage));
            _assert.NotNull(deserialized);
            _assert.IsType <TestNetworkMessage>(deserialized);

            var content = (TestNetworkMessage)deserialized;

            _assert.Equal(5, content.TestInteger);
            _assert.Equal("hello world", content.TestString);
        }
Example #2
0
        public void TestRequireComponentResolution()
        {
            var kernel = new StandardKernel();

            kernel.Load <ProtogameComponentModule>();

            var factory = kernel.Get <IEntityFactory>();

            factory.HierarchyRoot = new object();
            factory.PlanForEntityCreation <EntityA>();
            factory.PlanForEntityCreation <EntityD>();
            var entities = factory.CreateEntities();

            var entityA = entities.OfType <EntityA>().First();
            var entityD = entities.OfType <EntityD>().First();

            _assert.Same(entityA.ComponentA, entityD.ComponentA);
        }
Example #3
0
        public void TestIntArray()
        {
            var poolManager = new DefaultPoolManager();

            var pool = poolManager.NewArrayPool <int>("testintarray", 5, 10, x => { });

            _assert.Equal(5, pool.Free);
            _assert.Equal(0, pool.NextAvailable);
            _assert.Equal(-1, pool.NextReturn);

            var a1 = pool.Get();

            _assert.Equal(4, pool.Free);
            _assert.Equal(1, pool.NextAvailable);
            _assert.Equal(0, pool.NextReturn);

            var a2 = pool.Get();

            _assert.Equal(3, pool.Free);
            _assert.Equal(2, pool.NextAvailable);
            _assert.Equal(0, pool.NextReturn);

            var a3 = pool.Get();

            _assert.Equal(2, pool.Free);
            _assert.Equal(3, pool.NextAvailable);
            _assert.Equal(0, pool.NextReturn);

            var a4 = pool.Get();

            _assert.Equal(1, pool.Free);
            _assert.Equal(4, pool.NextAvailable);
            _assert.Equal(0, pool.NextReturn);

            var a5 = pool.Get();

            _assert.Equal(0, pool.Free);
            _assert.Equal(-1, pool.NextAvailable);
            _assert.Equal(0, pool.NextReturn);

            // Ensure instances are unique.
            _assert.NotSame(a1, a2);
            _assert.NotSame(a1, a3);
            _assert.NotSame(a1, a4);
            _assert.NotSame(a1, a5);
            _assert.NotSame(a2, a3);
            _assert.NotSame(a2, a4);
            _assert.NotSame(a2, a5);
            _assert.NotSame(a3, a4);
            _assert.NotSame(a3, a5);
            _assert.NotSame(a4, a5);

            // Test release
            pool.Release(a3);

            _assert.Equal(1, pool.Free);
            _assert.Equal(0, pool.NextAvailable);
            _assert.Equal(1, pool.NextReturn);

            pool.Release(a4);

            _assert.Equal(2, pool.Free);
            _assert.Equal(0, pool.NextAvailable);
            _assert.Equal(2, pool.NextReturn);

            var b3 = pool.Get();

            _assert.Equal(1, pool.Free);
            _assert.Equal(1, pool.NextAvailable);
            _assert.Equal(2, pool.NextReturn);

            var b4 = pool.Get();

            _assert.Equal(0, pool.Free);
            _assert.Equal(-1, pool.NextAvailable);
            _assert.Equal(2, pool.NextReturn);

            // Check same instances
            _assert.Same(a3, b3);
            _assert.Same(a4, b4);

            // Check offset release
            pool.Release(a1);

            _assert.Equal(1, pool.Free);
            _assert.Equal(2, pool.NextAvailable);
            _assert.Equal(3, pool.NextReturn);

            pool.Release(a2);

            _assert.Equal(2, pool.Free);
            _assert.Equal(2, pool.NextAvailable);
            _assert.Equal(4, pool.NextReturn);

            pool.Release(b3);

            _assert.Equal(3, pool.Free);
            _assert.Equal(2, pool.NextAvailable);
            _assert.Equal(0, pool.NextReturn);

            pool.Release(b4);

            _assert.Equal(4, pool.Free);
            _assert.Equal(2, pool.NextAvailable);
            _assert.Equal(1, pool.NextReturn);

            pool.Release(a5);

            _assert.Equal(5, pool.Free);
            _assert.Equal(2, pool.NextAvailable);
            _assert.Equal(-1, pool.NextReturn);

            // Check offset get
            var c1 = pool.Get();

            _assert.Equal(4, pool.Free);
            _assert.Equal(3, pool.NextAvailable);
            _assert.Equal(2, pool.NextReturn);

            var c2 = pool.Get();

            _assert.Equal(3, pool.Free);
            _assert.Equal(4, pool.NextAvailable);
            _assert.Equal(2, pool.NextReturn);

            var c3 = pool.Get();

            _assert.Equal(2, pool.Free);
            _assert.Equal(0, pool.NextAvailable);
            _assert.Equal(2, pool.NextReturn);

            var c4 = pool.Get();

            _assert.Equal(1, pool.Free);
            _assert.Equal(1, pool.NextAvailable);
            _assert.Equal(2, pool.NextReturn);

            var c5 = pool.Get();

            _assert.Equal(0, pool.Free);
            _assert.Equal(-1, pool.NextAvailable);
            _assert.Equal(2, pool.NextReturn);

            // Make sure reallocation got the right instances
            _assert.Same(a1, c1);
            _assert.Same(a2, c2);
            _assert.Same(a3, c3);
            _assert.Same(a4, c4);
            _assert.Same(a5, c5);
        }