Add unit tests for getInstantiations (#8741)
authorAndrew Reynolds <andrew.j.reynolds@gmail.com>
Mon, 9 May 2022 17:05:28 +0000 (12:05 -0500)
committerGitHub <noreply@github.com>
Mon, 9 May 2022 17:05:28 +0000 (10:05 -0700)
src/api/cpp/cvc5.cpp
test/unit/api/cpp/solver_black.cpp
test/unit/api/java/SolverTest.java
test/unit/api/python/test_solver.py

index d7e85c4d36a2f73b5bb35a135ed1640bc637a868..a69d1f6418ebd48d9ce44199931935934abd629a 100644 (file)
@@ -7288,6 +7288,12 @@ void Solver::blockModelValues(const std::vector<Term>& terms) const
 std::string Solver::getInstantiations() const
 {
   CVC5_API_TRY_CATCH_BEGIN;
+  CVC5_API_RECOVERABLE_CHECK(d_slv->getSmtMode() == internal::SmtMode::UNSAT
+                             || d_slv->getSmtMode() == internal::SmtMode::SAT
+                             || d_slv->getSmtMode()
+                                    == internal::SmtMode::SAT_UNKNOWN)
+      << "Cannot get instantiations unless after a UNSAT, SAT or UNKNOWN "
+         "response.";
   //////// all checks before this line
   std::stringstream ss;
   d_slv->printInstantiations(ss);
index 577c8ad4c33d638dfca6eddbd4b6de6752e3c6ee..b8d05446dc4e89f4050e52e910ded8f59e5fcab4 100644 (file)
@@ -2340,6 +2340,24 @@ TEST_F(TestApiBlackSolver, blockModelValues5)
   ASSERT_NO_THROW(d_solver.blockModelValues({x}));
 }
 
+TEST_F(TestApiBlackSolver, getInstantiations)
+{
+  Sort iSort = d_solver.getIntegerSort();
+  Sort boolSort = d_solver.getBooleanSort();
+  Term p = d_solver.declareFun("p", {iSort}, boolSort);
+  Term x = d_solver.mkVar(iSort, "x");
+  Term bvl = d_solver.mkTerm(VARIABLE_LIST, {x});
+  Term app = d_solver.mkTerm(APPLY_UF, {p, x});
+  Term q = d_solver.mkTerm(FORALL, {bvl, app});
+  d_solver.assertFormula(q);
+  Term five = d_solver.mkInteger(5);
+  Term app2 = d_solver.mkTerm(NOT, {d_solver.mkTerm(APPLY_UF, {p, five})});
+  d_solver.assertFormula(app2);
+  ASSERT_THROW(d_solver.getInstantiations(), CVC5ApiException);
+  d_solver.checkSat();
+  ASSERT_NO_THROW(d_solver.getInstantiations());
+}
+
 TEST_F(TestApiBlackSolver, setInfo)
 {
   ASSERT_THROW(d_solver.setInfo("cvc5-lagic", "QF_BV"), CVC5ApiException);
index f967efe3024cc270a3a696c11e9c3cefd9ef1ffb..0bcf3078147180fd0e6396d15a2e887e1df7b807 100644 (file)
@@ -2340,6 +2340,25 @@ class SolverTest
     assertDoesNotThrow(() -> d_solver.blockModelValues(new Term[] {x}));
   }
 
+  @Test
+  void getInstantiations() throws CVC5ApiException
+  {
+    Sort iSort = d_solver.getIntegerSort();
+    Sort boolSort = d_solver.getBooleanSort();
+    Term p = d_solver.declareFun("p", new Sort[] {iSort}, boolSort);
+    Term x = d_solver.mkVar(iSort, "x");
+    Term bvl = d_solver.mkTerm(VARIABLE_LIST, new Term[] {x});
+    Term app = d_solver.mkTerm(APPLY_UF, new Term[] {p, x});
+    Term q = d_solver.mkTerm(FORALL, new Term[] {bvl, app});
+    d_solver.assertFormula(q);
+    Term five = d_solver.mkInteger(5);
+    Term app2 = d_solver.mkTerm(NOT, new Term[] {d_solver.mkTerm(APPLY_UF, new Term[] {p, five})});
+    d_solver.assertFormula(app2);
+    assertThrows(CVC5ApiException.class, () -> d_solver.getInstantiations());
+    d_solver.checkSat();
+    assertDoesNotThrow(() -> d_solver.getInstantiations());
+  }
+
   @Test
   void setInfo() throws CVC5ApiException
   {
index 3939f2d09503102c005fabb5fac095d329f1adae..6705a940f8dcbfd6f144379eb9abc0035cd4c2cf 100644 (file)
@@ -1641,6 +1641,23 @@ def test_block_model_values5(solver):
     solver.checkSat()
     solver.blockModelValues([x])
 
+def getInstantiations():
+    iSort = solver.getIntegerSort()
+    boolSort = solver.getBooleanSort()
+    p = solver.declareFun("p", [iSort], boolSort)
+    x = solver.mkVar(iSort, "x")
+    bvl = solver.mkTerm(Kind.VARIABLE_LIST, [x])
+    app = solver.mkTerm(Kind.APPLY_UF, [p, x])
+    q = solver.mkTerm(Kind.FORALL, [bvl, app]);
+    solver.assertFormula(q)
+    five = solver.mkInteger(5)
+    app2 = solver.mkTerm(Kind.NOT, [solver.mkTerm(Kind.APPLY_UF, [p, five])])
+    solver.assertFormula(app2)
+    with pytest.raises(RuntimeError):
+        solver.getInstantiations()
+    solver.checkSat()
+    solver.getInstantiations()
+
 def test_get_statistics(solver):
     intSort = solver.getIntegerSort()
     x = solver.mkConst(intSort, "x")