View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.rng.simple.internal;
18  
19  import org.junit.Assert;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  import org.junit.runners.Parameterized;
23  import org.junit.runners.Parameterized.Parameters;
24  
25  import java.lang.reflect.Array;
26  
27  /**
28   * Tests for the {@link NativeSeedType} seed conversions. This test
29   * ensures that a seed can be created or converted from any supported input seed to each
30   * supported native seed type.
31   */
32  @RunWith(value = Parameterized.class)
33  public class NativeSeedTypeParametricTest {
34      /** This is a list of the class types that are supported native seeds. */
35      private static final Object[] SUPPORTED_NATIVE_TYPES = {
36          Integer.class,
37          Long.class,
38          int[].class,
39          long[].class
40      };
41      /** Example supported seeds for conversion to a native seed type. */
42      private static final Object[] SUPPORTED_SEEDS = {
43          Integer.valueOf(1),
44          Long.valueOf(2),
45          new int[] {3, 4, 5},
46          new long[] {6, 7, 8},
47          new byte[] {9, 10, 11},
48      };
49      /** Example unsupported seeds for conversion to a native seed type. */
50      private static final Object[] UNSUPPORTED_SEEDS = {
51          null,
52          Double.valueOf(Math.PI),
53      };
54  
55      /** The native seed type enum instance. */
56      private final NativeSeedType nativeSeedType;
57      /** The class type of the native seed. */
58      private final Class<?> type;
59  
60      /**
61       * Initializes the test instance.
62       *
63       * @param type The type of the native seed.
64       */
65      public NativeSeedTypeParametricTest(Class<?> type) {
66          this.type = type;
67          nativeSeedType = findNativeSeedType(type);
68      }
69  
70      /**
71       * Gets the supported native seed types.
72       *
73       * @return the types
74       */
75      @Parameters
76      public static Object[] getTypes() {
77          // Check that there are enum values for all supported types.
78          // This ensures the test is maintained to correspond to the enum.
79          Assert.assertEquals("Incorrect number of enum values for the supported native types",
80              SUPPORTED_NATIVE_TYPES.length, NativeSeedType.values().length);
81  
82          return SUPPORTED_NATIVE_TYPES;
83      }
84  
85      /**
86       * Creates the native seed type.
87       *
88       * @param type Class of the native seed.
89       * @return the native seed type
90       */
91      private static NativeSeedType findNativeSeedType(Class<?> type) {
92          for (final NativeSeedType nativeSeedType : NativeSeedType.values()) {
93              if (type.equals(nativeSeedType.getType())) {
94                  return nativeSeedType;
95              }
96          }
97          throw new AssertionError("No enum matching the type: " + type);
98      }
99  
100     /**
101      * Test the seed can be created as the correct type.
102      */
103     @Test
104     public void testCreateSeed() {
105         final int size = 3;
106         final Object seed = nativeSeedType.createSeed(size);
107         Assert.assertNotNull(seed);
108         Assert.assertEquals("Seed was not the correct class", type, seed.getClass());
109         if (type.isArray()) {
110             Assert.assertEquals("Seed was not created the correct length", size, Array.getLength(seed));
111         }
112     }
113 
114     /**
115      * Test the seed can be created, converted to a byte[] and then back to the native type.
116      */
117     @Test
118     public void testConvertSeedToBytes() {
119         final int size = 3;
120         final Object seed = nativeSeedType.createSeed(size);
121         Assert.assertNotNull("Null seed", seed);
122 
123         final byte[] bytes = NativeSeedType.convertSeedToBytes(seed);
124         Assert.assertNotNull("Null byte[] seed", bytes);
125 
126         final Object seed2 = nativeSeedType.convertSeed(bytes, size);
127         if (type.isArray()) {
128             // This handles nested primitive arrays
129             Assert.assertArrayEquals("byte[] seed was not converted back",
130                                      new Object[] {seed}, new Object[] {seed2});
131         } else {
132             Assert.assertEquals("byte[] seed was not converted back", seed, seed2);
133         }
134     }
135 
136     /**
137      * Test the seed can be converted to the correct type from any of the supported input types.
138      */
139     @Test
140     public void testConvertSupportedSeed() {
141         // Size can be ignored during conversion and so it not asserted
142         final int size = 3;
143         for (final Object input : SUPPORTED_SEEDS) {
144             final Object seed = nativeSeedType.convertSeed(input, size);
145             final String msg = input.getClass() + " input seed was not converted";
146             Assert.assertNotNull(msg, seed);
147             Assert.assertEquals(msg, type, seed.getClass());
148         }
149     }
150 
151     /**
152      * Test unsupported input seed types are rejected.
153      */
154     @Test
155     public void testCannotConvertUnsupportedSeed() {
156         final int size = 3;
157         for (final Object input : UNSUPPORTED_SEEDS) {
158             try {
159                 nativeSeedType.convertSeed(input, size);
160                 Assert.fail(input.getClass() + " input seed was not rejected as unsupported");
161             } catch (UnsupportedOperationException ex) {
162                 // This is expected
163             }
164         }
165     }
166 }