a35f6506bd18 — Michael Johnson 1 year, 3 months ago
Add more tests covering Filler.FillBytesViaNext
2 files changed, 172 insertions(+), 2 deletions(-)

M src/Core/Implementation/Filler.cs
M src/Tests/Implementation/FillerTests.cs
M src/Core/Implementation/Filler.cs +5 -2
@@ 41,13 41,16 @@ public static class Filler
     public static void FillBytesViaNext<TRng>(TRng rng, Span<Byte> 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<Byte> chunk = stackalloc Byte[8];

          
@@ 121,4 124,4 @@ public static class Filler
         }
         return (chunkSize, chunkSizeByte);
     }
-}
  No newline at end of file
+}

          
M src/Tests/Implementation/FillerTests.cs +167 -0
@@ 118,6 118,173 @@ public sealed class FillerTests
     }
 
     [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[]