View Javadoc

1   /*******************************************************************************
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004, 2012 Artois University and CNRS
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the Eclipse Public License v1.0
6    * which accompanies this distribution, and is available at
7    *  http://www.eclipse.org/legal/epl-v10.html
8    *
9    * Alternatively, the contents of this file may be used under the terms of
10   * either the GNU Lesser General Public License Version 2.1 or later (the
11   * "LGPL"), in which case the provisions of the LGPL are applicable instead
12   * of those above. If you wish to allow use of your version of this file only
13   * under the terms of the LGPL, and not to allow others to use your version of
14   * this file under the terms of the EPL, indicate your decision by deleting
15   * the provisions above and replace them with the notice and other provisions
16   * required by the LGPL. If you do not delete the provisions above, a recipient
17   * may use your version of this file under the terms of the EPL or the LGPL.
18   *
19   * Based on the original MiniSat specification from:
20   *
21   * An extensible SAT solver. Niklas Een and Niklas Sorensson. Proceedings of the
22   * Sixth International Conference on Theory and Applications of Satisfiability
23   * Testing, LNCS 2919, pp 502-518, 2003.
24   *
25   * See www.minisat.se for the original solver in C++.
26   *
27   * Contributors:
28   *   CRIL - initial API and implementation
29   *******************************************************************************/
30  package org.sat4j;
31  
32  import junit.framework.TestCase;
33  
34  import org.sat4j.core.VecInt;
35  import org.sat4j.minisat.SolverFactory;
36  import org.sat4j.specs.ContradictionException;
37  import org.sat4j.specs.ISolver;
38  import org.sat4j.specs.IVecInt;
39  import org.sat4j.specs.TimeoutException;
40  import org.sat4j.tools.SingleSolutionDetector;
41  
42  public class SingleSolutionTest extends TestCase {
43  
44      public SingleSolutionTest(String name) {
45          super(name);
46      }
47  
48      @Override
49      protected void setUp() throws Exception {
50          this.solver = SolverFactory.newDefault();
51          this.detector = new SingleSolutionDetector(this.solver);
52          this.detector.newVar(3);
53      }
54  
55      /*
56       * Test method for
57       * 'org.sat4j.tools.SingleSolutionDetector.hasASingleSolution()'
58       */
59      public void testHasASingleSolution() throws ContradictionException,
60              TimeoutException {
61          IVecInt clause = new VecInt();
62          clause.push(1).push(2);
63          this.detector.addClause(clause);
64          clause.clear();
65          clause.push(-1).push(-2);
66          this.detector.addClause(clause);
67          assertTrue(this.detector.isSatisfiable());
68          assertFalse(this.detector.hasASingleSolution());
69      }
70  
71      /*
72       * Test method for
73       * 'org.sat4j.tools.SingleSolutionDetector.hasASingleSolution()'
74       */
75      public void testHasNoSingleSolution() throws ContradictionException,
76              TimeoutException {
77          IVecInt clause = new VecInt();
78          clause.push(1).push(2);
79          this.detector.addClause(clause);
80          clause.clear();
81          clause.push(-1).push(-2);
82          this.detector.addClause(clause);
83          assertTrue(this.detector.isSatisfiable());
84          clause.clear();
85          clause.push(-1).push(2);
86          this.detector.addClause(clause);
87          assertTrue(this.detector.isSatisfiable());
88          assertTrue(this.detector.hasASingleSolution());
89          clause.clear();
90          clause.push(1).push(-2);
91          this.detector.addClause(clause);
92          assertFalse(this.detector.isSatisfiable());
93          try {
94              assertFalse(this.detector.hasASingleSolution());
95              fail();
96          } catch (UnsupportedOperationException e) {
97              // OK
98          }
99      }
100 
101     /*
102      * Test method for
103      * 'org.sat4j.tools.SingleSolutionDetector.hasASingleSolution()'
104      */
105     public void testHasNoSingleSolutionUNSAT() throws ContradictionException,
106             TimeoutException {
107         IVecInt clause = new VecInt();
108         clause.push(1).push(2);
109         this.detector.addClause(clause);
110         clause.clear();
111         clause.push(-1).push(-2);
112         this.detector.addClause(clause);
113         assertTrue(this.detector.isSatisfiable());
114         clause.clear();
115         clause.push(-1).push(2);
116         this.detector.addClause(clause);
117         assertTrue(this.detector.isSatisfiable());
118         clause.clear();
119         clause.push(1).push(-2);
120         this.detector.addClause(clause);
121         assertFalse(this.detector.isSatisfiable());
122         try {
123             assertFalse(this.detector.hasASingleSolution());
124             fail();
125         } catch (UnsupportedOperationException e) {
126             // OK
127         }
128     }
129 
130     /*
131      * Test method for
132      * 'org.sat4j.tools.SingleSolutionDetector.hasASingleSolution(IVecInt)'
133      */
134     public void testHasASingleSolutionIVecInt() throws ContradictionException,
135             TimeoutException {
136         IVecInt clause = new VecInt();
137         clause.push(1).push(2);
138         this.detector.addClause(clause);
139         IVecInt assumptions = new VecInt();
140         assumptions.push(1);
141         assertTrue(this.detector.isSatisfiable(assumptions));
142         assertFalse(this.detector.hasASingleSolution(assumptions));
143         clause.clear();
144         clause.push(-1).push(2);
145         this.detector.addClause(clause);
146         assertTrue(this.detector.isSatisfiable(assumptions));
147         assertTrue(this.detector.hasASingleSolution(assumptions));
148         clause.clear();
149         clause.push(-1).push(-2);
150         this.detector.addClause(clause);
151         assertFalse(this.detector.isSatisfiable(assumptions));
152         try {
153             assertFalse(this.detector.hasASingleSolution(assumptions));
154             fail();
155         } catch (UnsupportedOperationException e) {
156             // OK
157         }
158     }
159 
160     private ISolver solver;
161 
162     private SingleSolutionDetector detector;
163 }