示例#1
0
    public void AdvanceTo_PriorPositionInPriorBlock()
    {
        MockPool <char> mockPool = new MockPool <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);

        ReadOnlySequence <char> ros  = seq;
        SequencePosition        pos1 = ros.GetPosition(1);
        SequencePosition        pos4 = ros.GetPosition(4);

        seq.AdvanceTo(pos4);
        Assert.Throws <ArgumentException>(() => seq.AdvanceTo(pos1));
        ros = seq;
        Assert.Equal(2, ros.Length);
        Assert.Equal(ros.Length, seq.Length);
    }
示例#2
0
    public void AdvanceTo_LaterPositionInCurrentBlock()
    {
        ReadOnlySpan <char> original = "abcdefg".ToCharArray();
        var seq = new Sequence <char>();

        seq.Write(original);
        var ros = seq.AsReadOnlySequence;

        seq.AdvanceTo(ros.GetPosition(5, ros.Start));
        ros = seq.AsReadOnlySequence;
        Assert.Equal <char>(original.Slice(5).ToArray(), ros.First.ToArray());

        seq.AdvanceTo(ros.GetPosition(2, ros.Start));
        Assert.Equal(0, seq.AsReadOnlySequence.Length);
    }
    public void AdvanceTo_PositionFromUnrelatedSequence()
    {
        MockPool <char> mockPool = new MockPool <char>();
        var             seqA     = new Sequence <char>(mockPool);
        var             seqB     = new Sequence <char>(mockPool);

        var mem1 = seqA.GetMemory(3);

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

        var mem2 = seqB.GetMemory(3);

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

        ReadOnlySequence <char> rosA = seqA;
        ReadOnlySequence <char> rosB = seqB;

        var posB = rosB.GetPosition(2);

        Assert.Throws <ArgumentException>(() => seqA.AdvanceTo(posB));
        Assert.Equal(3, seqA.AsReadOnlySequence.Length);
        Assert.Equal(3, seqB.AsReadOnlySequence.Length);
    }
示例#4
0
    public void AdvanceTo_InterweavedWith_Advance()
    {
        ReadOnlySpan <char> original = "abcdefg".ToCharArray();
        ReadOnlySpan <char> later    = "hijkl".ToCharArray();
        var seq = new Sequence <char>();
        var mem = seq.GetMemory(30); // Specify a size with enough space to store both buffers

        original.CopyTo(mem.Span);
        seq.Advance(original.Length);

        var  originalRos          = seq.AsReadOnlySequence;
        var  origLastCharPosition = originalRos.GetPosition(originalRos.Length - 1);
        char origLastChar         = originalRos.Slice(origLastCharPosition, 1).First.Span[0];

        // "Consume" a few characters, but leave the origEnd an unconsumed position so it should be valid.
        seq.AdvanceTo(originalRos.GetPosition(3, originalRos.Start));

        // Verify that the SequencePosition we saved before still represents the same character.
        Assert.Equal(origLastChar, seq.AsReadOnlySequence.Slice(origLastCharPosition, 1).First.Span[0]);

        // Append several characters
        mem = seq.GetMemory(later.Length);
        later.CopyTo(mem.Span);
        seq.Advance(later.Length);

        // Verify that the SequencePosition we saved before still represents the same character.
        Assert.Equal(origLastChar, seq.AsReadOnlySequence.Slice(origLastCharPosition, 1).First.Span[0]);
    }
    public void AdvanceTo_ReturnsArraysToPool()
    {
        MockPool <char> mockPool = new MockPool <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);
    }
示例#6
0
    public void Reset_AfterPartialAdvance()
    {
        var seq = new Sequence <object>(new MockMemoryPool <object> {
            Contents = { new object[4] }
        });

        seq.Write(new object[3]);
        seq.AdvanceTo(seq.AsReadOnlySequence.GetPosition(2));
        seq.Reset();
    }
示例#7
0
    public void AdvanceTo_ReleasesReferences()
    {
        var seq = new Sequence <object>();

        WeakReference tracker = StoreReferenceInSequence(seq);

        GC.Collect();
        Assert.True(tracker.IsAlive);
        seq.AdvanceTo(seq.AsReadOnlySequence.GetPosition(1));
        GC.Collect();
        Assert.False(tracker.IsAlive);
    }
示例#8
0
    public void Advance_TwoBlocks_Advance()
    {
        var seq = new Sequence <char>();

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

        mem1.Span[0] = 'a';
        mem1.Span[1] = 'b';
        seq.Advance(2);

        Memory <char> mem2 = seq.GetMemory(2);

        mem2.Span[0] = 'c';
        mem2.Span[1] = 'd';
        seq.Advance(2);

        Assert.Equal("abcd".ToCharArray(), seq.AsReadOnlySequence.ToArray());

        seq.AdvanceTo(seq.AsReadOnlySequence.GetPosition(1));
        Assert.Equal("bcd".ToCharArray(), seq.AsReadOnlySequence.ToArray());
        seq.AdvanceTo(seq.AsReadOnlySequence.GetPosition(2));
        Assert.Equal("d".ToCharArray(), seq.AsReadOnlySequence.ToArray());
    }
示例#9
0
    public void AdvanceTo_InterweavedWith_Advance2()
    {
        // use the mock pool so that we can predict the actual array size will not exceed what we ask for.
        var seq = new Sequence <int>(new MockPool <int>());

        seq.GetSpan(10);
        seq.Advance(10);

        seq.AdvanceTo(seq.AsReadOnlySequence.GetPosition(3));

        seq.GetSpan(10);
        seq.Advance(10);

        seq.GetSpan(10);
        seq.Advance(10);

        Assert.Equal(10 - 3 + 10 + 10, seq.AsReadOnlySequence.Length);
    }
示例#10
0
    public void AdvanceTo_InterweavedWith_Advance2()
    {
        // use the mock pool so that we can predict the actual array size will not exceed what we ask for.
        var seq = new Sequence <int>(new MockMemoryPool <int>());

        var span = seq.GetSpan(10);

        Enumerable.Range(1, 10).ToArray().CopyTo(span);
        seq.Advance(10);

        seq.AdvanceTo(seq.AsReadOnlySequence.GetPosition(3));

        span = seq.GetSpan(10);
        Enumerable.Range(11, 10).ToArray().CopyTo(span);
        seq.Advance(10);

        span = seq.GetSpan(10);
        Enumerable.Range(21, 10).ToArray().CopyTo(span);
        seq.Advance(10);

        this.Logger.WriteLine(string.Join(", ", seq.AsReadOnlySequence.ToArray()));
        Assert.Equal(Enumerable.Range(4, 27), seq.AsReadOnlySequence.ToArray());
        Assert.Equal(10 - 3 + 10 + 10, seq.AsReadOnlySequence.Length);
    }
示例#11
0
 public void AdvanceTo_EmptySequence()
 {
     using var seq = new Sequence <byte>();
     seq.AdvanceTo(seq.AsReadOnlySequence.Start);
     Assert.Equal(0, seq.Length);
 }