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 java.util.Map;
20  import java.util.HashMap;
21  import org.junit.Assert;
22  import org.junit.Test;
23  
24  import org.apache.commons.rng.core.util.NumberFactory;
25  
26  /**
27   * Tests for {@link SeedFactory}.
28   */
29  public class SeedFactoryTest {
30      @Test
31      public void testCreateLong() {
32          final Map<Long, Integer> values = new HashMap<Long, Integer>();
33  
34          final int n = 100000;
35          for (int i = 0; i < n; i++) {
36              final long v = SeedFactory.createLong();
37  
38              Integer count = values.get(v);
39              if (count == null) {
40                  count = 0;
41              }
42              values.put(v, count + 1);
43          }
44  
45          // Check that all seeds are different.
46          assertDifferentValues(values);
47      }
48  
49      @Test
50      public void testCreateLongArray() {
51          final Map<Long, Integer> values = new HashMap<Long, Integer>();
52  
53          final int n = 100000;
54          final long[] array = SeedFactory.createLongArray(n);
55          Assert.assertEquals(n, array.length);
56  
57          for (long v : array) {
58              Integer count = values.get(v);
59              if (count == null) {
60                  count = 0;
61              }
62              values.put(v, count + 1);
63          }
64  
65          // Check that all seeds are different.
66          assertDifferentValues(values);
67      }
68  
69      @Test
70      public void testCreateIntArray() {
71          final Map<Long, Integer> values = new HashMap<Long, Integer>();
72  
73          for (int i = 0; i < 50000; i++) {
74              final int[] a = SeedFactory.createIntArray(2);
75              final long v = NumberFactory.makeLong(a[0], a[1]); 
76              Integer count = values.get(v);
77              if (count == null) {
78                  count = 0;
79              }
80              values.put(v, count + 1);
81          }
82  
83          // Check that all pairs in are different.
84          assertDifferentValues(values);
85      }
86  
87      /**
88       * Asserts that all the keys in given {@code map} have their
89       * value equal to 1.
90       *
91       * @param map Map to counts.
92       */
93      private static <T> void assertDifferentValues(Map<T, Integer> map) {
94          final StringBuilder sb = new StringBuilder();
95  
96          int duplicates = 0;
97          for (Map.Entry<T, Integer> entry : map.entrySet()) {
98              final int count = entry.getValue();
99              if (count <= 0) {
100                 throw new IllegalStateException();
101             }
102 
103             if (count > 1) {
104                 duplicates += count - 1;
105                 sb.append(entry.getKey() + ": " + count + "\n");
106             }
107         }
108 
109         if (duplicates > 0) {
110             Assert.fail(duplicates + " duplicates\n" + sb);
111         }
112     }
113 }