Beispiel #1
0
        public void AppendArrayWhenCountIsZero()
        {
            var controller = new BufferController(new byte[3], 0, 3);

            controller.Append(new byte[0], 0, 0).Should().BeNull();
            controller.Availability.Should().Be(3);
            controller.Count.Should().Be(0);
        }
Beispiel #2
0
        public void AppendBufferListThatIsEmpty()
        {
            var controller = new BufferController(new byte[3], 0, 3);

            controller.Append(Enumerable.Empty <byte[]>()).Should().BeEmpty();
            controller.Availability.Should().Be(3);
            controller.Count.Should().Be(0);
        }
Beispiel #3
0
        public void AppendWithReadDelegateThrowsIfNoAvailability()
        {
            var buffer     = new byte[0];
            var controller = new BufferController(buffer, 0, buffer.Length);

            Invoking(() => controller.Append(Read))
            .Should().Throw <InvalidOperationException>().WithMessage($"{nameof(BufferController)} has no more availability to append further bytes to buffer.");
        }
Beispiel #4
0
        public void AppendEmptyArray()
        {
            var controller = new BufferController(new byte[3], 0, 3);

            controller.Append(new byte[] { }).Should().BeEquivalentTo(new byte[] { });
            controller.Availability.Should().Be(3);
            controller.Count.Should().Be(0);
        }
Beispiel #5
0
        public void AppendWhenNoAvailability()
        {
            var controller = new BufferController(new byte[] { 1, 2, 3 }, 3, 0);

            controller.Append(new byte[] { 4, 5 }).Should().BeEquivalentTo(new byte[] { 4, 5 });
            controller.Availability.Should().Be(0);
            controller.Count.Should().Be(0);
        }
Beispiel #6
0
        public void AppendNullArray()
        {
            var controller = new BufferController(new byte[3], 0, 3);

            controller.Append((byte[])null).Should().BeNull();
            controller.Availability.Should().Be(3);
            controller.Count.Should().Be(0);
        }
Beispiel #7
0
        public void AppendMoreThanAvailable()
        {
            var controller = new BufferController(new byte[3], 0, 3);

            controller.Append(new byte[] { 1, 2, 3, 4, 5 }).Should().BeEquivalentTo(new byte[] { 4, 5 });
            controller.Availability.Should().Be(0);
            controller.Count.Should().Be(3);
        }
Beispiel #8
0
        public void AppendLessThanAvailable()
        {
            var controller = new BufferController(new byte[3], 0, 3);

            controller.Append(new byte[] { 1, 2, 3 }).Should().BeNull();
            controller.Availability.Should().Be(0);
            controller.Count.Should().Be(3);
        }
Beispiel #9
0
        public void AppendArrayWhenNoAvailability()
        {
            var controller = new BufferController(new byte[3], 3, 0);

            controller.Append(new byte[] { 1, 2, 3, 4, 5, 6, 7 }, 2, 5).Should().BeEquivalentTo(new byte[] { 3, 4, 5, 6, 7 });
            controller.Availability.Should().Be(0);
            controller.Count.Should().Be(0);
        }
Beispiel #10
0
        public void AppendBufferListThrows()
        {
            var controller = new BufferController(new byte[3], 0, 3);

            Assert.That(
                () => controller.Append((IEnumerable <byte[]>)null),
                Throws.InstanceOf <ArgumentNullException>()
                .With.Message.EqualTo("Value cannot be null.\r\nParameter name: buffers"));
        }
Beispiel #11
0
        public void AppendEmptyArray()
        {
            var controller = new BufferController(new byte[3], 0, 3);

            Assert.That(controller.Append(new byte[] { }), Is.EqualTo(new byte[] { }));
            Assert.That(controller.Availability, Is.EqualTo(3));
            Assert.That(controller.Count, Is.EqualTo(0));
            Assert.That(controller.Availability, Is.EqualTo(3));
            Assert.That(controller.Count, Is.EqualTo(0));
        }
Beispiel #12
0
        public void AppendFromArrayLessThanAvailable()
        {
            var buffer     = new byte[3];
            var controller = new BufferController(buffer, 0, buffer.Length);

            Assert.That(controller.Append(new byte[] { 1, 2, 3 }, 1, 2), Is.Empty);
            Assert.That(buffer, Is.EqualTo(new byte[] { 2, 3, 0 }));
            Assert.That(controller.Availability, Is.EqualTo(1));
            Assert.That(controller.Count, Is.EqualTo(2));
        }
