GCC Code Coverage Report
Directory: . Exec Total Coverage
File: test/unit/test_smt.h Lines: 40 70 57.1 %
Date: 2021-08-01 Branches: 30 151 19.9 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Aina Niemetz, Andrew Reynolds, Haniel Barbosa
4
 *
5
 * This file is part of the cvc5 project.
6
 *
7
 * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8
 * in the top-level source directory and their institutional affiliations.
9
 * All rights reserved.  See the file COPYING in the top-level source
10
 * directory for licensing information.
11
 * ****************************************************************************
12
 *
13
 * Common header for unit tests that need an SmtEngine.
14
 */
15
16
#ifndef CVC5__TEST__UNIT__TEST_SMT_H
17
#define CVC5__TEST__UNIT__TEST_SMT_H
18
19
#include "expr/dtype_cons.h"
20
#include "expr/node.h"
21
#include "expr/node_manager.h"
22
#include "expr/skolem_manager.h"
23
#include "proof/proof_checker.h"
24
#include "smt/smt_engine.h"
25
#include "smt/smt_engine_scope.h"
26
#include "test.h"
27
#include "theory/output_channel.h"
28
#include "theory/rewriter.h"
29
#include "theory/theory.h"
30
#include "theory/theory_state.h"
31
#include "theory/valuation.h"
32
#include "util/resource_manager.h"
33
#include "util/unsafe_interrupt_exception.h"
34
35
namespace cvc5 {
36
namespace test {
37
38
/* -------------------------------------------------------------------------- */
39
/* Test fixtures.                                                             */
40
/* -------------------------------------------------------------------------- */
41
42
640
class TestSmt : public TestInternal
43
{
44
 protected:
45
320
  void SetUp() override
46
  {
47
320
    d_nodeManager.reset(new NodeManager());
48
320
    d_skolemManager = d_nodeManager->getSkolemManager();
49
320
    d_nmScope.reset(new NodeManagerScope(d_nodeManager.get()));
50
320
    d_smtEngine.reset(new SmtEngine(d_nodeManager.get()));
51
320
    d_smtEngine->finishInit();
52
320
  }
53
54
  std::unique_ptr<NodeManagerScope> d_nmScope;
55
  std::unique_ptr<NodeManager> d_nodeManager;
56
  SkolemManager* d_skolemManager;
57
  std::unique_ptr<SmtEngine> d_smtEngine;
58
};
59
60
1104
class TestSmtNoFinishInit : public TestInternal
61
{
62
 protected:
63
552
  void SetUp() override
64
  {
65
552
    d_nodeManager.reset(new NodeManager());
66
552
    d_skolemManager = d_nodeManager->getSkolemManager();
67
552
    d_nmScope.reset(new NodeManagerScope(d_nodeManager.get()));
68
552
    d_smtEngine.reset(new SmtEngine(d_nodeManager.get()));
69
552
  }
70
71
  std::unique_ptr<NodeManagerScope> d_nmScope;
72
  std::unique_ptr<NodeManager> d_nodeManager;
73
  SkolemManager* d_skolemManager;
74
  std::unique_ptr<SmtEngine> d_smtEngine;
75
};
76
77
/* -------------------------------------------------------------------------- */
78
/* Helpers.                                                                   */
79
/* -------------------------------------------------------------------------- */
80
81
/**
82
 * Very basic OutputChannel for testing simple Theory Behaviour.
83
 * Stores a call sequence for the output channel
84
 */
85
enum OutputChannelCallType
86
{
87
  CONFLICT,
88
  PROPAGATE,
89
  PROPAGATE_AS_DECISION,
90
  AUG_LEMMA,
91
  LEMMA,
92
  EXPLANATION
93
};
94
95
inline std::ostream& operator<<(std::ostream& out, OutputChannelCallType type)
96
{
97
  switch (type)
98
  {
99
    case CONFLICT: return out << "CONFLICT";
100
    case PROPAGATE: return out << "PROPAGATE";
101
    case PROPAGATE_AS_DECISION: return out << "PROPAGATE_AS_DECISION";
102
    case AUG_LEMMA: return out << "AUG_LEMMA";
103
    case LEMMA: return out << "LEMMA";
104
    case EXPLANATION: return out << "EXPLANATION";
105
    default: return out << "UNDEFINED-OutputChannelCallType!" << int(type);
106
  }
107
}
108
109
class DummyOutputChannel : public cvc5::theory::OutputChannel
110
{
111
 public:
112
6
  DummyOutputChannel() {}
113
6
  ~DummyOutputChannel() override {}
114
115
  void safePoint(Resource r) override {}
116
  void conflict(TNode n) override { push(CONFLICT, n); }
117
118
  void trustedConflict(TrustNode n) override { push(CONFLICT, n.getNode()); }
119
120
  bool propagate(TNode n) override
121
  {
122
    push(PROPAGATE, n);
123
    return true;
124
  }
125
126
2
  void lemma(TNode n,
127
             theory::LemmaProperty p = theory::LemmaProperty::NONE) override
128
  {
129
2
    push(LEMMA, n);
130
2
  }
131
132
  void trustedLemma(TrustNode n, theory::LemmaProperty p) override
133
  {
134
    push(LEMMA, n.getNode());
135
  }
136
137
  void requirePhase(TNode, bool) override {}
138
  void setIncomplete(theory::IncompleteId id) override {}
139
  void handleUserAttribute(const char* attr, theory::Theory* t) override {}
140
141
2
  void splitLemma(TNode n, bool removable = false) override { push(LEMMA, n); }
142
143
6
  void clear() { d_callHistory.clear(); }
144
145
  Node getIthNode(int i) const
146
  {
147
    Node tmp = (d_callHistory[i]).second;
148
    return tmp;
149
  }
150
151
  OutputChannelCallType getIthCallType(int i) const
152
  {
153
    return (d_callHistory[i]).first;
154
  }
155
156
  unsigned getNumCalls() const { return d_callHistory.size(); }
157
158
  void printIth(std::ostream& os, int i) const
159
  {
160
    os << "[DummyOutputChannel " << i;
161
    os << " " << getIthCallType(i);
162
    os << " " << getIthNode(i) << "]";
163
  }
164
165
 private:
166
4
  void push(OutputChannelCallType call, TNode n)
167
  {
168
4
    d_callHistory.push_back(std::make_pair(call, n));
169
4
  }
170
171
  std::vector<std::pair<enum OutputChannelCallType, Node> > d_callHistory;
172
};
173
174
/* -------------------------------------------------------------------------- */
175
176
84
class DummyTheoryRewriter : public theory::TheoryRewriter
177
{
178
 public:
179
48
  theory::RewriteResponse preRewrite(TNode n) override
180
  {
181
48
    return theory::RewriteResponse(theory::REWRITE_DONE, n);
182
  }
183
184
96
  theory::RewriteResponse postRewrite(TNode n) override
185
  {
186
96
    return theory::RewriteResponse(theory::REWRITE_DONE, n);
187
  }
188
};
189
190
class DummyProofRuleChecker : public ProofRuleChecker
191
{
192
 public:
193
42
  DummyProofRuleChecker() {}
194
42
  ~DummyProofRuleChecker() {}
195
  void registerTo(ProofChecker* pc) override {}
196
197
 protected:
198
  Node checkInternal(PfRule id,
199
                     const std::vector<Node>& children,
200
                     const std::vector<Node>& args) override
201
  {
202
    return Node::null();
203
  }
204
};
205
206
/** Dummy Theory interface.  */
207
template <theory::TheoryId theoryId>
208
84
class DummyTheory : public theory::Theory
209
{
210
 public:
211
42
  DummyTheory(context::Context* ctxt,
212
              context::UserContext* uctxt,
213
              theory::OutputChannel& out,
214
              theory::Valuation valuation,
215
              const LogicInfo& logicInfo,
216
              ProofNodeManager* pnm)
217
      : Theory(theoryId, ctxt, uctxt, out, valuation, logicInfo, pnm),
218
42
        d_state(ctxt, uctxt, valuation)
219
  {
220
    // use a default theory state object
221
42
    d_theoryState = &d_state;
222
42
  }
223
224
36
  theory::TheoryRewriter* getTheoryRewriter() override { return &d_rewriter; }
225
  ProofRuleChecker* getProofChecker() override { return &d_checker; }
226
227
  void registerTerm(TNode n)
228
  {
229
    // check that we registerTerm() a term only once
230
    ASSERT_TRUE(d_registered.find(n) == d_registered.end());
231
232
    for (TNode::iterator i = n.begin(); i != n.end(); ++i)
233
    {
234
      // check that registerTerm() is called in reverse topological order
235
      ASSERT_TRUE(d_registered.find(*i) != d_registered.end());
236
    }
237
238
    d_registered.insert(n);
239
  }
240
241
  void presolve() override { Unimplemented(); }
242
  void preRegisterTerm(TNode n) override { Unimplemented(); }
243
  void propagate(Effort level) override { Unimplemented(); }
244
4
  bool preNotifyFact(
245
      TNode atom, bool pol, TNode fact, bool isPrereg, bool isInternal) override
246
  {
247
    // do not assert to equality engine, since this theory does not use one
248
4
    return true;
249
  }
250
  TrustNode explain(TNode n) override { return TrustNode::null(); }
251
  Node getValue(TNode n) { return Node::null(); }
252
  std::string identify() const override { return "DummyTheory" + d_id; }
253
254
  std::set<Node> d_registered;
255
256
 private:
257
  /** Default theory state object */
258
  theory::TheoryState d_state;
259
  /**
260
   * This fake theory class is equally useful for bool, uf, arith, etc.  It
261
   * keeps an identifier to identify itself.
262
   */
263
  std::string d_id;
264
  /** The theory rewriter for this theory. */
265
  DummyTheoryRewriter d_rewriter;
266
  /** The proof checker for this theory. */
267
  DummyProofRuleChecker d_checker;
268
};
269
270
/* -------------------------------------------------------------------------- */
271
}  // namespace test
272
}  // namespace cvc5
273
#endif