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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
/******************************************************************************
* Top contributors (to current version):
* Aina Niemetz, Andrew Reynolds, Christopher L. Conway
*
* 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 cvc5::parser::InputParser for SMT-LIbv2 inputs.
*/
#include <cvc5/cvc5.h>
#include <cvc5/cvc5_parser.h>
#include <sstream>
#include "base/output.h"
#include "options/base_options.h"
#include "options/language.h"
#include "options/options.h"
#include <cvc5/cvc5_parser.h>
#include "test.h"
using namespace cvc5::parser;
namespace cvc5::internal {
namespace test {
class TestParserBlack : public TestInternal
{
protected:
TestParserBlack(modes::InputLanguage lang) : d_lang(lang) {}
virtual ~TestParserBlack() {}
void SetUp() override
{
TestInternal::SetUp();
d_symman.reset(nullptr);
d_solver.reset(new cvc5::Solver(d_tm));
d_solver->setOption("parse-only", "true");
}
void TearDown() override
{
d_symman.reset(nullptr);
d_solver.reset(nullptr);
}
/* Set up declaration context for expr inputs */
void setupContext(InputParser& parser)
{
std::stringstream ss;
ss << "(set-logic ALL)" << std::endl;
ss << "(declare-fun a () Bool)" << std::endl;
ss << "(declare-fun b () Bool)" << std::endl;
ss << "(declare-fun c () Bool)" << std::endl;
ss << "(declare-sort t 0)" << std::endl;
ss << "(declare-sort u 0)" << std::endl;
ss << "(declare-sort v 0)" << std::endl;
ss << "(declare-fun f (t) u)" << std::endl;
ss << "(declare-fun g (u) v)" << std::endl;
ss << "(declare-fun h (v) t)" << std::endl;
ss << "(declare-fun x () t)" << std::endl;
ss << "(declare-fun y () u)" << std::endl;
ss << "(declare-fun z () v)" << std::endl;
parser.setStreamInput(
modes::InputLanguage::SMT_LIB_2_6, ss, "parser_black");
Command cmd;
std::stringstream tmp;
while (true)
{
cmd = parser.nextCommand();
if (cmd.isNull())
{
break;
}
cmd.invoke(d_solver.get(), d_symman.get(), tmp);
}
}
void tryGoodInput(const std::string goodInput)
{
d_solver.reset(new cvc5::Solver(d_tm));
d_symman.reset(new SymbolManager(d_tm));
InputParser parser(d_solver.get(), d_symman.get());
std::stringstream ss;
ss << goodInput;
parser.setStreamInput(
modes::InputLanguage::SMT_LIB_2_6, ss, "parser_black");
ASSERT_FALSE(parser.done());
Command cmd;
std::stringstream tmp;
while (true)
{
cmd = parser.nextCommand();
if (cmd.isNull())
{
break;
}
Trace("parser") << "Parsed command: " << cmd << std::endl;
cmd.invoke(d_solver.get(), d_symman.get(), tmp);
}
ASSERT_TRUE(parser.done());
}
void tryBadInput(const std::string badInput, bool strictMode = false)
{
d_solver.reset(new cvc5::Solver(d_tm));
d_solver->setOption("strict-parsing", strictMode ? "true" : "false");
d_symman.reset(new SymbolManager(d_tm));
InputParser parser(d_solver.get(), d_symman.get());
std::stringstream ss;
ss << badInput;
parser.setStreamInput(d_lang, ss, "parser_black");
ASSERT_THROW(
{
Command cmd;
std::stringstream tmp;
while (true)
{
cmd = parser.nextCommand();
if (cmd.isNull())
{
break;
}
Trace("parser") << "Parsed command: " << cmd << std::endl;
cmd.invoke(d_solver.get(), d_symman.get(), tmp);
}
std::cout << "\nBad input succeeded:\n" << badInput << std::endl;
},
ParserException);
}
void tryGoodExpr(const std::string goodExpr)
{
d_solver.reset(new cvc5::Solver(d_tm));
d_symman.reset(new SymbolManager(d_tm));
InputParser parser(d_solver.get(), d_symman.get());
setupContext(parser);
std::stringstream ss;
ss << goodExpr;
parser.setStreamInput(d_lang, ss, "parser_black");
ASSERT_FALSE(parser.done());
cvc5::Term e = parser.nextTerm();
ASSERT_FALSE(e.isNull());
e = parser.nextTerm();
ASSERT_TRUE(parser.done());
ASSERT_TRUE(e.isNull());
}
/**
* NOTE: The check implemented here may fail if a bad expression
* expression string has a prefix that is parseable as a good
* expression. E.g., the bad SMT v2 expression "#b10@@@@@@" will
* actually return the bit-vector 10 and ignore the tail of the
* input. It's more trouble than it's worth to check that the whole
* input was consumed here, so just be careful to avoid valid
* prefixes in tests.
*/
void tryBadExpr(const std::string badExpr, bool strictMode = false)
{
d_solver.reset(new cvc5::Solver(d_tm));
d_solver->setOption("strict-parsing", strictMode ? "true" : "false");
d_symman.reset(new SymbolManager(d_tm));
InputParser parser(d_solver.get(), d_symman.get());
setupContext(parser);
std::stringstream ss;
ss << badExpr;
parser.setStreamInput(d_lang, ss, "parser_black");
ASSERT_FALSE(parser.done());
ASSERT_THROW(cvc5::Term e = parser.nextTerm();
std::cout << std::endl
<< "Bad expr succeeded." << std::endl
<< "Input: <<" << badExpr << ">>" << std::endl
<< "Output: <<" << e << ">>" << std::endl;
, ParserException);
}
modes::InputLanguage d_lang;
cvc5::TermManager d_tm;
std::unique_ptr<cvc5::Solver> d_solver;
std::unique_ptr<SymbolManager> d_symman;
};
/* -------------------------------------------------------------------------- */
class TestParserBlackSmt2InputParser : public TestParserBlack
{
protected:
TestParserBlackSmt2InputParser()
: TestParserBlack(modes::InputLanguage::SMT_LIB_2_6)
{
}
};
TEST_F(TestParserBlackSmt2InputParser, good_inputs)
{
tryGoodInput(""); // empty string is OK
tryGoodInput("(set-logic QF_UF)");
tryGoodInput("(set-info :notes |This is a note, take note!|)");
tryGoodInput("(set-logic QF_UF) (assert true)");
tryGoodInput("(check-sat)");
tryGoodInput("(exit)");
tryGoodInput("(set-logic QF_UF) (assert false) (check-sat)");
tryGoodInput(
"(set-logic QF_UF) (declare-fun a () Bool) "
"(declare-fun b () Bool)");
tryGoodInput(
"(set-logic QF_UF) (declare-fun a () Bool) "
"(declare-fun b () Bool) (assert (=> (and (=> a b) a) b))");
tryGoodInput(
"(set-logic QF_UF) (declare-sort a 0) "
"(declare-fun f (a) a) (declare-fun x () a) "
"(assert (= (f x) x))");
tryGoodInput(
"(set-logic QF_UF) (declare-sort a 0) "
"(declare-fun x () a) (declare-fun y () a) "
"(assert (= (ite true x y) x))");
tryGoodInput(";; nothing but a comment");
tryGoodInput("; a comment\n(check-sat ; goodbye\n)");
}
TEST_F(TestParserBlackSmt2InputParser, bad_inputs)
{
// competition builds don't do any checking
#ifndef CVC5_COMPETITION_MODE
// no arguments
tryBadInput("(assert)");
// illegal character in symbol
tryBadInput("(set-info :notes |Symbols can't contain the | character|)");
// check-sat should not have an argument
tryBadInput("(set-logic QF_UF) (check-sat true)", true);
// no argument
tryBadInput("(declare-sort a)");
// double declaration
tryBadInput("(declare-sort a 0) (declare-sort a 0)");
// should be "(declare-fun p () Bool)"
tryBadInput("(set-logic QF_UF) (declare-fun p Bool)");
// strict mode
// no set-logic, core theory symbol "true" undefined
tryBadInput("(assert true)", true);
// core theory symbol "Bool" undefined
tryBadInput("(declare-fun p Bool)", true);
#endif
}
TEST_F(TestParserBlackSmt2InputParser, good_exprs)
{
tryGoodExpr("(and a b)");
tryGoodExpr("(or (and a b) c)");
tryGoodExpr("(=> (and (=> a b) a) b)");
tryGoodExpr("(and (= a b) (not a))");
tryGoodExpr("(= (xor a b) (and (or a b) (not (and a b))))");
tryGoodExpr("(ite a (f x) y)");
tryGoodExpr("1");
tryGoodExpr("0");
tryGoodExpr("1.5");
tryGoodExpr("#xfab09c7");
tryGoodExpr("#b0001011");
tryGoodExpr("(* 5 1)");
}
TEST_F(TestParserBlackSmt2InputParser, bad_exprs)
{
// competition builds don't do any checking
#ifndef CVC5_COMPETITION_MODE
tryBadExpr("(and)"); // wrong arity
tryBadExpr("(and a b"); // no closing paren
tryBadExpr("(a and b)"); // infix
tryBadExpr("(implies a b)"); // no implies in v2
tryBadExpr("(iff a b)"); // no iff in v2
tryBadExpr("(OR (AND a b) c)"); // wrong case
tryBadExpr("(a IMPLIES b)"); // infix AND wrong case
tryBadExpr("(not a b)"); // wrong arity
tryBadExpr("not a"); // needs parens
tryBadExpr("(ite a x)"); // wrong arity
tryBadExpr("(if_then_else a (f x) y)"); // no if_then_else in v2
tryBadExpr("(a b)"); // using non-function as function
tryBadExpr(".5"); // rational constants must have integer prefix
tryBadExpr("1."); // rational constants must have fractional suffix
tryBadExpr("#x"); // hex constants must have at least one digit
tryBadExpr("#b"); // ditto binary constants
tryBadExpr("#xg0f");
tryBadExpr("#b9");
// Bad strict exprs
tryBadExpr("(and a)", true); // no unary and's
tryBadExpr("(or a)", true); // no unary or's
tryBadExpr("(* 5 01)", true); // '01' is not a valid integer constant
#endif
}
} // namespace test
} // namespace cvc5::internal
|