From 023e61495f1ab2bd59efc6d3582f59d46f6fdcbf Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Thu, 10 May 2018 17:38:19 -0700 Subject: [PATCH] Test combining multi-byte character writes with individual chars (#2238) --- .../Protocol/Utf8BufferTextWriterTests.cs | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/Utf8BufferTextWriterTests.cs b/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/Utf8BufferTextWriterTests.cs index a109bf4719..b1fe8c76fb 100644 --- a/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/Utf8BufferTextWriterTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Common.Tests/Internal/Protocol/Utf8BufferTextWriterTests.cs @@ -292,11 +292,59 @@ namespace Microsoft.AspNetCore.SignalR.Common.Tests.Internal.Protocol Assert.Equal(testString, Encoding.UTF8.GetString(bufferWriter.ToArray())); } + public static IEnumerable CharAndSegmentSizes + { + get + { + foreach (var singleChar in new [] { '"', '恄' }) + { + for (int i = 4; i <= 16; i++) + { + yield return new object[] { singleChar, i }; + } + } + } + } + + [Theory] + [MemberData(nameof(CharAndSegmentSizes))] + 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 sealed class TestMemoryBufferWriter : IBufferWriter { private readonly int _segmentSize; - private List> _completedSegments = new List>(); + private readonly List> _completedSegments = new List>(); private int _totalLength; public Memory CurrentSegment { get; private set; }