Exemplo n.º 1
0
        public void AllocatesDynamicTypes()
        {
            using var memory = new DynamicAllocator(_logFactory);
            using var buffer = new NativeBuffer(memory);
            var d0 = buffer.Add(new Data()
            {
                x = 10
            });

            buffer.Length.ShouldBe(4);

            var d1 = buffer.Add(new Data1()
            {
                x = 22, y = 33
            });

            buffer.Length.ShouldBe(12);

            var d2 = buffer.Add(new Data2()
            {
                z = 44, b = 2
            });

            buffer.Length.ShouldBe(20);
        }
        public void ShouldResize()
        {
            using var m = new DynamicAllocator(_logFactory);
            using var x = new NativeList <Data>(m);

            var maxLength = x.MaxLength;

            for (var i = 0; i < 1024; i++)
            {
                var shouldGrow = x.Length == x.MaxLength;
                x.Add(new Data()
                {
                    x = 10 + i, y = 11 + i, b = 0x5a
                });
                x.Length.ShouldBe(i + 1);
                if (shouldGrow)
                {
                    x.MaxLength.ShouldNotBe(maxLength);
                    maxLength = x.MaxLength;
                }
                else
                {
                    x.MaxLength.ShouldBe(maxLength);
                }
            }

            for (var i = 0; i < x.Length; i++)
            {
                x[i].x.ShouldBe(10 + i);
                x[i].y.ShouldBe(11 + i);
                x[i].b.ShouldBe(0x5a);
            }
        }
Exemplo n.º 3
0
        public void ShouldReturnSameIdButDifferentVersion()
        {
            using var memory = new DynamicAllocator(_logFactory);
            using var pool   = new PagedObjectPool <int>(memory);
            pool.Take();
            var id = pool.Take();

            pool.Return(id);

            var newid = pool.Take();

            newid.ShouldNotBe(id);
            (newid & 0xffffff).ShouldBe(id & 0xffffff);
        }
Exemplo n.º 4
0
        public void ShouldAllocateFromFront()
        {
            //arrange
            using IAllocator memory = new DynamicAllocator(_logFactory);
            using IAllocator it     = new StackAllocator(memory, 1024);

            //act
            using var handle1 = it.TakeScoped <int>(1, _logFactory);

            //assert
            handle1.Id.ShouldBe((uint)0);

            handle1.Free();
            handle1.Address.ShouldBe(IntPtr.Zero);
            handle1.Id.ShouldBe(0u);
            handle1.Flags.ShouldBe(0u);
        }
Exemplo n.º 5
0
        public void ShoudAllocate()
        {
            //arrange
            using var allocator = new DynamicAllocator(_logFactory);

            //act
            using var handle0 = allocator.TakeScoped(1024);
            using var handle1 = allocator.TakeScoped(1024);

            //assert
            handle0.Address.ShouldNotBe(IntPtr.Zero);
            handle0.IsValid.ShouldBe(true);

            handle1.Address.ShouldNotBe(IntPtr.Zero);
            handle1.IsValid.ShouldBe(true);

            allocator.Size.ShouldBe(2048u);
        }
        public void ShouldAdd()
        {
            using var m = new DynamicAllocator(_logFactory);
            using var x = new NativeList <Data>(m);

            x.Add(new Data()
            {
                x = 10, y = 11, b = 12
            });
            x.Length.ShouldBe(1);
            x.Add(new Data()
            {
                x = 20, y = 21, b = 22
            });
            x.Length.ShouldBe(2);
            x.Add(new Data()
            {
                x = 30, y = 31, b = 32
            });
            x.Length.ShouldBe(3);

            x[0].x.ShouldBe(10);
            x[0].y.ShouldBe(11);
            x[0].b.ShouldBe(12);

            x[1].x.ShouldBe(20);
            x[1].y.ShouldBe(21);
            x[1].b.ShouldBe(22);

            x[2].x.ShouldBe(30);
            x[2].y.ShouldBe(31);
            x[2].b.ShouldBe(32);

            x.RemoveAt(1);
            x.Length.ShouldBe(2);

            x[0].x.ShouldBe(10);
            x[0].y.ShouldBe(11);
            x[0].b.ShouldBe(12);

            x[1].x.ShouldBe(30);
            x[1].y.ShouldBe(31);
            x[1].b.ShouldBe(32);
        }
Exemplo n.º 7
0
        public void ShouldThrowOnUnorderedFree()
        {
            //arrange
            using IAllocator memory = new DynamicAllocator(_logFactory);
            using IAllocator it     = new StackAllocator(memory, 1024);

            //act
            using var handle1 = it.TakeScoped <int>(1, _logFactory);
            var addr1 = handle1.Address;

            using var handle2 = it.TakeScoped <int>(1, _logFactory);
            var addr2 = handle2.Address;

            var allocHandle = handle1.Handle;

            Should.Throw <Exception>(() => it.Free(ref allocHandle));

            //assert
            addr2.ShouldNotBe(addr1);
            handle2.Id.ShouldNotBe(handle1.Id);
        }
Exemplo n.º 8
0
        public void GetsSameAddressBack()
        {
            //arrange
            using IAllocator memory = new DynamicAllocator(_logFactory);
            using IAllocator it     = new StackAllocator(memory, 1024);

            //act
            using var handle1 = it.TakeScoped <int>(1, _logFactory);
            var addr1 = handle1.Address;

            handle1.Free();

            using var handle2 = it.TakeScoped <int>(1, _logFactory);
            var addr2 = handle2.Address;

            handle2.Free();

            //assert
            addr2.ShouldBe(addr1);
            handle2.Id.ShouldBe(handle1.Id);
        }
        public unsafe void ShouldSlice()
        {
            using var memory = new DynamicAllocator(_logFactory);
            using var list   = new NativeList <Data>(memory);

            System.Span <Data> data = stackalloc[] {
                new Data()
                {
                    b = 1, x = 2, y = 3
                },
                new Data()
                {
                    b = 2, x = 1, y = 3
                },
                new Data()
                {
                    b = 3, x = 2, y = 1
                },
                new Data()
                {
                    b = 2, x = 3, y = 1
                },
                new Data()
                {
                    b = 1, x = 3, y = 2
                },
                new Data()
                {
                    b = 2, x = 1, y = 2
                }
            };

            for (var i = 0; i < data.Length; i++)
            {
                list.Add(data[i]);
            }


            var slice = list.Slice();

            slice.Length.ShouldBe(6);
            for (var i = 0; i < data.Length; i++)
            {
                slice[i].x.ShouldBe(data[i].x);
                slice[i].b.ShouldBe(data[i].b);
                slice[i].y.ShouldBe(data[i].y);
            }

            slice = list.Slice(2);
            slice.Length.ShouldBe(4);
            for (var i = 2; i < data.Length; i++)
            {
                slice[i - 2].x.ShouldBe(data[i].x);
                slice[i - 2].b.ShouldBe(data[i].b);
                slice[i - 2].y.ShouldBe(data[i].y);
            }

            slice = list.Slice(2, 2);
            slice.Length.ShouldBe(2);
            for (var i = 2; i < 4; i++)
            {
                slice[i - 2].x.ShouldBe(data[i].x);
                slice[i - 2].b.ShouldBe(data[i].b);
                slice[i - 2].y.ShouldBe(data[i].y);
            }
        }
    }