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.sampling;
18  
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.List;
22  
23  import org.junit.Assert;
24  import org.junit.Test;
25  import org.apache.commons.rng.UniformRandomProvider;
26  import org.apache.commons.rng.simple.RandomSource;
27  
28  /**
29   * Tests for {@link CollectionSampler}.
30   */
31  public class CollectionSamplerTest {
32  
33      @Test
34      public void testSampleTrivial() {
35          final ArrayList<String> list = new ArrayList<String>();
36          list.add("Apache");
37          list.add("Commons");
38          list.add("RNG");
39  
40          final CollectionSampler<String> sampler =
41              new CollectionSampler<String>(RandomSource.create(RandomSource.MWC_256),
42                                            list);
43          final String word = sampler.sample();
44          for (String w : list) {
45              if (word.equals(w)) {
46                  return;
47              }
48          }
49          Assert.fail(word + " not in list");
50      }
51  
52      @Test(expected = IllegalArgumentException.class)
53      public void testSamplePrecondition() {
54          // Must fail for empty collection.
55          new CollectionSampler<String>(RandomSource.create(RandomSource.MT),
56                                        new ArrayList<String>());
57      }
58  
59      /**
60       * Test the SharedStateSampler implementation.
61       */
62      @Test
63      public void testSharedStateSampler() {
64          final UniformRandomProvider rng1 = RandomSource.create(RandomSource.SPLIT_MIX_64, 0L);
65          final UniformRandomProvider rng2 = RandomSource.create(RandomSource.SPLIT_MIX_64, 0L);
66          final List<String> list = Arrays.asList("Apache", "Commons", "RNG");
67          final CollectionSampler<String> sampler1 =
68              new CollectionSampler<String>(rng1, list);
69          final CollectionSampler<String> sampler2 = sampler1.withUniformRandomProvider(rng2);
70          RandomAssert.assertProduceSameSequence(
71              new RandomAssert.Sampler<String>() {
72                  @Override
73                  public String sample() {
74                      return sampler1.sample();
75                  }
76              },
77              new RandomAssert.Sampler<String>() {
78                  @Override
79                  public String sample() {
80                      return sampler2.sample();
81                  }
82              });
83      }
84  }