GCC Code Coverage Report
Directory: . Exec Total Coverage
File: test/unit/main/interactive_shell_black.cpp Lines: 56 56 100.0 %
Date: 2021-09-18 Branches: 46 104 44.2 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Aina Niemetz, Christopher L. Conway, Andrew Reynolds
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
 * Black box testing of cvc5::InteractiveShell.
14
 */
15
16
#include <sstream>
17
#include <vector>
18
19
#include "api/cpp/cvc5.h"
20
#include "expr/symbol_manager.h"
21
#include "main/interactive_shell.h"
22
#include "options/base_options.h"
23
#include "options/language.h"
24
#include "options/options.h"
25
#include "parser/parser_builder.h"
26
#include "smt/command.h"
27
#include "test.h"
28
29
namespace cvc5 {
30
namespace test {
31
32
24
class TestMainBlackInteractiveShell : public TestInternal
33
{
34
 protected:
35
12
  void SetUp() override
36
  {
37
12
    TestInternal::SetUp();
38
39
12
    d_sin = std::make_unique<std::stringstream>();
40
12
    d_sout = std::make_unique<std::stringstream>();
41
42
12
    d_solver.reset(new cvc5::api::Solver());
43
12
    d_solver->setOption("input-language", "smt2");
44
12
    d_symman.reset(new SymbolManager(d_solver.get()));
45
12
  }
46
47
12
  void TearDown() override
48
  {
49
12
    d_sin.reset(nullptr);
50
12
    d_sout.reset(nullptr);
51
    // ensure that symbol manager is destroyed before solver
52
12
    d_symman.reset(nullptr);
53
12
    d_solver.reset(nullptr);
54
12
  }
55
56
  /**
57
   * Read up to maxCommands+1 from the shell and throw an assertion error if
58
   * it's fewer than minCommands and more than maxCommands.  Note that an
59
   * empty string followed by EOF may be returned as an empty command, and
60
   * not NULL (subsequent calls to readCommand() should return NULL). E.g.,
61
   * "CHECKSAT;\n" may return two commands: the CHECKSAT, followed by an
62
   * empty command, followed by NULL.
63
   */
64
12
  void countCommands(InteractiveShell& shell,
65
                     uint32_t minCommands,
66
                     uint32_t maxCommands)
67
  {
68
    Command* cmd;
69
12
    uint32_t n = 0;
70
40
    while (n <= maxCommands && (cmd = shell.readCommand()) != NULL)
71
    {
72
14
      ++n;
73
14
      delete cmd;
74
    }
75
12
    ASSERT_LE(n, maxCommands);
76
12
    ASSERT_GE(n, minCommands);
77
  }
78
79
  std::unique_ptr<std::stringstream> d_sin;
80
  std::unique_ptr<std::stringstream> d_sout;
81
  std::unique_ptr<SymbolManager> d_symman;
82
  std::unique_ptr<cvc5::api::Solver> d_solver;
83
};
84
85
15
TEST_F(TestMainBlackInteractiveShell, assert_true)
86
{
87
2
  *d_sin << "(assert true)\n" << std::flush;
88
4
  InteractiveShell shell(d_solver.get(), d_symman.get(), *d_sin, *d_sout);
89
2
  countCommands(shell, 1, 1);
90
2
}
91
92
15
TEST_F(TestMainBlackInteractiveShell, query_false)
93
{
94
2
  *d_sin << "(check-sat)\n" << std::flush;
95
4
  InteractiveShell shell(d_solver.get(), d_symman.get(), *d_sin, *d_sout);
96
2
  countCommands(shell, 1, 1);
97
2
}
98
99
15
TEST_F(TestMainBlackInteractiveShell, def_use1)
100
{
101
4
  InteractiveShell shell(d_solver.get(), d_symman.get(), *d_sin, *d_sout);
102
2
  *d_sin << "(declare-const x Real) (assert (> x 0))\n" << std::flush;
103
  /* readCommand may return a sequence, so we can't say for sure
104
     whether it will return 1 or 2... */
105
2
  countCommands(shell, 1, 2);
106
2
}
107
108
15
TEST_F(TestMainBlackInteractiveShell, def_use2)
109
{
110
4
  InteractiveShell shell(d_solver.get(), d_symman.get(), *d_sin, *d_sout);
111
  /* readCommand may return a sequence, see above. */
112
2
  *d_sin << "(declare-const x Real)\n" << std::flush;
113
2
  Command* tmp = shell.readCommand();
114
2
  *d_sin << "(assert (> x 0))\n" << std::flush;
115
2
  countCommands(shell, 1, 1);
116
2
  delete tmp;
117
2
}
118
119
15
TEST_F(TestMainBlackInteractiveShell, empty_line)
120
{
121
4
  InteractiveShell shell(d_solver.get(), d_symman.get(), *d_sin, *d_sout);
122
2
  *d_sin << std::flush;
123
2
  countCommands(shell, 0, 0);
124
2
}
125
126
15
TEST_F(TestMainBlackInteractiveShell, repeated_empty_lines)
127
{
128
2
  *d_sin << "\n\n\n";
129
4
  InteractiveShell shell(d_solver.get(), d_symman.get(), *d_sin, *d_sout);
130
  /* Might return up to four empties, might return nothing */
131
2
  countCommands(shell, 0, 3);
132
2
}
133
}  // namespace test
134
21
}  // namespace cvc5