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.minisat.core;
31  
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.assertFalse;
34  import static org.junit.Assert.assertTrue;
35  
36  import org.junit.Test;
37  import org.sat4j.core.VecInt;
38  import org.sat4j.minisat.SolverFactory;
39  import org.sat4j.specs.ContradictionException;
40  import org.sat4j.specs.ISolver;
41  import org.sat4j.specs.IVecInt;
42  import org.sat4j.specs.TimeoutException;
43  
44  public class BugTrivialAssumption {
45  
46      @Test
47      public void testUnitClauseInFormulaUnsat() throws ContradictionException,
48              TimeoutException {
49          final ISolver solver = SolverFactory.newDefault();
50          solver.newVar(1);
51          solver.addClause(new VecInt(new int[] { 1 }));
52          assertFalse(solver.isSatisfiable(new VecInt(new int[] { -1 })));
53          IVecInt explanation = solver.unsatExplanation();
54          assertTrue(explanation.contains(-1));
55          assertEquals(1, explanation.size());
56      }
57  
58      @Test
59      public void testUnitClauseInFormulaSat() throws ContradictionException,
60              TimeoutException {
61          final ISolver solver = SolverFactory.newDefault();
62          solver.addClause(new VecInt(new int[] { 1 }));
63          assertTrue(solver.isSatisfiable(new VecInt(new int[] { 1 })));
64      }
65  
66      @Test
67      public void testBinaryClauseInFormula() throws ContradictionException,
68              TimeoutException {
69          final ISolver solver = SolverFactory.newDefault();
70          solver.addClause(new VecInt(new int[] { 1, 2 }));
71          assertFalse(solver.isSatisfiable(new VecInt(new int[] { -1, -2 })));
72          IVecInt explanation = solver.unsatExplanation();
73          assertTrue(explanation.contains(-1));
74          assertTrue(explanation.contains(-2));
75          assertEquals(2, explanation.size());
76      }
77  
78      @Test
79      public void testEasyInconsistencyInAssumption()
80              throws ContradictionException, TimeoutException {
81          final ISolver solver = SolverFactory.newDefault();
82          solver.newVar(1);
83          assertFalse(solver.isSatisfiable(new VecInt(new int[] { -1, 1 })));
84          IVecInt explanation = solver.unsatExplanation();
85          assertTrue(explanation.contains(-1));
86          assertTrue(explanation.contains(1));
87          assertEquals(2, explanation.size());
88      }
89  
90      @Test
91      public void testInconsistencyInAssumption() throws ContradictionException,
92              TimeoutException {
93          final ISolver solver = SolverFactory.newDefault();
94          solver.newVar(3);
95          assertFalse(solver.isSatisfiable(new VecInt(new int[] { -1, 2, 3, 1 })));
96          IVecInt explanation = solver.unsatExplanation();
97          assertTrue(explanation.contains(-1));
98          assertTrue(explanation.contains(1));
99          assertEquals(2, explanation.size());
100     }
101 
102     @Test
103     public void testVoidFormula() throws ContradictionException,
104             TimeoutException {
105         final ISolver solver = SolverFactory.newDefault();
106         assertTrue(solver.isSatisfiable(new VecInt(new int[] { -1 })));
107         assertTrue(solver.isSatisfiable(new VecInt(new int[] { 1 })));
108     }
109 }