Пример #1
0
            public void WhenBothAreMemoryOwners_ShouldSwap()
            {
                MemorySource <int> a = this.AllocateMemorySource <int>(13);
                MemorySource <int> b = this.AllocateMemorySource <int>(17);

                IMemoryOwner <int> aa = a.MemoryOwner;
                IMemoryOwner <int> bb = b.MemoryOwner;

                Memory <int> aaa = a.Memory;
                Memory <int> bbb = b.Memory;

                MemorySource <int> .SwapOrCopyContent(ref a, ref b);

                Assert.Equal(bb, a.MemoryOwner);
                Assert.Equal(aa, b.MemoryOwner);

                Assert.Equal(bbb, a.Memory);
                Assert.Equal(aaa, b.Memory);
                Assert.NotEqual(a.Memory, b.Memory);
            }
Пример #2
0
            public void WhenDestIsNotMemoryOwner_DifferentSize_Throws(bool sourceIsOwner)
            {
                var data  = new Rgba32[21];
                var color = new Rgba32(1, 2, 3, 4);

                var destOwner = new TestMemoryManager <Rgba32>(data);
                var dest      = new MemorySource <Rgba32>(destOwner.Memory);

                IMemoryOwner <Rgba32> sourceOwner = this.MemoryAllocator.Allocate <Rgba32>(22);

                MemorySource <Rgba32> source = sourceIsOwner
                                                   ? new MemorySource <Rgba32>(sourceOwner, true)
                                                   : new MemorySource <Rgba32>(sourceOwner.Memory);

                sourceOwner.Memory.Span[10] = color;

                // Act:
                Assert.ThrowsAny <InvalidOperationException>(() => MemorySource <Rgba32> .SwapOrCopyContent(ref dest, ref source));

                Assert.Equal(color, source.Memory.Span[10]);
                Assert.NotEqual(color, dest.Memory.Span[10]);
            }
Пример #3
0
            public void WhenDestIsNotMemoryOwner_SameSize_ShouldCopy(bool sourceIsOwner, bool isInternalMemorySource)
            {
                var data  = new Rgba32[21];
                var color = new Rgba32(1, 2, 3, 4);

                var destOwner = new TestMemoryManager <Rgba32>(data);
                var dest      = new MemorySource <Rgba32>(destOwner.Memory);

                IMemoryOwner <Rgba32> sourceOwner = this.MemoryAllocator.Allocate <Rgba32>(21);

                MemorySource <Rgba32> source = sourceIsOwner
                                                   ? new MemorySource <Rgba32>(sourceOwner, isInternalMemorySource)
                                                   : new MemorySource <Rgba32>(sourceOwner.Memory);

                sourceOwner.Memory.Span[10] = color;

                // Act:
                MemorySource <Rgba32> .SwapOrCopyContent(ref dest, ref source);

                // Assert:
                Assert.Equal(color, dest.Memory.Span[10]);
                Assert.NotEqual(sourceOwner, dest.MemoryOwner);
                Assert.NotEqual(destOwner, source.MemoryOwner);
            }