1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Aina Niemetz, Morgan Deters |
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 |
|
* "Ouroborous" test: does cvc5 read its own output? |
14 |
|
* |
15 |
|
* The "Ouroborous" test, named after the serpent that swallows its |
16 |
|
* own tail, ensures that cvc5 can parse some input, output it again |
17 |
|
* (in any of its languages) and then parse it again. The result of |
18 |
|
* the first parse must be equal to the result of the second parse; |
19 |
|
* both strings and expressions are compared for equality. |
20 |
|
* |
21 |
|
* To add a new test, simply add a call to runTestString() under |
22 |
|
* runTest(), below. If you don't specify an input language, |
23 |
|
* LANG_SMTLIB_V2 is used. If your example depends on variables, |
24 |
|
* you'll need to declare them in the "declarations" global, just |
25 |
|
* below, in SMT-LIBv2 form (but they're good for all languages). |
26 |
|
*/ |
27 |
|
|
28 |
|
#include <cassert> |
29 |
|
#include <iostream> |
30 |
|
#include <string> |
31 |
|
|
32 |
|
#include "api/cpp/cvc5.h" |
33 |
|
#include "options/set_language.h" |
34 |
|
#include "parser/parser.h" |
35 |
|
#include "parser/parser_builder.h" |
36 |
|
#include "smt/command.h" |
37 |
|
|
38 |
|
using namespace cvc5; |
39 |
|
using namespace cvc5::parser; |
40 |
|
using namespace cvc5::language; |
41 |
|
|
42 |
|
int runTest(); |
43 |
|
|
44 |
1 |
int main() |
45 |
|
{ |
46 |
|
try |
47 |
|
{ |
48 |
1 |
return runTest(); |
49 |
|
} |
50 |
|
catch (api::CVC5ApiException& e) |
51 |
|
{ |
52 |
|
std::cerr << e.getMessage() << std::endl; |
53 |
|
} |
54 |
|
catch (parser::ParserException& e) |
55 |
|
{ |
56 |
|
std::cerr << e.getMessage() << std::endl; |
57 |
|
} |
58 |
|
catch (...) |
59 |
|
{ |
60 |
|
std::cerr << "non-cvc5 exception thrown" << std::endl; |
61 |
|
} |
62 |
|
return 1; |
63 |
|
} |
64 |
|
|
65 |
12 |
std::string parse(std::string instr, |
66 |
|
std::string input_language, |
67 |
|
std::string output_language) |
68 |
|
{ |
69 |
12 |
assert(input_language == "smt2"); |
70 |
12 |
assert(output_language == "smt2"); |
71 |
|
|
72 |
24 |
std::string declarations; |
73 |
|
|
74 |
12 |
declarations = |
75 |
|
"\ |
76 |
|
(declare-sort U 0)\n\ |
77 |
|
(declare-fun f (U) U)\n\ |
78 |
|
(declare-fun x () U)\n\ |
79 |
|
(declare-fun y () U)\n\ |
80 |
|
(assert (= (f x) x))\n\ |
81 |
|
(declare-fun a () (Array U (Array U U)))\n\ |
82 |
|
"; |
83 |
|
|
84 |
24 |
api::Solver solver; |
85 |
24 |
std::string ilang = "LANG_SMTLIB_V2_6"; |
86 |
|
|
87 |
12 |
solver.setOption("input-language", input_language); |
88 |
12 |
solver.setOption("output-language", output_language); |
89 |
24 |
SymbolManager symman(&solver); |
90 |
|
std::unique_ptr<Parser> parser( |
91 |
24 |
ParserBuilder(&solver, &symman, false) |
92 |
24 |
.withInputLanguage(solver.getOption("input-language")) |
93 |
24 |
.build()); |
94 |
12 |
parser->setInput( |
95 |
|
Input::newStringInput(ilang, declarations, "internal-buffer")); |
96 |
|
// we don't need to execute the commands, but we DO need to parse them to |
97 |
|
// get the declarations |
98 |
84 |
while (Command* c = parser->nextCommand()) |
99 |
|
{ |
100 |
72 |
delete c; |
101 |
72 |
} |
102 |
12 |
assert(parser->done()); // parser should be done |
103 |
12 |
parser->setInput(Input::newStringInput(ilang, instr, "internal-buffer")); |
104 |
24 |
api::Term e = parser->nextExpression(); |
105 |
12 |
std::string s = e.toString(); |
106 |
12 |
assert(parser->nextExpression().isNull()); // next expr should be null |
107 |
24 |
return s; |
108 |
|
} |
109 |
|
|
110 |
6 |
std::string translate(std::string instr, |
111 |
|
std::string input_language, |
112 |
|
std::string output_language) |
113 |
|
{ |
114 |
6 |
assert(input_language == "smt2"); |
115 |
6 |
assert(output_language == "smt2"); |
116 |
|
|
117 |
6 |
std::cout << "==============================================" << std::endl |
118 |
6 |
<< "translating from " << Language::LANG_SMTLIB_V2_6 << " to " |
119 |
6 |
<< Language::LANG_SMTLIB_V2_6 << " this string:" << std::endl |
120 |
6 |
<< instr << std::endl; |
121 |
6 |
std::string outstr = parse(instr, input_language, output_language); |
122 |
6 |
std::cout << "got this:" << std::endl |
123 |
6 |
<< outstr << std::endl |
124 |
6 |
<< "reparsing as " << Language::LANG_SMTLIB_V2_6 << std::endl; |
125 |
12 |
std::string poutstr = parse(outstr, output_language, output_language); |
126 |
6 |
assert(outstr == poutstr); |
127 |
6 |
std::cout << "got same expressions " << outstr << " and " << poutstr |
128 |
6 |
<< std::endl |
129 |
6 |
<< "==============================================" << std::endl; |
130 |
12 |
return outstr; |
131 |
|
} |
132 |
|
|
133 |
3 |
void runTestString(std::string instr, std::string instr_language) |
134 |
|
{ |
135 |
3 |
std::cout << std::endl |
136 |
3 |
<< "starting with: " << instr << std::endl |
137 |
3 |
<< " in language " << Language::LANG_SMTLIB_V2_6 << std::endl; |
138 |
6 |
std::string smt2str = translate(instr, instr_language, "smt2"); |
139 |
3 |
std::cout << "in SMT2 : " << smt2str << std::endl; |
140 |
6 |
std::string outstr = translate(smt2str, "smt2", "smt2"); |
141 |
3 |
std::cout << "to SMT2 : " << outstr << std::endl << std::endl; |
142 |
|
|
143 |
3 |
assert(outstr == smt2str); // differences in output |
144 |
3 |
} |
145 |
|
|
146 |
1 |
int32_t runTest() |
147 |
|
{ |
148 |
1 |
runTestString("(= (f (f y)) x)", "smt2"); |
149 |
1 |
runTestString("(= ((_ extract 2 1) (bvnot (bvadd #b000 #b011))) #b10)", |
150 |
|
"smt2"); |
151 |
1 |
runTestString("((_ extract 2 0) (bvnot (bvadd (bvmul #b001 #b011) #b011)))", |
152 |
|
"smt2"); |
153 |
1 |
return 0; |
154 |
3 |
} |