File: api_proof_black.cpp

package info (click to toggle)
cvc5 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 87,260 kB
  • sloc: cpp: 383,850; java: 12,207; python: 12,090; sh: 5,679; ansic: 4,729; lisp: 763; perl: 208; makefile: 38
file content (148 lines) | stat: -rw-r--r-- 4,379 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/******************************************************************************
 * Top contributors (to current version):
 *   Abdalrhman Mohamed, Hans-Joerg Schurr, Aina Niemetz
 *
 * 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 guards of the C++ API functions.
 */

#include <gtest/gtest.h>

#include "test_api.h"

namespace cvc5::internal {

namespace test {

class TestApiBlackProof : public TestApi
{
 protected:
  Proof createProof()
  {
    d_solver->setOption("produce-proofs", "true");

    Sort uSort = d_tm.mkUninterpretedSort("u");
    Sort intSort = d_tm.getIntegerSort();
    Sort boolSort = d_tm.getBooleanSort();
    Sort uToIntSort = d_tm.mkFunctionSort({uSort}, intSort);
    Sort intPredSort = d_tm.mkFunctionSort({intSort}, boolSort);
    std::vector<Proof> proof;

    Term x = d_tm.mkConst(uSort, "x");
    Term y = d_tm.mkConst(uSort, "y");
    Term f = d_tm.mkConst(uToIntSort, "f");
    Term p = d_tm.mkConst(intPredSort, "p");
    Term zero = d_tm.mkInteger(0);
    Term one = d_tm.mkInteger(1);
    Term f_x = d_tm.mkTerm(Kind::APPLY_UF, {f, x});
    Term f_y = d_tm.mkTerm(Kind::APPLY_UF, {f, y});
    Term sum = d_tm.mkTerm(Kind::ADD, {f_x, f_y});
    Term p_0 = d_tm.mkTerm(Kind::APPLY_UF, {p, zero});
    Term p_f_y = d_tm.mkTerm(Kind::APPLY_UF, {p, f_y});
    d_solver->assertFormula(d_tm.mkTerm(Kind::GT, {zero, f_x}));
    d_solver->assertFormula(d_tm.mkTerm(Kind::GT, {zero, f_y}));
    d_solver->assertFormula(d_tm.mkTerm(Kind::GT, {sum, one}));
    d_solver->assertFormula(p_0);
    d_solver->assertFormula(p_f_y.notTerm());
    d_solver->checkSat();

    return d_solver->getProof().front();
  }

  Proof createRewriteProof()
  {
    d_solver->setOption("produce-proofs", "true");
    d_solver->setOption("proof-granularity", "dsl-rewrite");
    Sort intSort = d_tm.getIntegerSort();
    Term x = d_tm.mkConst(intSort, "x");
    Term zero = d_tm.mkInteger(0);
    Term geq = d_tm.mkTerm(Kind::GEQ, {x, zero});
    Term leq = d_tm.mkTerm(Kind::LEQ, {zero, x});
    d_solver->assertFormula(d_tm.mkTerm(Kind::DISTINCT, {geq, leq}));
    d_solver->checkSat();
    return d_solver->getProof().front();
  }
};

TEST_F(TestApiBlackProof, nullProof)
{
  Proof proof;
  ASSERT_EQ(proof.getRule(), ProofRule::UNKNOWN);
  ASSERT_EQ(std::hash<cvc5::ProofRule>()(ProofRule::UNKNOWN),
            static_cast<size_t>(ProofRule::UNKNOWN));
  ASSERT_TRUE(proof.getResult().isNull());
  ASSERT_TRUE(proof.getChildren().empty());
  ASSERT_TRUE(proof.getArguments().empty());
}

TEST_F(TestApiBlackProof, getRule)
{
  Proof proof = createProof();
  ASSERT_EQ(proof.getRule(), ProofRule::SCOPE);
}

TEST_F(TestApiBlackProof, getRewriteRule)
{
  Proof proof = createRewriteProof();
  ASSERT_THROW(proof.getRewriteRule(), CVC5ApiException);
  ProofRule rule;
  std::vector<Proof> stack;
  stack.push_back(proof);
  do
  {
    proof = stack.back();
    stack.pop_back();
    rule = proof.getRule();
    std::vector<Proof> children = proof.getChildren();
    stack.insert(stack.cend(), children.cbegin(), children.cend());
  } while (rule != ProofRule::DSL_REWRITE);
  ASSERT_NO_THROW(proof.getRewriteRule());
}

TEST_F(TestApiBlackProof, getResult)
{
  Proof proof = createProof();
  ASSERT_NO_THROW(proof.getResult());
}

TEST_F(TestApiBlackProof, getChildren)
{
  Proof proof = createProof();
  std::vector<Proof> children;
  ASSERT_NO_THROW(children = proof.getChildren());
  ASSERT_FALSE(children.empty());
}

TEST_F(TestApiBlackProof, getArguments)
{
  Proof proof = createProof();
  ASSERT_NO_THROW(proof.getArguments());
}

TEST_F(TestApiBlackProof, equalhash)
{
  Proof x = createProof();
  Proof y = x.getChildren()[0];
  Proof z;

  ASSERT_TRUE(x == x);
  ASSERT_FALSE(x != x);
  ASSERT_FALSE(x == y);
  ASSERT_TRUE(x != y);
  ASSERT_FALSE(x == z);
  ASSERT_TRUE(x != z);

  ASSERT_TRUE(std::hash<Proof>()(x) == std::hash<Proof>()(x));
  (void)std::hash<Proof>{}(Proof());
  ASSERT_FALSE(std::hash<Proof>()(x) == std::hash<Proof>()(y));
}

}  // namespace test
}  // namespace cvc5::internal