Files
cvc5/test/unit/api/cpp/api_op_black.cpp

194 lines
7.1 KiB
C++
Raw Permalink Normal View History

/******************************************************************************
* Top contributors (to current version):
2024-03-12 09:35:09 -07:00
* Aina Niemetz, Andres Noetzli, Gereon Kremer
*
* This file is part of the cvc5 project.
*
* Copyright (c) 2009-2025 by the authors listed in the file AUTHORS
* in the top-level source directory and their institutional affiliations.
* All rights reserved. See the file COPYING in the top-level source
* directory for licensing information.
* ****************************************************************************
*
* Black box testing of the Op class.
*/
#include "test_api.h"
namespace cvc5::internal {
namespace test {
class TestApiBlackOp : public TestApi
{
};
TEST_F(TestApiBlackOp, hash)
{
std::hash<Op> h;
ASSERT_EQ(h(d_tm.mkOp(Kind::BITVECTOR_EXTRACT, {31, 1})),
h(d_tm.mkOp(Kind::BITVECTOR_EXTRACT, {31, 1})));
ASSERT_NE(h(d_tm.mkOp(Kind::BITVECTOR_EXTRACT, {31, 1})),
h(d_tm.mkOp(Kind::BITVECTOR_EXTRACT, {31, 2})));
(void)std::hash<Op>{}(Op());
}
TEST_F(TestApiBlackOp, getKind)
{
Op x = d_tm.mkOp(Kind::BITVECTOR_EXTRACT, {31, 1});
ASSERT_EQ(x.getKind(), Kind::BITVECTOR_EXTRACT);
}
TEST_F(TestApiBlackOp, isNull)
{
OpTerm Refactor: Allow retrieving OpTerm used to create Term in public C++ API (#3355) * Treat uninterpreted functions as a child in Term iteration * Remove unnecessary const_iterator constructor * Add parameter comments to const_iterator constructor * Use operator[] instead of storing a vector of Expr children * Switch pos member variable from int to uint32_t * Add comment about how UFs are treated in iteration * Allow OpTerm to contain a single Kind, update OpTerm construction * Update mkTerm to use only an OpTerm (and not also a Kind) * Remove unnecessary function checkMkOpTerm * Update mkOpTerm comments to not use _OP Kinds * Update examples to use new mkTerm * First pass on fixing unit test * Override kind for Constructor and Selector Terms * More fixes to unit tests * Updates to parser * Remove old assert (for Kind, OpTerm pattern which was removed) * Remove *_OP kinds from public API * Add hasOpTerm and getOpTerm methods to Term * Add test for UF iteration * Add unit test for getOpTerm * Move OpTerm implementation above Term implemenation to match header file Moved in header because Term::getOpTerm() returns an OpTerm and the compiler complains if OpTerm is not defined earlier. Simply moving the declaration is easier/cleaner than forward declaring within the same file that it's declared. * Fix mkTerm in datatypes-new.cpp example * Use helper function for creating term from Kind to avoid nested API calls * Rename: OpTerm->Op in API * Update OpTerm->Op in examples/tests/parser * Add case for APPLY_TESTER * operator term -> operator * Update src/api/cvc4cpp.h Co-Authored-By: Aina Niemetz <aina.niemetz@gmail.com> * Comment comment suggestion Co-Authored-By: Aina Niemetz <aina.niemetz@gmail.com> * Add not-null checks and implement Op from a single Kind constructor * Undo sed mistake for OpTerm replacement * Add 'd_' prefix to member vars * Fix comment and remove old commented-out code * Formatting * Revert "Formatting" This reverts commit d1d5fc1fb71496daeba668e97cad84c213200ba9. * More fixes for sed mistakes * Minor formatting * Undo changes in CVC parser * Add isIndexed and prefix with d_ * Create helper function for isIndexed to avoid calling API functions in other API functions
2019-12-02 13:36:19 -08:00
Op x;
ASSERT_TRUE(x.isNull());
Op y = d_tm.mkOp(Kind::BITVECTOR_EXTRACT, {31, 1});
ASSERT_FALSE(y.isNull());
ASSERT_NE(x, y);
}
TEST_F(TestApiBlackOp, opFromKind)
{
ASSERT_NO_THROW(d_tm.mkOp(Kind::ADD));
ASSERT_THROW(d_tm.mkOp(Kind::BITVECTOR_EXTRACT), CVC5ApiException);
OpTerm Refactor: Allow retrieving OpTerm used to create Term in public C++ API (#3355) * Treat uninterpreted functions as a child in Term iteration * Remove unnecessary const_iterator constructor * Add parameter comments to const_iterator constructor * Use operator[] instead of storing a vector of Expr children * Switch pos member variable from int to uint32_t * Add comment about how UFs are treated in iteration * Allow OpTerm to contain a single Kind, update OpTerm construction * Update mkTerm to use only an OpTerm (and not also a Kind) * Remove unnecessary function checkMkOpTerm * Update mkOpTerm comments to not use _OP Kinds * Update examples to use new mkTerm * First pass on fixing unit test * Override kind for Constructor and Selector Terms * More fixes to unit tests * Updates to parser * Remove old assert (for Kind, OpTerm pattern which was removed) * Remove *_OP kinds from public API * Add hasOpTerm and getOpTerm methods to Term * Add test for UF iteration * Add unit test for getOpTerm * Move OpTerm implementation above Term implemenation to match header file Moved in header because Term::getOpTerm() returns an OpTerm and the compiler complains if OpTerm is not defined earlier. Simply moving the declaration is easier/cleaner than forward declaring within the same file that it's declared. * Fix mkTerm in datatypes-new.cpp example * Use helper function for creating term from Kind to avoid nested API calls * Rename: OpTerm->Op in API * Update OpTerm->Op in examples/tests/parser * Add case for APPLY_TESTER * operator term -> operator * Update src/api/cvc4cpp.h Co-Authored-By: Aina Niemetz <aina.niemetz@gmail.com> * Comment comment suggestion Co-Authored-By: Aina Niemetz <aina.niemetz@gmail.com> * Add not-null checks and implement Op from a single Kind constructor * Undo sed mistake for OpTerm replacement * Add 'd_' prefix to member vars * Fix comment and remove old commented-out code * Formatting * Revert "Formatting" This reverts commit d1d5fc1fb71496daeba668e97cad84c213200ba9. * More fixes for sed mistakes * Minor formatting * Undo changes in CVC parser * Add isIndexed and prefix with d_ * Create helper function for isIndexed to avoid calling API functions in other API functions
2019-12-02 13:36:19 -08:00
}
TEST_F(TestApiBlackOp, getNumIndices)
{
// Operators with 0 indices
Op plus = d_tm.mkOp(Kind::ADD);
ASSERT_EQ(0, plus.getNumIndices());
// Operators with 1 index
Op divisible = d_tm.mkOp(Kind::DIVISIBLE, {4});
Op bvRepeat = d_tm.mkOp(Kind::BITVECTOR_REPEAT, {5});
Op bvZeroExtend = d_tm.mkOp(Kind::BITVECTOR_ZERO_EXTEND, {6});
Op bvSignExtend = d_tm.mkOp(Kind::BITVECTOR_SIGN_EXTEND, {7});
Op bvRotateLeft = d_tm.mkOp(Kind::BITVECTOR_ROTATE_LEFT, {8});
Op bvRotateRight = d_tm.mkOp(Kind::BITVECTOR_ROTATE_RIGHT, {9});
Op intToBv = d_tm.mkOp(Kind::INT_TO_BITVECTOR, {10});
Op iand = d_tm.mkOp(Kind::IAND, {11});
Op fpToUbv = d_tm.mkOp(Kind::FLOATINGPOINT_TO_UBV, {12});
Op fpToSbv = d_tm.mkOp(Kind::FLOATINGPOINT_TO_SBV, {13});
ASSERT_EQ(1, divisible.getNumIndices());
ASSERT_EQ(1, bvRepeat.getNumIndices());
ASSERT_EQ(1, bvZeroExtend.getNumIndices());
ASSERT_EQ(1, bvSignExtend.getNumIndices());
ASSERT_EQ(1, bvRotateLeft.getNumIndices());
ASSERT_EQ(1, bvRotateRight.getNumIndices());
ASSERT_EQ(1, intToBv.getNumIndices());
ASSERT_EQ(1, iand.getNumIndices());
ASSERT_EQ(1, fpToUbv.getNumIndices());
ASSERT_EQ(1, fpToSbv.getNumIndices());
// Operators with 2 indices
Op bvExtract = d_tm.mkOp(Kind::BITVECTOR_EXTRACT, {1, 0});
Op toFpFromIeeeBv = d_tm.mkOp(Kind::FLOATINGPOINT_TO_FP_FROM_IEEE_BV, {3, 2});
Op toFpFromFp = d_tm.mkOp(Kind::FLOATINGPOINT_TO_FP_FROM_FP, {5, 4});
Op toFpFromReal = d_tm.mkOp(Kind::FLOATINGPOINT_TO_FP_FROM_REAL, {7, 6});
Op toFpFromSbv = d_tm.mkOp(Kind::FLOATINGPOINT_TO_FP_FROM_SBV, {9, 8});
Op toFpFromUbv = d_tm.mkOp(Kind::FLOATINGPOINT_TO_FP_FROM_UBV, {11, 10});
Op regexpLoop = d_tm.mkOp(Kind::REGEXP_LOOP, {15, 14});
ASSERT_EQ(2, bvExtract.getNumIndices());
ASSERT_EQ(2, toFpFromIeeeBv.getNumIndices());
ASSERT_EQ(2, toFpFromFp.getNumIndices());
ASSERT_EQ(2, toFpFromReal.getNumIndices());
ASSERT_EQ(2, toFpFromSbv.getNumIndices());
ASSERT_EQ(2, toFpFromUbv.getNumIndices());
ASSERT_EQ(2, regexpLoop.getNumIndices());
// Operators with n indices
std::vector<uint32_t> indices = {0, 3, 2, 0, 1, 2};
Op tupleProject = d_tm.mkOp(Kind::TUPLE_PROJECT, indices);
ASSERT_EQ(indices.size(), tupleProject.getNumIndices());
Op relationProject = d_tm.mkOp(Kind::RELATION_PROJECT, indices);
ASSERT_EQ(indices.size(), relationProject.getNumIndices());
Op tableProject = d_tm.mkOp(Kind::TABLE_PROJECT, indices);
ASSERT_EQ(indices.size(), tableProject.getNumIndices());
}
TEST_F(TestApiBlackOp, subscriptOperator)
{
// Operators with 0 indices
Op plus = d_tm.mkOp(Kind::ADD);
ASSERT_THROW(plus[0], CVC5ApiException);
// Operators with 1 index
Op divisible = d_tm.mkOp(Kind::DIVISIBLE, {4});
Op bvRepeat = d_tm.mkOp(Kind::BITVECTOR_REPEAT, {5});
Op bvZeroExtend = d_tm.mkOp(Kind::BITVECTOR_ZERO_EXTEND, {6});
Op bvSignExtend = d_tm.mkOp(Kind::BITVECTOR_SIGN_EXTEND, {7});
Op bvRotateLeft = d_tm.mkOp(Kind::BITVECTOR_ROTATE_LEFT, {8});
Op bvRotateRight = d_tm.mkOp(Kind::BITVECTOR_ROTATE_RIGHT, {9});
Op intToBv = d_tm.mkOp(Kind::INT_TO_BITVECTOR, {10});
Op iand = d_tm.mkOp(Kind::IAND, {11});
Op fpToUbv = d_tm.mkOp(Kind::FLOATINGPOINT_TO_UBV, {12});
Op fpToSbv = d_tm.mkOp(Kind::FLOATINGPOINT_TO_SBV, {13});
Op regexpRepeat = d_tm.mkOp(Kind::REGEXP_REPEAT, {14});
ASSERT_EQ(4, divisible[0].getUInt32Value());
ASSERT_EQ(5, bvRepeat[0].getUInt32Value());
ASSERT_EQ(6, bvZeroExtend[0].getUInt32Value());
ASSERT_EQ(7, bvSignExtend[0].getUInt32Value());
ASSERT_EQ(8, bvRotateLeft[0].getUInt32Value());
ASSERT_EQ(9, bvRotateRight[0].getUInt32Value());
ASSERT_EQ(10, intToBv[0].getUInt32Value());
ASSERT_EQ(11, iand[0].getUInt32Value());
ASSERT_EQ(12, fpToUbv[0].getUInt32Value());
ASSERT_EQ(13, fpToSbv[0].getUInt32Value());
ASSERT_EQ(14, regexpRepeat[0].getUInt32Value());
// Operators with 2 indices
Op bvExtract = d_tm.mkOp(Kind::BITVECTOR_EXTRACT, {1, 0});
Op toFpFromIeeeBv = d_tm.mkOp(Kind::FLOATINGPOINT_TO_FP_FROM_IEEE_BV, {3, 2});
Op toFpFromFp = d_tm.mkOp(Kind::FLOATINGPOINT_TO_FP_FROM_FP, {5, 4});
Op toFpFromReal = d_tm.mkOp(Kind::FLOATINGPOINT_TO_FP_FROM_REAL, {7, 6});
Op toFpFromSbv = d_tm.mkOp(Kind::FLOATINGPOINT_TO_FP_FROM_SBV, {9, 8});
Op toFpFromUbv = d_tm.mkOp(Kind::FLOATINGPOINT_TO_FP_FROM_UBV, {11, 10});
Op regexpLoop = d_tm.mkOp(Kind::REGEXP_LOOP, {15, 14});
ASSERT_EQ(1, bvExtract[0].getUInt32Value());
ASSERT_EQ(0, bvExtract[1].getUInt32Value());
ASSERT_EQ(3, toFpFromIeeeBv[0].getUInt32Value());
ASSERT_EQ(2, toFpFromIeeeBv[1].getUInt32Value());
ASSERT_EQ(5, toFpFromFp[0].getUInt32Value());
ASSERT_EQ(4, toFpFromFp[1].getUInt32Value());
ASSERT_EQ(7, toFpFromReal[0].getUInt32Value());
ASSERT_EQ(6, toFpFromReal[1].getUInt32Value());
ASSERT_EQ(9, toFpFromSbv[0].getUInt32Value());
ASSERT_EQ(8, toFpFromSbv[1].getUInt32Value());
ASSERT_EQ(11, toFpFromUbv[0].getUInt32Value());
ASSERT_EQ(10, toFpFromUbv[1].getUInt32Value());
ASSERT_EQ(15, regexpLoop[0].getUInt32Value());
ASSERT_EQ(14, regexpLoop[1].getUInt32Value());
// Operators with n indices
std::vector<uint32_t> indices = {0, 3, 2, 0, 1, 2};
Op tupleProject = d_tm.mkOp(Kind::TUPLE_PROJECT, indices);
for (size_t i = 0, size = tupleProject.getNumIndices(); i < size; i++)
{
ASSERT_EQ(indices[i], tupleProject[i].getUInt32Value());
}
}
TEST_F(TestApiBlackOp, opScopingToString)
{
Op bitvector_repeat_ot = d_tm.mkOp(Kind::BITVECTOR_REPEAT, {5});
std::string op_repr = bitvector_repeat_ot.toString();
ASSERT_EQ(bitvector_repeat_ot.toString(), op_repr);
{
std::stringstream ss;
ss << bitvector_repeat_ot;
ASSERT_EQ(ss.str(), op_repr);
}
}
} // namespace test
} // namespace cvc5::internal