Beispiel #1
0
    public void GetMemory_TwiceInARowRecyclesOldArray(int leadingBlocks)
    {
        MockMemoryPool <char> mockPool = new MockMemoryPool <char>();
        var seq = new Sequence <char>(mockPool);

        seq.MinimumSpanLength = 1;

        for (int i = 0; i < leadingBlocks; i++)
        {
            seq.GetMemory(1);
            seq.Advance(1);
        }

        Memory <char> mem1 = seq.GetMemory(16);

        // This second request cannot be satisfied by the first one since it's larger. So the first should be freed.
        Memory <char> mem2 = seq.GetMemory(32);

        mockPool.AssertContents(mem1);

        // This third one *can* be satisfied by the 32 byte array allocation requested previously, so no recycling should take place.
        Memory <char> mem3 = seq.GetMemory(24);

        mockPool.AssertContents(mem1);
    }
Beispiel #2
0
    public void AdvanceTo_ReturnsArraysToPool()
    {
        MockMemoryPool <char> mockPool = new MockMemoryPool <char>();
        var seq = new Sequence <char>(mockPool);

        var mem1 = seq.GetMemory(3);

        mem1.Span.Fill('a');
        seq.Advance(mem1.Length);

        var mem2 = seq.GetMemory(3);

        mem2.Span.Fill('b');
        seq.Advance(mem2.Length);

        var mem3 = seq.GetMemory(3);

        mem3.Span.Fill('c');
        seq.Advance(mem3.Length);

        // Assert that the used arrays are not in the pool.
        Assert.Empty(mockPool.Contents);

        // Advance, but don't go beyond the first array.
        seq.AdvanceTo(seq.AsReadOnlySequence.GetPosition(mem1.Length - 1));
        Assert.Empty(mockPool.Contents);

        // Now advance beyond the first array and assert that it has been returned to the pool.
        seq.AdvanceTo(seq.AsReadOnlySequence.GetPosition(1));
        mockPool.AssertContents(mem1);

        // Skip past the second array.
        seq.AdvanceTo(seq.AsReadOnlySequence.GetPosition(mem2.Length));
        mockPool.AssertContents(mem1, mem2);

        // Advance part way through the third array.
        seq.AdvanceTo(seq.AsReadOnlySequence.GetPosition(mem3.Length - 2));
        mockPool.AssertContents(mem1, mem2);

        // Now advance to the end.
        seq.AdvanceTo(seq.AsReadOnlySequence.GetPosition(2));
        Assert.True(seq.AsReadOnlySequence.IsEmpty);
        mockPool.AssertContents(mem1, mem2, mem3);
    }
Beispiel #3
0
    public void Dispose_ReturnsArraysToPool_MemoryPool()
    {
        MockMemoryPool <char> mockPool = new MockMemoryPool <char>();
        var seq      = new Sequence <char>(mockPool);
        var expected = new List <Memory <char> >();

        for (int i = 0; i < 3; i++)
        {
            var mem = seq.GetMemory(3);
            expected.Add(mem);
            seq.Advance(mem.Length);
        }

        seq.Dispose();
        Assert.True(seq.AsReadOnlySequence.IsEmpty);
        mockPool.AssertContents(expected);
    }