-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for non-uniform random value generators (#26)
* Added failing unit test * Fix identified. System.Random requires a lock for multithread access * Issue resolved. Random values are now uniform * Moved tests to a dedicated folder
- Loading branch information
Showing
5 changed files
with
184 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System.Collections.Concurrent; | ||
using RandomDataGenerator.FieldOptions; | ||
using RandomDataGenerator.Randomizers; | ||
using Xunit.Abstractions; | ||
|
||
namespace RandomDataGenerator.Tests | ||
{ | ||
public class CityRandomizerTests | ||
{ | ||
private readonly ITestOutputHelper _output; | ||
static readonly Random random = new System.Random(420); | ||
static readonly object randLock = new object(); | ||
|
||
public CityRandomizerTests(ITestOutputHelper output) | ||
{ | ||
this._output = output; | ||
} | ||
|
||
|
||
[Theory] | ||
[InlineData(1)] | ||
[InlineData(2)] | ||
[InlineData(4)] | ||
[InlineData(8)] | ||
public void CityDistributionMustBeUniform(int degree) | ||
{ | ||
var locationGenerator = RandomizerFactory.GetRandomizer(new FieldOptionsCity { ValueAsString = true, UseNullValues = false }); | ||
var concurrentDictionary = new ConcurrentDictionary<string, long>(); | ||
var options = new ParallelOptions { MaxDegreeOfParallelism = degree }; | ||
Parallel.For(0, 1000, options, i => | ||
{ | ||
Parallel.For(0, 1000, options, j => | ||
{ | ||
var location = locationGenerator.Generate(); | ||
concurrentDictionary.AddOrUpdate(location, _ => 1, (k, v) => v + 1); | ||
}); | ||
}); | ||
var topCount = concurrentDictionary.OrderByDescending(pair => pair.Value).First(); | ||
var bottomCount = concurrentDictionary.OrderBy(pair => pair.Value).First(); | ||
_output.WriteLine($"{topCount}"); | ||
_output.WriteLine($"{bottomCount}"); | ||
Assert.True(topCount.Value/bottomCount.Value<2); | ||
Assert.NotEqual(topCount.Key, bottomCount.Key); | ||
} | ||
|
||
[Fact] | ||
public void TwoRandomCitiesMustNotBeTheSame() | ||
{ | ||
var locationGenerator = RandomizerFactory.GetRandomizer(new FieldOptionsCity { ValueAsString = true, UseNullValues = false }); | ||
var locationOne = locationGenerator.Generate(); | ||
var locationTwo = locationGenerator.Generate(); | ||
Assert.NotEqual(locationOne, locationTwo); | ||
} | ||
|
||
[Fact] | ||
public void SystemRandomDistributionMustBeUniform() | ||
{ | ||
var concurrentDictionary = new ConcurrentDictionary<int, long>(); | ||
Parallel.For(0, 1000, i => | ||
{ | ||
Parallel.For(0, 1000, j => | ||
{ | ||
int index; | ||
lock (randLock) | ||
{ | ||
index = random.Next(0, 2000); | ||
} | ||
concurrentDictionary.AddOrUpdate(index, _ => 1, (k, v) => v + 1); | ||
}); | ||
}); | ||
var topCount = concurrentDictionary.OrderByDescending(pair => pair.Value).First(); | ||
var bottomCount = concurrentDictionary.OrderBy(pair => pair.Value).First(); | ||
_output.WriteLine($"{topCount}"); | ||
_output.WriteLine($"{bottomCount}"); | ||
Assert.True(topCount.Value / bottomCount.Value < 2); | ||
Assert.NotEqual(topCount.Key, bottomCount.Key); | ||
|
||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
tests/RandomDataGenerator.Tests/RandomDataGenerator.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" /> | ||
<PackageReference Include="xunit" Version="2.4.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\RandomDataGenerator\RandomDataGenerator.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using Xunit; |