1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.rng.sampling;
18
19 import java.util.ArrayList;
20
21 import org.junit.Assert;
22 import org.junit.Test;
23
24 import org.apache.commons.rng.simple.RandomSource;
25
26
27
28
29 public class CollectionSamplerTest {
30
31 @Test
32 public void testSampleTrivial() {
33 final ArrayList<String> list = new ArrayList<String>();
34 list.add("Apache");
35 list.add("Commons");
36 list.add("RNG");
37
38 final CollectionSampler<String> sampler =
39 new CollectionSampler<String>(RandomSource.create(RandomSource.MWC_256),
40 list);
41 final String word = sampler.sample();
42 for (String w : list) {
43 if (word.equals(w)) {
44 return;
45 }
46 }
47 Assert.fail(word + " not in list");
48 }
49
50 @Test(expected=IllegalArgumentException.class)
51 public void testSamplePrecondition() {
52
53 new CollectionSampler<String>(RandomSource.create(RandomSource.MT),
54 new ArrayList<String>());
55 }
56 }