示例#1
0
        public static void ReadWriteUsingArray()
        {
            using var writer = new PooledArrayBufferWriter <byte>(ArrayPool <byte> .Shared, 25);
            True(writer.Capacity >= 25);
            True(writer.WrittenArray.Count == 0);
            Equal(0, writer.WrittenCount);

            var memory = writer.GetArray(100);

            new ArraySegment <byte>(new byte[] { 10, 20, 30 }).CopyTo(memory);
            writer.Advance(3);

            var result = writer.WrittenArray;

            Equal(3, result.Count);
            Equal(3, writer.WrittenCount);
            Equal(10, result[0]);
            Equal(20, result[1]);
            Equal(30, result[2]);

            memory = writer.GetArray(3);
            new ArraySegment <byte>(new byte[] { 40, 50, 60 }).CopyTo(memory);
            writer.Advance(3);

            result = writer.WrittenArray;
            Equal(6, writer.WrittenCount);
            Equal(6, result.Count);
            Equal(10, result[0]);
            Equal(20, result[1]);
            Equal(30, result[2]);
            Equal(40, result[3]);
            Equal(50, result[4]);
            Equal(60, result[5]);
        }
示例#2
0
        public static void WriterAsReadOnlyCollection()
        {
            using var writer = new PooledArrayBufferWriter <int>();
            IReadOnlyList <int> collection = writer;

            Empty(collection);

            writer.Add(42);
            Equal(1, writer.WrittenCount);
            Equal(1, collection.Count);
            Equal(42, collection[0]);
            Throws <IndexOutOfRangeException>(() => collection[1]);
            Equal(42, Enumerable.First(collection));
            Equal(1, Enumerable.Count(collection));

            writer.AddAll(new[] { 43, 44 });
            Equal(3, writer.WrittenCount);
            Equal(3, collection.Count);
            Equal(42, collection[0]);
            Equal(43, collection[1]);
            Equal(44, collection[2]);
            Throws <IndexOutOfRangeException>(() => collection[3]);
            Equal(3, Enumerable.Count(collection));
            Equal(new[] { 42, 43, 44 }, Enumerable.ToArray(collection));
        }
示例#3
0
 public static void PooledArrayBufferWriterDefaultCapacity()
 {
     using (var writer = new PooledArrayBufferWriter <byte>(ArrayPool <byte> .Shared))
         WriteReadUsingSpan(writer);
     using (var writer = new PooledArrayBufferWriter <byte>(ArrayPool <byte> .Shared))
         WriteReadUsingMemory(writer);
 }
示例#4
0
 public static void PooledArrayBufferWriterWithCapacity()
 {
     Throws <ArgumentOutOfRangeException>(new Action(() => new PooledArrayBufferWriter <byte>(ArrayPool <byte> .Shared, 0)));
     using (var writer = new PooledArrayBufferWriter <byte>(ArrayPool <byte> .Shared, 30))
         WriteReadUsingSpan(writer);
     using (var writer = new PooledArrayBufferWriter <byte>(ArrayPool <byte> .Shared, 20))
         WriteReadUsingMemory(writer);
 }
示例#5
0
        public static void WriterAsList()
        {
            using var writer = new PooledArrayBufferWriter <int>();
            IList <int> list = writer;

            False(list.IsReadOnly);
            Empty(writer);
            True(list.IndexOf(0) < 0);
            False(list.Contains(0));
            False(list.Remove(0));
            Throws <ArgumentOutOfRangeException>(() => list.RemoveAt(0));

            list.Add(42);
            Equal(1, writer.WrittenCount);
            Equal(1, list.Count);
            Equal(42, list[0]);
            True(list.Contains(42));
            False(list.Contains(0));
            Equal(0, list.IndexOf(42));
            Throws <ArgumentOutOfRangeException>(() => list[1]);
            Equal(42, Enumerable.First(list));
            Equal(1, Enumerable.Count(list));
            list[0] = 10;
            Equal(10, writer.WrittenArray[0]);

            list.Insert(0, 42);
            Equal(2, writer.WrittenCount);
            Equal(2, list.Count);
            True(list.Contains(10));
            True(list.Contains(42));
            Equal(0, list.IndexOf(42));
            Equal(1, list.IndexOf(10));
            Equal(42, list[0]);
            Equal(10, list[1]);

            list.RemoveAt(0);
            Equal(1, writer.WrittenCount);
            Equal(1, list.Count);
            Equal(10, list[0]);
            True(list.Contains(10));
            False(list.Contains(42));

            True(list.Remove(10));
            Empty(list);
            Throws <ArgumentOutOfRangeException>(() => list.Insert(1, 56));

            list.Insert(0, 56);
            Equal(1, writer.WrittenCount);
            Equal(1, list.Count);
            Equal(56, list[0]);

            //check insertion with overflow
            for (var i = writer.Capacity; i > 0; i--)
            {
                list.Insert(0, i + 100);
            }
        }
