Skip to content

Commit

Permalink
Full refactoring
Browse files Browse the repository at this point in the history
+ Random Service Builder with extension methods;
+ ITypedRandomizer<T>;
+ INumberRandomizer<T>;
+ IArrayRandomizer<T>;
+ Cleaning for tests;
+ Cleaning for benchmarks;
  • Loading branch information
g0dzZz-coder committed Oct 18, 2022
1 parent a7a0c4d commit 923b4f6
Show file tree
Hide file tree
Showing 91 changed files with 1,778 additions and 1,280 deletions.
2 changes: 1 addition & 1 deletion Depra.Random.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benchmarks", "benchmarks",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Random.Benchmarks", "Random.Benchmarks\Random.Benchmarks.csproj", "{5B172691-3700-4D92-99C3-8099E50A63C7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Random.UnitTests", "Random.UnitTests\Random.UnitTests.csproj", "{F85C7F8D-3FD8-41C7-BF0B-8F3271B0F697}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Random.Application.UnitTests", "Random.Application.UnitTests\Random.Application.UnitTests.csproj", "{F85C7F8D-3FD8-41C7-BF0B-8F3271B0F697}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// SPDX-License-Identifier: Apache-2.0

using System.Collections;
using Depra.Random.System;
using Depra.Random.Application.System;
using Depra.Random.Domain.Randomizers;

namespace Depra.Random.UnitTests;
namespace Depra.Random.Application.UnitTests;

