public void ThrowsForInvalidParameters(int arrayLength, int offset, int length)
        {
            var writer = OutputWriter.Create(_pipe.Writer);
            var array  = new byte[arrayLength];

            for (int i = 0; i < array.Length; i++)
            {
                array[i] = (byte)(i + 1);
            }

            writer.Write(new Span <byte>(array, 0, 0));
            writer.Write(new Span <byte>(array, array.Length, 0));

            try
            {
                writer.Write(new Span <byte>(array, offset, length));
                Assert.True(false);
            }
            catch (Exception ex)
            {
                Assert.True(ex is ArgumentOutOfRangeException);
            }

            writer.Write(new Span <byte>(array, 0, array.Length));
            Assert.Equal(array, Read());
        }
        public void CanWriteIntoHeadlessBuffer()
        {
            var writer = OutputWriter.Create(_pipe.Writer);

            writer.Write(new byte[] { 1, 2, 3 });
            Assert.Equal(new byte[] { 1, 2, 3 }, Read());
        }
예제 #3
0
        private Task WriteAsync(
            ArraySegment <byte> buffer,
            CancellationToken cancellationToken)
        {
            var  writableBuffer = default(PipeWriter);
            long bytesWritten   = 0;

            lock (_contextLock)
            {
                if (_completed)
                {
                    return(Task.CompletedTask);
                }

                writableBuffer = _pipeWriter;
                var writer = OutputWriter.Create(writableBuffer);
                if (buffer.Count > 0)
                {
                    writer.Write(new ReadOnlySpan <byte>(buffer.Array, buffer.Offset, buffer.Count));
                    bytesWritten += buffer.Count;
                }

                writableBuffer.Commit();
            }

            return(FlushAsync(writableBuffer, bytesWritten, cancellationToken));
        }
        public void ExposesSpan()
        {
            var initialLength = _pipe.Writer.GetMemory().Length;
            var writer        = OutputWriter.Create(_pipe.Writer);

            Assert.Equal(initialLength, writer.Span.Length);
            Assert.Equal(new byte[] { }, Read());
        }
예제 #5
0
        public void ExposesSpan()
        {
            _buffer = _pipe.Writer.Alloc(1);
            var writer = OutputWriter.Create(_buffer);

            Assert.Equal(_buffer.Buffer.Length, writer.Span.Length);
            Assert.Equal(new byte[] { }, Read());
        }
예제 #6
0
        public void ExposesSpan()
        {
            int initialLength = Pipe.Writer.GetMemory().Length;
            OutputWriter <PipeWriter> writer = OutputWriter.Create(Pipe.Writer);

            Assert.Equal(initialLength, writer.Span.Length);
            Assert.Equal(new byte[] { }, Read());
        }
        public void CanWriteWithOffsetAndLenght(int alloc, int offset, int length)
        {
            var writer = OutputWriter.Create(_pipe.Writer);
            var array  = new byte[] { 1, 2, 3 };

            writer.Write(new Span <byte>(array, offset, length));

            Assert.Equal(array.Skip(offset).Take(length).ToArray(), Read());
        }
예제 #8
0
        public void CanWriteIntoHeadlessBuffer()
        {
            OutputWriter <PipeWriter> writer = OutputWriter.Create(Pipe.Writer);

            writer.Write(new byte[] { 1, 2, 3 });
            writer.Commit();

            Assert.Equal(new byte[] { 1, 2, 3 }, Read());
        }
        public void EnsureAllocatesSpan()
        {
            var writer = OutputWriter.Create(_pipe.Writer);

            writer.Ensure(10);

            Assert.True(writer.Span.Length > 10);
            Assert.Equal(new byte[] {}, Read());
        }
        public void CanWriteEmpty()
        {
            var writer = OutputWriter.Create(_pipe.Writer);
            var array  = new byte[] { };

            writer.Write(array);
            writer.Write(new Span <byte>(array, 0, array.Length));

            Assert.Equal(array, Read());
        }
        public void CanWriteMultipleTimes()
        {
            var writer = OutputWriter.Create(_pipe.Writer);

            writer.Write(new byte[] { 1 });
            writer.Write(new byte[] { 2 });
            writer.Write(new byte[] { 3 });

            Assert.Equal(new byte[] { 1, 2, 3 }, Read());
        }