示例#6
0
        public static void BufferSizeCallback()
        {
            var counter = new AllocationEventCounter();

            using (var writer = new PooledArrayBufferWriter <byte> {
                BufferSizeCallback = counter.WriteMetric
            })
                writer.Write(new byte[] { 1, 2, 3 });
            True(counter.Value >= 3);
        }
        public string BuildStringUsingPooledArrayBufferWriter()
        {
            using var writer = new PooledArrayBufferWriter <char>();
            for (var i = 0; i < 100; i++)
            {
                writer.Write(StringValue);
                writer.WriteInt32(int.MaxValue);
                writer.WriteLine();
            }

            return(writer.BuildString());
        }
示例#8
0
 public static void RemoveHeadElements()
 {
     using var writer = new PooledArrayBufferWriter <string>();
     writer.Add("a");
     writer.Add("b");
     writer.Add("c");
     writer.RemoveFirst(2);
     Equal(1, writer.WrittenCount);
     Equal("c", writer[0]);
     writer.RemoveFirst(1);
     Equal(0, writer.WrittenCount);
     Throws <ArgumentOutOfRangeException>(() => writer.RemoveFirst(-1));
 }
示例#9
0
        public static void ReuseArrayWriter()
        {
            using var writer = new PooledArrayBufferWriter <byte>();
            var span = writer.GetSpan(10);

            span[0] = 20;
            span[9] = 30;
            writer.Advance(10);
            writer.Clear();

            span    = writer.GetSpan(10);
            span[0] = 40;
            span[9] = 50;
            writer.Advance(10);

            Equal(40, writer.WrittenMemory.Span[0]);
            Equal(50, writer.WrittenMemory.Span[9]);
        }
示例#10
0
        public static void StreamInterop()
        {
            using var writer = new PooledArrayBufferWriter <byte>();
            var span = writer.GetSpan(10);

            new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }.AsSpan().CopyTo(span);
            writer.Advance(10);
            using var stream = StreamSource.GetWrittenBytesAsStream(writer);
            True(stream.CanRead);
            False(stream.CanWrite);
            Equal(0, stream.Position);
            Equal(10, stream.Length);
            var buffer = new byte[10];

            Equal(10, stream.Read(buffer, 0, 10));
            for (var i = 0; i < buffer.Length; i++)
            {
                Equal(i, buffer[i]);
            }
        }
示例#11
0
        public static void StressTest()
        {
            var dict = new Dictionary <string, string>
            {
                { "Key1", "Value1" },
                { "Key2", "Value2" }
            };

            using var writer = new PooledArrayBufferWriter <byte>();
            // serialize dictionary to memory
            using (var output = StreamSource.AsStream(writer))
            {
                DictionarySerializer.Serialize(dict, output);
            }
            // deserialize from memory
            using (var input = StreamSource.AsStream(writer.WrittenArray))
            {
                Equal(dict, DictionarySerializer.Deserialize(input));
            }
        }
示例#12
0
        public static void StressTest()
        {
            var dict = new Dictionary <string, string>
            {
                { "Key1", "Value1" },
                { "Key2", "Value2" }
            };
            var formatter = new BinaryFormatter();

            using var writer = new PooledArrayBufferWriter <byte>();
            // serialize dictionary to memory
            using (var output = StreamSource.AsStream(writer))
            {
                formatter.Serialize(output, dict);
            }
            // deserialize from memory
            using (var input = StreamSource.GetWrittenBytesAsStream(writer))
            {
                Equal(dict, formatter.Deserialize(input));
            }
        }
示例#13
0
        public static void Insertion()
        {
            Span <byte> block = stackalloc byte[] { 10, 20, 30 };

            using var writer = new PooledArrayBufferWriter <byte>();

            writer.Insert(0, block);
            Equal(3, writer.WrittenCount);
            Equal(10, writer[0]);
            Equal(20, writer[1]);
            Equal(30, writer[2]);

            block[0] = 40;
            block[1] = 50;
            block[2] = 60;
            writer.Insert(3, block);
            Equal(6, writer.WrittenCount);
            Equal(10, writer[0]);
            Equal(20, writer[1]);
            Equal(30, writer[2]);
            Equal(40, writer[3]);
            Equal(50, writer[4]);
            Equal(60, writer[5]);

            writer.Clear(true);
            var random = RandomBytes(writer.FreeCapacity);

            writer.Write(random);
            Equal(0, writer.FreeCapacity);

            block[0] = 100;
            block[1] = 110;
            block[2] = 120;

            writer.Insert(writer.WrittenCount - 1, block);
            Equal(random[0..(random.Length - 1)], writer.WrittenMemory.Span.Slice(0, random.Length - 1).ToArray());
示例#14
0
 public void AddRangeToArrayWriter()
 {
     using var writer = new PooledArrayBufferWriter <int>(10);
     writer.Write(inputArray);
 }
示例#15
0
 public void WriteToGrowableBuffer()
 {
     using var buffer = new PooledArrayBufferWriter <byte>();
     using var ms     = buffer.AsStream();
     Write(ms);
 }
示例#16
0
 public static void ReadWriteUsingPooledArrayBufferWriter()
 {
     using var writer = new PooledArrayBufferWriter <byte>();
     Null(writer.As <IGrowableBuffer <byte> >().Capacity);
     ReadWriteTest(writer);
 }