GCC Code Coverage Report
Directory: . Exec Total Coverage
File: test/unit/main/interactive_shell_black.cpp Lines: 58 58 100.0 %
Date: 2021-05-22 Branches: 56 124 45.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
24
    d_sin.reset(new std::stringstream);
39
24
    d_sout.reset(new std::stringstream);
40
24
    d_options.set(options::in, d_sin.get());
41
24
    d_options.set(options::out, d_sout.get());
42
24
    d_options.set(options::inputLanguage, language::input::LANG_CVC);
43
12
    d_solver.reset(new cvc5::api::Solver(&d_options));
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
  Options d_options;
84
};
85
86
15
TEST_F(TestMainBlackInteractiveShell, assert_true)
87
{
88
2
  *d_sin << "ASSERT TRUE;\n" << std::flush;
89
4
  InteractiveShell shell(d_solver.get(), d_symman.get());
90
2
  countCommands(shell, 1, 1);
91
2
}
92
93
15
TEST_F(TestMainBlackInteractiveShell, query_false)
94
{
95
2
  *d_sin << "QUERY FALSE;\n" << std::flush;
96
4
  InteractiveShell shell(d_solver.get(), d_symman.get());
97
2
  countCommands(shell, 1, 1);
98
2
}
99
100
15
TEST_F(TestMainBlackInteractiveShell, def_use1)
101
{
102
4
  InteractiveShell shell(d_solver.get(), d_symman.get());
103
2
  *d_sin << "x : REAL; ASSERT x > 0;\n" << std::flush;
104
  /* readCommand may return a sequence, so we can't say for sure
105
     whether it will return 1 or 2... */
106
2
  countCommands(shell, 1, 2);
107
2
}
108
109
15
TEST_F(TestMainBlackInteractiveShell, def_use2)
110
{
111
4
  InteractiveShell shell(d_solver.get(), d_symman.get());
112
  /* readCommand may return a sequence, see above. */
113
2
  *d_sin << "x : REAL;\n" << std::flush;
114
2
  Command* tmp = shell.readCommand();
115
2
  *d_sin << "ASSERT x > 0;\n" << std::flush;
116
2
  countCommands(shell, 1, 1);
117
2
  delete tmp;
118
2
}
119
120
15
TEST_F(TestMainBlackInteractiveShell, empty_line)
121
{
122
4
  InteractiveShell shell(d_solver.get(), d_symman.get());
123
2
  *d_sin << std::flush;
124
2
  countCommands(shell, 0, 0);
125
2
}
126
127
15
TEST_F(TestMainBlackInteractiveShell, repeated_empty_lines)
128
{
129
2
  *d_sin << "\n\n\n";
130
4
  InteractiveShell shell(d_solver.get(), d_symman.get());
131
  /* Might return up to four empties, might return nothing */
132
2
  countCommands(shell, 0, 3);
133
2
}
134
}  // namespace test
135
57
}  // namespace cvc5