Skip to content

Commit 8d7608d

Browse files
authored
Adding test to cover new UTF8 helper changes in native (#294)
***NO_CI***
1 parent 85bfd76 commit 8d7608d

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

‎Tests/NFUnitTestEncoding/EncodingTests.cs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,40 @@ public void Utf8EncodingTests_TestIncrementalDecodingMultiByte()
456456
Assert.AreEqual('€', output[0]);
457457
}
458458

459+
[TestMethod]
460+
public void Utf8Decoder_IncompleteSequence()
461+
{
462+
Decoder decoder = Encoding.UTF8.GetDecoder();
463+
byte[] bytes = { 0xE2, 0x82, 0xAC }; // U+20AC
464+
char[] chars = new char[4];
465+
466+
// Only the first two bytes are available; preserve the incomplete prefix.
467+
decoder.Convert(bytes, 0, 2, chars, 0, chars.Length, false,
468+
out int bytesUsed, out int charsUsed, out bool completed);
469+
470+
Assert.AreEqual(0, bytesUsed);
471+
Assert.AreEqual(0, charsUsed);
472+
Assert.IsFalse(completed);
473+
474+
// Retry the unconsumed prefix with the complete character.
475+
decoder.Convert(bytes, 0, 3, chars, 0, chars.Length, false,
476+
out bytesUsed, out charsUsed, out completed);
477+
478+
Assert.AreEqual(3, bytesUsed);
479+
Assert.AreEqual(1, charsUsed);
480+
Assert.AreEqual('\u20AC', chars[0]);
481+
Assert.IsTrue(completed);
482+
483+
// At EOF, consume a truncated sequence once and emit one replacement.
484+
decoder.Convert(bytes, 0, 2, chars, 0, chars.Length, true,
485+
out bytesUsed, out charsUsed, out completed);
486+
487+
Assert.AreEqual(2, bytesUsed);
488+
Assert.AreEqual(1, charsUsed);
489+
Assert.AreEqual('\uFFFD', chars[0]);
490+
Assert.IsTrue(completed);
491+
}
492+
459493
[TestMethod]
460494
public void Utf8EncodingTests_TestOverlongEncodingRejection()
461495
{

0 commit comments

Comments
 (0)