4a51478c1069 — Michael Johnson 11 months ago
Add more testing around disposal of RngSeeder sources
A => src/Mocks/DisposableRng.cs +20 -0
@@ 0,0 1,20 @@ 
+using System;
+
+namespace RandN;
+
+public sealed class DisposableRng : ICryptoRng, IDisposable
+{
+    private readonly IRng _wrapped;
+
+    public DisposableRng(IRng wrapped) => _wrapped = wrapped;
+
+    public Boolean Disposed { get; private set; }
+
+    public void Dispose() => Disposed = true;
+
+    public void Fill(Span<Byte> buffer) => _wrapped.Fill(buffer);
+
+    public UInt32 NextUInt32() => _wrapped.NextUInt32();
+
+    public UInt64 NextUInt64() => _wrapped.NextUInt64();
+}

          
M src/Tests/Compat/RandomNumberGeneratorShimTests.cs +1 -14
@@ 78,7 78,7 @@ public sealed class RandomNumberGenerato
     [Fact]
     public void Disposal()
     {
-        var rng = new DisposableRng();
+        var rng = new DisposableRng(new StepRng(10));
         var shim = RandomNumberGeneratorShim.Create(rng);
         shim.Dispose();
         Assert.True(rng.Disposed);

          
@@ 93,17 93,4 @@ public sealed class RandomNumberGenerato
         Assert.Throws<ArgumentNullException>(() => rng.GetBytes(null!));
         Assert.Throws<ArgumentNullException>(() => rng.GetNonZeroBytes(null!));
     }
-
-    private sealed class DisposableRng : ICryptoRng, IDisposable
-    {
-        public Boolean Disposed { get; private set; }
-
-        public void Dispose() => Disposed = true;
-
-        public void Fill(Span<Byte> buffer) => buffer.Fill(0);
-
-        public UInt32 NextUInt32() => 0;
-
-        public UInt64 NextUInt64() => 0;
-    }
 }

          
M src/Tests/RngSeederTests.cs +15 -13
@@ 9,27 9,29 @@ public class RngSeederTests
     public void CryptoRng()
     {
         var rngFactory = new StepRng.Factory(increment: 0);
-        var rngSeeder = RngSeeder.Create(rngFactory);
+        using var rngSeeder = RngSeeder.Create(rngFactory);
         var rng = rngSeeder.Create();
-        Assert.NotNull(rng);
-        rngSeeder.Dispose();
+        Assert.IsType<StepRng>(rng);
     }
 
     [Fact]
     public void SequentialSeedStaticRng()
     {
-        var seedSource = new StepRng(0);
+        var seedSource = new DisposableRng(new StepRng(0));
         var rngFactory = new StepRng.Factory(increment: 0);
-        using var rngSeeder = RngSeeder.Create(rngFactory, seedSource);
-
-        for (UInt64 i = 0; i < 10; i++)
+        using (var rngSeeder = RngSeeder.Create(rngFactory, seedSource))
         {
-            var rng = rngSeeder.Create();
-            Assert.Equal(i, rng.NextUInt64());
-            Assert.Equal(i, rng.NextUInt64());
-            Assert.Equal(i, rng.NextUInt64());
-            Assert.Equal(i, rng.NextUInt64());
+            for (UInt64 i = 0; i < 10; i++)
+            {
+                var rng = rngSeeder.Create();
+                Assert.IsType<StepRng>(rng);
+                Assert.Equal(i, rng.NextUInt64());
+                Assert.Equal(i, rng.NextUInt64());
+                Assert.Equal(i, rng.NextUInt64());
+                Assert.Equal(i, rng.NextUInt64());
+            }
         }
+        Assert.True(seedSource.Disposed);
     }
 
     [Fact]

          
@@ 43,4 45,4 @@ public class RngSeederTests
         Assert.Throws<ArgumentNullException>(() => RngSeeder.Create<StepRng, StepRng, UInt64>(null!, null!));
         Assert.Throws<ArgumentNullException>(() => RngSeeder.Create<StepRng, StepRng>(null!));
     }
-}
  No newline at end of file
+}