commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: r719995 - /commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/util/MathUtilsTest.java
Date Sun, 23 Nov 2008 15:35:11 GMT
Author: psteitz
Date: Sun Nov 23 07:35:11 2008
New Revision: 719995

URL: http://svn.apache.org/viewvc?rev=719995&view=rev
Log:
Added test case to ensure permuting arrays changes hash.

Modified:
    commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/util/MathUtilsTest.java

Modified: commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/util/MathUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/util/MathUtilsTest.java?rev=719995&r1=719994&r2=719995&view=diff
==============================================================================
--- commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/util/MathUtilsTest.java
(original)
+++ commons/proper/math/branches/MATH_2_0/src/test/org/apache/commons/math/util/MathUtilsTest.java
Sun Nov 23 07:35:11 2008
@@ -19,6 +19,7 @@
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
 
+import org.apache.commons.math.random.RandomDataImpl;
 import org.apache.commons.math.TestUtils;
 
 /**
@@ -331,6 +332,35 @@
         assertFalse(MathUtils.hash(new double[] { 1d }) ==
                     MathUtils.hash(new double[] { 1d, 1d }));
     }
+    
+    /**
+     * Make sure that permuted arrays do not hash to the same value.
+     */
+    public void testPermutedArrayHash() {
+        double[] original = new double[10];
+        double[] permuted = new double[10];
+        RandomDataImpl random = new RandomDataImpl();
+        
+        // Generate 10 distinct random values
+        for (int i = 0; i < 10; i++) {
+            original[i] = random.nextUniform((double)i + 0.5, (double)i + 0.75);
+        }
+        
+        // Generate a random permutation, making sure it is not the identity
+        boolean isIdentity = true;
+        do {
+            int[] permutation = random.nextPermutation(10, 10);
+            for (int i = 0; i < 10; i++) {
+                if (i != permutation[i]) {
+                    isIdentity = false;
+                }
+                permuted[i] = original[permutation[i]];
+            }
+        } while (isIdentity);
+        
+        // Verify that permuted array has different hash
+        assertFalse(MathUtils.hash(original) == MathUtils.hash(permuted));
+    }
 
     public void testIndicatorByte() {
         assertEquals((byte)1, MathUtils.indicator((byte)2));



Mime
View raw message