Beispiel #13
0
        public void AppendFromArrayMoreThanAvailable()
        {
            var buffer     = new byte[3];
            var controller = new BufferController(buffer, 0, buffer.Length);

            Assert.That(controller.Append(new byte[] { 1, 2, 3, 4, 5, 6, 7 }, 2, 5), Is.EqualTo(new byte[] { 6, 7 }));
            Assert.That(buffer, Is.EqualTo(new byte[] { 3, 4, 5 }));
            Assert.That(controller.Availability, Is.EqualTo(0));
            Assert.That(controller.Count, Is.EqualTo(3));
        }
Beispiel #14
0
        public void AppendWithReadDelegateThrowsIfNoAvailability()
        {
            var buffer     = new byte[0];
            var controller = new BufferController(buffer, 0, buffer.Length);

            Assert.That(
                () => controller.Append(Read),
                Throws.TypeOf <InvalidOperationException>()
                .With.Message.EqualTo(string.Format("{0} has no more availability to append further bytes to buffer.", typeof(BufferController).Name)));
        }
Beispiel #15
0
        public void AppendArrayThatExceedsAvailability()
        {
            var buffer     = new byte[3];
            var controller = new BufferController(buffer, 0, buffer.Length);

            controller.Append(new byte[] { 1, 2, 3, 4, 5, 6, 7 }, 2, 5).Should().BeEquivalentTo(new byte[] { 6, 7 });
            controller.Availability.Should().Be(0);
            controller.Count.Should().Be(3);

            buffer.Should().BeEquivalentTo(new byte[] { 3, 4, 5 });
        }
Beispiel #16
0
        public void AppendArrayThatDoesNotExceedAvailability()
        {
            var buffer     = new byte[3];
            var controller = new BufferController(buffer, 0, buffer.Length);

            controller.Append(new byte[] { 1, 2, 3 }, 1, 2).Should().BeEmpty();
            controller.Availability.Should().Be(1);
            controller.Count.Should().Be(2);

            buffer.Should().BeEquivalentTo(new byte[] { 2, 3, 0 });
        }
        public override int Read(byte[] buffer, int offset, int count)
        {
            var bufferController = new BufferController(buffer, offset, count);

            // try to exhaust backlog if any while keeping any extra of it
            _backlog = bufferController.Append(_backlog);
            while (bufferController.Availability > 0 && _enumerator.MoveNext())
            {
                _backlog = bufferController.Append(_enumerator.Current);
            }
            _position += bufferController.Count;
            return(bufferController.Count);
        }
Beispiel #18
0
        public void AppendBufferListWhenNoAvailability()
        {
            var controller = new BufferController(new byte[3], 3, 0);
            var buffers    = new[] {
                new byte[] { 1, 2, 3 },
                new byte[] { 4, 5, 6, 7 },
                new byte[] { 8, 9, 8 }
            };

            Assert.That(controller.Append(buffers), Is.EqualTo(buffers));
            Assert.That(controller.Availability, Is.EqualTo(0));
            Assert.That(controller.Count, Is.EqualTo(0));
        }
Beispiel #19
0
        public void AppendWithReadDelegate()
        {
            var buffer     = new byte[10];
            var controller = new BufferController(buffer, 0, buffer.Length);

            controller.Append(new byte[] { 0, 1 });

            controller.Append(Read);

            Assert.That(buffer, Is.EqualTo(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 0, 0 }));
            Assert.That(controller.Availability, Is.EqualTo(2));
            Assert.That(controller.Count, Is.EqualTo(8));
        }
Beispiel #20
0
        public void AppendBufferListWhenNoAvailability()
        {
            var controller = new BufferController(new byte[3], 3, 0);
            var buffers    = new[] {
                new byte[] { 1, 2, 3 },
                new byte[] { 4, 5, 6, 7 },
                new byte[] { 8, 9, 8 }
            };

            controller.Append(buffers).Should().BeEquivalentTo(buffers);
            controller.Availability.Should().Be(0);
            controller.Count.Should().Be(0);
        }
