-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/Crypto/Digest/AsconHash
- Loading branch information
Showing
25 changed files
with
1,815 additions
and
119 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System; | ||
using System.Numerics; | ||
using Algorithms.Numeric; | ||
using NUnit.Framework; | ||
|
||
namespace Algorithms.Tests.Numeric; | ||
|
||
public static class AbsTests | ||
{ | ||
[TestCase(0, 0)] | ||
[TestCase(34, 34)] | ||
[TestCase(-100000000000.0d, 100000000000.0d)] | ||
[TestCase(-3, 3)] | ||
[TestCase(-3.1443123d, 3.1443123d)] | ||
public static void GetsAbsVal<T>(T inputNum, T expected) where T : INumber<T> | ||
{ | ||
// Act | ||
var result = Abs.AbsVal(inputNum); | ||
|
||
// Assert | ||
Assert.That(result, Is.EqualTo(expected)); | ||
} | ||
|
||
[TestCase(new[] { -3, -1, 2, -11 }, -11)] | ||
[TestCase(new[] { 0, 5, 1, 11 }, 11)] | ||
[TestCase(new[] { 3.0, -10.0, -2.0 }, -10.0d)] | ||
public static void GetAbsMax<T>(T[] inputNums, T expected) where T : INumber<T> | ||
{ | ||
// Act | ||
var result = Abs.AbsMax(inputNums); | ||
|
||
// Assert | ||
Assert.That(result, Is.EqualTo(expected)); | ||
} | ||
|
||
[Test] | ||
public static void AbsMaxThrowsArgumentException() | ||
{ | ||
// Arrange | ||
var inputNums = Array.Empty<int>(); | ||
|
||
// Assert | ||
Assert.Throws<ArgumentException>(() => Abs.AbsMax(inputNums)); | ||
} | ||
|
||
[TestCase(new[] { -3, -1, 2, -11 }, -1)] | ||
[TestCase(new[] { -3, -5, 1, -11 }, 1)] | ||
[TestCase(new[] { 0, 5, 1, 11 }, 0)] | ||
public static void GetAbsMin<T>(T[] inputNums, T expected) where T : INumber<T> | ||
{ | ||
// Act | ||
var result = Abs.AbsMin(inputNums); | ||
|
||
// Assert | ||
Assert.That(result, Is.EqualTo(expected)); | ||
} | ||
|
||
[Test] | ||
public static void AbsMinThrowsArgumentException() | ||
{ | ||
// Arrange | ||
var inputNums = Array.Empty<int>(); | ||
|
||
// Assert | ||
Assert.Throws<ArgumentException>(() => Abs.AbsMin(inputNums)); | ||
} | ||
} |
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,48 @@ | ||
using System; | ||
using Algorithms.Numeric; | ||
using NUnit.Framework; | ||
|
||
namespace Algorithms.Tests.Numeric; | ||
|
||
public static class SoftMaxTests | ||
{ | ||
[TestCase(new[] {5.0, 5.0}, new[] {0.5, 0.5})] | ||
[TestCase(new[] {1.0, 2.0, 3.0}, new[] {0.09003057317038046, 0.24472847105479767, 0.6652409557748219})] | ||
[TestCase(new[] {0.0}, new[] {1.0})] | ||
public static void SoftMaxFunction(double[] input, double[] expected) | ||
{ | ||
// Act | ||
var result = SoftMax.Compute(input); | ||
|
||
// Assert | ||
Assert.That(result, Is.EqualTo(expected).Within(1e-9)); | ||
} | ||
|
||
[Test] | ||
public static void SoftMaxFunctionThrowsArgumentException() | ||
{ | ||
// Arrange | ||
var input = Array.Empty<double>(); | ||
|
||
// Assert | ||
Assert.Throws<ArgumentException>(() => SoftMax.Compute(input)); | ||
} | ||
|
||
[TestCase(new[] {1.0, 2.0, 3.0, 4.0, 5.0})] | ||
[TestCase(new[] {0.0, 0.0, 0.0, 0.0, 0.0})] | ||
[TestCase(new[] {5.0})] | ||
public static void SoftMaxFunctionSumsToOne(double[] input) | ||
{ | ||
// Act | ||
var result = SoftMax.Compute(input); | ||
|
||
var sum = 0.0; | ||
foreach (var value in result) | ||
{ | ||
sum += value; | ||
} | ||
|
||
// Assert | ||
Assert.That(sum, Is.EqualTo(1.0).Within(1e-9)); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
Algorithms.Tests/Sorters/Comparison/BasicTeamSorterTests.cs
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,87 @@ | ||
using Algorithms.Sorters.Comparison; | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Algorithms.Tests.Sorters.Comparison | ||
{ | ||
[TestFixture] | ||
public class BasicTimSorterTests | ||
{ | ||
private readonly BasicTimSorter<int> sorter = new(Comparer<int>.Default); | ||
|
||
[Test] | ||
public void Sort_EmptyArray_DoesNotThrow() | ||
{ | ||
var array = Array.Empty<int>(); | ||
Assert.DoesNotThrow(() => sorter.Sort(array)); | ||
Assert.That(array, Is.Empty); | ||
} | ||
|
||
[Test] | ||
public void Sort_SingleElementArray_DoesNotChangeArray() | ||
{ | ||
var array = new[] { 1 }; | ||
sorter.Sort(array); | ||
Assert.That(array, Is.EqualTo(new[] { 1 })); | ||
} | ||
|
||
[Test] | ||
public void Sort_AlreadySortedArray_DoesNotChangeArray() | ||
{ | ||
var array = new[] { 1, 2, 3, 4, 5 }; | ||
sorter.Sort(array); | ||
Assert.That(array, Is.EqualTo(new[] { 1, 2, 3, 4, 5 })); | ||
} | ||
|
||
[Test] | ||
public void Sort_UnsortedArray_SortsCorrectly() | ||
{ | ||
var array = new[] { 5, 3, 1, 4, 2 }; | ||
sorter.Sort(array); | ||
Assert.That(array, Is.EqualTo(new[] { 1, 2, 3, 4, 5 })); | ||
} | ||
|
||
[Test] | ||
public void Sort_ReverseSortedArray_SortsCorrectly() | ||
{ | ||
var array = new[] { 5, 4, 3, 2, 1 }; | ||
sorter.Sort(array); | ||
Assert.That(array, Is.EqualTo(new[] { 1, 2, 3, 4, 5 })); | ||
} | ||
|
||
[Test] | ||
public void Sort_ArrayWithDuplicates_SortsCorrectly() | ||
{ | ||
var array = new[] { 3, 1, 2, 3, 1, 2 }; | ||
sorter.Sort(array); | ||
Assert.That(array, Is.EqualTo(new[] { 1, 1, 2, 2, 3, 3 })); | ||
} | ||
|
||
[Test] | ||
public void Sort_LargeArray_SortsCorrectly() | ||
{ | ||
var array = new int[1000]; | ||
for (var i = 0; i < 1000; i++) | ||
{ | ||
array[i] = 1000 - i; | ||
} | ||
sorter.Sort(array); | ||
array.Should().BeInAscendingOrder(); | ||
} | ||
|
||
[Test] | ||
public void Sort_LargeRandomArray_SortsCorrectly() | ||
{ | ||
var array = new int[1000]; | ||
var random = new Random(); | ||
for (var i = 0; i < 1000; i++) | ||
{ | ||
array[i] = random.Next(1, 1001); | ||
} | ||
sorter.Sort(array); | ||
array.Should().BeInAscendingOrder(); | ||
} | ||
} | ||
} |
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
120 changes: 120 additions & 0 deletions
120
Algorithms.Tests/Sorters/Utils/GallopingStrategyTests.cs
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,120 @@ | ||
using Algorithms.Sorters.Utils; | ||
using NUnit.Framework; | ||
using System.Collections.Generic; | ||
|
||
namespace Algorithms.Tests.Sorters.Utils | ||
{ | ||
[TestFixture] | ||
public class GallopingStrategyTests | ||
{ | ||
private readonly IComparer<int> comparer = Comparer<int>.Default; | ||
|
||
[Test] | ||
public void GallopLeft_KeyPresent_ReturnsCorrectIndex() | ||
{ | ||
var array = new[] { 1, 2, 3, 4, 5 }; | ||
var index = GallopingStrategy<int>.GallopLeft(array, 3, 0, array.Length, comparer); | ||
Assert.That(index, Is.EqualTo(2)); | ||
} | ||
|
||
[Test] | ||
public void GallopLeft_KeyNotPresent_ReturnsCorrectIndex() | ||
{ | ||
var array = new[] { 1, 2, 4, 5 }; | ||
var index = GallopingStrategy<int>.GallopLeft(array, 3, 0, array.Length, comparer); | ||
Assert.That(index, Is.EqualTo(2)); | ||
} | ||
|
||
[Test] | ||
public void GallopLeft_KeyLessThanAll_ReturnsZero() | ||
{ | ||
var array = new[] { 2, 3, 4, 5 }; | ||
var index = GallopingStrategy<int>.GallopLeft(array, 1, 0, array.Length, comparer); | ||
Assert.That(index, Is.EqualTo(0)); | ||
} | ||
|
||
[Test] | ||
public void GallopLeft_KeyGreaterThanAll_ReturnsLength() | ||
{ | ||
var array = new[] { 1, 2, 3, 4 }; | ||
var index = GallopingStrategy<int>.GallopLeft(array, 5, 0, array.Length, comparer); | ||
Assert.That(index, Is.EqualTo(array.Length)); | ||
} | ||
|
||
[Test] | ||
public void GallopRight_KeyPresent_ReturnsCorrectIndex() | ||
{ | ||
var array = new[] { 1, 2, 3, 4, 5 }; | ||
var index = GallopingStrategy<int>.GallopRight(array, 3, 0, array.Length, comparer); | ||
Assert.That(index, Is.EqualTo(3)); | ||
} | ||
|
||
[Test] | ||
public void GallopRight_KeyNotPresent_ReturnsCorrectIndex() | ||
{ | ||
var array = new[] { 1, 2, 4, 5 }; | ||
var index = GallopingStrategy<int>.GallopRight(array, 3, 0, array.Length, comparer); | ||
Assert.That(index, Is.EqualTo(2)); | ||
} | ||
|
||
[Test] | ||
public void GallopRight_KeyLessThanAll_ReturnsZero() | ||
{ | ||
var array = new[] { 2, 3, 4, 5 }; | ||
var index = GallopingStrategy<int>.GallopRight(array, 1, 0, array.Length, comparer); | ||
Assert.That(index, Is.EqualTo(0)); | ||
} | ||
|
||
[Test] | ||
public void GallopRight_KeyGreaterThanAll_ReturnsLength() | ||
{ | ||
var array = new[] { 1, 2, 3, 4 }; | ||
var index = GallopingStrategy<int>.GallopRight(array, 5, 0, array.Length, comparer); | ||
Assert.That(index, Is.EqualTo(array.Length)); | ||
} | ||
|
||
[Test] | ||
public void GallopLeft_EmptyArray_ReturnsZero() | ||
{ | ||
var array = new int[] { }; | ||
var index = GallopingStrategy<int>.GallopLeft(array, 1, 0, array.Length, comparer); | ||
Assert.That(index, Is.EqualTo(0)); | ||
} | ||
|
||
[Test] | ||
public void GallopRight_EmptyArray_ReturnsZero() | ||
{ | ||
var array = new int[] { }; | ||
var index = GallopingStrategy<int>.GallopRight(array, 1, 0, array.Length, comparer); | ||
Assert.That(index, Is.EqualTo(0)); | ||
} | ||
|
||
// Test when (shiftable << 1) < 0 is true | ||
[Test] | ||
public void TestBoundLeftShift_WhenShiftableCausesNegativeShift_ReturnsShiftedValuePlusOne() | ||
{ | ||
// Arrange | ||
int shiftable = int.MaxValue; // This should cause a negative result after left shift | ||
|
||
// Act | ||
int result = GallopingStrategy<int>.BoundLeftShift(shiftable); | ||
|
||
// Assert | ||
Assert.That((shiftable << 1) + 1, Is.EqualTo(result)); // True branch | ||
} | ||
|
||
// Test when (shiftable << 1) < 0 is false | ||
[Test] | ||
public void TestBoundLeftShift_WhenShiftableDoesNotCauseNegativeShift_ReturnsMaxValue() | ||
{ | ||
// Arrange | ||
int shiftable = 1; // This will not cause a negative result after left shift | ||
|
||
// Act | ||
int result = GallopingStrategy<int>.BoundLeftShift(shiftable); | ||
|
||
// Assert | ||
Assert.That(int.MaxValue, Is.EqualTo(result)); // False branch | ||
} | ||
} | ||
} |
Oops, something went wrong.