Java and python unit tests for mkCardinalityConstraint (#7486)
authorAndrew Reynolds <andrew.j.reynolds@gmail.com>
Mon, 25 Oct 2021 20:23:31 +0000 (15:23 -0500)
committerGitHub <noreply@github.com>
Mon, 25 Oct 2021 20:23:31 +0000 (20:23 +0000)
Adds leftover missing unit tests for new API call for mkCardinalityConstraint from eeb78c8.

src/api/cpp/cvc5.cpp
src/api/cpp/cvc5.h
src/api/java/io/github/cvc5/api/Solver.java
src/api/java/jni/solver.cpp
src/api/python/cvc5.pxd
src/api/python/cvc5.pxi
test/python/unit/api/test_solver.py
test/unit/api/java/SolverTest.java

index 280b07bb2dafe14feab22b3e9036419776c6f826..5e38096c830ca938afd12c3d88323ffe1f99b28b 100644 (file)
@@ -6054,16 +6054,16 @@ Term Solver::mkFloatingPoint(uint32_t exp, uint32_t sig, Term val) const
   CVC5_API_TRY_CATCH_END;
 }
 
-Term Solver::mkCardinalityConstraint(const Sort& sort, uint32_t ubound) const
+Term Solver::mkCardinalityConstraint(const Sort& sort, uint32_t upperBound) const
 {
   CVC5_API_TRY_CATCH_BEGIN;
   CVC5_API_SOLVER_CHECK_SORT(sort);
   CVC5_API_ARG_CHECK_EXPECTED(sort.isUninterpretedSort(), sort)
       << "an uninterpreted sort";
-  CVC5_API_ARG_CHECK_EXPECTED(ubound > 0, ubound) << "a value > 0";
+  CVC5_API_ARG_CHECK_EXPECTED(upperBound > 0, upperBound) << "a value > 0";
   //////// all checks before this line
   Node cco =
-      d_nodeMgr->mkConst(cvc5::CardinalityConstraint(*sort.d_type, ubound));
+      d_nodeMgr->mkConst(cvc5::CardinalityConstraint(*sort.d_type, upperBound));
   Node cc = d_nodeMgr->mkNode(cvc5::Kind::CARDINALITY_CONSTRAINT, cco);
   return Term(this, cc);
   ////////
index 57c3f311dfbb80ab91831c04862c3d61aec8c6b1..c618106a6ff30d964c122bcd731fbf62b10ce66b 100644 (file)
@@ -3600,10 +3600,10 @@ class CVC5_EXPORT Solver
   /**
    * Create a cardinality constraint for an uninterpreted sort.
    * @param sort the sort the cardinality constraint is for
-   * @param val the upper bound on the cardinality of the sort
+   * @param upperBound the upper bound on the cardinality of the sort
    * @return the cardinality constraint
    */
-  Term mkCardinalityConstraint(const Sort& sort, uint32_t ubound) const;
+  Term mkCardinalityConstraint(const Sort& sort, uint32_t upperBound) const;
 
   /* .................................................................... */
   /* Create Variables                                                     */
index 670ab7cbd8ca2ebad44f175d2400513987e29264..3a9f450a438f359ee06d158c4e6847392cabedd9 100644 (file)
@@ -1184,6 +1184,21 @@ public class Solver implements IPointer
 
   private native long mkFloatingPoint(long pointer, int exp, int sig, long valPointer);
 
+  /**
+   * Create a cardinality constraint for an uninterpreted sort.
+   * @param sort the sort the cardinality constraint is for
+   * @param upperBound the upper bound on the cardinality of the sort
+   * @return the cardinality constraint
+   */
+  public Term mkCardinalityConstraint(Sort sort, int upperBound) throws CVC5ApiException
+  {
+    Utils.validateUnsigned(upperBound, "upperBound");
+    long termPointer = mkCardinalityConstraint(pointer, sort.getPointer(), upperBound);
+    return new Term(this, termPointer);
+  }
+
+  private native long mkCardinalityConstraint(long pointer, long sortPointer, int upperBound);
+
   /* .................................................................... */
   /* Create Variables                                                     */
   /* .................................................................... */
index 882a747945e33d5427077bd13af77ce4a1d39ff9..af3d7e59e8a59e1758ec6daa1e207f401e42729c 100644 (file)
@@ -1331,6 +1331,23 @@ JNIEXPORT jlong JNICALL Java_io_github_cvc5_api_Solver_mkFloatingPoint(
   CVC5_JAVA_API_TRY_CATCH_END_RETURN(env, 0);
 }
 
+/*
+ * Class:     io_github_cvc5_api_Solver
+ * Method:    mkCardinalityConstraint
+ * Signature: (JJI)J
+ */
+JNIEXPORT jlong JNICALL Java_io_github_cvc5_api_Solver_mkCardinalityConstraint(
+    JNIEnv* env, jobject, jlong pointer, jlong sortPointer, jint upperBound)
+{
+  CVC5_JAVA_API_TRY_CATCH_BEGIN;
+  Solver* solver = reinterpret_cast<Solver*>(pointer);
+  Sort* sort = reinterpret_cast<Sort*>(sortPointer);
+  Term* retPointer =
+      new Term(solver->mkCardinalityConstraint(*sort, (int32_t)upperBound));
+  return reinterpret_cast<jlong>(retPointer);
+  CVC5_JAVA_API_TRY_CATCH_END_RETURN(env, 0);
+}
+
 /*
  * Class:     io_github_cvc5_api_Solver
  * Method:    mkConst
@@ -2707,4 +2724,4 @@ Java_io_github_cvc5_api_Solver_getNullDatatypeDecl(JNIEnv* env, jobject, jlong)
   DatatypeDecl* ret = new DatatypeDecl();
   return reinterpret_cast<jlong>(ret);
   CVC5_JAVA_API_TRY_CATCH_END_RETURN(env, 0);
-}
\ No newline at end of file
+}
index 02b5721209fc8284bbc7ff8906434b723b1e82b5..cf405b5194cfac146301e295ae21a1a501cafb32 100644 (file)
@@ -241,6 +241,7 @@ cdef extern from "api/cpp/cvc5.h" namespace "cvc5::api":
         Term mkUninterpretedConst(Sort sort, int32_t index) except +
         Term mkAbstractValue(const string& index) except +
         Term mkFloatingPoint(uint32_t exp, uint32_t sig, Term val) except +
+        Term mkCardinalityConstraint(Sort sort, int32_t index) except +
         Term mkConst(Sort sort, const string& symbol) except +
         # default value for symbol defined in cpp/cvc5.h
         Term mkConst(Sort sort) except +
index 4627859b9b6210e27452d8a5161a4ff321e169e2..9391dbdd1ddee27926ed25c954af6ab3dc06afb5 100644 (file)
@@ -1391,6 +1391,16 @@ cdef class Solver:
         term.cterm = self.csolver.mkFloatingPoint(exp, sig, val.cterm)
         return term
 
+    def mkCardinalityConstraint(self, Sort sort, int index):
+        """Create cardinality constraint.
+
+        :param sort: Sort of the constraint
+        :param index: The upper bound for the cardinality of the sort
+        """
+        cdef Term term = Term(self)
+        term.cterm = self.csolver.mkCardinalityConstraint(sort.csort, index)
+        return term
+
     def mkConst(self, Sort sort, symbol=None):
         """
         Create (first-order) constant (0-arity function symbol).
index 6052a057fda5ab81d9ecc35b48481e630b23d6c2..04a2757414824f6038f236264a15ac8edb51a577 100644 (file)
@@ -422,6 +422,17 @@ def test_mk_floating_point(solver):
     with pytest.raises(RuntimeError):
         slv.mkFloatingPoint(3, 5, t1)
 
+def test_mk_cardinality_constraint(solver):
+    su = solver.mkUninterpretedSort("u")
+    si = solver.getIntegerSort()
+    solver.mkCardinalityConstraint(su, 3)
+    with pytest.raises(RuntimeError):
+        solver.mkEmptySet(solver.mkCardinalityConstraint(si, 3))
+    with pytest.raises(RuntimeError):
+        solver.mkEmptySet(solver.mkCardinalityConstraint(su, 0))
+    slv = pycvc5.Solver()
+    with pytest.raises(RuntimeError):
+        slv.mkCardinalityConstraint(su, 3)
 
 def test_mk_empty_set(solver):
     slv = pycvc5.Solver()
index 8511827a82a8afc1c4c2e1917b228759d86fa4c2..1f88add2d154cb3547d10f85771e31c75a3d790a 100644 (file)
@@ -387,16 +387,6 @@ class SolverTest
     assertDoesNotThrow(() -> d_solver.mkRoundingMode(RoundingMode.ROUND_TOWARD_ZERO));
   }
 
-  @Test void mkUninterpretedConst() throws CVC5ApiException
-  {
-    assertDoesNotThrow(() -> d_solver.mkUninterpretedConst(d_solver.getBooleanSort(), 1));
-    assertThrows(
-        CVC5ApiException.class, () -> d_solver.mkUninterpretedConst(d_solver.getNullSort(), 1));
-    Solver slv = new Solver();
-    assertThrows(
-        CVC5ApiException.class, () -> slv.mkUninterpretedConst(d_solver.getBooleanSort(), 1));
-  }
-
   @Test void mkAbstractValue() throws CVC5ApiException
   {
     assertDoesNotThrow(() -> d_solver.mkAbstractValue(("1")));
@@ -434,6 +424,20 @@ class SolverTest
     assertThrows(CVC5ApiException.class, () -> slv.mkFloatingPoint(3, 5, t1));
   }
 
+  @Test void mkCardinalityConstraint() throws CVC5ApiException
+  {
+    Sort su = d_solver.mkUninterpretedSort("u");
+    Sort si = d_solver.getIntegerSort();
+    assertDoesNotThrow(() -> d_solver.mkCardinalityConstraint(su, 3));
+    assertThrows(
+        CVC5ApiException.class, () -> d_solver.mkCardinalityConstraint(si, 3));
+    assertThrows(
+        CVC5ApiException.class, () -> d_solver.mkCardinalityConstraint(su, 0));
+    Solver slv = new Solver();
+    assertThrows(
+        CVC5ApiException.class, () -> slv.mkCardinalityConstraint(su, 3));
+  }
+
   @Test void mkEmptySet() throws CVC5ApiException
   {
     Solver slv = new Solver();