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.
diff --git a/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/NetworkTest.java b/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/NetworkTest.java
index 5342124..f959c2a 100644
--- a/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/NetworkTest.java
+++ b/commons-math-neuralnet/src/test/java/org/apache/commons/math4/neuralnet/NetworkTest.java
@@ -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 @@
}
@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 @@
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;
+ }
}