[TestFixture(TestOf = typeof(ConcurrentPseudoRandom))]
internal class ConcurrentPseudoRandomTests
Expand All @@ -19,7 +20,7 @@ public void WhenGettingNextInt32Parallel_AndRangeIsDefault_ThenZerosNotFound(
[Values(10_000)] int samplesCount)
{
// Arrange.
var random = _concurrentPseudoRandom;
var randomizer = _concurrentPseudoRandom;
var allThreadIssues = 0;

// Act.
Expand All @@ -28,7 +29,7 @@ public void WhenGettingNextInt32Parallel_AndRangeIsDefault_ThenZerosNotFound(
var randomNumbers = new int[samplesCount];
for (var i = 0; i < samplesCount; i++)
{
randomNumbers[i] = random.Next();
randomNumbers[i] = randomizer.Next();
}

var threadIssues = randomNumbers.Count(x => x == 0);
Expand All @@ -48,7 +49,7 @@ public void WhenGettingNextInt32Parallel_AndInRangeWithMinAndMax_ThenZerosNotFou
// Arrange.
const int minValue = int.MinValue;
const int maxValue = int.MaxValue;
var random = _concurrentPseudoRandom;
var randomizer = _concurrentPseudoRandom;
var allThreadIssues = 0;

// Act.
Expand All @@ -57,7 +58,7 @@ public void WhenGettingNextInt32Parallel_AndInRangeWithMinAndMax_ThenZerosNotFou
var randomNumbers = new int[samplesCount];
for (var i = 0; i < samplesCount; i++)
{
randomNumbers[i] = random.Next(minValue, maxValue);
randomNumbers[i] = randomizer.Next(minValue, maxValue);
}

var threadIssues = randomNumbers.Count(x => x == 0);
Expand All @@ -75,7 +76,7 @@ public void WhenGettingNextDoubleParallel_AndRangeIsDefault_ThenZerosNotFound(
[Values(10_000)] int samplesCount)
{
// Arrange.
var random = _concurrentPseudoRandom;
ITypedRandomizer<double> randomizer = _concurrentPseudoRandom;
var allThreadIssues = 0;

// Act.
Expand All @@ -84,7 +85,7 @@ public void WhenGettingNextDoubleParallel_AndRangeIsDefault_ThenZerosNotFound(
var randomNumbers = new double[samplesCount];
for (var i = 0; i < samplesCount; i++)
{
randomNumbers[i] = random.NextDouble();
randomNumbers[i] = randomizer.Next();
}

var threadIssues = randomNumbers.Count(x => x == 0);
Expand All @@ -103,7 +104,7 @@ public void WhenGettingNextBytesParallel_AndBufferLength8_ThenZerosNotFound(
{
// Arrange.
const int bufferLength = 8;
var random = _concurrentPseudoRandom;
IArrayRandomizer<byte[]> randomizer = _concurrentPseudoRandom;
var sourceBuffer = new byte[bufferLength];
var allThreadIssues = 0;

Expand All @@ -114,7 +115,7 @@ public void WhenGettingNextBytesParallel_AndBufferLength8_ThenZerosNotFound(
for (var i = 0; i < samplesCount; i++)
{
var bufferCopy = sourceBuffer.ToArray();
random.NextBytes(bufferCopy);
randomizer.Next(bufferCopy);
results.Add(bufferCopy);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@
// SPDX-License-Identifier: Apache-2.0

using System.Collections;
using Depra.Random.System;
using Depra.Random.UnitTests.Helpers;
using Depra.Random.Application.System;
using Depra.Random.Application.UnitTests.Helpers;
using Depra.Random.Domain.Randomizers;

namespace Depra.Random.UnitTests;
namespace Depra.Random.Application.UnitTests;

[TestFixture(TestOf = typeof(CryptoRandom))]
[TestFixture(TestOf = typeof(CryptoRandomizer))]
internal class CryptoRandomTests
{
private CryptoRandom _cryptoRandom = null!;
private CryptoRandomizer _cryptoRandomizer = null!;

[SetUp]
public void SetUp() => _cryptoRandom = new CryptoRandom();
public void SetUp() => _cryptoRandomizer = new CryptoRandomizer();

[TearDown]
public void TearDown() => _cryptoRandom.Dispose();
public void TearDown() => _cryptoRandomizer.Dispose();

[Test]
public void WhenGettingNextInt32_AndRangeIsDefault_ThenRandomNumbersAreUnique(
[Values(10_000)] int samplesCount)
{
// Arrange.
var randomizer = _cryptoRandom;
var randomizer = _cryptoRandomizer;
var randomNumbers = new int[samplesCount];

// Act.
Expand All @@ -45,13 +46,13 @@ public void WhenGettingNextInt32_AndInRangeWithMinAndMax_ThenRandomNumbersAreUni
// Arrange.
const int minValue = int.MinValue;
const int maxValue = int.MaxValue;
var random = _cryptoRandom;
var randomizer = _cryptoRandomizer;
var randomNumbers = new int[samplesCount];

// Act.
for (var i = 0; i < samplesCount; i++)
{
randomNumbers[i] = random.Next(minValue, maxValue);
randomNumbers[i] = randomizer.Next(minValue, maxValue);
}

ConsoleHelper.PrintRandomizeResultForCollection(randomNumbers, minValue, maxValue);
Expand All @@ -65,13 +66,13 @@ public void WhenGettingNextDouble_AndRangeIsDefault_ThenRandomNumbersAreUnique(
[Values(10_000)] int samplesCount)
{
// Arrange.
var random = _cryptoRandom;
ITypedRandomizer<double> randomizer = _cryptoRandomizer;
var randomNumbers = new double[samplesCount];

// Act.
for (var i = 0; i < samplesCount; i++)
{
randomNumbers[i] = random.NextDouble();
randomNumbers[i] = randomizer.Next();
}

ConsoleHelper.PrintCollection(randomNumbers);
Expand All @@ -86,14 +87,14 @@ public void WhenGettingNextBytes_AndBufferLength8_ThenRandomByteArraysAreUnique(
{
// Arrange.
const int bufferLength = 8;
var random = _cryptoRandom;
IArrayRandomizer<byte[]> randomizer = _cryptoRandomizer;
var bytesStack = new Stack<byte[]>();

// Act.
for (var i = 0; i < samplesCount; i++)
{
var buffer = new byte[bufferLength];
random.NextBytes(buffer);
randomizer.Next(buffer);
bytesStack.Push(buffer);

ConsoleHelper.PrintBytes(buffer);
Expand All @@ -110,7 +111,7 @@ public void WhenGettingNextInt32Parallel_AndInRangeWithMinAndMax_ThenZerosNotFou
// Arrange.
const int minValue = int.MinValue;
const int maxValue = int.MaxValue;
var random = _cryptoRandom;
var randomizer = _cryptoRandomizer;
var allThreadIssues = 0;

// Act.
Expand All @@ -119,7 +120,7 @@ public void WhenGettingNextInt32Parallel_AndInRangeWithMinAndMax_ThenZerosNotFou
var numbers = new int[samplesCount];
for (var i = 0; i < samplesCount; i++)
{
numbers[i] = random.Next(minValue, maxValue);
numbers[i] = randomizer.Next(minValue, maxValue);
}

var threadIssues = numbers.Count(x => x == 0);
Expand All @@ -137,7 +138,7 @@ public void WhenGettingNextDoubleParallel_AndRangeIsDefault_ThenZerosNotFound(
[Values(10_000)] int samplesCount)
{
// Arrange.
var random = _cryptoRandom;
ITypedRandomizer<double> randomizer = _cryptoRandomizer;
var allThreadIssues = 0;

// Act.
Expand All @@ -146,7 +147,7 @@ public void WhenGettingNextDoubleParallel_AndRangeIsDefault_ThenZerosNotFound(
var numbers = new double[samplesCount];
for (var i = 0; i < samplesCount; i++)
{
numbers[i] = random.NextDouble();
numbers[i] = randomizer.Next();
}

var threadIssues = numbers.Count(x => x == 0);
Expand All @@ -165,7 +166,7 @@ public void WhenGettingNextBytesParallel_AndBufferLength8_ThenZerosNotFound(
{
// Arrange.
const int bufferLength = 8;
var random = _cryptoRandom;
IArrayRandomizer<byte[]> randomizer = _cryptoRandomizer;
var sourceBuffer = new byte[bufferLength];
var allThreadIssues = 0;

Expand All @@ -176,7 +177,7 @@ public void WhenGettingNextBytesParallel_AndBufferLength8_ThenZerosNotFound(
for (var i = 0; i < samplesCount; i++)
{
var bufferCopy = sourceBuffer.ToArray();
random.NextBytes(bufferCopy);
randomizer.Next(bufferCopy);
results.Add(bufferCopy);
}

Expand Down
60 changes: 60 additions & 0 deletions Random.Application.UnitTests/Helpers/ConsoleHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright © 2022 Nikolay Melnikov. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

namespace Depra.Random.Application.UnitTests.Helpers;

internal static class ConsoleHelper
{
public static void PrintCollection<T>(IEnumerable<T> collection)
{
foreach (var element in collection)
{
Console.WriteLine(element);
}
}

public static void PrintRandomizeResultForCollection<T>(IEnumerable<T> collection, T minInclusive,
T maxExclusive)
{
Console.WriteLine($"minInclusive: {minInclusive}\n" +
$"maxExclusive: {maxExclusive}\n");

PrintCollection(collection);
}

public static void PrintBytes(IEnumerable<byte> bytes)
{
foreach (var @byte in bytes)
{
Console.Write(@byte + " ");
}

Console.WriteLine();
}

public static void PrintRandomizeResultForBytes(IEnumerable<byte> bytes, byte minInclusive, byte maxExclusive)
{
Console.WriteLine($"minInclusive: {minInclusive}\n" +
$"maxExclusive: {maxExclusive}\n");

PrintBytes(bytes);
}

public static void PrintSBytes(IEnumerable<sbyte> bytes)
{
foreach (var @byte in bytes)
{
Console.Write(@byte + " ");
}

Console.WriteLine();
}

public static void PrintRandomizeResultForSBytes(IEnumerable<sbyte> bytes, sbyte minInclusive, sbyte maxExclusive)
{
Console.WriteLine($"minInclusive: {minInclusive}\n" +
$"maxExclusive: {maxExclusive}\n");

PrintSBytes(bytes);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright © 2022 Nikolay Melnikov. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

using Depra.Random.System;
using Depra.Random.UnitTests.Helpers;
using Depra.Random.Application.System;
using Depra.Random.Application.UnitTests.Helpers;

namespace Depra.Random.UnitTests;
namespace Depra.Random.Application.UnitTests;

[TestFixture]
internal class PseudoRandomTests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

<IsPackable>false</IsPackable>

<AssemblyName>Depra.Random.UnitTests</AssemblyName>
<AssemblyName>Depra.Random.Application.UnitTests</AssemblyName>

<RootNamespace>Depra.Random.UnitTests</RootNamespace>
<RootNamespace>Depra.Random.Application.UnitTests</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 923b4f6

Please sign in to comment.