Skip to content

Commit 8ade55d

Browse files
committed
Fix PMD static import violation and cover Perceptron gaps
Reduce static imports to PMD's limit, assert the learned bias and weights, and cover the length-mismatch, null-vector, and divergence paths.
1 parent 9cdd051 commit 8ade55d

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

‎src/test/java/com/thealgorithms/machinelearning/PerceptronTest.java‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
44
import static org.junit.jupiter.api.Assertions.assertEquals;
5-
import static org.junit.jupiter.api.Assertions.assertFalse;
65
import static org.junit.jupiter.api.Assertions.assertThrows;
76
import static org.junit.jupiter.api.Assertions.assertTrue;
87

8+
import org.junit.jupiter.api.Assertions;
99
import org.junit.jupiter.api.Test;
1010

1111
class PerceptronTest {
@@ -23,6 +23,17 @@ void learnsAndFunction() {
2323
assertTrue(perceptron.getEpochsRun() <= 20);
2424
}
2525

26+
@Test
27+
void learnsExpectedWeightsAndBias() {
28+
Perceptron perceptron = new Perceptron(1.0, 20);
29+
perceptron.fit(new double[][] {{0}, {1}}, new int[] {0, 1});
30+
31+
assertArrayEquals(new double[] {1.0}, perceptron.getWeights());
32+
assertEquals(-1.0, perceptron.getBias());
33+
assertTrue(perceptron.hasConverged());
34+
assertEquals(3, perceptron.getEpochsRun());
35+
}
36+
2637
@Test
2738
void predictsUnseenSamples() {
2839
double[][] features = {{-2, -1}, {-1, -2}, {1, 2}, {2, 1}};
@@ -65,7 +76,7 @@ void nonSeparableDataStopsAtEpochLimitWithoutConverging() {
6576
Perceptron perceptron = new Perceptron(1.0, 8);
6677
perceptron.fit(features, labels);
6778

68-
assertFalse(perceptron.hasConverged());
79+
Assertions.assertFalse(perceptron.hasConverged());
6980
assertEquals(8, perceptron.getEpochsRun());
7081
}
7182

@@ -119,8 +130,10 @@ void invalidTrainingDataThrows() {
119130
assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {{0}}, null));
120131
assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {}, new int[] {}));
121132
assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {{0}}, new int[] {}));
133+
assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {{0}, {1}}, new int[] {0}));
122134
assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {{0}, {1, 2}}, new int[] {0, 1}));
123135
assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {null}, new int[] {0}));
136+
assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {{0}, null}, new int[] {0, 1}));
124137
assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {{}}, new int[] {0}));
125138
assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {{0}}, new int[] {2}));
126139
assertThrows(IllegalArgumentException.class, () -> perceptron.fit(new double[][] {{Double.NaN}}, new int[] {0}));
@@ -138,4 +151,13 @@ void invalidPredictionDataThrows() {
138151
assertThrows(IllegalArgumentException.class, () -> perceptron.predict((double[][]) null));
139152
assertThrows(IllegalArgumentException.class, () -> perceptron.predict(new double[][] {{0, 0}, null}));
140153
}
154+
155+
@Test
156+
void divergingTrainingIsRejectedAndLeavesClassifierUnfitted() {
157+
Perceptron perceptron = new Perceptron(1.0e300, 5);
158+
159+
assertThrows(ArithmeticException.class, () -> perceptron.fit(new double[][] {{1.0e300}, {1.0e300}}, new int[] {0, 1}));
160+
assertThrows(IllegalStateException.class, perceptron::getWeights);
161+
assertThrows(IllegalStateException.class, perceptron::getBias);
162+
}
141163
}

0 commit comments

Comments
 (0)