-
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/basic-timsorter
- Loading branch information
Showing
17 changed files
with
1,114 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)); | ||
} | ||
} |
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 | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
Algorithms.Tests/Strings/PatternMatching/WildCardMatcherTests.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,36 @@ | ||
using Algorithms.Strings.PatternMatching; | ||
using NUnit.Framework; | ||
|
||
namespace Algorithms.Tests.Strings.PatternMatching; | ||
|
||
public static class WildCardMatcherTests | ||
{ | ||
[TestCase("aab", "c*a*b", true)] | ||
[TestCase("aaa", "aa", false)] | ||
[TestCase("aaa", "a.a", true)] | ||
[TestCase("aaab", "aa*", false)] | ||
[TestCase("aaab", ".*", true)] | ||
[TestCase("a", "bbbb", false)] | ||
[TestCase("", "bbbb", false)] | ||
[TestCase("a", "", false)] | ||
[TestCase("", "", true)] | ||
public static void MatchPattern(string inputString, string pattern, bool expected) | ||
{ | ||
// Act | ||
var result = WildCardMatcher.MatchPattern(inputString, pattern); | ||
|
||
// Assert | ||
Assert.That(result, Is.EqualTo(expected)); | ||
} | ||
|
||
[Test] | ||
public static void MatchPatternThrowsArgumentException() | ||
{ | ||
// Arrange | ||
var inputString = "abc"; | ||
var pattern = "*abc"; | ||
|
||
// Assert | ||
Assert.Throws<System.ArgumentException>(() => WildCardMatcher.MatchPattern(inputString, pattern)); | ||
} | ||
} |
Oops, something went wrong.