# HG changeset patch # User Michael Johnson # Date 1678755848 28800 # Mon Mar 13 17:04:08 2023 -0800 # Node ID a35f6506bd186839f7702098c4eae455ff8b4644 # Parent 9bef8409687173836571d19e440d76f0be406a17 Add more tests covering Filler.FillBytesViaNext diff --git a/src/Core/Implementation/Filler.cs b/src/Core/Implementation/Filler.cs --- a/src/Core/Implementation/Filler.cs +++ b/src/Core/Implementation/Filler.cs @@ -41,13 +41,16 @@ public static void FillBytesViaNext(TRng rng, Span destination) where TRng : notnull, IRng { - while (destination.Length > 8) + // Do bulk filling until we have less than 8 bytes left + while (destination.Length >= 8) { var num = rng.NextUInt64(); BinaryPrimitives.WriteUInt64LittleEndian(destination, num); destination = destination.Slice(8); } + // Clean up the last few bytes. If we have 5-7 bytes left, use NextUInt64. If we have 1-4 bytes left, use NextUInt32. + // Otherwise we have no bytes to fill and can simply exit. if (destination.Length > 4) { Span chunk = stackalloc Byte[8]; @@ -121,4 +124,4 @@ } return (chunkSize, chunkSizeByte); } -} \ No newline at end of file +} diff --git a/src/Tests/Implementation/FillerTests.cs b/src/Tests/Implementation/FillerTests.cs --- a/src/Tests/Implementation/FillerTests.cs +++ b/src/Tests/Implementation/FillerTests.cs @@ -118,6 +118,173 @@ } [Fact] + public void FillViaNext0Bytes() + { + var rng = new StepRng(0x0123456789ABCDEF) + { + Increment = 0x0001000100010001 + }; + var buffer = new Byte[0]; + Filler.FillBytesViaNext(rng, buffer); + var expected = new Byte[0]; + + Assert.Equal(expected, buffer); + } + + [Fact] + public void FillViaNext1Byte() + { + var rng = new StepRng(0x0123456789ABCDEF) + { + Increment = 0x0001000100010001 + }; + var buffer = new Byte[1]; + Filler.FillBytesViaNext(rng, buffer); + var expected = new Byte[] + { + 0xEF + }; + + Assert.Equal(expected, buffer); + } + + [Fact] + public void FillViaNext2Bytes() + { + var rng = new StepRng(0x0123456789ABCDEF) + { + Increment = 0x0001000100010001 + }; + var buffer = new Byte[2]; + Filler.FillBytesViaNext(rng, buffer); + var expected = new Byte[] + { + 0xEF, 0xCD, + }; + + Assert.Equal(expected, buffer); + } + + [Fact] + public void FillViaNext3Bytes() + { + var rng = new StepRng(0x0123456789ABCDEF) + { + Increment = 0x0001000100010001 + }; + var buffer = new Byte[3]; + Filler.FillBytesViaNext(rng, buffer); + var expected = new Byte[] + { + 0xEF, 0xCD, 0xAB, + }; + + Assert.Equal(expected, buffer); + } + + [Fact] + public void FillViaNext4Bytes() + { + var rng = new StepRng(0x0123456789ABCDEF) + { + Increment = 0x0001000100010001 + }; + var buffer = new Byte[4]; + Filler.FillBytesViaNext(rng, buffer); + var expected = new Byte[] + { + 0xEF, 0xCD, 0xAB, 0x89, + }; + + Assert.Equal(expected, buffer); + } + + [Fact] + public void FillViaNext5Bytes() + { + var rng = new StepRng(0x0123456789ABCDEF) + { + Increment = 0x0001000100010001 + }; + var buffer = new Byte[5]; + Filler.FillBytesViaNext(rng, buffer); + var expected = new Byte[] + { + 0xEF, 0xCD, 0xAB, 0x89, 0x67, + }; + + Assert.Equal(expected, buffer); + } + + [Fact] + public void FillViaNext6Bytes() + { + var rng = new StepRng(0x0123456789ABCDEF) + { + Increment = 0x0001000100010001 + }; + var buffer = new Byte[6]; + Filler.FillBytesViaNext(rng, buffer); + var expected = new Byte[] + { + 0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x45, + }; + + Assert.Equal(expected, buffer); + } + + [Fact] + public void FillViaNext7Bytes() + { + var rng = new StepRng(0x0123456789ABCDEF) + { + Increment = 0x0001000100010001 + }; + var buffer = new Byte[7]; + Filler.FillBytesViaNext(rng, buffer); + var expected = new Byte[] + { + 0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x45, 0x23, + }; + + Assert.Equal(expected, buffer); + } + + [Fact] + public void FillViaNext8Bytes() + { + var rng = new StepRng(0x0123456789ABCDEF) + { + Increment = 0x0001000100010001 + }; + var buffer = new Byte[8]; + Filler.FillBytesViaNext(rng, buffer); + var expected = new Byte[] + { + 0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x45, 0x23, 0x01, + }; + + Assert.Equal(expected, buffer); + } + + [Fact] + public void FillViaNext9Bytes() + { + var rng = new StepRng(0x0123456789ABCDEF) + { + Increment = 0x0001000100010001 + }; + var buffer = new Byte[9]; + Filler.FillBytesViaNext(rng, buffer); + var expected = new Byte[] + { + 0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x45, 0x23, 0x01, 0xF0, + }; + + Assert.Equal(expected, buffer); + } + + [Fact] public void FillViaUInt32ChunksIncomplete() { var source = new UInt32[]