445db1d9ac2e — Michael Johnson 2 months ago
Add more tests for BigIntegers and disallow unsafe blocks in MersenneTwister
M src/MersenneTwister/MersenneTwister.csproj +1 -1
@@ 7,7 7,7 @@ 
         <AssemblyName>RandN.MersenneTwister</AssemblyName>
         <RootNamespace>RandN.Rngs</RootNamespace>
         <Nullable>enable</Nullable>
-        <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+        <AllowUnsafeBlocks>false</AllowUnsafeBlocks>
         <LangVersion>12.0</LangVersion>
         <AnalysisLevel>8</AnalysisLevel>
         <Version>0.5.0</Version>

          
M src/Tests/Distributions/UniformBigIntegerTests.cs +13 -1
@@ 1,5 1,4 @@ 
 using System;
-using System.Collections.Generic;
 using System.Numerics;
 using Xunit;
 using RandN.Rngs;

          
@@ 186,4 185,17 @@ public class UniformBigIntegerTests
         Assert.True(Statistics.WithinConfidence(populationMean, popStdDev, exclusiveMean, iterations));
         Assert.True(Statistics.WithinConfidence(populationMean, popStdDev, inclusiveMean, iterations));
     }
+    
+    [Fact]
+    public void Rejections()
+    {
+        var rng = new SequenceRng(new[] { 0b10000000u, 0b10000001u, 0x81u, 0x81u });
+        var lower = BigInteger.Zero;
+        var upper = new BigInteger(0b10000000u);
+
+        var dist = Uniform.NewInclusive(lower, upper);
+        Assert.True(dist.TrySample(rng, out var result));
+        Assert.Equal(0b10000000u, result);
+        Assert.False(dist.TrySample(rng, out _));
+    }
 }

          
M src/Tests/Rngs/CryptoServiceProviderTests.cs +1 -1
@@ 13,7 13,7 @@ public sealed class CryptoServiceProvide
 #pragma warning disable CS0618
         var factory = CryptoServiceProvider.GetFactory();
 #pragma warning restore CS0618
-        var rng = factory.Create();
+        using var rng = factory.Create();
         Assert.True(Statistics.TestMonobitFrequency32(rng, 100_000, Statistics.WideZScore));
         Assert.True(Statistics.TestMonobitFrequency64(rng, 100_000, Statistics.WideZScore));
         Assert.True(Statistics.TestMonobitFrequencyFill(rng, 32, Statistics.WideZScore));

          
M src/Tests/Tests.csproj +6 -0
@@ 3,6 3,7 @@ 
   <PropertyGroup>
     <OutputType>Library</OutputType>
     <TargetFrameworks>net8.0;net6.0;netcoreapp3.0;net48</TargetFrameworks>
+    <RollForward>LatestMajor</RollForward>
     <Optimize>false</Optimize>
     <AssemblyName>Tests</AssemblyName>
     <RootNamespace>RandN</RootNamespace>

          
@@ 14,6 15,11 @@ 
     <CheckEolTargetFramework>false</CheckEolTargetFramework>
   </PropertyGroup>
 
+  <!-- If the globalization packages can't be found, tests break. To avoid this, we use invariant globalization for tests. -->
+  <PropertyGroup>
+    <InvariantGlobalization>true</InvariantGlobalization>
+  </PropertyGroup>
+
   <ItemGroup>
     <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.3" />
     <PackageReference Include="xunit" Version="2.8.1" />