MATH-1651: Modified unit test.

The old test was flaky because it assumed a fixed iteration order.
[Thanks to Anant Dahiya for pointing it out.]
The new test indirectly checks how neuron's identifiers are assigned.

Closes #213.
This commit is contained in:
Gilles Sadowski 2022-11-03 12:23:43 +01:00
parent 24e4acc7de
commit 50977290cd
1 changed files with 25 additions and 17 deletions

View File

@ -18,6 +18,7 @@
package org.apache.commons.math4.neuralnet;
import java.util.Collection;
import java.util.Arrays;
import java.util.NoSuchElementException;
import org.junit.Assert;
@ -108,25 +109,16 @@ public class NetworkTest {
}
@Test
public void testIterationOrder() {
public void testIdentifierAssignment() {
final FeatureInitializer[] initArray = {init};
final Network net = new NeuronSquareMesh2D(4, false,
3, true,
SquareNeighbourhood.VON_NEUMANN,
initArray).getNetwork();
final long[] ids = getIdentifiers(new NeuronSquareMesh2D(4, false,
3, true,
SquareNeighbourhood.VON_NEUMANN,
initArray).getNetwork());
// Check that the comparator provides a specific order.
boolean isUnspecifiedOrder = false;
long previousId = Long.MIN_VALUE;
for (Neuron n : net.getNeurons()) {
final long currentId = n.getIdentifier();
if (currentId < previousId) {
isUnspecifiedOrder = true;
break;
}
previousId = currentId;
}
Assert.assertFalse(isUnspecifiedOrder);
Assert.assertEquals(12, ids.length);
Assert.assertEquals(0, ids[0]);
Assert.assertEquals(11, ids[ids.length - 1]);
}
/*
@ -169,4 +161,20 @@ public class NetworkTest {
Assert.assertFalse(netNeighbours.contains(netNeuron1));
Assert.assertTrue(copyNeighbours.contains(copyNeuron1));
}
/**
* @param net Network.
* @return the sorted list identifiers.
*/
private long[] getIdentifiers(Network net) {
final Collection<Neuron> neurons = net.getNeurons();
final long[] identifiers = new long[neurons.size()];
int idx = 0;
for (Neuron n : neurons) {
identifiers[idx++] = n.getIdentifier();
}
Arrays.sort(identifiers);
return identifiers;
}
}