This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git


The following commit(s) were added to refs/heads/master by this push:
     new 50977290c MATH-1651: Modified unit test.
50977290c is described below

commit 50977290cdda79896ccb6304067341486fe104d2
Author: Gilles Sadowski <gillese...@gmail.com>
AuthorDate: Thu Nov 3 12:23:43 2022 +0100

    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.
---
 .../commons/math4/neuralnet/NetworkTest.java       | 44 +++++++++++++---------
 1 file changed, 26 insertions(+), 18 deletions(-)

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 53421242e..f959c2a52 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 @@ 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();
-
-        // 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);
+        final long[] ids = getIdentifiers(new NeuronSquareMesh2D(4, false,
+                                                                3, true,
+                                                                
SquareNeighbourhood.VON_NEUMANN,
+                                                                
initArray).getNetwork());
+
+        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;
+    }
 }

Reply via email to