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.ArrayList;
19  import java.util.HashSet;
20  import java.util.List;
21  import java.util.Set;
22  
23  import junit.framework.TestCase;
24  
25  /**
26   * Tests for {@link Assignment} and {@link AssignmentEnumerator}.
27   * 
28   * @author Christina Bohk
29   * @author Roland Ewald
30   * 
31   */
32  public class TestAssignmentAndAssignmentEnumerator extends TestCase {
33  
34    /** The Constant SOME_PROBABILITY. Has to be in [0,1). */
35    final static double SOME_PROBABILITY = .5;
36  
37    /** Test assignment #1. */
38    Assignment a1;
39  
40    /** Test assignment #2. */
41    Assignment a2;
42  
43    /** Test assignment #3. */
44    Assignment a3;
45  
46    /** A deep copy of test assignment #1. */
47    Assignment a1Copy;
48  
49    @Override
50    public void setUp() {
51  
52      List<Integer> indexList1 = new ArrayList<Integer>();
53      indexList1.add(0);
54      indexList1.add(0);
55      indexList1.add(0);
56      a1 = new Assignment(indexList1, SOME_PROBABILITY);
57  
58      List<Integer> indexList2 = new ArrayList<Integer>(indexList1);
59      a2 = new Assignment(indexList2, SOME_PROBABILITY * SOME_PROBABILITY);
60  
61      List<Integer> indexList3 = new ArrayList<Integer>(indexList1);
62      indexList3.remove(0);
63      indexList3.add(0, 1);
64      a3 = new Assignment(indexList3, SOME_PROBABILITY);
65  
66      a1Copy = new Assignment(new ArrayList<Integer>(a1.getAssignmentIndices()),
67          a1.getProbability());
68    }
69  
70    public void testAssignment() {
71  
72      assertEquals("0-0-0-", a1.getID());
73      assertEquals("0-0-0-", a2.getID());
74      assertEquals("1-0-0-", a3.getID());
75  
76      assertFalse(a1.equals(a2));
77      assertFalse(a1.equals(a3));
78      assertTrue(a1.equals(a1Copy));
79    }
80  
81    public void testAssignmentEnumerator() {
82      AssignmentEnumerator assignmentEnumerator = new AssignmentEnumerator(a2);
83      assertEquals(a2, assignmentEnumerator.getMostProbable());
84  
85      Set<Assignment> assignmentSet = new HashSet<Assignment>();
86      assignmentSet.add(a1);
87      assignmentEnumerator.add(assignmentSet);
88      // Should be assignment #2, assignment #1 should have been rejected because
89      // of identical ID
90      assertNotSame(a1, assignmentEnumerator.getMostProbable());
91      assertEquals(a2, assignmentEnumerator.getMostProbable());
92  
93      assignmentSet.clear();
94      assignmentSet.add(a3);
95      assignmentEnumerator.add(assignmentSet);
96      // Assignment #3 should be accepted
97      assertNotSame(a2, assignmentEnumerator.getMostProbable());
98      assertEquals(a3, assignmentEnumerator.getMostProbable());
99  
100     // Only two elements in there:
101     assertNotNull(assignmentEnumerator.removeMostProbable());
102     assertNotNull(assignmentEnumerator.removeMostProbable());
103     assertNull(assignmentEnumerator.removeMostProbable());
104   }
105 }