예제 #12
0
        public void CanWriteMultipleTimes()
        {
            OutputWriter <PipeWriter> writer = OutputWriter.Create(Pipe.Writer);

            writer.Write(new byte[] { 1 });
            writer.Write(new byte[] { 2 });
            writer.Write(new byte[] { 3 });
            writer.Commit();

            Assert.Equal(new byte[] { 1, 2, 3 }, Read());
        }
        public void SlicesSpanAndAdvancesAfterWrite()
        {
            var initialLength = _pipe.Writer.GetMemory().Length;

            var writer = OutputWriter.Create(_pipe.Writer);

            writer.Write(new byte[] { 1, 2, 3 });

            Assert.Equal(initialLength - 3, writer.Span.Length);
            Assert.Equal(_pipe.Writer.GetMemory().Length, writer.Span.Length);
            Assert.Equal(new byte[] { 1, 2, 3 }, Read());
        }
        public void CanWriteOverTheBlockLength()
        {
            var memory = _pipe.Writer.GetMemory();
            var writer = OutputWriter.Create(_pipe.Writer);

            var source        = Enumerable.Range(0, memory.Length).Select(i => (byte)i);
            var expectedBytes = source.Concat(source).Concat(source).ToArray();

            writer.Write(expectedBytes);

            Assert.Equal(expectedBytes, Read());
        }
예제 #15
0
        void ObjectOutput(object color, PrintOptionsImpl printOptions, object value, bool startOnNewLine = false)
        {
            var writable = GetOutputWritable(printOptions, value);

            if (writable != null)
            {
                writable.WriteTo(OutputWriter.Create(this, startOnNewLine));
            }
            else
            {
                ReplEditor.OutputPrint(Format(value, printOptions.RoslynPrintOptions), color, startOnNewLine);
            }
        }
예제 #16
0
        public void WriteAsciiNoValidationWritesOnlyOneBytePerChar(string input)
        {
            // WriteAscii doesn't validate if characters are in the ASCII range
            // but it shouldn't produce more than one byte per character
            var writerBuffer = _pipe.Writer;
            var writer       = OutputWriter.Create(writerBuffer);

            writer.WriteAsciiNoValidation(input);
            writerBuffer.FlushAsync().GetAwaiter().GetResult();
            var reader = _pipe.Reader.ReadAsync().GetAwaiter().GetResult();

            Assert.Equal(input.Length, reader.Buffer.Length);
        }
예제 #17
0
        public void CanWriteOverTheBlockLength()
        {
            Memory <byte>             memory = Pipe.Writer.GetMemory();
            OutputWriter <PipeWriter> writer = OutputWriter.Create(Pipe.Writer);

            IEnumerable <byte> source = Enumerable.Range(0, memory.Length).Select(i => (byte)i);

            byte[] expectedBytes = source.Concat(source).Concat(source).ToArray();

            writer.Write(expectedBytes);

            Assert.Equal(expectedBytes, Read());
        }
예제 #18
0
        public void SlicesSpanAndAdvancesAfterWrite()
        {
            _buffer = _pipe.Writer.Alloc(1);
            var initialLength = _buffer.Buffer.Length;

            var writer = OutputWriter.Create(_buffer);

            writer.Write(new byte[] { 1, 2, 3 });

            Assert.Equal(initialLength - 3, writer.Span.Length);
            Assert.Equal(_buffer.Buffer.Length, writer.Span.Length);
            Assert.Equal(new byte[] { 1, 2, 3 }, Read());
        }
예제 #19
0
        public void SlicesSpanAndAdvancesAfterWrite()
        {
            int initialLength = Pipe.Writer.GetMemory().Length;

            OutputWriter <PipeWriter> writer = OutputWriter.Create(Pipe.Writer);

            writer.Write(new byte[] { 1, 2, 3 });
            writer.Commit();

            Assert.Equal(initialLength - 3, writer.Span.Length);
            Assert.Equal(Pipe.Writer.GetMemory().Length, writer.Span.Length);
            Assert.Equal(new byte[] { 1, 2, 3 }, Read());
        }
예제 #20
0
        public void WritesNumericToAscii(ulong number)
        {
            var writerBuffer = _pipe.Writer;
            var writer       = OutputWriter.Create(writerBuffer);

            writer.WriteNumeric(number);
            writerBuffer.FlushAsync().GetAwaiter().GetResult();

            var reader   = _pipe.Reader.ReadAsync().GetAwaiter().GetResult();
            var numAsStr = number.ToString();
            var expected = Encoding.ASCII.GetBytes(numAsStr);

            AssertExtensions.Equal(expected, reader.Buffer.Slice(0, numAsStr.Length).ToArray());
        }
