예제 #1
0
        public void Test_MemoryBufferWriterOfT_AllocateAndGetMemoryAndSpan()
        {
            Memory <byte> memory = new byte[256];

            var writer = new MemoryBufferWriter <byte>(memory);

            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.AreEqual(span.Length, memory.Length);

            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.AreEqual(memory.Length - 43, writer.GetSpan().Length);
            Assert.AreEqual(memory.Length - 43, writer.GetMemory().Length);
            Assert.AreEqual(memory.Length - 43, writer.GetSpan(22).Length);
            Assert.AreEqual(memory.Length - 43, writer.GetMemory(22).Length);

            Assert.ThrowsException <ArgumentOutOfRangeException>(() => writer.Advance(-1));
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => writer.GetMemory(-1));
            Assert.ThrowsException <ArgumentException>(() => writer.GetSpan(1024));
            Assert.ThrowsException <ArgumentException>(() => writer.GetMemory(1024));
            Assert.ThrowsException <ArgumentException>(() => writer.Advance(1024));
        }
예제 #2
0
        public void Test_MemoryBufferWriterOfT_Clear()
        {
            Memory <byte> memory = new byte[256];

            var writer = new MemoryBufferWriter <byte>(memory);

            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));
        }