Skip to content

Commit ba1e791

Browse files
authored
Merge branch 'master' into feature/Crypto/Digest/AsconHash
2 parents 30af764 + 36a7dd6 commit ba1e791

25 files changed

+1815
-119
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Numerics;
3+
using Algorithms.Numeric;
4+
using NUnit.Framework;
5+
6+
namespace Algorithms.Tests.Numeric;
7+
8+
public static class AbsTests
9+
{
10+
[TestCase(0, 0)]
11+
[TestCase(34, 34)]
12+
[TestCase(-100000000000.0d, 100000000000.0d)]
13+
[TestCase(-3, 3)]
14+
[TestCase(-3.1443123d, 3.1443123d)]
15+
public static void GetsAbsVal<T>(T inputNum, T expected) where T : INumber<T>
16+
{
17+
// Act
18+
var result = Abs.AbsVal(inputNum);
19+
20+
// Assert
21+
Assert.That(result, Is.EqualTo(expected));
22+
}
23+
24+
[TestCase(new[] { -3, -1, 2, -11 }, -11)]
25+
[TestCase(new[] { 0, 5, 1, 11 }, 11)]
26+
[TestCase(new[] { 3.0, -10.0, -2.0 }, -10.0d)]
27+
public static void GetAbsMax<T>(T[] inputNums, T expected) where T : INumber<T>
28+
{
29+
// Act
30+
var result = Abs.AbsMax(inputNums);
31+
32+
// Assert
33+
Assert.That(result, Is.EqualTo(expected));
34+
}
35+
36+
[Test]
37+
public static void AbsMaxThrowsArgumentException()
38+
{
39+
// Arrange
40+
var inputNums = Array.Empty<int>();
41+
42+
// Assert
43+
Assert.Throws<ArgumentException>(() => Abs.AbsMax(inputNums));
44+
}
45+
46+
[TestCase(new[] { -3, -1, 2, -11 }, -1)]
47+
[TestCase(new[] { -3, -5, 1, -11 }, 1)]
48+
[TestCase(new[] { 0, 5, 1, 11 }, 0)]
49+
public static void GetAbsMin<T>(T[] inputNums, T expected) where T : INumber<T>
50+
{
51+
// Act
52+
var result = Abs.AbsMin(inputNums);
53+
54+
// Assert
55+
Assert.That(result, Is.EqualTo(expected));
56+
}
57+
58+
[Test]
59+
public static void AbsMinThrowsArgumentException()
60+
{
61+
// Arrange
62+
var inputNums = Array.Empty<int>();
63+
64+
// Assert
65+
Assert.Throws<ArgumentException>(() => Abs.AbsMin(inputNums));
66+
}
67+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using Algorithms.Numeric;
3+
using NUnit.Framework;
4+
5+
namespace Algorithms.Tests.Numeric;
6+
7+
public static class SoftMaxTests
8+
{
9+
[TestCase(new[] {5.0, 5.0}, new[] {0.5, 0.5})]
10+
[TestCase(new[] {1.0, 2.0, 3.0}, new[] {0.09003057317038046, 0.24472847105479767, 0.6652409557748219})]
11+
[TestCase(new[] {0.0}, new[] {1.0})]
12+
public static void SoftMaxFunction(double[] input, double[] expected)
13+
{
14+
// Act
15+
var result = SoftMax.Compute(input);
16+
17+
// Assert
18+
Assert.That(result, Is.EqualTo(expected).Within(1e-9));
19+
}
20+
21+
[Test]
22+
public static void SoftMaxFunctionThrowsArgumentException()
23+
{
24+
// Arrange
25+
var input = Array.Empty<double>();
26+
27+
// Assert
28+
Assert.Throws<ArgumentException>(() => SoftMax.Compute(input));
29+
}
30+
31+
[TestCase(new[] {1.0, 2.0, 3.0, 4.0, 5.0})]
32+
[TestCase(new[] {0.0, 0.0, 0.0, 0.0, 0.0})]
33+
[TestCase(new[] {5.0})]
34+
public static void SoftMaxFunctionSumsToOne(double[] input)
35+
{
36+
// Act
37+
var result = SoftMax.Compute(input);
38+
39+
var sum = 0.0;
40+
foreach (var value in result)
41+
{
42+
sum += value;
43+
}
44+
45+
// Assert
46+
Assert.That(sum, Is.EqualTo(1.0).Within(1e-9));
47+
}
48+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using Algorithms.Sorters.Comparison;
2+
using FluentAssertions;
3+
using NUnit.Framework;
4+
using System;
5+
using System.Collections.Generic;
6+
7+
namespace Algorithms.Tests.Sorters.Comparison
8+
{
9+
[TestFixture]
10+
public class BasicTimSorterTests
11+
{
12+
private readonly BasicTimSorter<int> sorter = new(Comparer<int>.Default);
13+
14+
[Test]
15+
public void Sort_EmptyArray_DoesNotThrow()
16+
{
17+
var array = Array.Empty<int>();
18+
Assert.DoesNotThrow(() => sorter.Sort(array));
19+
Assert.That(array, Is.Empty);
20+
}
21+
22+
[Test]
23+
public void Sort_SingleElementArray_DoesNotChangeArray()
24+
{
25+
var array = new[] { 1 };
26+
sorter.Sort(array);
27+
Assert.That(array, Is.EqualTo(new[] { 1 }));
28+
}
29+
30+
[Test]
31+
public void Sort_AlreadySortedArray_DoesNotChangeArray()
32+
{
33+
var array = new[] { 1, 2, 3, 4, 5 };
34+
sorter.Sort(array);
35+
Assert.That(array, Is.EqualTo(new[] { 1, 2, 3, 4, 5 }));
36+
}
37+
38+
[Test]
39+
public void Sort_UnsortedArray_SortsCorrectly()
40+
{
41+
var array = new[] { 5, 3, 1, 4, 2 };
42+
sorter.Sort(array);
43+
Assert.That(array, Is.EqualTo(new[] { 1, 2, 3, 4, 5 }));
44+
}
45+
46+
[Test]
47+
public void Sort_ReverseSortedArray_SortsCorrectly()
48+
{
49+
var array = new[] { 5, 4, 3, 2, 1 };
50+
sorter.Sort(array);
51+
Assert.That(array, Is.EqualTo(new[] { 1, 2, 3, 4, 5 }));
52+
}
53+
54+
[Test]
55+
public void Sort_ArrayWithDuplicates_SortsCorrectly()
56+
{
57+
var array = new[] { 3, 1, 2, 3, 1, 2 };
58+
sorter.Sort(array);
59+
Assert.That(array, Is.EqualTo(new[] { 1, 1, 2, 2, 3, 3 }));
60+
}
61+
62+
[Test]
63+
public void Sort_LargeArray_SortsCorrectly()
64+
{
65+
var array = new int[1000];
66+
for (var i = 0; i < 1000; i++)
67+
{
68+
array[i] = 1000 - i;
69+
}
70+
sorter.Sort(array);
71+
array.Should().BeInAscendingOrder();
72+
}
73+
74+
[Test]
75+
public void Sort_LargeRandomArray_SortsCorrectly()
76+
{
77+
var array = new int[1000];
78+
var random = new Random();
79+
for (var i = 0; i < 1000; i++)
80+
{
81+
array[i] = random.Next(1, 1001);
82+
}
83+
sorter.Sort(array);
84+
array.Should().BeInAscendingOrder();
85+
}
86+
}
87+
}

Algorithms.Tests/Sorters/Comparison/TimSorterTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ namespace Algorithms.Tests.Sorters.Comparison;
99
public static class TimSorterTests
1010
{
1111
private static readonly IntComparer IntComparer = new();
12+
private static readonly TimSorterSettings Settings = new();
1213

1314
[Test]
1415
public static void ArraySorted(
1516
[Random(0, 10_000, 2000)] int n)
1617
{
1718
// Arrange
18-
var sorter = new TimSorter<int>();
19+
var sorter = new TimSorter<int>(Settings, IntComparer);
1920
var (correctArray, testArray) = RandomHelper.GetArrays(n);
2021

2122
// Act
@@ -30,7 +31,7 @@ public static void ArraySorted(
3031
public static void TinyArray()
3132
{
3233
// Arrange
33-
var sorter = new TimSorter<int>();
34+
var sorter = new TimSorter<int>(Settings, IntComparer);
3435
var tinyArray = new[] { 1 };
3536
var correctArray = new[] { 1 };
3637

@@ -45,7 +46,7 @@ public static void TinyArray()
4546
public static void SmallChunks()
4647
{
4748
// Arrange
48-
var sorter = new TimSorter<int>();
49+
var sorter = new TimSorter<int>(Settings, IntComparer);
4950
var (correctArray, testArray) = RandomHelper.GetArrays(800);
5051
Array.Sort(correctArray, IntComparer);
5152
Array.Sort(testArray, IntComparer);
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using Algorithms.Sorters.Utils;
2+
using NUnit.Framework;
3+
using System.Collections.Generic;
4+
5+
namespace Algorithms.Tests.Sorters.Utils
6+
{
7+
[TestFixture]
8+
public class GallopingStrategyTests
9+
{
10+
private readonly IComparer<int> comparer = Comparer<int>.Default;
11+
12+
[Test]
13+
public void GallopLeft_KeyPresent_ReturnsCorrectIndex()
14+
{
15+
var array = new[] { 1, 2, 3, 4, 5 };
16+
var index = GallopingStrategy<int>.GallopLeft(array, 3, 0, array.Length, comparer);
17+
Assert.That(index, Is.EqualTo(2));
18+
}
19+
20+
[Test]
21+
public void GallopLeft_KeyNotPresent_ReturnsCorrectIndex()
22+
{
23+
var array = new[] { 1, 2, 4, 5 };
24+
var index = GallopingStrategy<int>.GallopLeft(array, 3, 0, array.Length, comparer);
25+
Assert.That(index, Is.EqualTo(2));
26+
}
27+
28+
[Test]
29+
public void GallopLeft_KeyLessThanAll_ReturnsZero()
30+
{
31+
var array = new[] { 2, 3, 4, 5 };
32+
var index = GallopingStrategy<int>.GallopLeft(array, 1, 0, array.Length, comparer);
33+
Assert.That(index, Is.EqualTo(0));
34+
}
35+
36+
[Test]
37+
public void GallopLeft_KeyGreaterThanAll_ReturnsLength()
38+
{
39+
var array = new[] { 1, 2, 3, 4 };
40+
var index = GallopingStrategy<int>.GallopLeft(array, 5, 0, array.Length, comparer);
41+
Assert.That(index, Is.EqualTo(array.Length));
42+
}
43+
44+
[Test]
45+
public void GallopRight_KeyPresent_ReturnsCorrectIndex()
46+
{
47+
var array = new[] { 1, 2, 3, 4, 5 };
48+
var index = GallopingStrategy<int>.GallopRight(array, 3, 0, array.Length, comparer);
49+
Assert.That(index, Is.EqualTo(3));
50+
}
51+
52+
[Test]
53+
public void GallopRight_KeyNotPresent_ReturnsCorrectIndex()
54+
{
55+
var array = new[] { 1, 2, 4, 5 };
56+
var index = GallopingStrategy<int>.GallopRight(array, 3, 0, array.Length, comparer);
57+
Assert.That(index, Is.EqualTo(2));
58+
}
59+
60+
[Test]
61+
public void GallopRight_KeyLessThanAll_ReturnsZero()
62+
{
63+
var array = new[] { 2, 3, 4, 5 };
64+
var index = GallopingStrategy<int>.GallopRight(array, 1, 0, array.Length, comparer);
65+
Assert.That(index, Is.EqualTo(0));
66+
}
67+
68+
[Test]
69+
public void GallopRight_KeyGreaterThanAll_ReturnsLength()
70+
{
71+
var array = new[] { 1, 2, 3, 4 };
72+
var index = GallopingStrategy<int>.GallopRight(array, 5, 0, array.Length, comparer);
73+
Assert.That(index, Is.EqualTo(array.Length));
74+
}
75+
76+
[Test]
77+
public void GallopLeft_EmptyArray_ReturnsZero()
78+
{
79+
var array = new int[] { };
80+
var index = GallopingStrategy<int>.GallopLeft(array, 1, 0, array.Length, comparer);
81+
Assert.That(index, Is.EqualTo(0));
82+
}
83+
84+
[Test]
85+
public void GallopRight_EmptyArray_ReturnsZero()
86+
{
87+
var array = new int[] { };
88+
var index = GallopingStrategy<int>.GallopRight(array, 1, 0, array.Length, comparer);
89+
Assert.That(index, Is.EqualTo(0));
90+
}
91+
92+
// Test when (shiftable << 1) < 0 is true
93+
[Test]
94+
public void TestBoundLeftShift_WhenShiftableCausesNegativeShift_ReturnsShiftedValuePlusOne()
95+
{
96+
// Arrange
97+
int shiftable = int.MaxValue; // This should cause a negative result after left shift
98+
99+
// Act
100+
int result = GallopingStrategy<int>.BoundLeftShift(shiftable);
101+
102+
// Assert
103+
Assert.That((shiftable << 1) + 1, Is.EqualTo(result)); // True branch
104+
}
105+
106+
// Test when (shiftable << 1) < 0 is false
107+
[Test]
108+
public void TestBoundLeftShift_WhenShiftableDoesNotCauseNegativeShift_ReturnsMaxValue()
109+
{
110+
// Arrange
111+
int shiftable = 1; // This will not cause a negative result after left shift
112+
113+
// Act
114+
int result = GallopingStrategy<int>.BoundLeftShift(shiftable);
115+
116+
// Assert
117+
Assert.That(int.MaxValue, Is.EqualTo(result)); // False branch
118+
}
119+
}
120+
}

0 commit comments

Comments
 (0)