api: Add missing guard for Datatype::isFinite(). (#7879)
authorAina Niemetz <aina.niemetz@gmail.com>
Wed, 5 Jan 2022 20:23:17 +0000 (12:23 -0800)
committerGitHub <noreply@github.com>
Wed, 5 Jan 2022 20:23:17 +0000 (20:23 +0000)
src/api/cpp/cvc5.cpp
test/unit/api/cpp/datatype_api_black.cpp
test/unit/api/java/DatatypeTest.java
test/unit/api/python/test_datatype_api.py

index e794606aa450209f70da054be90a3e79bc370503..65ea108df19e4164405bc0cf3180d55eb371270e 100644 (file)
@@ -4193,6 +4193,8 @@ bool Datatype::isFinite() const
 {
   CVC5_API_TRY_CATCH_BEGIN;
   CVC5_API_CHECK_NOT_NULL;
+  CVC5_API_CHECK(!d_dtype->isParametric())
+      << "Invalid call to 'isFinite()', expected non-parametric Datatype";
   //////// all checks before this line
   // we assume that finite model finding is disabled by passing false as the
   // second argument
index bf4a2cd118f43ea5c62c30237620941db1f722ca..f241c4821af4dc79ff11fea05715acf9eb8b2898 100644 (file)
@@ -337,6 +337,25 @@ TEST_F(TestApiBlackDatatype, parametricDatatype)
   ASSERT_TRUE(pairIntInt.isSubsortOf(pairIntInt));
 }
 
+TEST_F(TestApiBlackDatatype, isFinite)
+{
+  DatatypeDecl dtypedecl = d_solver.mkDatatypeDecl("dt", {});
+  DatatypeConstructorDecl ctordecl = d_solver.mkDatatypeConstructorDecl("cons");
+  ctordecl.addSelector("sel", d_solver.getBooleanSort());
+  dtypedecl.addConstructor(ctordecl);
+  Sort dtype = d_solver.mkDatatypeSort(dtypedecl);
+  ASSERT_TRUE(dtype.getDatatype().isFinite());
+
+  Sort p = d_solver.mkParamSort("p1");
+  DatatypeDecl pdtypedecl = d_solver.mkDatatypeDecl("dt", {p});
+  DatatypeConstructorDecl pctordecl =
+      d_solver.mkDatatypeConstructorDecl("cons");
+  pctordecl.addSelector("sel", p);
+  pdtypedecl.addConstructor(pctordecl);
+  Sort pdtype = d_solver.mkDatatypeSort(pdtypedecl);
+  ASSERT_THROW(pdtype.getDatatype().isFinite(), CVC5ApiException);
+}
+
 TEST_F(TestApiBlackDatatype, datatypeSimplyRec)
 {
   /* Create mutual datatypes corresponding to this definition block:
index afca8a92f0a77d47a8c99776fa3b8e1ccd2973fa..246e75ac1527f56fd65ba6b0e1faf6ebb6ff26f9 100644 (file)
@@ -310,6 +310,26 @@ class DatatypeTest
     assertTrue(pairIntInt.isSubsortOf(pairIntInt));
   }
 
+  @Test void datatypeIsFinite() throws CVC5ApiException
+  {
+    List<Sort> v = new ArrayList<>();
+    DatatypeDecl dtypedecl = d_solver.mkDatatypeDecl("dt", v);
+    DatatypeConstructorDecl ctordecl = d_solver.mkDatatypeConstructorDecl("cons");
+    ctordecl.addSelector("sel", d_solver.getBooleanSort());
+    dtypedecl.addConstructor(ctordecl);
+    Sort dtype = d_solver.mkDatatypeSort(dtypedecl);
+    assertTrue(dtype.getDatatype().isFinite());
+
+    Sort p = d_solver.mkParamSort("p1");
+    v.add(p);
+    DatatypeDecl pdtypedecl = d_solver.mkDatatypeDecl("dt", v);
+    DatatypeConstructorDecl pctordecl = d_solver.mkDatatypeConstructorDecl("cons");
+    pctordecl.addSelector("sel", p);
+    pdtypedecl.addConstructor(pctordecl);
+    Sort pdtype = d_solver.mkDatatypeSort(pdtypedecl);
+    assertThrows(CVC5ApiException.class, () -> pdtype.getDatatype().isFinite());
+  }
+
   @Test void datatypeSimplyRec() throws CVC5ApiException
   {
     /* Create mutual datatypes corresponding to this definition block:
index a883596deda054d2ce671b086f9abbc78a63253c..d0cf6d4e50cc3a42edf219e335fa62188f3dea8b 100644 (file)
@@ -264,7 +264,7 @@ def test_parametric_datatype(solver):
     v.append(t1)
     v.append(t2)
     pairSpec = solver.mkDatatypeDecl("pair", v)
-        
+
     mkpair = solver.mkDatatypeConstructorDecl("mk-pair")
     mkpair.addSelector("first", t1)
     mkpair.addSelector("second", t2)
@@ -334,6 +334,22 @@ def test_parametric_datatype(solver):
     assert not pairRealInt.isSubsortOf(pairIntInt)
     assert pairIntInt.isSubsortOf(pairIntInt)
 
+def test_is_finite(solver):
+    dtypedecl = solver.mkDatatypeDecl("dt", [])
+    ctordecl = solver.mkDatatypeConstructorDecl("cons")
+    ctordecl.addSelector("sel", solver.getBooleanSort())
+    dtypedecl.addConstructor(ctordecl)
+    dtype = solver.mkDatatypeSort(dtypedecl)
+    assert dtype.getDatatype().isFinite()
+
+    p = solver.mkParamSort("p1")
+    pdtypedecl = solver.mkDatatypeDecl("dt", [p])
+    pctordecl = solver.mkDatatypeConstructorDecl("cons")
+    pctordecl.addSelector("sel", p)
+    pdtypedecl.addConstructor(pctordecl)
+    pdtype = solver.mkDatatypeSort(pdtypedecl)
+    with pytest.raises(RuntimeError):
+        pdtype.getDatatype().isFinite()
 
 def test_datatype_simply_rec(solver):
     # Create mutual datatypes corresponding to this definition block: