You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by sr...@apache.org on 2010/09/26 13:54:34 UTC

svn commit: r1001416 - in /mahout/trunk/math/src: main/java/org/apache/mahout/common/RandomUtils.java test/java/org/apache/mahout/common/ test/java/org/apache/mahout/common/RandomUtilsTest.java

Author: srowen
Date: Sun Sep 26 11:54:34 2010
New Revision: 1001416

URL: http://svn.apache.org/viewvc?rev=1001416&view=rev
Log:
Added RandomUtilsTest and fixed embarrassing if practically inconsequential bugs in prime methods

Added:
    mahout/trunk/math/src/test/java/org/apache/mahout/common/
    mahout/trunk/math/src/test/java/org/apache/mahout/common/RandomUtilsTest.java
Modified:
    mahout/trunk/math/src/main/java/org/apache/mahout/common/RandomUtils.java

Modified: mahout/trunk/math/src/main/java/org/apache/mahout/common/RandomUtils.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/common/RandomUtils.java?rev=1001416&r1=1001415&r2=1001416&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/common/RandomUtils.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/common/RandomUtils.java Sun Sep 26 11:54:34 2010
@@ -78,9 +78,11 @@ public final class RandomUtils {
   }
   
   public static long seedBytesToLong(byte[] seed) {
-    return (seed[0] & 0xFFL) << 56 | (seed[1] & 0xFFL) << 48 | (seed[2] & 0xFFL) << 40
-           | (seed[3] & 0xFFL) << 32 | (seed[4] & 0xFFL) << 24 | (seed[5] & 0xFFL) << 16
-           | (seed[6] & 0xFFL) << 8 | seed[7] & 0xFFL;
+    long result = 0L;
+    for (int i = 0; i < 8; i++) {
+      result |= (seed[i] & 0xFFL) << (8 * (7 - i));
+    }
+    return result;
   }
   
   /** @return what {@link Double#hashCode()} would return for the same value */
