Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 76C6C200BC1 for ; Wed, 2 Nov 2016 00:03:20 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 75435160B07; Tue, 1 Nov 2016 23:03:20 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id C5977160B0A for ; Wed, 2 Nov 2016 00:03:18 +0100 (CET) Received: (qmail 85291 invoked by uid 500); 1 Nov 2016 23:03:17 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 84620 invoked by uid 99); 1 Nov 2016 23:03:16 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 01 Nov 2016 23:03:16 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id A0F9DE93E5; Tue, 1 Nov 2016 23:03:16 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: erans@apache.org To: commits@commons.apache.org Date: Tue, 01 Nov 2016 23:03:20 -0000 Message-Id: <40486ad4a2654d4fbbecd110c0913259@git.apache.org> In-Reply-To: <52175208aee14ac29183c49ce5b792fc@git.apache.org> References: <52175208aee14ac29183c49ce5b792fc@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [5/8] commons-rng git commit: New maven modules. archived-at: Tue, 01 Nov 2016 23:03:20 -0000 http://git-wip-us.apache.org/repos/asf/commons-rng/blob/651c21da/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/ProvidersCommonParametricTest.java ---------------------------------------------------------------------- diff --git a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/ProvidersCommonParametricTest.java b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/ProvidersCommonParametricTest.java new file mode 100644 index 0000000..1746cb1 --- /dev/null +++ b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/ProvidersCommonParametricTest.java @@ -0,0 +1,352 @@ +/* + * 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.simple; + +import java.util.Arrays; +import java.util.List; +import java.util.ArrayList; +import java.util.concurrent.Callable; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.io.ObjectInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ByteArrayInputStream; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.Assume; +import org.junit.Ignore; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import org.apache.commons.rng.UniformRandomProvider; +import org.apache.commons.rng.RandomProviderState; +import org.apache.commons.rng.RestorableUniformRandomProvider; +import org.apache.commons.rng.internal.RandomProviderDefaultState; + +/** + * Tests which all generators must pass. + */ +@RunWith(value=Parameterized.class) +public class ProvidersCommonParametricTest { + /** RNG under test. */ + private final UniformRandomProvider generator; + /** RNG specifier. */ + private final RandomSource originalSource; + /** Seed (constructor's first parameter). */ + private final Object originalSeed; + /** Constructor's additional parameters. */ + private final Object[] originalArgs; + + /** + * Initializes generator instance. + * + * @param rng RNG to be tested. + */ + public ProvidersCommonParametricTest(ProvidersList.Data data) { + originalSource = data.getSource(); + originalSeed = data.getSeed(); + originalArgs = data.getArgs(); + generator = RandomSource.create(originalSource, originalSeed, originalArgs); + } + + @Parameters(name = "{index}: data={0}") + public static Iterable getList() { + return ProvidersList.list(); + } + + // Seeding tests. + + @Test(expected=UnsupportedOperationException.class) + public void testUnsupportedSeedType() { + final byte seed = 123; + RandomSource.create(originalSource, seed, originalArgs); + } + + @Test + public void testAllSeedTypes() { + final Integer intSeed = -12131415; + 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, + byteArraySeed }; + + int nonNativeSeedCount = 0; + int seedCount = 0; + for (Object s : seeds) { + ++seedCount; + if (!(originalSource.isNativeSeed(s))) { + ++nonNativeSeedCount; + } + + Assert.assertNotEquals(intSeed, originalSeed); + RandomSource.create(originalSource, s, originalArgs); + } + + Assert.assertEquals(6, seedCount); + Assert.assertEquals(5, nonNativeSeedCount); + } + + @Test + public void testEmptyIntArraySeed() { + final int[] empty = new int[0]; + Assume.assumeTrue(originalSource.isNativeSeed(empty)); + + // Exercise the default seeding procedure. + final UniformRandomProvider rng = RandomSource.create(originalSource, empty, originalArgs); + checkNextIntegerInRange(rng, 10, 10000); + } + + @Test + public void testEmptyLongArraySeed() { + final long[] empty = new long[0]; + Assume.assumeTrue(originalSource.isNativeSeed(empty)); + + // Exercise the default seeding procedure. + final UniformRandomProvider rng = RandomSource.create(originalSource, empty, originalArgs); + checkNextIntegerInRange(rng, 10, 10000); + } + + @Ignore@Test + public void testZeroIntArraySeed() { + // Exercise capacity to escape all "zero" state. + final int[] zero = new int[2000]; // Large enough to fill the entire state with zeroes. + final UniformRandomProvider rng = RandomSource.create(originalSource, zero, originalArgs); + checkNextIntegerInRange(rng, 10, 10000); + } + + @Ignore@Test + public void testZeroLongArraySeed() { + // Exercise capacity to escape all "zero" state. + final long[] zero = new long[2000]; // Large enough to fill the entire state with zeroes. + final UniformRandomProvider rng = RandomSource.create(originalSource, zero, originalArgs); + checkNextIntegerInRange(rng, 10, 10000); + } + + // State save and restore tests. + + @Test + public void testUnrestorable() { + // Create two generators of the same type as the one being tested. + final UniformRandomProvider rng1 = RandomSource.create(originalSource, originalSeed, originalArgs); + final UniformRandomProvider rng2 = RandomSource.unrestorable(RandomSource.create(originalSource, originalSeed, originalArgs)); + + // Ensure that they generate the same values. + RandomAssert.assertProduceSameSequence(rng1, rng2); + + // Cast must work. + final RestorableUniformRandomProvider restorable = (RestorableUniformRandomProvider) rng1; + // Cast must fail. + try { + final RestorableUniformRandomProvider dummy = (RestorableUniformRandomProvider) rng2; + Assert.fail("Cast should have failed"); + } catch (ClassCastException e) { + // Expected. + } + } + + @Test + public void testSerializingState() + throws IOException, + ClassNotFoundException { + // Large "n" is not necessary here as we only test the serialization. + final int n = 100; + + // Cast is OK: all instances created by this library inherit from "BaseProvider". + final RestorableUniformRandomProvider restorable = (RestorableUniformRandomProvider) generator; + + // Save. + final RandomProviderState stateOrig = restorable.saveState(); + // Serialize. + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(bos); + oos.writeObject(((RandomProviderDefaultState) stateOrig).getState()); + + // Store some values. + final List listOrig = makeList(n); + + // Discard a few more. + final List listDiscard = makeList(n); + Assert.assertTrue(listDiscard.size() != 0); + Assert.assertFalse(listOrig.equals(listDiscard)); + + // Retrieve from serialized stream. + ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); + ObjectInputStream ois = new ObjectInputStream(bis); + final RandomProviderState stateNew = new RandomProviderDefaultState((byte[]) ois.readObject()); + + Assert.assertTrue(stateOrig != stateNew); + + // Reset. + restorable.restoreState(stateNew); + + // Replay. + final List listReplay = makeList(n); + Assert.assertFalse(listOrig == listReplay); + + // Check that the serialized data recreated the orginal state. + Assert.assertTrue(listOrig.equals(listReplay)); + } + + ///// Support methods below. + + + // The methods + // * makeList + // * checkNextIntegerInRange + // * checkNextInRange" + // have been copied from "src/test" in module "commons-rng-core". + // TODO: check whether it is possible to have a single implementation. + + /** + * Populates a list with random numbers. + * + * @param n Loop counter. + * @return a list containing {@code 11 * n} random numbers. + */ + private List makeList(int n) { + final List list = new ArrayList(); + + for (int i = 0; i < n; i++) { + // Append 11 values. + list.add(generator.nextInt()); + list.add(generator.nextInt(21)); + list.add(generator.nextInt(436)); + list.add(generator.nextLong()); + list.add(generator.nextLong(157894)); + list.add(generator.nextLong(5745833)); + list.add(generator.nextFloat()); + list.add(generator.nextFloat()); + list.add(generator.nextDouble()); + list.add(generator.nextDouble()); + list.add(generator.nextDouble()); + } + + return list; + } + + /** + * Tests uniformity of the distribution produced by {@code nextInt(int)}. + * + * @param rng Generator. + * @param max Upper bound. + * @param sampleSize Number of random values generated. + */ + private void checkNextIntegerInRange(final UniformRandomProvider rng, + final int max, + int sampleSize) { + final Callable nextMethod = new Callable() { + @Override + public Integer call() throws Exception { + return rng.nextInt(max); + } + }; + + checkNextInRange(max, sampleSize, nextMethod); + } + + /** + * Tests uniformity of the distribution produced by the given + * {@code nextMethod}. + * It performs a chi-square test of homogeneity of the observed + * distribution with the expected uniform distribution. + * Tests are performed at the 1% level and an average failure rate + * higher than 2% (i.e. more than 20 null hypothesis rejections) + * causes the test case to fail. + * + * @param max Upper bound. + * @param nextMethod method to call. + * @param sampleSize Number of random values generated. + */ + private void checkNextInRange(T max, + int sampleSize, + Callable nextMethod) { + final int numTests = 500; + + // Do not change (statistical test assumes that dof = 10). + final int numBins = 10; + + // Set up bins. + final long n = max.longValue(); + final long[] binUpperBounds = new long[numBins]; + final double step = n / (double) numBins; + for (int k = 0; k < numBins; k++) { + binUpperBounds[k] = (long) ((k + 1) * step); + } + + // Run the tests. + int numFailures = 0; + + final double[] expected = new double[numBins]; + long previousUpperBound = 0; + for (int k = 0; k < numBins; k++) { + final long range = binUpperBounds[k] - previousUpperBound; + expected[k] = sampleSize * (range / (double) n); + previousUpperBound = binUpperBounds[k]; + } + + final int[] observed = new int[numBins]; + // Chi-square critical value with 10 degrees of freedom + // and 1% significance level. + final double chi2CriticalValue = 23.209; + + try { + for (int i = 0; i < numTests; i++) { + Arrays.fill(observed, 0); + for (int j = 0; j < sampleSize; j++) { + final long value = nextMethod.call().longValue(); + Assert.assertTrue("Range", (value >= 0) && (value < n)); + + for (int k = 0; k < numBins; k++) { + if (value < binUpperBounds[k]) { + ++observed[k]; + break; + } + } + } + + // Compute chi-square. + double chi2 = 0; + for (int k = 0; k < numBins; k++) { + final double diff = observed[k] - expected[k]; + chi2 += diff * diff / expected[k]; + } + + // Statistics check. + if (chi2 > chi2CriticalValue) { + ++numFailures; + } + } + } catch (Exception e) { + // Should never happen. + throw new RuntimeException("Unexpected", e); + } + + if ((double) numFailures / (double) numTests > 0.02) { + Assert.fail(generator + ": Too many failures for n = " + n + + " (" + numFailures + " out of " + numTests + " tests failed)"); + } + } +} http://git-wip-us.apache.org/repos/asf/commons-rng/blob/651c21da/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/ProvidersList.java ---------------------------------------------------------------------- diff --git a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/ProvidersList.java b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/ProvidersList.java new file mode 100644 index 0000000..2271cf8 --- /dev/null +++ b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/ProvidersList.java @@ -0,0 +1,159 @@ +/* + * 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.simple; + +import java.util.Arrays; +import java.util.List; +import java.util.ArrayList; +import java.util.Collections; + +/** + * The purpose of this class is to provide the list of all generators + * implemented in the library. + * The list must be updated with each new RNG implementation. + * + * @see #list() + * @see #list32() + * @see #list64() + */ +public class ProvidersList { + /** List of all RNGs implemented in the library. */ + private static final List LIST = new ArrayList(); + /** List of 32-bits based RNGs. */ + private static final List LIST32 = new ArrayList(); + /** List of 64-bits based RNGs. */ + private static final List LIST64 = new ArrayList(); + + static { + try { + // "int"-based RNGs. + add(LIST32, RandomSource.JDK, -122333444455555L); + add(LIST32, RandomSource.MT, new int[] { -123, -234, -345 }); + add(LIST32, RandomSource.WELL_512_A, new int[] { -23, -34, -45 }); + add(LIST32, RandomSource.WELL_1024_A, new int[] { -1234, -2345, -3456 }); + add(LIST32, RandomSource.WELL_19937_A, new int[] { -2123, -3234, -4345 }); + add(LIST32, RandomSource.WELL_19937_C, new int[] { -123, -234, -345, -456 }); + add(LIST32, RandomSource.WELL_44497_A, new int[] { -12345, -23456, -34567 }); + add(LIST32, RandomSource.WELL_44497_B, new int[] { 123, 234, 345 }); + add(LIST32, RandomSource.ISAAC, new int[] { 123, -234, 345, -456 }); + add(LIST32, RandomSource.MWC_256, new int[] { 12, -1234, -3456, 45678 }); + add(LIST32, RandomSource.KISS, new int[] { 12, 1234, 23456, 345678 }); + // ... add more here. + + // "long"-based RNGs. + add(LIST64, RandomSource.SPLIT_MIX_64, -988777666655555L); + add(LIST64, RandomSource.XOR_SHIFT_1024_S, new long[] { 123456L, 234567L, -345678L }); + add(LIST64, RandomSource.TWO_CMRES, 55443322); + add(LIST64, RandomSource.TWO_CMRES_SELECT, -987654321, 5, 8); + add(LIST64, RandomSource.MT_64, new long[] { 1234567L, 2345678L, -3456789L }); + // ... add more here. + + // Do not modify the remaining statements. + // Complete list. + LIST.addAll(LIST32); + LIST.addAll(LIST64); + } catch (Exception e) { + System.err.println("Unexpected exception while creating the list of generators: " + e); + e.printStackTrace(System.err); + throw new RuntimeException(e); + } + } + + /** + * Class contains only static methods. + */ + private ProvidersList() {} + + /** + * Helper to statisfy Junit requirement that each parameter set contains + * the same number of objects. + */ + private static void add(List list, + RandomSource source, + Object ... data) { + final RandomSource rng = source; + final Object seed = data.length > 0 ? data[0] : null; + final Object[] args = data.length > 1 ? Arrays.copyOfRange(data, 1, data.length) : null; + + list.add(new Data[] { new Data(rng, seed, args) }); + } + + /** + * Subclasses that are "parametric" tests can forward the call to + * the "@Parameters"-annotated method to this method. + * + * @return the list of all generators. + */ + public static Iterable list() { + return Collections.unmodifiableList(LIST); + } + + /** + * Subclasses that are "parametric" tests can forward the call to + * the "@Parameters"-annotated method to this method. + * + * @return the list of 32-bits based generators. + */ + public static Iterable list32() { + return Collections.unmodifiableList(LIST32); + } + + /** + * Subclasses that are "parametric" tests can forward the call to + * the "@Parameters"-annotated method to this method. + * + * @return the list of 64-bits based generators. + */ + public static Iterable list64() { + return Collections.unmodifiableList(LIST64); + } + + /** + * Helper. + * Better not to mix Junit assumptions of the usage of "Object[]". + */ + public static class Data { + private final RandomSource source; + private final Object seed; + private final Object[] args; + + public Data(RandomSource source, + Object seed, + Object[] args) { + this.source = source; + this.seed = seed; + this.args = args; + } + + public RandomSource getSource() { + return source; + } + + public Object getSeed() { + return seed; + } + + public Object[] getArgs() { + return args == null ? null : Arrays.copyOf(args, args.length); + } + + @Override + public String toString() { + return source.toString() + " seed=" + seed + " args=" + Arrays.toString(args); + } + } +} http://git-wip-us.apache.org/repos/asf/commons-rng/blob/651c21da/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/RandomAssert.java ---------------------------------------------------------------------- diff --git a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/RandomAssert.java b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/RandomAssert.java new file mode 100644 index 0000000..f96ac87 --- /dev/null +++ b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/RandomAssert.java @@ -0,0 +1,81 @@ +/* + * 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.simple; + +import java.util.Arrays; +import org.junit.Assert; + +import org.apache.commons.rng.UniformRandomProvider; + +public class RandomAssert { + /** + * Exercise all methods from the UniformRandomProvider interface, and + * ensure that the two generators produce the same sequence. + * + * @param rng1 RNG. + * @param rng2 RNG. + */ + public static void assertProduceSameSequence(UniformRandomProvider rng1, + UniformRandomProvider rng2) { + for (int i = 0; i < 54; i++) { + Assert.assertTrue(rng1.nextBoolean() == rng2.nextBoolean()); + } + for (int i = 0; i < 23; i++) { + Assert.assertEquals(rng1.nextInt(), rng2.nextInt()); + } + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 5; j++) { + final int max = 107 * i + 374 * j + 11; + Assert.assertEquals(rng1.nextInt(max), rng2.nextInt(max)); + } + } + for (int i = 0; i < 23; i++) { + Assert.assertEquals(rng1.nextLong(), rng2.nextLong()); + } + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 5; j++) { + final long max = (Long.MAX_VALUE << 2) + 107 * i + 374 * j + 11; + Assert.assertEquals(rng1.nextLong(max), rng2.nextLong(max)); + } + } + for (int i = 0; i < 103; i++) { + Assert.assertEquals(rng1.nextFloat(), rng2.nextFloat(), 0); + } + for (int i = 0; i < 79; i++) { + Assert.assertEquals(rng1.nextDouble(), rng2.nextDouble(), 0); + } + + final int size = 345; + final byte[] a1 = new byte[size]; + final byte[] a2 = new byte[size]; + + for (int i = 0; i < 3; i++) { + rng1.nextBytes(a1); + rng2.nextBytes(a2); + Assert.assertTrue(Arrays.equals(a1, a2)); + } + + for (int i = 0; i < 5; i++) { + final int offset = 200 + i; + final int n = 23 + i; + rng1.nextBytes(a1, offset, n); + rng2.nextBytes(a2, offset, n); + Assert.assertTrue(Arrays.equals(a1, a2)); + } + } +} http://git-wip-us.apache.org/repos/asf/commons-rng/blob/651c21da/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/RandomSourceTest.java ---------------------------------------------------------------------- diff --git a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/RandomSourceTest.java b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/RandomSourceTest.java new file mode 100644 index 0000000..b478f98 --- /dev/null +++ b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/RandomSourceTest.java @@ -0,0 +1,69 @@ +/* + * 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.simple; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests for {@link RandomSource}. + */ +public class RandomSourceTest { + @Test + public void testCreateInt() { + final int n = 4; + for (int i = 0; i < n; i++) { + // Can fail, but unlikely given the range. + Assert.assertNotEquals(RandomSource.createInt(), + RandomSource.createInt()); + } + } + + @Test + public void testCreateLong() { + final int n = 6; + for (int i = 0; i < n; i++) { + // Can fail, but unlikely given the range. + Assert.assertNotEquals(RandomSource.createLong(), + RandomSource.createLong()); + } + } + + @Test + public void testCreateIntArray() { + final int n = 13; + final int[] seed = RandomSource.createIntArray(n); + Assert.assertEquals(n, seed.length); + + for (int i = 1; i < n; i++) { + // Can fail, but unlikely given the range. + Assert.assertNotEquals(seed[i - 1], seed[i]); + } + } + + @Test + public void testCreateLongArray() { + final int n = 9; + final long[] seed = RandomSource.createLongArray(n); + Assert.assertEquals(n, seed.length); + + for (int i = 1; i < n; i++) { + // Can fail, but unlikely given the range. + Assert.assertNotEquals(seed[i - 1], seed[i]); + } + } +} http://git-wip-us.apache.org/repos/asf/commons-rng/blob/651c21da/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/ByteArray2IntArrayTest.java ---------------------------------------------------------------------- diff --git a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/ByteArray2IntArrayTest.java b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/ByteArray2IntArrayTest.java new file mode 100644 index 0000000..dddc7a4 --- /dev/null +++ b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/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.simple.internal; + +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/651c21da/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/ByteArray2LongArrayTest.java ---------------------------------------------------------------------- diff --git a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/ByteArray2LongArrayTest.java b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/ByteArray2LongArrayTest.java new file mode 100644 index 0000000..1c9a424 --- /dev/null +++ b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/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.simple.internal; + +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); + } + } +} http://git-wip-us.apache.org/repos/asf/commons-rng/blob/651c21da/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/IntArray2LongArrayTest.java ---------------------------------------------------------------------- diff --git a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/IntArray2LongArrayTest.java b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/IntArray2LongArrayTest.java new file mode 100644 index 0000000..e665ca2 --- /dev/null +++ b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/IntArray2LongArrayTest.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.simple.internal; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests for the {@link IntArray2LongArray} converter. + */ +public class IntArray2LongArrayTest { + @Test + public void testSeedSizeIsMultipleOfLongSize() { + final int[] seed = new int[12]; + final long[] out = new IntArray2LongArray().convert(seed); + Assert.assertEquals(6, out.length); + } + + @Test + public void testSeedSizeIsNotMultipleOfLongSize() { + final int[] seed = new int[13]; + final long[] out = new IntArray2LongArray().convert(seed); + Assert.assertEquals(7, out.length); + } +} http://git-wip-us.apache.org/repos/asf/commons-rng/blob/651c21da/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/SeedFactoryTest.java ---------------------------------------------------------------------- diff --git a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/SeedFactoryTest.java b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/SeedFactoryTest.java new file mode 100644 index 0000000..d8f4f2c --- /dev/null +++ b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/SeedFactoryTest.java @@ -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.commons.rng.simple.internal; + +import java.util.Map; +import java.util.HashMap; +import org.junit.Assert; +import org.junit.Test; + +import org.apache.commons.rng.internal.util.NumberFactory; + +/** + * Tests for {@link SeedFactory}. + */ +public class SeedFactoryTest { + @Test + public void testCreateLong() { + final Map values = new HashMap(); + + final int n = 100000; + for (int i = 0; i < n; i++) { + final long v = SeedFactory.createLong(); + + Integer count = values.get(v); + if (count == null) { + count = 0; + } + values.put(v, count + 1); + } + + // Check that all seeds are different. + assertDifferentValues(values); + } + + @Test + public void testCreateLongArray() { + final Map values = new HashMap(); + + final int n = 100000; + final long[] array = SeedFactory.createLongArray(n); + Assert.assertEquals(n, array.length); + + for (long v : array) { + Integer count = values.get(v); + if (count == null) { + count = 0; + } + values.put(v, count + 1); + } + + // Check that all seeds are different. + assertDifferentValues(values); + } + + @Test + public void testCreateIntArray() { + final Map values = new HashMap(); + + for (int i = 0; i < 50000; i++) { + final int[] a = SeedFactory.createIntArray(2); + final long v = NumberFactory.makeLong(a[0], a[1]); + Integer count = values.get(v); + if (count == null) { + count = 0; + } + values.put(v, count + 1); + } + + // Check that all pairs in are different. + assertDifferentValues(values); + } + + /** + * Asserts that all the keys in given {@code map} have their + * value equal to 1. + * + * @param map Map to counts. + */ + private static void assertDifferentValues(Map map) { + final StringBuilder sb = new StringBuilder(); + + int duplicates = 0; + for (Map.Entry entry : map.entrySet()) { + final int count = entry.getValue(); + if (count <= 0) { + throw new IllegalStateException(); + } + + if (count > 1) { + duplicates += count - 1; + sb.append(entry.getKey() + ": " + count + "\n"); + } + } + + if (duplicates > 0) { + Assert.fail(duplicates + " duplicates\n" + sb); + } + } +} http://git-wip-us.apache.org/repos/asf/commons-rng/blob/651c21da/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index bb56df2..371cb6d 100644 --- a/pom.xml +++ b/pom.xml @@ -70,20 +70,6 @@ - org.openjdk.jmh - jmh-core - ${jmh.version} - test - - - - org.openjdk.jmh - jmh-generator-annprocess - ${jmh.version} - test - - - junit junit 4.12 @@ -138,10 +124,6 @@ {0,date,yyyy-MM-dd HH:mm:ssZ} - - - 1.13 - benchmarks @@ -607,61 +589,15 @@ - - benchmark - - true - org.apache - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.1 - - ${maven.compiler.target} - ${maven.compiler.target} - ${maven.compiler.target} - - - - org.codehaus.mojo - exec-maven-plugin - - - benchmark - test - - exec - - - test - java - - -classpath - - org.openjdk.jmh.Main - -rf - json - -rff - target/jmh-result.${benchmark}.json - ${benchmark} - - - - - - - - commons-rng-build-tools commons-rng-client-api commons-rng-core + commons-rng-simple + commons-rng-jmh + commons-rng-examples - \ No newline at end of file +