예제 #21
0
        public void WriteResponseHeaders(int statusCode, string reasonPhrase, HttpResponseHeaders responseHeaders)
        {
            lock (_contextLock)
            {
                if (_completed)
                {
                    return;
                }

                var buffer = _pipeWriter;
                var writer = OutputWriter.Create(buffer);

                writer.Write(_bytesHttpVersion11);
                var statusBytes = ReasonPhrases.ToStatusBytes(statusCode, reasonPhrase);
                writer.Write(statusBytes);
                responseHeaders.CopyTo(ref writer);
                writer.Write(_bytesEndHeaders);
                buffer.Commit();
            }
        }
예제 #22
0
        public void EncodesAsAscii(string input, byte[] expected)
        {
            var writerBuffer = _pipe.Writer;
            var writer       = OutputWriter.Create(writerBuffer);

            writer.WriteAsciiNoValidation(input);
            writerBuffer.FlushAsync().GetAwaiter().GetResult();
            var reader = _pipe.Reader.ReadAsync().GetAwaiter().GetResult();

            if (expected.Length > 0)
            {
                AssertExtensions.Equal(
                    expected,
                    reader.Buffer.ToArray());
            }
            else
            {
                Assert.Equal(0, reader.Buffer.Length);
            }
        }
예제 #23
0
        public void WriteAsciiNoValidation()
        {
            const byte maxAscii     = 0x7f;
            var        writerBuffer = _pipe.Writer;
            var        writer       = OutputWriter.Create(writerBuffer);

            for (var i = 0; i < maxAscii; i++)
            {
                writer.WriteAsciiNoValidation(new string((char)i, 1));
            }
            writerBuffer.FlushAsync().GetAwaiter().GetResult();

            var reader = _pipe.Reader.ReadAsync().GetAwaiter().GetResult();
            var data   = reader.Buffer.Slice(0, maxAscii).ToArray();

            for (var i = 0; i < maxAscii; i++)
            {
                Assert.Equal(i, data[i]);
            }
        }
예제 #24
0
        public void WritesNumericAcrossSpanBoundaries(int gapSize)
        {
            var writerBuffer = _pipe.Writer;
            var writer       = OutputWriter.Create(writerBuffer);
            // almost fill up the first block
            var spacer = new byte[writer.Span.Length - gapSize];

            writer.Write(spacer);

            var bufferLength = writer.Span.Length;

            writer.WriteNumeric(ulong.MaxValue);
            Assert.NotEqual(bufferLength, writer.Span.Length);

            writerBuffer.FlushAsync().GetAwaiter().GetResult();

            var reader      = _pipe.Reader.ReadAsync().GetAwaiter().GetResult();
            var numAsString = ulong.MaxValue.ToString();
            var written     = reader.Buffer.Slice(spacer.Length, numAsString.Length);

            Assert.False(written.IsSingleSegment, "The buffer should cross spans");
            AssertExtensions.Equal(Encoding.ASCII.GetBytes(numAsString), written.ToArray());
        }
예제 #25
0
        public void WritesAsciiAcrossBlockBoundaries(int stringLength, int gapSize)
        {
            var testString   = new string(' ', stringLength);
            var writerBuffer = _pipe.Writer;
            var writer       = OutputWriter.Create(writerBuffer);
            // almost fill up the first block
            var spacer = new byte[writer.Span.Length - gapSize];

            writer.Write(spacer);
            Assert.Equal(gapSize, writer.Span.Length);

            var bufferLength = writer.Span.Length;

            writer.WriteAsciiNoValidation(testString);
            Assert.NotEqual(bufferLength, writer.Span.Length);

            writerBuffer.FlushAsync().GetAwaiter().GetResult();

            var reader  = _pipe.Reader.ReadAsync().GetAwaiter().GetResult();
            var written = reader.Buffer.Slice(spacer.Length, stringLength);

            Assert.False(written.IsSingleSegment, "The buffer should cross spans");
            AssertExtensions.Equal(Encoding.ASCII.GetBytes(testString), written.ToArray());
        }