Beispiel #21
0
        public void AppendWithReadDelegate()
        {
            var buffer     = new byte[10];
            var controller = new BufferController(buffer, 0, buffer.Length);

            controller.Append(new byte[] { 0, 1 });

            controller.Append(Read);

            controller.Availability.Should().Be(2);
            controller.Count.Should().Be(8);

            buffer.Should().BeEquivalentTo(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 0, 0 });
        }
        public override int Read(byte[] buffer, int offset, int count)
        {
            var bufferController = new BufferController(buffer, offset, count);

            // try to exhaust backlog if any while keeping any overflowing content
            _backlog = bufferController.Append(_backlog);
            while (bufferController.Availability > 0 && !EOF)
            {
                // append to buffer and keep any overflowing content
                _backlog = bufferController.Append(ReadNextNode(), Encoding);
            }
            _position += bufferController.Count;
            return(bufferController.Count);
        }
Beispiel #23
0
        public void AppendBufferListLessThanAvailable()
        {
            var buffer     = new byte[10];
            var controller = new BufferController(buffer, 0, buffer.Length);
            var buffers    = new[] {
                new byte[] { 1, 2, 3 },
                new byte[] { 4, 5, 6, 7 },
                new byte[] { 8, 9 }
            };

            Assert.That(controller.Append(buffers), Is.Empty);
            Assert.That(buffer, Is.EqualTo(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }));
            Assert.That(controller.Availability, Is.EqualTo(1));
            Assert.That(controller.Count, Is.EqualTo(9));
        }
Beispiel #24
0
        public void AppendBufferListThatDoNoExceedAvailability()
        {
            var buffer     = new byte[10];
            var controller = new BufferController(buffer, 0, buffer.Length);
            var buffers    = new[] {
                new byte[] { 1, 2, 3 },
                new byte[] { 4, 5, 6, 7 },
                new byte[] { 8, 9 }
            };

            controller.Append(buffers).Should().BeEmpty();
            buffer.Should().BeEquivalentTo(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 });
            controller.Availability.Should().Be(1);
            controller.Count.Should().Be(9);
        }
Beispiel #25
0
        public void AppendArrayThrows()
        {
            var controller = new BufferController(new byte[3], 0, 3);

            Invoking(() => controller.Append(null, 0, 0))
            .Should().Throw <ArgumentNullException>().Where(e => e.ParamName == "bytes");
            Invoking(() => controller.Append(new byte[0], -1, 0))
            .Should().Throw <ArgumentException>().WithMessage("Offset cannot be negative.*");
            Invoking(() => controller.Append(new byte[0], 1, -1))
            .Should().Throw <ArgumentException>().WithMessage("Count cannot be negative.*");
            Invoking(() => controller.Append(new byte[0], 1, 0))
            .Should().Throw <ArgumentException>().WithMessage("The sum of offset and count is greater than the byte array length.");
            Invoking(() => controller.Append(new byte[2], 0, 3))
            .Should().Throw <ArgumentException>().WithMessage("The sum of offset and count is greater than the byte array length.");
            Invoking(() => controller.Append(new byte[0], 0, 0))
            .Should().NotThrow();
        }
Beispiel #26
0
        public void AppendBufferListMoreThanAvailable()
        {
            var buffer     = new byte[9];
            var controller = new BufferController(buffer, 0, buffer.Length);
            var buffers    = new[] {
                new byte[] { 1, 2, 3 },
                new byte[] { 4, 5, 6, 7 },
                new byte[] { 8, 9, 8 },
                new byte[] { 7, 6, 5 }
            };

            Assert.That(
                controller.Append(buffers),
                Is.EqualTo(
                    new[] {
                new byte[] { 8 },
                new byte[] { 7, 6, 5 }
            }));
            Assert.That(buffer, Is.EqualTo(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }));
            Assert.That(controller.Availability, Is.EqualTo(0));
            Assert.That(controller.Count, Is.EqualTo(9));
        }
Beispiel #27
0
        public void AppendBufferListThrows()
        {
            var controller = new BufferController(new byte[3], 0, 3);

            Invoking(() => controller.Append((IEnumerable <byte[]>)null)).Should().Throw <ArgumentNullException>().Where(e => e.ParamName == "buffers");
        }