Esempio n. 1
0
        public void Test_ArrayPoolBufferWriterOfT_AllocateAndGetMemoryAndSpan()
        {
            var writer = new ArrayPoolBufferWriter <byte>();

            Assert.AreEqual(writer.Capacity, 256);
            Assert.AreEqual(writer.FreeCapacity, 256);
            Assert.AreEqual(writer.WrittenCount, 0);
            Assert.IsTrue(writer.WrittenMemory.IsEmpty);
            Assert.IsTrue(writer.WrittenSpan.IsEmpty);

            Span <byte> span = writer.GetSpan(43);

            Assert.IsTrue(span.Length >= 43);

            writer.Advance(43);

            Assert.AreEqual(writer.Capacity, 256);
            Assert.AreEqual(writer.FreeCapacity, 256 - 43);
            Assert.AreEqual(writer.WrittenCount, 43);
            Assert.AreEqual(writer.WrittenMemory.Length, 43);
            Assert.AreEqual(writer.WrittenSpan.Length, 43);

            Assert.ThrowsException <ArgumentOutOfRangeException>(() => writer.Advance(-1));
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => writer.GetMemory(-1));
            Assert.ThrowsException <ArgumentException>(() => writer.Advance(1024));

            writer.Dispose();

            Assert.ThrowsException <ObjectDisposedException>(() => writer.WrittenMemory);
            Assert.ThrowsException <ObjectDisposedException>(() => writer.WrittenSpan.Length);
            Assert.ThrowsException <ObjectDisposedException>(() => writer.Capacity);
            Assert.ThrowsException <ObjectDisposedException>(() => writer.FreeCapacity);
            Assert.ThrowsException <ObjectDisposedException>(() => writer.Clear());
            Assert.ThrowsException <ObjectDisposedException>(() => writer.Advance(1));
        }
        internal static void Serialize(ref MessagePackWriter writer, T value, MessagePackSerializerOptions options, ArrayPoolBufferWriter <byte> bodyWriter, IMessagePackFormatter <T> formatterT)
        {
            MessagePackWriter cloneWriter = writer.Clone(bodyWriter);

            formatterT.Serialize(ref cloneWriter, value, options);
            cloneWriter.Flush();
            writer.WriteArrayHeader(Frame <T> .parallelItemTotElems);
            writer.Write((uint)bodyWriter.WrittenCount);
            writer.WriteRaw(bodyWriter.WrittenSpan);
            bodyWriter.Clear();
        }
Esempio n. 3
0
        public void Test_ArrayPoolBufferWriterOfT_Clear()
        {
            using var writer = new ArrayPoolBufferWriter <byte>();

            Span <byte> span = writer.GetSpan(4).Slice(0, 4);

            byte[] data = { 1, 2, 3, 4 };

            data.CopyTo(span);

            writer.Advance(4);

            Assert.AreEqual(writer.WrittenCount, 4);
            Assert.IsTrue(span.SequenceEqual(data));

            writer.Clear();

            Assert.AreEqual(writer.WrittenCount, 0);
            Assert.IsTrue(span.ToArray().All(b => b == 0));
        }
 public bool Return(ArrayPoolBufferWriter <T> obj)
 {
     obj.Clear();
     return(true);
 }