You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2019/06/06 08:00:18 UTC
[commons-rng] 05/11: RNG-75: Add coverage tests.
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-rng.git
commit de68ad80d3f28228989066bcf4c71904c22813ce
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Sat Jun 1 21:27:36 2019 +0100
RNG-75: Add coverage tests.
---
.../rng/simple/internal/ProviderBuilder.java | 2 +-
.../rng/simple/internal/Long2IntArrayTest.java | 33 ++++++
.../rng/simple/internal/Long2LongArrayTest.java | 33 ++++++
.../internal/NativeSeedTypeParametricTest.java | 124 +++++++++++++++++++++
.../rng/simple/internal/NativeSeedTypeTest.java | 36 ++++++
.../rng/simple/internal/NoOpConverterTest.java | 39 +++++++
.../RandomSourceInternalParametricTest.java | 112 +++++++++++++++++++
.../simple/internal/SeedConverterComposerTest.java | 37 ++++++
8 files changed, 415 insertions(+), 1 deletion(-)
diff --git a/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/internal/ProviderBuilder.java b/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/internal/ProviderBuilder.java
index 6973ff8..b0d470d 100644
--- a/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/internal/ProviderBuilder.java
+++ b/commons-rng-simple/src/main/java/org/apache/commons/rng/simple/internal/ProviderBuilder.java
@@ -124,7 +124,7 @@ public final class ProviderBuilder {
* <li>{@code byte[]}</li>
* </ul>
*/
- private enum NativeSeedType {
+ enum NativeSeedType {
/** The seed type is {@code Integer}. */
INT {
@Override
diff --git a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/Long2IntArrayTest.java b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/Long2IntArrayTest.java
new file mode 100644
index 0000000..fad7d37
--- /dev/null
+++ b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/Long2IntArrayTest.java
@@ -0,0 +1,33 @@
+/*
+ * 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 Long2IntArray} converter.
+ */
+public class Long2IntArrayTest {
+ @Test
+ public void testFixedLengthConversion() {
+ final Long seed = 567L;
+ final int length = 5;
+ final int[] out = new Long2IntArray(length).convert(seed);
+ Assert.assertEquals(length, out.length);
+ }
+}
diff --git a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/Long2LongArrayTest.java b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/Long2LongArrayTest.java
new file mode 100644
index 0000000..36d3960
--- /dev/null
+++ b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/Long2LongArrayTest.java
@@ -0,0 +1,33 @@
+/*
+ * 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 Long2LongArray} converter.
+ */
+public class Long2LongArrayTest {
+ @Test
+ public void testFixedLengthConversion() {
+ final Long seed = 567L;
+ final int length = 3;
+ final long[] out = new Long2LongArray(length).convert(seed);
+ Assert.assertEquals(length, out.length);
+ }
+}
diff --git a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/NativeSeedTypeParametricTest.java b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/NativeSeedTypeParametricTest.java
new file mode 100644
index 0000000..921ec6e
--- /dev/null
+++ b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/NativeSeedTypeParametricTest.java
@@ -0,0 +1,124 @@
+/*
+ * 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.apache.commons.rng.simple.internal.ProviderBuilder.NativeSeedType;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+import java.lang.reflect.Array;
+
+/**
+ * Tests for the {@link ProviderBuilder.NativeSeedType} seed conversions. This test
+ * ensures that a seed can be created or converted from any supported input seed to each
+ * supported native seed type.
+ */
+@RunWith(value=Parameterized.class)
+public class NativeSeedTypeParametricTest {
+ /** The supported seeds for conversion to a native seed type. */
+ private static final Object[] SUPPORTED_SEEDS = {
+ Integer.valueOf(1),
+ Long.valueOf(2),
+ new int[] { 3, 4, 5 },
+ new long[] { 6, 7, 8 },
+ new byte[] { 9, 10, 11 },
+ };
+ /** Example unsupported seeds for conversion to a native seed type. */
+ private static final Object[] UNSUPPORTED_SEEDS = {
+ null,
+ Double.valueOf(Math.PI),
+ };
+
+ /** The class type of the native seed. */
+ private final Class<?> type;
+ /** The native seed type enum instance. */
+ private final NativeSeedType nativeSeedType;
+
+ /**
+ * Initializes the test instance.
+ *
+ * @param type The type of the native seed.
+ */
+ public NativeSeedTypeParametricTest(Class<?> type) {
+ this.type = type;
+ nativeSeedType = NativeSeedType.createNativeSeedType(type);
+ }
+
+ /**
+ * Gets the supported native seed types.
+ *
+ * @return the types
+ */
+ @Parameters
+ public static Object[] getTypes() {
+ // This is a list of the class types that are supported native seeds.
+ return new Object[] {
+ Integer.class,
+ Long.class,
+ int[].class,
+ long[].class
+ };
+ }
+
+ /**
+ * Test the seed can be created as the correct type.
+ */
+ @Test
+ public void testCreateSeed() {
+ final int size = 3;
+ final Object seed = nativeSeedType.createSeed(size);
+ Assert.assertNotNull(seed);
+ Assert.assertEquals("Seed was not the correct class", type, seed.getClass());
+ if (type.isArray()) {
+ Assert.assertEquals("Seed was not created the correct length", size, Array.getLength(seed));
+ }
+ }
+
+ /**
+ * Test the seed can be converted to the correct type from any of the supported input types.
+ */
+ @Test
+ public void testConvertSupportedSeed() {
+ // Size can be ignored during conversion and so it not asserted
+ final int size = 3;
+ for (final Object input : SUPPORTED_SEEDS) {
+ final Object seed = nativeSeedType.convertSeed(input, size);
+ final String msg = input.getClass() + " input seed was not converted";
+ Assert.assertNotNull(msg, seed);
+ Assert.assertEquals(msg, type, seed.getClass());
+ }
+ }
+
+ /**
+ * Test unsupported input seed types are rejected.
+ */
+ @Test
+ public void testCannotConvertUnsupportedSeed() {
+ final int size = 3;
+ for (final Object input : UNSUPPORTED_SEEDS) {
+ try {
+ nativeSeedType.convertSeed(input, size);
+ Assert.fail(input.getClass() + " input seed was not rejected as unsupported");
+ } catch (UnsupportedOperationException ex) {
+ // This is expected
+ }
+ }
+ }
+}
diff --git a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/NativeSeedTypeTest.java b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/NativeSeedTypeTest.java
new file mode 100644
index 0000000..a4f570b
--- /dev/null
+++ b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/NativeSeedTypeTest.java
@@ -0,0 +1,36 @@
+/*
+ * 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.apache.commons.rng.simple.internal.ProviderBuilder.NativeSeedType;
+import org.junit.Test;
+
+/**
+ * Tests for the {@link ProviderBuilder.NativeSeedType} construction. This test edge cases
+ * where the seed type is not supported.
+ */
+public class NativeSeedTypeTest {
+ @Test(expected = NullPointerException.class)
+ public void testCreateWithNull() {
+ NativeSeedType.createNativeSeedType(null);
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void testCreateWithDouble() {
+ NativeSeedType.createNativeSeedType(Double.class);
+ }
+}
diff --git a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/NoOpConverterTest.java b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/NoOpConverterTest.java
new file mode 100644
index 0000000..759f004
--- /dev/null
+++ b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/NoOpConverterTest.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 NoOpConverter}.
+ */
+public class NoOpConverterTest {
+ @Test
+ public void testNoOpIntegerCoversion() {
+ final NoOpConverter<Integer> converter = new NoOpConverter<Integer>();
+ final Integer in = 123;
+ Assert.assertSame(in, converter.convert(in));
+ }
+
+ @Test
+ public void testNoOpLongArrayCoversion() {
+ final NoOpConverter<long[]> converter = new NoOpConverter<long[]>();
+ final long[] in = {123L, 456L, Long.MAX_VALUE};
+ Assert.assertSame(in, converter.convert(in));
+ }
+}
diff --git a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/RandomSourceInternalParametricTest.java b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/RandomSourceInternalParametricTest.java
new file mode 100644
index 0000000..ad2355c
--- /dev/null
+++ b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/RandomSourceInternalParametricTest.java
@@ -0,0 +1,112 @@
+/*
+ * 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.apache.commons.rng.simple.internal.ProviderBuilder.RandomSourceInternal;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+/**
+ * Tests for the {@link ProviderBuilder.RandomSourceInternal} seed conversions. This test
+ * ensures that all random sources can create a seed or convert any supported seed to the
+ * correct type for the constructor.
+ */
+@RunWith(value=Parameterized.class)
+public class RandomSourceInternalParametricTest {
+ /** The supported seeds for conversion to a native seed type. */
+ private static final Object[] SUPPORTED_SEEDS = {
+ Integer.valueOf(1),
+ Long.valueOf(2),
+ new int[] { 3, 4, 5 },
+ new long[] { 6, 7, 8 },
+ new byte[] { 9, 10, 11 },
+ };
+ /** Example unsupported seeds for conversion to a native seed type. */
+ private static final Object[] UNSUPPORTED_SEEDS = {
+ null,
+ Double.valueOf(Math.PI),
+ };
+
+ /** Internal identifier for the random source. */
+ private final RandomSourceInternal randomSourceInternal;
+ /** The class type of the native seed. */
+ private final Class<?> type;
+
+ /**
+ * Initializes the test instance.
+ *
+ * @param randomSourceInternal Internal identifier for the random source.
+ */
+ public RandomSourceInternalParametricTest(RandomSourceInternal randomSourceInternal) {
+ this.randomSourceInternal = randomSourceInternal;
+ // The first constructor argument is always the seed type
+ this.type = randomSourceInternal.getArgs()[0];
+ }
+
+ /**
+ * Gets the supported native seed types.
+ *
+ * @return the types
+ */
+ @Parameters
+ public static Object[] getTypes() {
+ return RandomSourceInternal.values();
+ }
+
+ /**
+ * Test the seed can be created as the correct type.
+ */
+ @Test
+ public void testCreateSeed() {
+ final Object seed = randomSourceInternal.createSeed();
+ Assert.assertNotNull(seed);
+ Assert.assertEquals("Seed was not the correct class", type, seed.getClass());
+ Assert.assertTrue("Seed was not identified as the native type", randomSourceInternal.isNativeSeed(seed));
+ }
+
+ /**
+ * Test the seed can be converted to the correct type from any of the supported input types.
+ */
+ @Test
+ public void testConvertSupportedSeed() {
+ for (final Object input : SUPPORTED_SEEDS) {
+ final Object seed = randomSourceInternal.convertSeed(input);
+ final String msg = input.getClass() + " input seed was not converted";
+ Assert.assertNotNull(msg, seed);
+ Assert.assertEquals(msg, type, seed.getClass());
+ Assert.assertTrue(msg, randomSourceInternal.isNativeSeed(seed));
+ }
+ }
+
+ /**
+ * Test unsupported input seed types are rejected.
+ */
+ @Test
+ public void testCannotConvertUnsupportedSeed() {
+ for (final Object input : UNSUPPORTED_SEEDS) {
+ try {
+ randomSourceInternal.convertSeed(input);
+ Assert.fail(input.getClass() + " input seed was not rejected as unsupported");
+ } catch (UnsupportedOperationException ex) {
+ // This is expected
+ }
+ }
+ }
+}
diff --git a/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/SeedConverterComposerTest.java b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/SeedConverterComposerTest.java
new file mode 100644
index 0000000..3554dba
--- /dev/null
+++ b/commons-rng-simple/src/test/java/org/apache/commons/rng/simple/internal/SeedConverterComposerTest.java
@@ -0,0 +1,37 @@
+/*
+ * 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 SeedConverterComposer}.
+ */
+public class SeedConverterComposerTest {
+ @Test
+ public void testComposedCoversion() {
+ final Int2Long int2Long = new Int2Long();
+ final Long2LongArray long2LongArray = new Long2LongArray(3);
+ final SeedConverterComposer<Integer, Long, long[]> composer =
+ new SeedConverterComposer<Integer, Long, long[]>(int2Long, long2LongArray);
+ final Integer in = 123;
+ final Object out = composer.convert(in);
+ Assert.assertTrue("Bad type conversion", out instanceof long[]);
+ Assert.assertEquals("Incorrect long[] length", 3, ((long[])out).length);
+ }
+}