Skip to content

Commit

Permalink
Merge branch 'master' into feature/basic-timsorter
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalkwst authored Sep 23, 2024
2 parents 1dad0c8 + ab2b5cc commit 6262ead
Show file tree
Hide file tree
Showing 17 changed files with 1,114 additions and 119 deletions.
67 changes: 67 additions & 0 deletions Algorithms.Tests/Numeric/AbsTests.cs
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));
}
}
48 changes: 48 additions & 0 deletions Algorithms.Tests/Numeric/SoftMaxTests.cs
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));
}
}
7 changes: 4 additions & 3 deletions Algorithms.Tests/Sorters/Comparison/TimSorterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ namespace Algorithms.Tests.Sorters.Comparison;
public static class TimSorterTests
{
private static readonly IntComparer IntComparer = new();
private static readonly TimSorterSettings Settings = new();

[Test]
public static void ArraySorted(
[Random(0, 10_000, 2000)] int n)
{
// Arrange
var sorter = new TimSorter<int>();
var sorter = new TimSorter<int>(Settings, IntComparer);
var (correctArray, testArray) = RandomHelper.GetArrays(n);

// Act
Expand All @@ -30,7 +31,7 @@ public static void ArraySorted(
public static void TinyArray()
{
// Arrange
var sorter = new TimSorter<int>();
var sorter = new TimSorter<int>(Settings, IntComparer);
var tinyArray = new[] { 1 };
var correctArray = new[] { 1 };

Expand All @@ -45,7 +46,7 @@ public static void TinyArray()
public static void SmallChunks()
{
// Arrange
var sorter = new TimSorter<int>();
var sorter = new TimSorter<int>(Settings, IntComparer);
var (correctArray, testArray) = RandomHelper.GetArrays(800);
Array.Sort(correctArray, IntComparer);
Array.Sort(testArray, IntComparer);
Expand Down
120 changes: 120 additions & 0 deletions Algorithms.Tests/Sorters/Utils/GallopingStrategyTests.cs
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 Algorithms.Tests/Strings/PatternMatching/WildCardMatcherTests.cs
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));
}
}
Loading

0 comments on commit 6262ead

Please sign in to comment.