1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
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   * Tests for the probability configuration validity checks.
29   * 
30   * Created: August 17, 2008
31   * 
32   * @author Christina Bohk
33   * @author Roland Ewald
34   * 
35   */
36  public class TestRandomNumberChecker extends TestCase {
37  
38  	/** Assertion message. */
39  	static final String ONE_ERR_MSG = "There should be one error message in the log.";
40  
41  	/** The checker for random numbers. */
42  	RandomNumberChecks rnc;
43  
44  	/** Error log. */
45  	List<GeneratorError> errorLog;
46  
47  	/** Number of test entities to be used. */
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  	 * Tests for bad lists as parameters.
66  	 */
67  	public void testEmptyOrNullList() {
68  		createListWithBadParameter(null);
69  		createListWithBadParameter(new ArrayList<TestEntity>());
70  	}
71  
72  	/**
73  	 * Checks given list as a bad parameter.
74  	 * 
75  	 * @param list
76  	 *          the bad parameter
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  	 * Tests for probability normalisation.
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 	 * Checks behaviour in case of zero probability.
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  * Test entity for probability validity check.
131  * 
132  * Created: August 17, 2008
133  * 
134  * @author Christina Bohk
135  * @author Roland Ewald
136  * 
137  */
138 class TestEntity implements IStochasticOccurrence {
139 
140 	/**
141 	 * Probability of test entity.
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 }