Skip to content

Commit 963315d

Browse files
Add tests for very long arrays
1 parent 0a7a7e5 commit 963315d

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/libraries/System.Runtime.Serialization.Xml/tests/XmlDictionaryWriterTest.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ public static void BinaryWriter_PrimitiveTypes()
349349
double d = 1.0 / 3.0;
350350
Guid guid = Guid.NewGuid();
351351

352+
// Remarks: Of these types only timespan handles BigEndian/LittleEndian so for other types compare against bare bytes
352353
AssertBytesWritten(x => x.WriteValue(decimal.MaxValue), XmlBinaryNodeType.DecimalText, MemoryMarshal.AsBytes(new ReadOnlySpan<decimal>(in decMax)));
353354
AssertBytesWritten(x => x.WriteValue(f), XmlBinaryNodeType.FloatText, MemoryMarshal.AsBytes(new ReadOnlySpan<float>(in f)));
354355
AssertBytesWritten(x => x.WriteValue(guid), XmlBinaryNodeType.GuidText, guid.ToByteArray());
@@ -381,7 +382,7 @@ void AssertBytesWritten(Action<XmlDictionaryWriter> action, XmlBinaryNodeType no
381382

382383

383384
[Fact]
384-
public static void BinaryWriter_Array()
385+
public static void BinaryWriter_Arrays()
385386
{
386387
using var ms = new MemoryStream();
387388
using var writer = XmlDictionaryWriter.CreateBinaryWriter(ms);
@@ -393,6 +394,10 @@ public static void BinaryWriter_Array()
393394

394395
AssertBytesWritten(x => x.WriteArray(null, "a", null, ints, offset, count), XmlBinaryNodeType.Int32TextWithEndElement, count, new byte[] { 4, 3, 2, 1, 0x44, 0x33, 0x22, 0x11 });
395396

397+
// Write more than 512 bytes in a single call to trigger different writing logic in XmlStreamNodeWriter.WriteBytes
398+
long[] longs = Enumerable.Range(0x01020304, 127).Select(i => (long)i | (long)(~i << 32)).ToArray();
399+
AssertBytesWritten(x => x.WriteArray(null, "a", null, longs, 0, longs.Length), XmlBinaryNodeType.Int64TextWithEndElement, longs.Length, MemoryMarshal.AsBytes(longs.AsSpan()));
400+
396401
void AssertBytesWritten(Action<XmlDictionaryWriter> action, XmlBinaryNodeType nodeType, int count, ReadOnlySpan<byte> expected)
397402
{
398403
// Reset stream so we only compare the actual value written (including end element)
@@ -407,11 +412,9 @@ void AssertBytesWritten(Action<XmlDictionaryWriter> action, XmlBinaryNodeType no
407412

408413
var actual = segement.AsSpan();
409414
Assert.Equal(XmlBinaryNodeType.Array, (XmlBinaryNodeType)actual[0]);
410-
411-
//// start element: ShortDictionaryElement + lenght of str + str
412415
Assert.Equal(XmlBinaryNodeType.ShortElement, (XmlBinaryNodeType)actual[1]);
413416
int elementLenght = actual[2];
414-
Assert.InRange(actual[2], 0, 0x8f);
417+
Assert.InRange(elementLenght, 0, 0x8f); // verify count is single byte
415418
Assert.Equal(XmlBinaryNodeType.EndElement, (XmlBinaryNodeType)actual[3 + elementLenght]);
416419

417420
actual = actual.Slice(4 + elementLenght);
@@ -420,7 +423,6 @@ void AssertBytesWritten(Action<XmlDictionaryWriter> action, XmlBinaryNodeType no
420423
Assert.Equal(checked((sbyte)count), (sbyte)actual[1]);
421424

422425
AssertExtensions.SequenceEqual(expected, actual.Slice(2));
423-
writer.WriteEndElement();
424426
}
425427
}
426428

0 commit comments

Comments
 (0)