Skip to content

Commit f6aa804

Browse files
committed
Update the Unit test for KNN
1 parent 403de49 commit f6aa804

File tree

1 file changed

+70
-71
lines changed

1 file changed

+70
-71
lines changed

Algorithms.Tests/MachineLearning/KNearestNeighborsTests.cs

Lines changed: 70 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,86 +2,85 @@
22
using Algorithms.MachineLearning;
33
using System;
44

5-
namespace Algorithms.Tests.MachineLearning
5+
namespace Algorithms.Tests.MachineLearning;
6+
7+
[TestFixture]
8+
public class KNearestNeighborsTests
69
{
7-
[TestFixture]
8-
public class KNearestNeighborsTests
10+
[Test]
11+
public void Constructor_InvalidK_ThrowsException()
912
{
10-
[Test]
11-
public void Constructor_InvalidK_ThrowsException()
12-
{
13-
Assert.Throws<ArgumentOutOfRangeException>(() => new KNearestNeighbors<string>(0));
14-
}
13+
Assert.Throws<ArgumentOutOfRangeException>(() => new KNearestNeighbors<string>(0));
14+
}
1515

16-
[Test]
17-
public void AddSample_NullFeatures_ThrowsException()
18-
{
19-
var knn = new KNearestNeighbors<string>(3);
20-
double[]? features = null;
21-
Assert.Throws<ArgumentNullException>(() => knn.AddSample(features!, "A"));
22-
}
16+
[Test]
17+
public void AddSample_NullFeatures_ThrowsException()
18+
{
19+
var knn = new KNearestNeighbors<string>(3);
20+
double[]? features = null;
21+
Assert.Throws<ArgumentNullException>(() => knn.AddSample(features!, "A"));
22+
}
2323

24-
[Test]
25-
public void Predict_NoTrainingData_ThrowsException()
26-
{
27-
var knn = new KNearestNeighbors<string>(1);
28-
Assert.Throws<InvalidOperationException>(() => knn.Predict(new double[] { 1.0 }));
29-
}
24+
[Test]
25+
public void Predict_NoTrainingData_ThrowsException()
26+
{
27+
var knn = new KNearestNeighbors<string>(1);
28+
Assert.Throws<InvalidOperationException>(() => knn.Predict(new double[] { 1.0 }));
29+
}
3030

31-
[Test]
32-
public void Predict_NullFeatures_ThrowsException()
33-
{
34-
var knn = new KNearestNeighbors<string>(1);
35-
knn.AddSample(new double[] { 1.0 }, "A");
36-
double[]? features = null;
37-
Assert.Throws<ArgumentNullException>(() => knn.Predict(features!));
38-
}
31+
[Test]
32+
public void Predict_NullFeatures_ThrowsException()
33+
{
34+
var knn = new KNearestNeighbors<string>(1);
35+
knn.AddSample(new double[] { 1.0 }, "A");
36+
double[]? features = null;
37+
Assert.Throws<ArgumentNullException>(() => knn.Predict(features!));
38+
}
3939

40-
[Test]
41-
public void EuclideanDistance_DifferentLengths_ThrowsException()
42-
{
43-
Assert.Throws<ArgumentException>(() => KNearestNeighbors<string>.EuclideanDistance(new double[] { 1.0 }, new double[] { 1.0, 2.0 }));
44-
}
40+
[Test]
41+
public void EuclideanDistance_DifferentLengths_ThrowsException()
42+
{
43+
Assert.Throws<ArgumentException>(() => KNearestNeighbors<string>.EuclideanDistance(new double[] { 1.0 }, new double[] { 1.0, 2.0 }));
44+
}
4545

46-
[Test]
47-
public void EuclideanDistance_CorrectResult()
48-
{
49-
double[] a = { 1.0, 2.0 };
50-
double[] b = { 4.0, 6.0 };
51-
double expected = 5.0;
52-
double actual = KNearestNeighbors<string>.EuclideanDistance(a, b);
53-
Assert.That(actual, Is.EqualTo(expected).Within(1e-9));
54-
}
46+
[Test]
47+
public void EuclideanDistance_CorrectResult()
48+
{
49+
double[] a = { 1.0, 2.0 };
50+
double[] b = { 4.0, 6.0 };
51+
double expected = 5.0;
52+
double actual = KNearestNeighbors<string>.EuclideanDistance(a, b);
53+
Assert.That(actual, Is.EqualTo(expected).Within(1e-9));
54+
}
5555

56-
[Test]
57-
public void Predict_SingleNeighbor_CorrectLabel()
58-
{
59-
var knn = new KNearestNeighbors<string>(1);
60-
knn.AddSample(new double[] { 1.0, 2.0 }, "A");
61-
knn.AddSample(new double[] { 3.0, 4.0 }, "B");
62-
var label = knn.Predict(new double[] { 1.1, 2.1 });
63-
Assert.That(label, Is.EqualTo("A"));
64-
}
56+
[Test]
57+
public void Predict_SingleNeighbor_CorrectLabel()
58+
{
59+
var knn = new KNearestNeighbors<string>(1);
60+
knn.AddSample(new double[] { 1.0, 2.0 }, "A");
61+
knn.AddSample(new double[] { 3.0, 4.0 }, "B");
62+
var label = knn.Predict(new double[] { 1.1, 2.1 });
63+
Assert.That(label, Is.EqualTo("A"));
64+
}
6565

66-
[Test]
67-
public void Predict_MajorityVote_CorrectLabel()
68-
{
69-
var knn = new KNearestNeighbors<string>(3);
70-
knn.AddSample(new double[] { 0.0, 0.0 }, "A");
71-
knn.AddSample(new double[] { 0.1, 0.1 }, "A");
72-
knn.AddSample(new double[] { 1.0, 1.0 }, "B");
73-
var label = knn.Predict(new double[] { 0.05, 0.05 });
74-
Assert.That(label, Is.EqualTo("A"));
75-
}
66+
[Test]
67+
public void Predict_MajorityVote_CorrectLabel()
68+
{
69+
var knn = new KNearestNeighbors<string>(3);
70+
knn.AddSample(new double[] { 0.0, 0.0 }, "A");
71+
knn.AddSample(new double[] { 0.1, 0.1 }, "A");
72+
knn.AddSample(new double[] { 1.0, 1.0 }, "B");
73+
var label = knn.Predict(new double[] { 0.05, 0.05 });
74+
Assert.That(label, Is.EqualTo("A"));
75+
}
7676

77-
[Test]
78-
public void Predict_TieBreaker_ReturnsConsistentLabel()
79-
{
80-
var knn = new KNearestNeighbors<string>(2);
81-
knn.AddSample(new double[] { 0.0, 0.0 }, "A");
82-
knn.AddSample(new double[] { 1.0, 1.0 }, "B");
83-
var label = knn.Predict(new double[] { 0.5, 0.5 });
84-
Assert.That(label, Is.EqualTo("B"));
85-
}
77+
[Test]
78+
public void Predict_TieBreaker_ReturnsConsistentLabel()
79+
{
80+
var knn = new KNearestNeighbors<string>(2);
81+
knn.AddSample(new double[] { 0.0, 0.0 }, "A");
82+
knn.AddSample(new double[] { 1.0, 1.0 }, "B");
83+
var label = knn.Predict(new double[] { 0.5, 0.5 });
84+
Assert.That(label, Is.EqualTo("B"));
8685
}
8786
}

0 commit comments

Comments
 (0)