Skip to content

Commit

Permalink
Merge branch 'TheAlgorithms:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalkwst authored Oct 14, 2024
2 parents 211f644 + 36a7dd6 commit 745fffa
Show file tree
Hide file tree
Showing 25 changed files with 1,815 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));
}
}
87 changes: 87 additions & 0 deletions Algorithms.Tests/Sorters/Comparison/BasicTeamSorterTests.cs
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();
}
}
}
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
}
}
}
Loading

0 comments on commit 745fffa

Please sign in to comment.