|
| 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 | +} |
0 commit comments