Skip to content

Commit

Permalink
Improvements for performance; Cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
g0dzZz-coder committed Oct 18, 2022
1 parent 923b4f6 commit 54ff0fc
Show file tree
Hide file tree
Showing 80 changed files with 860 additions and 504 deletions.
14 changes: 7 additions & 7 deletions Random.Application.UnitTests/ConcurrentPseudoRandomTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// SPDX-License-Identifier: Apache-2.0

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

namespace Depra.Random.Application.UnitTests;

[TestFixture(TestOf = typeof(ConcurrentPseudoRandom))]
[TestFixture(TestOf = typeof(ConcurrentPseudoRandomizers))]
internal class ConcurrentPseudoRandomTests
{
private ConcurrentPseudoRandom _concurrentPseudoRandom = null!;
Expand Down Expand Up @@ -76,7 +76,7 @@ public void WhenGettingNextDoubleParallel_AndRangeIsDefault_ThenZerosNotFound(
[Values(10_000)] int samplesCount)
{
// Arrange.
ITypedRandomizer<double> randomizer = _concurrentPseudoRandom;
var randomizer = _concurrentPseudoRandom;
var allThreadIssues = 0;

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

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

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

Expand Down
33 changes: 16 additions & 17 deletions Random.Application.UnitTests/CryptoRandomTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,28 @@
// SPDX-License-Identifier: Apache-2.0

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

namespace Depra.Random.Application.UnitTests;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
// Copyright © 2022 Nikolay Melnikov. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

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

namespace Depra.Random.Application.UnitTests;

[TestFixture]
internal class PseudoRandomTests
[TestFixture(TestOf = typeof(PseudoRandomizers))]
internal class PseudoRandomizersTests
{
private PseudoRandom _pseudoRandom = null!;
private PseudoRandomizers _pseudoRandomizers = null!;

[SetUp]
public void SetUp() => _pseudoRandom = new PseudoRandom();
public void SetUp() => _pseudoRandomizers = new PseudoRandomizers();

[Test]
public void WhenGettingNextInt32_AndRangeIsDefault_ThenRandomNumbersAreNotTheSame(
[Values(10)] int samplesCount)
{
// Arrange.
var randomizer = _pseudoRandom;
var randomNumbers = new int[samplesCount];
var randomizer = _pseudoRandomizers.GetNumberRandomizer<int>();

// Act.
for (var i = 0; i < samplesCount; i++)
Expand All @@ -41,8 +43,8 @@ public void WhenGettingNextInt32_AndInRangeWithMin_ThenRandomNumbersAreInGivenRa
// Arrange.
const int minValue = 0;
const int maxValue = int.MaxValue;
var randomizer = _pseudoRandom;
var randomNumbers = new int[samplesCount];
var randomizer = _pseudoRandomizers.GetNumberRandomizer<int>();

// Act.
for (var i = 0; i < samplesCount; i++)
Expand All @@ -63,8 +65,8 @@ public void WhenGettingNextInt32_AndInRangeWithMinAndMax_ThenRandomNumbersAreInG
// Arrange.
const int minValue = int.MinValue;
const int maxValue = int.MaxValue;
var randomizer = _pseudoRandom;
var randomNumbers = new int[samplesCount];
var randomizer = _pseudoRandomizers.GetNumberRandomizer<int>();

// Act.
for (var i = 0; i < samplesCount; i++)
Expand All @@ -85,8 +87,8 @@ public void WhenGettingNextDouble_AndInDefaultRange_ThenRandomNumbersAreNotTheSa
{
// Arrange.
const double tolerance = 0.01;
var randomizer = _pseudoRandom;
var randomNumbers = new double[samplesCount];
var randomizer = _pseudoRandomizers.GetTypedRandomizer<double>();

// Act.
for (var i = 0; i < samplesCount; i++)
Expand All @@ -104,11 +106,12 @@ public void WhenGettingNextDouble_AndInDefaultRange_ThenRandomNumbersAreNotTheSa
public void WhenGettingNextByteArray_AndBufferWithLenght64_ThenBufferIsNotNullOrEmpty()
{
// Arrange.
var randomizer = _pseudoRandom;
var buffer = new byte[64];
const int bufferLenght = 64;
var buffer = new byte[bufferLenght];
var randomizer = _pseudoRandomizers.GetArrayRandomizer<byte[]>();

// Act.
randomizer.NextBytes(buffer);
randomizer.Next(buffer);
ConsoleHelper.PrintBytes(buffer);

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

using Depra.Random.Application.ServiceBuilder;
using Depra.Random.Domain.Exceptions;
using Depra.Random.Domain.Randomizers;
Expand Down
19 changes: 13 additions & 6 deletions Random.Application.UnitTests/RandomizerExtensionsTests.Boolean.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright © 2022 Nikolay Melnikov. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

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

Expand All @@ -12,17 +13,23 @@ internal static partial class RandomizerExtensionsTests
[TestFixture]
internal class Boolean
{
private PseudoRandom _randomizer = null!;
private INumberRandomizer<int> _intRandomizer = null!;
private ITypedRandomizer<double> _doubleRandomizer = null!;

[SetUp]
public void SetUp() => _randomizer = new PseudoRandom();
public void SetUp()
{
var randomizers = new PseudoRandomizers();
_intRandomizer = randomizers.GetNumberRandomizer<int>();
_doubleRandomizer = randomizers.GetTypedRandomizer<double>();
}

[Test]
public void WhenGettingNextBooleans_AndProbabilityIsDefault_ThenRandomBooleansAreNotTheSame(
[Values(100)] int samplesCount)
{
// Arrange.
INumberRandomizer<int> randomizer = _randomizer;
var randomizer = _intRandomizer;
var randomBooleans = new bool[samplesCount];

// Act.
Expand All @@ -41,7 +48,7 @@ public void WhenGettingNextBoolean_AndProbabilityIsOne_ThenRandomBooleansAreAlwa
{
// Arrange.
const int probability = 1;
ITypedRandomizer<double> randomizer = _randomizer;
var randomizer = _doubleRandomizer;
var randomBooleans = new bool[samplesCount];

// Act.
Expand All @@ -60,7 +67,7 @@ public void WhenGettingNextBoolean_AndProbabilityIsZero_ThenRandomBooleansAreAlw
{
// Arrange.
const int probability = 0;
ITypedRandomizer<double> randomizer = _randomizer;
var randomizer = _doubleRandomizer;
var randomBooleans = new bool[samplesCount];

// Act.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright © 2022 Nikolay Melnikov. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

using Depra.Random.Application.System;
using Depra.Random.Application.Extensions;
using Depra.Random.Application.System.Collections;
using Depra.Random.Application.UnitTests.Helpers;
using Depra.Random.Domain.Extensions;
using Depra.Random.Domain.Randomizers;
Expand All @@ -16,7 +17,7 @@ internal class Byte
private INumberRandomizer<int> _randomizer = null!;

[SetUp]
public void SetUp() => _randomizer = new PseudoRandom();
public void SetUp() => _randomizer = new PseudoRandomizers().GetNumberRandomizer<int>();

[Test]
public void WhenGettingNextByte_AndInRangeWithMax_ThenRandomBytesAreInGivenRange(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using Depra.Random.Application.System;
// Copyright © 2022 Nikolay Melnikov. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

using Depra.Random.Application.Extensions;
using Depra.Random.Application.System.Collections;
using Depra.Random.Application.UnitTests.Helpers;
using Depra.Random.Domain.Extensions;
using Depra.Random.Domain.Randomizers;
Expand All @@ -13,7 +17,7 @@ internal class ByteArray
private IArrayRandomizer<byte[]> _randomizer = null!;

[SetUp]
public void SetUp() => _randomizer = new PseudoRandom();
public void SetUp() => _randomizer = new PseudoRandomizers().GetArrayRandomizer<byte[]>();

[Test]
public void WhenGettingNextByteArray_ThenResultingArrayLengthIsEqualInitial(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright © 2022 Nikolay Melnikov. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

using Depra.Random.Application.System;
using Depra.Random.Application.Extensions;
using Depra.Random.Application.System.Collections;
using Depra.Random.Application.UnitTests.Helpers;
using Depra.Random.Domain.Extensions;
using Depra.Random.Domain.Randomizers;
Expand All @@ -16,7 +17,7 @@ internal class CharArray
private INumberRandomizer<int> _randomizer = null!;

[SetUp]
public void SetUp() => _randomizer = new PseudoRandom();
public void SetUp() => _randomizer = new PseudoRandomizers().GetNumberRandomizer<int>();

[Test]
public void WhenGettingNextChars_AndUsingBuffer_ThenRandomCharArrayIsNotNullOrEmpty(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright © 2022 Nikolay Melnikov. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

using Depra.Random.Application.System;
using Depra.Random.Application.Extensions;
using Depra.Random.Application.System.Collections;
using Depra.Random.Application.UnitTests.Helpers;
using Depra.Random.Domain.Extensions;
using Depra.Random.Domain.Randomizers;
Expand All @@ -16,7 +17,7 @@ internal class Decimal
private INumberRandomizer<int> _randomizer = null!;

[SetUp]
public void SetUp() => _randomizer = new PseudoRandom();
public void SetUp() => _randomizer = new PseudoRandomizers().GetNumberRandomizer<int>();

[Test]
public void WhenGettingNextDecimal_AndRangeIsDefault_ThenRandomNumbersAreNotTheSame()
Expand Down
Loading

0 comments on commit 54ff0fc

Please sign in to comment.