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.simulation.assignments.exhaustive;
17  
18  import java.util.Map;
19  
20  import junit.framework.TestCase;
21  import p3j.pppm.parameters.ParameterAssignment;
22  import p3j.pppm.parameters.ParameterInstance;
23  import p3j.pppm.parameters.Parameters;
24  import p3j.pppm.sets.Set;
25  import p3j.pppm.sets.SetType;
26  
27  /**
28   * Test for the {@link SetManager}.
29   * 
30   * @author Christina Bohk
31   * @author Roland Ewald
32   */
33  public class TestSetManager extends TestCase {
34  
35  	/** The set manager. */
36  	SetManager setManager;
37  
38  	/**
39  	 * The probability of the first assignment. Has to be larger or equal than
40  	 * 0.5.
41  	 */
42  	protected final static double probFirst = 0.8;
43  
44  	/** The probability of the second assignment (have to sum up to 1). */
45  	protected final static double probSecond = 1 - probFirst;
46  
47  	/** The first parameter instance. */
48  	protected final ParameterInstance instance1 = new ParameterInstance(0,
49  	    Parameters.EMIG_MORT_X_F);
50  
51  	/** The second parameter instance. */
52  	protected final ParameterInstance instance2 = new ParameterInstance(1,
53  	    Parameters.EMIG_MORT_X_M);
54  
55  	/** The first assignment for the first inst. */
56  	final ParameterAssignment assignment1Inst1 = new ParameterAssignment(
57  	    instance1, "#1-1", "", probFirst, 0.0, null);
58  
59  	/** The second assignment for the first instance. */
60  	final ParameterAssignment assignment2Inst1 = new ParameterAssignment(
61  	    instance1, "#1-2", "", probSecond, 0.0, null);
62  
63  	/** The first assignment1 for the second instance. */
64  	final ParameterAssignment assignment1Inst2 = new ParameterAssignment(
65  	    instance2, "#2-1", "", probFirst, 0.0, null);
66  
67  	/** The second assignment for the second instance. */
68  	final ParameterAssignment assignment2Inst2 = new ParameterAssignment(
69  	    instance2, "#2-2", "", probSecond, 0.0, null);
70  
71  	/** The test Settype. */
72  	protected final SetType setType = new SetType("Test Settype",
73  	    "This is just a Settype of testing.");
74  	{
75  		setType.addInstance(instance1);
76  		setType.addInstance(instance2);
77  	}
78  
79  	/** The test set. */
80  	protected final Set set = setType.createSet("Test Set",
81  	    "This is just a set for testing.", 1.0);
82  	{
83  		set.addParameterAssignment(assignment1Inst1);
84  		set.addParameterAssignment(assignment1Inst2);
85  		set.addParameterAssignment(assignment2Inst1);
86  		set.addParameterAssignment(assignment2Inst2);
87  	}
88  
89  	/*
90  	 * (non-Javadoc)
91  	 * 
92  	 * @see junit.framework.TestCase#setUp()
93  	 */
94  	@Override
95  	public void setUp() {
96  		setManager = new SetManager(set, setType);
97  	}
98  
99  	/**
100 	 * Test set manager.
101 	 */
102 	public void testSetManager() {
103 		assertEquals(probFirst * probFirst, setManager.getCurrentAssignmentProb());
104 		testMapping(setManager.getCurrentMapping(), assignment1Inst1,
105 		    assignment1Inst2);
106 
107 		assertTrue(setManager.nextAssignment());
108 		assertEquals(probFirst * probSecond, setManager.getCurrentAssignmentProb());
109 
110 		assertTrue(setManager.nextAssignment());
111 		assertEquals(probFirst * probSecond, setManager.getCurrentAssignmentProb());
112 
113 		assertTrue(setManager.nextAssignment());
114 		assertEquals(probSecond * probSecond, setManager.getCurrentAssignmentProb());
115 		testMapping(setManager.getCurrentMapping(), assignment2Inst1,
116 		    assignment2Inst2);
117 
118 		assertFalse(setManager.nextAssignment());
119 	}
120 
121 	/**
122 	 * Test mapping.
123 	 * 
124 	 * @param currentMapping
125 	 *          the current mapping
126 	 * @param assignmentFirstInst
127 	 *          the assignment for the first instance
128 	 * @param assignmentSecondInst
129 	 *          the assignment for the second instance
130 	 */
131 	private void testMapping(
132 	    Map<ParameterInstance, ParameterAssignment> currentMapping,
133 	    ParameterAssignment assignmentFirstInst,
134 	    ParameterAssignment assignmentSecondInst) {
135 		assertEquals(2, currentMapping.size());
136 		assertEquals(assignmentFirstInst, currentMapping.get(instance1));
137 		assertEquals(assignmentSecondInst, currentMapping.get(instance2));
138 	}
139 
140 }