You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2016/09/01 23:12:34 UTC

[2/4] commons-rng git commit: RNG-14

RNG-14

Seed can be passed as "byte[]".


Project: http://git-wip-us.apache.org/repos/asf/commons-rng/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-rng/commit/ce472749
Tree: http://git-wip-us.apache.org/repos/asf/commons-rng/tree/ce472749
Diff: http://git-wip-us.apache.org/repos/asf/commons-rng/diff/ce472749

Branch: refs/heads/master
Commit: ce4727490a97a4d108fd611b62b769ab87320e2d
Parents: c8cc2ee
Author: Gilles <er...@apache.org>
Authored: Fri Sep 2 02:49:10 2016 +0200
Committer: Gilles <er...@apache.org>
Committed: Fri Sep 2 02:49:10 2016 +0200

----------------------------------------------------------------------
 .../commons/rng/internal/ProviderBuilder.java   | 18 ++++++++
 .../rng/internal/util/ByteArray2IntArray.java   | 39 ++++++++++++++++++
 .../rng/internal/util/ByteArray2LongArray.java  | 39 ++++++++++++++++++
 .../rng/ProvidersCommonParametricTest.java      | 12 ++++--
 .../internal/util/ByteArray2IntArrayTest.java   | 43 ++++++++++++++++++++
 .../internal/util/ByteArray2LongArrayTest.java  | 43 ++++++++++++++++++++
 6 files changed, 191 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-rng/blob/ce472749/src/main/java/org/apache/commons/rng/internal/ProviderBuilder.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/rng/internal/ProviderBuilder.java b/src/main/java/org/apache/commons/rng/internal/ProviderBuilder.java
index 0e100fe..93ad3d9 100644
--- a/src/main/java/org/apache/commons/rng/internal/ProviderBuilder.java
+++ b/src/main/java/org/apache/commons/rng/internal/ProviderBuilder.java
@@ -35,6 +35,8 @@ import org.apache.commons.rng.internal.util.IntArray2LongArray;
 import org.apache.commons.rng.internal.util.LongArray2IntArray;
 import org.apache.commons.rng.internal.util.LongArray2Long;
 import org.apache.commons.rng.internal.util.IntArray2Int;
+import org.apache.commons.rng.internal.util.ByteArray2IntArray;
+import org.apache.commons.rng.internal.util.ByteArray2LongArray;
 import org.apache.commons.rng.internal.util.SeedConverter;
 import org.apache.commons.rng.internal.util.SeedConverterComposer;
 import org.apache.commons.rng.internal.source32.JDKRandom;
