1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package p3j.misc.math;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import junit.framework.TestCase;
22 import p3j.misc.Misc;
23 import p3j.misc.errors.GeneratorError;
24 import p3j.misc.gui.GUI;
25 import p3j.pppm.IStochasticOccurrence;
26
27
28
29
30
31
32
33
34
35
36 public class TestRandomNumberChecker extends TestCase {
37
38
39 static final String ONE_ERR_MSG = "There should be one error message in the log.";
40
41
42 RandomNumberChecks rnc;
43
44
45 List<GeneratorError> errorLog;
46
47
48 static final int NUM_TEST_ENTITIES = 10;
49
50 @Override
51 public void setUp() throws Exception {
52 super.setUp();
53 rnc = RandomNumberChecks.getInstance();
54 errorLog = new ArrayList<GeneratorError>();
55 GUI.setHeadless(true);
56 }
57
58 @Override
59 public void tearDown() throws Exception {
60 GUI.setHeadless(false);
61 super.tearDown();
62 }
63
64
65
66
67 public void testEmptyOrNullList() {
68 createListWithBadParameter(null);
69 createListWithBadParameter(new ArrayList<TestEntity>());
70 }
71
72
73
74
75
76
77
78 public void createListWithBadParameter(List<TestEntity> list) {
79 try {
80 rnc.checkProbabilitySetting("badParamTest", list, errorLog);
81 fail("No exception has been thrown.");
82 } catch (RuntimeException ex) {
83 assertEquals(
84 "IllegalArgumentException should be thrown when confronted with list '"
85 + list + "'", IllegalArgumentException.class, ex.getClass());
86 }
87 }
88
89
90
91
92 public void testNormalisation() {
93 List<TestEntity> testList = new ArrayList<TestEntity>();
94 double testEntitySum = 0;
95 for (int i = 0; i < NUM_TEST_ENTITIES; i++) {
96 testList.add(new TestEntity(i));
97 testEntitySum += i;
98 }
99
100 rnc.checkProbabilitySetting("normalisation", testList, errorLog);
101 assertEquals(ONE_ERR_MSG, 1, errorLog.size());
102 for (int i = 0; i < testList.size(); i++) {
103 assertTrue(
104 "If probabilities sum is larger than 1, probabilities need to be normalised.",
105 Misc.numEqual(i / testEntitySum, testList.get(i).getProbability()));
106 }
107 rnc.checkProbabilitySetting("test3", testList, errorLog);
108 }
109
110
111
112
113 public void testZeroProbability() {
114 List<TestEntity> testList = new ArrayList<TestEntity>();
115 for (int i = 0; i < NUM_TEST_ENTITIES; i++) {
116 testList.add(new TestEntity(0));
117 }
118
119 rnc.checkProbabilitySetting("zeroProbability", testList, errorLog);
120 assertEquals(ONE_ERR_MSG, 1, errorLog.size());
121 for (TestEntity testEntity : testList) {
122 assertEquals(
123 "If no probabilities were set, uniform distribution shall be assumed.",
124 1.0 / testList.size(), testEntity.getProbability());
125 }
126 }
127 }
128
129
130
131
132
133
134
135
136
137
138 class TestEntity implements IStochasticOccurrence {
139
140
141
142
143 double probability;
144
145 TestEntity(double prob) {
146 probability = prob;
147 }
148
149 @Override
150 public double getProbability() {
151 return probability;
152 }
153
154 @Override
155 public void setProbability(double prob) {
156 probability = prob;
157 }
158
159 }