public void WriteUnicodeStringAndCharsWithVaryingSegmentSizes(char singleChar, int segmentSize)
        {
            const string testString = "aいbろ";
            const int    iterations = 10;

            var testBufferWriter = new TestMemoryBufferWriter(segmentSize);
            var sb = new StringBuilder();

            using (var textWriter = new Utf8BufferTextWriter())
            {
                textWriter.SetWriter(testBufferWriter);

                for (int i = 0; i < iterations; i++)
                {
                    textWriter.Write(singleChar);
                    textWriter.Write(testString);
                    textWriter.Write(singleChar);

                    sb.Append(singleChar);
                    sb.Append(testString);
                    sb.Append(singleChar);
                }
            }

            var expected = sb.ToString();

            var data   = testBufferWriter.ToArray();
            var result = Encoding.UTF8.GetString(data);

            Assert.Equal(expected, result);
        }
        private void WriteMultiByteCharactersToSmallBuffers()
        {
            // Test string breakdown (char => UTF-8 hex values):
            // a => 61
            // い => E3-81-84
            // b => 62
            // ろ => E3-82-8D
            // c => 63
            // d => 64
            // は => E3-81-AF
            // に => E3-81-AB
            // e => 65
            // ほ => E3-81-BB
            // f => 66
            // へ => E3-81-B8
            // ど => E3-81-A9
            // g => 67
            // h => 68
            // i => 69
            // \uD800\uDC00 => F0-90-80-80 (this is a surrogate pair that is represented as a single 4-byte UTF-8 encoding)
            const string testString = "aいbろcdはにeほfへどghi\uD800\uDC00";

            // By mixing single byte and multi-byte characters, we know that there will
            // be spaces in the active segment that cannot fit the current character. This
            // means we'll be testing the GetMemory(minimumSize) logic.
            var bufferWriter = new TestMemoryBufferWriter(segmentSize: 5);

            var writer = new Utf8BufferTextWriter();

            writer.SetWriter(bufferWriter);
            writer.Write(testString);
            writer.Flush();

            // Verify the output
            var allSegments = bufferWriter.GetSegments().Select(s => s.ToArray()).ToArray();

            Assert.Collection(allSegments,
                              seg => Assert.Equal(new byte[] { 0x61, 0xE3, 0x81, 0x84, 0x62 }, seg),  // "aいb"
                              seg => Assert.Equal(new byte[] { 0xE3, 0x82, 0x8D, 0x63, 0x64 }, seg),  // "ろcd"
                              seg => Assert.Equal(new byte[] { 0xE3, 0x81, 0xAF }, seg),              // "は"
                              seg => Assert.Equal(new byte[] { 0xE3, 0x81, 0xAB, 0x65 }, seg),        // "にe"
                              seg => Assert.Equal(new byte[] { 0xE3, 0x81, 0xBB, 0x66 }, seg),        // "ほf"
                              seg => Assert.Equal(new byte[] { 0xE3, 0x81, 0xB8 }, seg),              // "へ"
                              seg => Assert.Equal(new byte[] { 0xE3, 0x81, 0xA9, 0x67, 0x68 }, seg),  // "どgh"
                              seg => Assert.Equal(new byte[] { 0x69, 0xF0, 0x90, 0x80, 0x80 }, seg)); // "i\uD800\uDC00"

            Assert.Equal(testString, Encoding.UTF8.GetString(bufferWriter.ToArray()));
        }
        public void GetAndReturnCachedBufferTextWriter()
        {
            var bufferWriter1 = new TestMemoryBufferWriter();

            var textWriter1 = Utf8BufferTextWriter.Get(bufferWriter1);

            textWriter1.Write("Hello");
            textWriter1.Flush();
            Utf8BufferTextWriter.Return(textWriter1);

            Assert.Equal("Hello", Encoding.UTF8.GetString(bufferWriter1.ToArray()));

            var bufferWriter2 = new TestMemoryBufferWriter();

            var textWriter2 = Utf8BufferTextWriter.Get(bufferWriter2);

            textWriter2.Write("World");
            textWriter2.Flush();
            Utf8BufferTextWriter.Return(textWriter2);

            Assert.Equal("World", Encoding.UTF8.GetString(bufferWriter2.ToArray()));

            Assert.Same(textWriter1, textWriter2);
        }