@@ -81,6 +83,10 @@ public class ProviderBuilder {
     private static final LongArray2IntArray LONG_ARRAY_TO_INT_ARRAY = new LongArray2IntArray();
     /** Seed converter. */
     private static final IntArray2LongArray INT_ARRAY_TO_LONG_ARRAY = new IntArray2LongArray();
+    /** Seed converter. */
+    private static final ByteArray2IntArray BYTE_ARRAY_TO_INT_ARRAY = new ByteArray2IntArray();
+    /** Seed converter. */
+    private static final ByteArray2LongArray BYTE_ARRAY_TO_LONG_ARRAY = new ByteArray2LongArray();
     /** Map to convert "Integer" seeds. */
     private static final Map<Class<?>, SeedConverter<Integer,?>> CONV_INT =
         new HashMap<Class<?>, SeedConverter<Integer,?>>();
@@ -93,6 +99,9 @@ public class ProviderBuilder {
     /** Map to convert "long[]" seeds. */
     private static final Map<Class<?>, SeedConverter<long[],?>> CONV_LONG_ARRAY =
         new HashMap<Class<?>, SeedConverter<long[],?>>();
+    /** Map to convert "byte[]" seeds. */
+    private static final Map<Class<?>, SeedConverter<byte[],?>> CONV_BYTE_ARRAY =
+        new HashMap<Class<?>, SeedConverter<byte[],?>>();
 
     static {
         // Input seed type is "Long".
@@ -122,6 +131,13 @@ public class ProviderBuilder {
         CONV_LONG_ARRAY.put(Long.class, LONG_ARRAY_TO_LONG);
         CONV_LONG_ARRAY.put(int[].class, LONG_ARRAY_TO_INT_ARRAY);
         CONV_LONG_ARRAY.put(long[].class, new NoOpConverter<long[]>());
+
+        // Input seed type is "byte[]".
+        // Key is the implementation's "native" seed type.
+        CONV_BYTE_ARRAY.put(Integer.class, new SeedConverterComposer<byte[],int[],Integer>(BYTE_ARRAY_TO_INT_ARRAY, INT_ARRAY_TO_INT));
+        CONV_BYTE_ARRAY.put(Long.class, new SeedConverterComposer<byte[],long[],Long>(BYTE_ARRAY_TO_LONG_ARRAY, LONG_ARRAY_TO_LONG));
+        CONV_BYTE_ARRAY.put(int[].class, BYTE_ARRAY_TO_INT_ARRAY);
+        CONV_BYTE_ARRAY.put(long[].class, BYTE_ARRAY_TO_LONG_ARRAY);
     }
 
     /**
@@ -195,6 +211,8 @@ public class ProviderBuilder {
                 nativeSeed = CONV_INT_ARRAY.get(source.getSeed()).convert((int[]) seed);
             } else if (seed instanceof long[]) {
                 nativeSeed = CONV_LONG_ARRAY.get(source.getSeed()).convert((long[]) seed);
+            } else if (seed instanceof byte[]) {
+                nativeSeed = CONV_BYTE_ARRAY.get(source.getSeed()).convert((byte[]) seed);
             }
 
             if (nativeSeed == null) {

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/ce472749/src/main/java/org/apache/commons/rng/internal/util/ByteArray2IntArray.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/rng/internal/util/ByteArray2IntArray.java b/src/main/java/org/apache/commons/rng/internal/util/ByteArray2IntArray.java
new file mode 100644
index 0000000..17bb19b
--- /dev/null
+++ b/src/main/java/org/apache/commons/rng/internal/util/ByteArray2IntArray.java
@@ -0,0 +1,39 @@
+/*
+ * 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.commons.rng.internal.util;
+
+import java.util.Arrays;
+
+/**
+ * Creates a {@code int[]} from a {@code byte[]}.
+ *
+ * @since 1.0
+ */
+public class ByteArray2IntArray implements SeedConverter<byte[], int[]> {
+    /** Number of bytes in an {@code int}. */
+    private static final int INT_SIZE = 4;
+
+    /** {@inheritDoc} */
+    @Override
+    public int[] convert(byte[] seed) {
+        final byte[] tmp = seed.length % INT_SIZE == 0 ?
+            seed :
+            Arrays.copyOf(seed, INT_SIZE * ((seed.length + INT_SIZE - 1) / INT_SIZE));
+
+        return NumberFactory.makeIntArray(tmp);
+    }
+}

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/ce472749/src/main/java/org/apache/commons/rng/internal/util/ByteArray2LongArray.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/rng/internal/util/ByteArray2LongArray.java b/src/main/java/org/apache/commons/rng/internal/util/ByteArray2LongArray.java
new file mode 100644
index 0000000..f564c01
--- /dev/null
+++ b/src/main/java/org/apache/commons/rng/internal/util/ByteArray2LongArray.java
@@ -0,0 +1,39 @@
+/*
+ * 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.commons.rng.internal.util;
+
+import java.util.Arrays;
+
+/**
+ * Creates a {@code long[]} from a {@code byte[]}.
+ *
+ * @since 1.0
+ */
+public class ByteArray2LongArray implements SeedConverter<byte[], long[]> {
+    /** Number of bytes in a {@code long}. */
+    private static final int LONG_SIZE = 8;
+
+    /** {@inheritDoc} */
+    @Override
+    public long[] convert(byte[] seed) {
+        final byte[] tmp = seed.length % LONG_SIZE == 0 ?
+            seed :
+            Arrays.copyOf(seed, LONG_SIZE * ((seed.length + LONG_SIZE - 1) / LONG_SIZE));
+
+        return NumberFactory.makeLongArray(tmp);
+    }
+}

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/ce472749/src/test/java/org/apache/commons/rng/ProvidersCommonParametricTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/rng/ProvidersCommonParametricTest.java b/src/test/java/org/apache/commons/rng/ProvidersCommonParametricTest.java
index 6e140ed..acfea7a 100644
--- a/src/test/java/org/apache/commons/rng/ProvidersCommonParametricTest.java
+++ b/src/test/java/org/apache/commons/rng/ProvidersCommonParametricTest.java
@@ -248,8 +248,14 @@ public class ProvidersCommonParametricTest {
         final Long longSeed = -1213141516171819L;
         final int[] intArraySeed = new int[] { 0, 11, -22, 33, -44, 55, -66, 77, -88, 99 };
         final long[] longArraySeed = new long[] { 11111L, -222222L, 3333333L, -44444444L };
+        final byte[] byteArraySeed = new byte[] { -128, -91, -45, -32, -1, 0, 11, 23, 54, 88, 127 };
 
-        final Object[] seeds = new Object[] { null, intSeed, longSeed, intArraySeed, longArraySeed };
+        final Object[] seeds = new Object[] { null,
+                                              intSeed,
+                                              longSeed,
+                                              intArraySeed,
+                                              longArraySeed,
+                                              byteArraySeed };
 
         int nonNativeSeedCount = 0;
         int seedCount = 0;
@@ -263,8 +269,8 @@ public class ProvidersCommonParametricTest {
             RandomSource.create(originalSource, s, originalArgs);
         }
 
-        Assert.assertEquals(5, seedCount);
-        Assert.assertEquals(4, nonNativeSeedCount);
+        Assert.assertEquals(6, seedCount);
+        Assert.assertEquals(5, nonNativeSeedCount);
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/ce472749/src/test/java/org/apache/commons/rng/internal/util/ByteArray2IntArrayTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/rng/internal/util/ByteArray2IntArrayTest.java b/src/test/java/org/apache/commons/rng/internal/util/ByteArray2IntArrayTest.java
new file mode 100644
index 0000000..e1e1e87
--- /dev/null
+++ b/src/test/java/org/apache/commons/rng/internal/util/ByteArray2IntArrayTest.java
@@ -0,0 +1,43 @@
+/*
+ * 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.commons.rng.internal.util;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Tests for the {@link ByteArray2IntArray} converter.
+ */
+public class ByteArray2IntArrayTest {
+    @Test
+    public void testSeedSizeIsMultipleOfIntSize() {
+        final byte[] seed = new byte[128];
+        final int[] out = new ByteArray2IntArray().convert(seed);
+        Assert.assertEquals(32, out.length);
+    }
+
+    @Test
+    public void testSeedSizeIsNotMultipleOfIntSize() {
+        final int len = 16;
+        final ByteArray2IntArray conv = new ByteArray2IntArray();
+        for (int i = 1; i < 4; i++) {
+            final byte[] seed = new byte[len + i];
+            final int[] out = conv.convert(seed);
+            Assert.assertEquals(5, out.length);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/ce472749/src/test/java/org/apache/commons/rng/internal/util/ByteArray2LongArrayTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/rng/internal/util/ByteArray2LongArrayTest.java b/src/test/java/org/apache/commons/rng/internal/util/ByteArray2LongArrayTest.java
new file mode 100644
index 0000000..c42a7b9
--- /dev/null
+++ b/src/test/java/org/apache/commons/rng/internal/util/ByteArray2LongArrayTest.java
@@ -0,0 +1,43 @@
+/*
+ * 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.commons.rng.internal.util;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Tests for the {@link ByteArray2LongArray} converter.
+ */
+public class ByteArray2LongArrayTest {
+    @Test
+    public void testSeedSizeIsMultipleOfLongSize() {
+        final byte[] seed = new byte[128];
+        final long[] out = new ByteArray2LongArray().convert(seed);
+        Assert.assertEquals(16, out.length);
+    }
+
+    @Test
+    public void testSeedSizeIsNotMultipleOfLongSize() {
+        final int len = 16;
+        final ByteArray2LongArray conv = new ByteArray2LongArray();
+        for (int i = 1; i < 8; i++) {
+            final byte[] seed = new byte[len + i];
+            final long[] out = conv.convert(seed);
+            Assert.assertEquals(3, out.length);
+        }
+    }
+}