Skip to content

Commit b930208

Browse files
committed
Merge branch 'master' into feature/tsp-traveling-salesman-problem
2 parents d9e2234 + e2c20ed commit b930208

File tree

102 files changed

+704
-593
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+704
-593
lines changed

Algorithms.Tests/Crypto/Digests/AsconDigestTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public void AsconHash_WhenGetByteLengthIsCalled_ReturnsCorrectValue()
159159
public void Update_ShouldProcessByte_WhenBufferIsFull()
160160
{
161161
// Arrange
162-
byte[] inputBytes = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 }; // 8 bytes to fill the buffer
162+
byte[] inputBytes = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77]; // 8 bytes to fill the buffer
163163

164164
// Act
165165
foreach (var input in inputBytes)
@@ -178,7 +178,7 @@ public void Update_ShouldProcessByte_WhenBufferIsFull()
178178
public void Update_ShouldNotProcess_WhenBufferIsNotFull()
179179
{
180180
// Arrange
181-
byte[] inputBytes = { 0x00, 0x11, 0x22, 0x33 }; // Only 4 bytes (buffer is not full)
181+
byte[] inputBytes = [0x00, 0x11, 0x22, 0x33]; // Only 4 bytes (buffer is not full)
182182

183183
// Act
184184
foreach (var input in inputBytes)
@@ -233,7 +233,7 @@ public void Update_ShouldHandleSingleByteCorrectly()
233233
public void Update_ShouldAccumulateStateWithMultipleUpdates()
234234
{
235235
// Arrange
236-
byte[] inputBytes = { 0x00, 0x11, 0x22 }; // Partial input
236+
byte[] inputBytes = [0x00, 0x11, 0x22]; // Partial input
237237

238238
// Act
239239
foreach (var input in inputBytes)
@@ -242,7 +242,7 @@ public void Update_ShouldAccumulateStateWithMultipleUpdates()
242242
}
243243

244244
// Add more data to fill the buffer.
245-
byte[] additionalBytes = { 0x33, 0x44, 0x55, 0x66, 0x77 };
245+
byte[] additionalBytes = [0x33, 0x44, 0x55, 0x66, 0x77];
246246
foreach (var input in additionalBytes)
247247
{
248248
asconHashA.Update(input);

Algorithms.Tests/Crypto/Paddings/Iso7816D4PaddingTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Algorithms.Crypto.Paddings;
1+
using Algorithms.Crypto.Paddings;
22

33
namespace Algorithms.Tests.Crypto.Paddings;
44

@@ -75,7 +75,7 @@ public void RemovePadding_WhenCalledWithValidInput_shouldReturnCorrectData()
7575

7676
var result = padding.RemovePadding(inputData);
7777

78-
result.Should().Equal(new byte[] { 1, 2, 3, 4, 5 });
78+
result.Should().Equal([1, 2, 3, 4, 5]);
7979
}
8080

8181
[Test]

Algorithms.Tests/Crypto/Utils/ByteEncodingUtils.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class ByteEncodingUtilsTests
99
public void BigEndianToUint64_ByteArray_ShouldConvertCorrectly()
1010
{
1111
// Arrange
12-
byte[] input = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
12+
byte[] input = [0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF];
1313
var expected = 0x0123456789ABCDEFUL;
1414

1515
// Act
@@ -23,7 +23,7 @@ public void BigEndianToUint64_ByteArray_ShouldConvertCorrectly()
2323
public void BigEndianToUint64_ByteArray_WithOffset_ShouldConvertCorrectly()
2424
{
2525
// Arrange
26-
byte[] input = { 0x00, 0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
26+
byte[] input = [0x00, 0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF];
2727
var expected = 0x0123456789ABCDEFUL;
2828

2929
// Act
@@ -53,7 +53,7 @@ public void UInt64ToBigEndian_ShouldWriteCorrectly()
5353
// Arrange
5454
var value = 0x0123456789ABCDEFUL;
5555
Span<byte> output = stackalloc byte[8];
56-
byte[] expected = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };
56+
byte[] expected = [0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF];
5757

5858
// Act
5959
ByteEncodingUtils.UInt64ToBigEndian(value, output);
@@ -66,7 +66,7 @@ public void UInt64ToBigEndian_ShouldWriteCorrectly()
6666
public void BigEndianToUint64_InvalidOffset_ShouldThrowException()
6767
{
6868
// Arrange
69-
byte[] input = { 0x01, 0x23 };
69+
byte[] input = [0x01, 0x23];
7070

7171
// Act
7272
Action act = () => ByteEncodingUtils.BigEndianToUint64(input, 1);

Algorithms.Tests/Encoders/NysiisEncoderTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ namespace Algorithms.Tests.Encoders;
55
public class NysiisEncoderTests
66
{
77
private static readonly string[] Names =
8-
{
8+
[
99
"Jay", "John", "Jane", "Zayne", "Guerra", "Iga", "Cowan", "Louisa", "Arnie", "Olsen", "Corban", "Nava",
1010
"Cynthia Malone", "Amiee MacKee", "MacGyver", "Yasmin Edge",
11-
};
11+
];
1212

1313
private static readonly string[] Expected =
14-
{
14+
[
1515
"JY", "JAN", "JAN", "ZAYN", "GAR", "IG", "CAN", "LAS", "ARNY", "OLSAN", "CARBAN", "NAV", "CYNTANALAN",
1616
"ANANACY", "MCGYVAR", "YASNANADG",
17-
};
17+
];
1818

1919
private static IEnumerable<string[]> TestData => Names.Zip(Expected, (l, r) => new[] { l, r });
2020

Algorithms.Tests/Encoders/SoundexEncoderTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ namespace Algorithms.Tests.Encoders;
55
public static class SoundexEncoderTest
66
{
77
private static readonly string[] Names =
8-
{
8+
[
99
"Robert", "Rupert", "Rubin", "Ashcraft", "Ashcroft", "Tymczak", "Pfister", "Honeyman",
10-
};
10+
];
1111

12-
private static readonly string[] Expected = { "R163", "R163", "R150", "A261", "A261", "T522", "P236", "H555" };
12+
private static readonly string[] Expected = ["R163", "R163", "R150", "A261", "A261", "T522", "P236", "H555"];
1313

1414
private static IEnumerable<string[]> TestData => Names.Zip(Expected, (l, r) => new[] { l, r });
1515

Algorithms.Tests/Graph/BellmanFordTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void CorrectDistancesTest()
3535
{ vertex5, -4 }
3636
};
3737

38-
var bellmanFord = new BellmanFord<int>(graph, new Dictionary<Vertex<int>, double>(), new Dictionary<Vertex<int>, Vertex<int>?>());
38+
var bellmanFord = new BellmanFord<int>(graph, [], []);
3939

4040
var calculatedDistances = bellmanFord.Run(vertex1);
4141

@@ -61,7 +61,7 @@ public void NegativeWeightCycleTest()
6161
graph.AddEdge(vertex2, vertex3, -2);
6262
graph.AddEdge(vertex3, vertex1, -3);
6363

64-
var bellmanFord = new BellmanFord<int>(graph, new Dictionary<Vertex<int>, double>(), new Dictionary<Vertex<int>, Vertex<int>?>());
64+
var bellmanFord = new BellmanFord<int>(graph, [], []);
6565

6666
Action action = () => bellmanFord.Run(vertex1);
6767

Algorithms.Tests/Graph/BreadthFirstTreeTraversalTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public static class BreadthFirstTreeTraversalTests
99
public static void CorrectLevelOrderTraversal()
1010
{
1111
// Arrange
12-
int[] correctPath = { 7, 4, 13, 2, 5, 11, 15, 14, 16 };
13-
int[] insertionOrder = { 7, 13, 11, 15, 14, 4, 5, 16, 2 };
12+
int[] correctPath = [7, 4, 13, 2, 5, 11, 15, 14, 16];
13+
int[] insertionOrder = [7, 13, 11, 15, 14, 4, 5, 16, 2];
1414
BinarySearchTree<int> testTree = new BinarySearchTree<int>();
1515
foreach (int data in insertionOrder)
1616
{
@@ -60,7 +60,7 @@ public static void DeepestNodeInTree()
6060
{
6161
// Arrange
6262
BinarySearchTree<int> testTree = new BinarySearchTree<int>();
63-
int[] insertion = { 7, 13, 11, 15, 4, 5, 12, 2, 9 };
63+
int[] insertion = [7, 13, 11, 15, 4, 5, 12, 2, 9];
6464
foreach (int data in insertion)
6565
{
6666
testTree.Add(data);

Algorithms.Tests/LinearAlgebra/Eigenvalue/PowerIterationTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Algorithms.Tests.LinearAlgebra.Eigenvalue;
55
public class PowerIterationTests
66
{
77
private static readonly object[] DominantVectorTestCases =
8-
{
8+
[
99
new object[]
1010
{
1111
3.0,
@@ -18,7 +18,7 @@ public class PowerIterationTests
1818
new[] { 0.91287093, 0.40824829 },
1919
new[,] { { 2.0, 5.0 }, { 1.0, 2.0 } },
2020
},
21-
};
21+
];
2222

2323
private readonly double epsilon = Math.Pow(10, -5);
2424

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using Algorithms.MachineLearning;
2+
3+
namespace Algorithms.Tests.MachineLearning;
4+
5+
/// <summary>
6+
/// Unit tests for the LinearRegression class.
7+
/// </summary>
8+
public class LinearRegressionTests
9+
{
10+
[Test]
11+
public void Fit_ThrowsException_WhenInputIsNull()
12+
{
13+
var lr = new LinearRegression();
14+
Assert.Throws<ArgumentException>(() => lr.Fit(null!, new List<double> { 1 }));
15+
Assert.Throws<ArgumentException>(() => lr.Fit(new List<double> { 1 }, null!));
16+
}
17+
18+
[Test]
19+
public void Fit_ThrowsException_WhenInputIsEmpty()
20+
{
21+
var lr = new LinearRegression();
22+
Assert.Throws<ArgumentException>(() => lr.Fit(new List<double>(), new List<double>()));
23+
}
24+
25+
[Test]
26+
public void Fit_ThrowsException_WhenInputLengthsDiffer()
27+
{
28+
var lr = new LinearRegression();
29+
Assert.Throws<ArgumentException>(() => lr.Fit(new List<double> { 1 }, new List<double> { 2, 3 }));
30+
}
31+
32+
[Test]
33+
public void Fit_ThrowsException_WhenXVarianceIsZero()
34+
{
35+
var lr = new LinearRegression();
36+
Assert.Throws<ArgumentException>(() => lr.Fit(new List<double> { 1, 1, 1 }, new List<double> { 2, 3, 4 }));
37+
}
38+
39+
[Test]
40+
public void Predict_ThrowsException_IfNotFitted()
41+
{
42+
var lr = new LinearRegression();
43+
Assert.Throws<InvalidOperationException>(() => lr.Predict(1.0));
44+
Assert.Throws<InvalidOperationException>(() => lr.Predict(new List<double> { 1.0 }));
45+
}
46+
47+
[Test]
48+
public void FitAndPredict_WorksForSimpleData()
49+
{
50+
// y = 2x + 1
51+
var x = new List<double> { 1, 2, 3, 4 };
52+
var y = new List<double> { 3, 5, 7, 9 };
53+
var lr = new LinearRegression();
54+
lr.Fit(x, y);
55+
Assert.That(lr.IsFitted, Is.True);
56+
Assert.That(lr.Intercept, Is.EqualTo(1.0).Within(1e-6));
57+
Assert.That(lr.Slope, Is.EqualTo(2.0).Within(1e-6));
58+
Assert.That(lr.Predict(5), Is.EqualTo(11.0).Within(1e-6));
59+
}
60+
61+
[Test]
62+
public void FitAndPredict_WorksForNegativeSlope()
63+
{
64+
// y = -3x + 4
65+
var x = new List<double> { 0, 1, 2 };
66+
var y = new List<double> { 4, 1, -2 };
67+
var lr = new LinearRegression();
68+
lr.Fit(x, y);
69+
Assert.That(lr.Intercept, Is.EqualTo(4.0).Within(1e-6));
70+
Assert.That(lr.Slope, Is.EqualTo(-3.0).Within(1e-6));
71+
Assert.That(lr.Predict(3), Is.EqualTo(-5.0).Within(1e-6));
72+
}
73+
74+
[Test]
75+
public void Predict_List_WorksCorrectly()
76+
{
77+
var x = new List<double> { 1, 2, 3 };
78+
var y = new List<double> { 2, 4, 6 };
79+
var lr = new LinearRegression();
80+
lr.Fit(x, y); // y = 2x
81+
var predictions = lr.Predict(new List<double> { 4, 5 });
82+
Assert.That(predictions[0], Is.EqualTo(8.0).Within(1e-6));
83+
Assert.That(predictions[1], Is.EqualTo(10.0).Within(1e-6));
84+
}
85+
}

Algorithms.Tests/Numeric/AutomorphicNumberTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,21 @@ public void TestInvalidAutomorphicNumbers(int number)
5555
[TestCase(1, 100)]
5656
public void TestAutomorphicNumberSequence(int lower, int upper)
5757
{
58-
List<long> automorphicList = new() { 1, 5, 6, 25, 76 };
58+
List<long> automorphicList = [1, 5, 6, 25, 76];
5959
Assert.That(AutomorphicNumber.GetAutomorphicNumbers(lower, upper), Is.EqualTo(automorphicList));
6060
}
6161

6262
[TestCase(8, 12)]
6363
public void TestNoAutomorphicNumberInTheSequence(int lower, int upper)
6464
{
65-
List<long> automorphicList = new();
65+
List<long> automorphicList = [];
6666
Assert.That(AutomorphicNumber.GetAutomorphicNumbers(lower, upper), Is.EqualTo(automorphicList));
6767
}
6868

6969
[TestCase(25, 25)]
7070
public void TestAutomorphicNumberSequenceSameBounds(int lower, int upper)
7171
{
72-
List<long> automorphicList = new() { 25 };
72+
List<long> automorphicList = [25];
7373
Assert.That(AutomorphicNumber.GetAutomorphicNumbers(lower, upper), Is.EqualTo(automorphicList));
7474
}
7575

0 commit comments

Comments
 (0)