@@ -109,7 +111,7 @@ public final class RandomUtils {
       throw new IllegalArgumentException();
     }
     if (n <= 3) {
-      return 3;
+      return 5;
     }
     int next = nextPrime(n);
     while (isNotPrime(next + 2)) {
@@ -124,7 +126,7 @@ public final class RandomUtils {
    * </p>
    */
   public static int nextPrime(int n) {
-    if (n < 2) {
+    if (n <= 2) {
       return 2;
     }
     // Make sure the number is odd. Is this too clever?
@@ -139,7 +141,7 @@ public final class RandomUtils {
   /** @return <code>true</code> iff n is not a prime */
   public static boolean isNotPrime(int n) {
     if (n < 2 || (n & 0x1) == 0) { // < 2 or even
-      return true;
+      return n != 2;
     }
     int max = 1 + (int) Math.sqrt(n);
     for (int d = 3; d <= max; d += 2) {

Added: mahout/trunk/math/src/test/java/org/apache/mahout/common/RandomUtilsTest.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/test/java/org/apache/mahout/common/RandomUtilsTest.java?rev=1001416&view=auto
==============================================================================
--- mahout/trunk/math/src/test/java/org/apache/mahout/common/RandomUtilsTest.java (added)
+++ mahout/trunk/math/src/test/java/org/apache/mahout/common/RandomUtilsTest.java Sun Sep 26 11:54:34 2010
@@ -0,0 +1,113 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.mahout.common;
+
+import org.apache.mahout.math.MahoutTestCase;
+import org.junit.Test;
+
+import java.util.Random;
+
+/**
+ * Tests {@linkRandomUtils}.
+ */
+public final class RandomUtilsTest extends MahoutTestCase {
+
+  @Test
+  public void testIsNotPrime() {
+    assertTrue(RandomUtils.isNotPrime(Integer.MIN_VALUE));
+    assertTrue(RandomUtils.isNotPrime(-1));
+    assertTrue(RandomUtils.isNotPrime(0));
+    assertTrue(RandomUtils.isNotPrime(1));
+    assertTrue(!RandomUtils.isNotPrime(2));
+    assertTrue(!RandomUtils.isNotPrime(3));
+    assertTrue(RandomUtils.isNotPrime(4));
+    assertTrue(!RandomUtils.isNotPrime(5));
+    assertTrue(RandomUtils.isNotPrime(Integer.MAX_VALUE - 1));   
+    assertTrue(!RandomUtils.isNotPrime(Integer.MAX_VALUE)); // 2^31 - 1
+  }
+
+  @Test
+  public void testNextPrime() {
+    assertEquals(2, RandomUtils.nextPrime(-1));
+    assertEquals(2, RandomUtils.nextPrime(1));
+    assertEquals(2, RandomUtils.nextPrime(2));
+    assertEquals(3, RandomUtils.nextPrime(3));
+    assertEquals(5, RandomUtils.nextPrime(4));
+    assertEquals(5, RandomUtils.nextPrime(5));
+    assertEquals(7, RandomUtils.nextPrime(6));    
+    assertEquals(Integer.MAX_VALUE, RandomUtils.nextPrime(Integer.MAX_VALUE - 1));
+  }
+
+  @Test
+  public void testNextTwinPrime() {
+    assertEquals(5, RandomUtils.nextTwinPrime(-1));
+    assertEquals(5, RandomUtils.nextTwinPrime(1));
+    assertEquals(5, RandomUtils.nextTwinPrime(2));
+    assertEquals(5, RandomUtils.nextTwinPrime(3));
+    assertEquals(7, RandomUtils.nextTwinPrime(4));
+    assertEquals(7, RandomUtils.nextTwinPrime(5));
+    assertEquals(13, RandomUtils.nextTwinPrime(6));
+    assertEquals(RandomUtils.MAX_INT_SMALLER_TWIN_PRIME + 2,
+                 RandomUtils.nextTwinPrime(RandomUtils.MAX_INT_SMALLER_TWIN_PRIME));
+    try {
+      RandomUtils.nextTwinPrime(RandomUtils.MAX_INT_SMALLER_TWIN_PRIME + 1);
+      fail();
+    } catch (IllegalArgumentException iae) {
+      // good
+    }
+  }
+
+  @Test
+  public void testLongToSeed() {
+    Random r = RandomUtils.getRandom();
+    for (int i = 0; i < 10000; i++) {
+      long l = r.nextLong();
+      byte[] bytes = RandomUtils.longSeedtoBytes(l);
+      long back = RandomUtils.seedBytesToLong(bytes);
+      assertEquals(l, back);
+    }
+  }
+
+  @Test
+  public void testHashDouble() {
+    assertEquals(0, RandomUtils.hashDouble(0.0));
+    assertEquals(1072693248, RandomUtils.hashDouble(1.0));
+    assertEquals(2146959360, RandomUtils.hashDouble(Double.NaN));
+    assertEquals(2146435072, RandomUtils.hashDouble(Double.POSITIVE_INFINITY));
+  }
+
+  @Test
+  public void testHashFloat() {
+    assertEquals(0, RandomUtils.hashFloat(0.0f));
+    assertEquals(1065353216, RandomUtils.hashFloat(1.0f));
+    assertEquals(2143289344, RandomUtils.hashFloat(Float.NaN));
+    assertEquals(2139095040, RandomUtils.hashFloat(Float.POSITIVE_INFINITY));
+  }
+
+  @Test
+  public void testHashLong() {
+    assertEquals(0, RandomUtils.hashLong(-1L));
+    assertEquals(0, RandomUtils.hashLong(0L));
+    assertEquals(1, RandomUtils.hashLong(1L));
+    assertEquals(Integer.MAX_VALUE, RandomUtils.hashLong(Integer.MAX_VALUE));
+    assertEquals(Integer.MIN_VALUE, RandomUtils.hashLong((long) Integer.MAX_VALUE + 1L));
+    assertEquals(Integer.MIN_VALUE, RandomUtils.hashLong(Long.MAX_VALUE));
+    assertEquals(Integer.MIN_VALUE, RandomUtils.hashLong(Long.MIN_VALUE));
+  }
+
+}