1 package org.sat4j.core; 2 3 import static org.junit.Assert.assertEquals; 4 import static org.mockito.Mockito.inOrder; 5 import static org.mockito.Mockito.mock; 6 import static org.mockito.Mockito.verify; 7 import static org.mockito.Mockito.when; 8 9 import java.io.ByteArrayInputStream; 10 import java.io.IOException; 11 import java.io.PrintWriter; 12 import java.io.StringWriter; 13 14 import org.junit.Before; 15 import org.junit.Test; 16 import org.mockito.InOrder; 17 import org.sat4j.minisat.SolverFactory; 18 import org.sat4j.reader.JSONReader; 19 import org.sat4j.reader.ParseFormatException; 20 import org.sat4j.specs.ContradictionException; 21 import org.sat4j.specs.ISolver; 22 import org.sat4j.specs.IVecInt; 23 import org.sat4j.specs.TimeoutException; 24 25 public class JsonReaderTest { 26 27 private ISolver solver; 28 private JSONReader<ISolver> reader; 29 30 @Before 31 public void setUp() throws Exception { 32 solver = mock(ISolver.class); 33 reader = new JSONReader<ISolver>(solver); 34 } 35 36 @Test 37 public void testReadingSimpleClause() throws ParseFormatException, 38 ContradictionException, IOException { 39 String json = "[[1,-2,3]]"; 40 reader.parseString(json); 41 IVecInt clause = new VecInt().push(1).push(-2).push(3); 42 verify(solver).addClause(clause); 43 44 } 45 46 @Test(expected = ContradictionException.class) 47 public void testReadingNullClause() throws ParseFormatException, 48 ContradictionException, IOException { 49 when(solver.addClause(VecInt.EMPTY)).thenThrow( 50 new ContradictionException()); 51 String json = "[[]]"; 52 reader.parseString(json); 53 IVecInt clause = new VecInt(); 54 verify(solver).addClause(clause); 55 } 56 57 @Test 58 public void testReadingTwoClauses() throws ParseFormatException, 59 ContradictionException, IOException { 60 when(solver.addClause(VecInt.EMPTY)).thenThrow( 61 new ContradictionException()); 62 String json = "[[1,2,5],[3,-6]]"; 63 reader.parseString(json); 64 IVecInt clause1 = new VecInt().push(1).push(2).push(5); 65 IVecInt clause2 = new VecInt().push(3).push(-6); 66 InOrder inOrder = inOrder(solver); 67 inOrder.verify(solver).addClause(clause1); 68 inOrder.verify(solver).addClause(clause2); 69 70 } 71 72 @Test 73 public void testReadingACardExactly() throws ParseFormatException, 74 ContradictionException { 75 String json = "[[[1,-2,3],'=',1]]"; 76 reader.parseString(json); 77 IVecInt clause = new VecInt().push(1).push(-2).push(3); 78 verify(solver).addExactly(clause, 1); 79 } 80 81 @Test 82 public void testReadingACardAtMostEqual() throws ParseFormatException, 83 ContradictionException { 84 String json = "[[[1,-2,3],'<=',1]]"; 85 reader.parseString(json); 86 IVecInt clause = new VecInt().push(1).push(-2).push(3); 87 verify(solver).addAtMost(clause, 1); 88 } 89 90 @Test 91 public void testReadingACardAtMostStrictly() throws ParseFormatException, 92 ContradictionException { 93 String json = "[[[1,-2,3],'<',1]]"; 94 reader.parseString(json); 95 IVecInt clause = new VecInt().push(1).push(-2).push(3); 96 verify(solver).addAtMost(clause, 0); 97 } 98 99 @Test 100 public void testReadingACardAtLeastEqual() throws ParseFormatException, 101 ContradictionException { 102 String json = "[[[1,-2,3],'>=',2]]"; 103 reader.parseString(json); 104 IVecInt clause = new VecInt().push(1).push(-2).push(3); 105 verify(solver).addAtLeast(clause, 2); 106 } 107 108 @Test 109 public void testReadingACardAtLeastStrictly() throws ParseFormatException, 110 ContradictionException { 111 String json = "[[[1,-2,3],'>',2]]"; 112 reader.parseString(json); 113 IVecInt clause = new VecInt().push(1).push(-2).push(3); 114 verify(solver).addAtLeast(clause, 3); 115 } 116 117 @Test 118 public void testMixOfClausesAndCard() throws ParseFormatException, 119 ContradictionException { 120 String json = "[[-1,-2,-3],[[1,-2,3],'>',2],[4,-3,6]]"; 121 reader.parseString(json); 122 IVecInt clause1 = new VecInt().push(-1).push(-2).push(-3); 123 IVecInt card = new VecInt().push(1).push(-2).push(3); 124 IVecInt clause2 = new VecInt().push(4).push(-3).push(6); 125 verify(solver).addClause(clause1); 126 verify(solver).addAtLeast(card, 3); 127 verify(solver).addClause(clause2); 128 } 129 130 @Test 131 public void testOrderofMixedConstraints() throws ParseFormatException, 132 ContradictionException { 133 String json = "[[-1,-2,-3],[[1,-2,3],'>',2],[4,-3,6]]"; 134 reader.parseString(json); 135 IVecInt clause1 = new VecInt().push(-1).push(-2).push(-3); 136 IVecInt card = new VecInt().push(1).push(-2).push(3); 137 IVecInt clause2 = new VecInt().push(4).push(-3).push(6); 138 InOrder inOrder = inOrder(solver); 139 inOrder.verify(solver).addClause(clause1); 140 inOrder.verify(solver).addAtLeast(card, 3); 141 inOrder.verify(solver).addClause(clause2); 142 } 143 144 @Test 145 public void testInputStream() throws ParseFormatException, 146 ContradictionException, IOException { 147 String json = "[[[1,-2,3],'>',2]]"; 148 reader.parseInstance(new ByteArrayInputStream(json.getBytes())); 149 IVecInt clause = new VecInt().push(1).push(-2).push(3); 150 verify(solver).addAtLeast(clause, 3); 151 } 152 153 @Test 154 public void testJsonOutput() throws ParseFormatException, 155 ContradictionException, TimeoutException { 156 String json = "[[1,-2,3],[1,2,3],[1,-3],[-1,-3],[-1,2,3]]"; 157 ISolver realSolver = SolverFactory.newDefault(); 158 JSONReader jsonReader = new JSONReader(realSolver); 159 jsonReader.parseString(json); 160 int[] model = realSolver.findModel(); 161 StringWriter strw = new StringWriter(); 162 PrintWriter out = new PrintWriter(strw); 163 jsonReader.decode(model, out); 164 assertEquals("[1,2,-3]", strw.toString()); 165 } 166 }