|
| 1 | +// Author: Diffblue Ltd. |
| 2 | + |
| 3 | +#include <testing-utils/use_catch.h> |
| 4 | + |
| 5 | +#include <solvers/smt2/smt2irep.h> |
| 6 | +#include <util/message.h> |
| 7 | + |
| 8 | +#include <sstream> |
| 9 | +#include <string> |
| 10 | +#include <utility> |
| 11 | + |
| 12 | +struct smt2_parser_test_resultt |
| 13 | +{ |
| 14 | + optionalt<irept> parsed_output; |
| 15 | + std::string messages; |
| 16 | +}; |
| 17 | + |
| 18 | +bool operator==( |
| 19 | + const smt2_parser_test_resultt &left, |
| 20 | + const smt2_parser_test_resultt &right) |
| 21 | +{ |
| 22 | + return left.parsed_output == right.parsed_output && |
| 23 | + left.messages == right.messages; |
| 24 | +} |
| 25 | + |
| 26 | +static smt2_parser_test_resultt smt2irep(const std::string &input) |
| 27 | +{ |
| 28 | + std::stringstream in_stream(input); |
| 29 | + std::stringstream out_stream; |
| 30 | + stream_message_handlert message_handler(out_stream); |
| 31 | + return {smt2irep(in_stream, message_handler), out_stream.str()}; |
| 32 | +} |
| 33 | + |
| 34 | +std::ostream &operator<<( |
| 35 | + std::ostream &output_stream, |
| 36 | + const smt2_parser_test_resultt &test_result) |
| 37 | +{ |
| 38 | + const std::string printed_irep = |
| 39 | + test_result.parsed_output.has_value() |
| 40 | + ? '{' + test_result.parsed_output->pretty(0, 0) + '}' |
| 41 | + : "empty optional irep"; |
| 42 | + output_stream << '{' << printed_irep << ", \"" << test_result.messages |
| 43 | + << "\"}"; |
| 44 | + return output_stream; |
| 45 | +} |
| 46 | + |
| 47 | +class smt2_parser_error_containingt |
| 48 | + : public Catch::MatcherBase<smt2_parser_test_resultt> |
| 49 | +{ |
| 50 | +public: |
| 51 | + explicit smt2_parser_error_containingt(std::string expected_error); |
| 52 | + bool match(const smt2_parser_test_resultt &exception) const override; |
| 53 | + std::string describe() const override; |
| 54 | + |
| 55 | +private: |
| 56 | + std::string expected_error; |
| 57 | +}; |
| 58 | + |
| 59 | +smt2_parser_error_containingt::smt2_parser_error_containingt( |
| 60 | + std::string expected_error) |
| 61 | + : expected_error{std::move(expected_error)} |
| 62 | +{ |
| 63 | +} |
| 64 | + |
| 65 | +bool smt2_parser_error_containingt::match( |
| 66 | + const smt2_parser_test_resultt &result) const |
| 67 | +{ |
| 68 | + return !result.parsed_output.has_value() && |
| 69 | + result.messages.find(expected_error) != std::string::npos; |
| 70 | +} |
| 71 | + |
| 72 | +std::string smt2_parser_error_containingt::describe() const |
| 73 | +{ |
| 74 | + return "Expecting empty parse tree and \"" + expected_error + |
| 75 | + "\" printed to output."; |
| 76 | +} |
| 77 | + |
| 78 | +static smt2_parser_test_resultt smt2_parser_success(irept parse_tree) |
| 79 | +{ |
| 80 | + return {std::move(parse_tree), ""}; |
| 81 | +} |
| 82 | + |
| 83 | +TEST_CASE("smt2irep error handling", "[core][solvers][smt2]") |
| 84 | +{ |
| 85 | + CHECK_THAT( |
| 86 | + smt2irep("|"), smt2_parser_error_containingt{"EOF within quoted symbol"}); |
| 87 | + CHECK_THAT( |
| 88 | + smt2irep(":"), |
| 89 | + smt2_parser_error_containingt{"expecting symbol after colon"}); |
| 90 | + CHECK_THAT( |
| 91 | + smt2irep(":foo"), smt2_parser_error_containingt{"unexpected token"}); |
| 92 | + CHECK(smt2irep("") == smt2_parser_test_resultt{{}, ""}); |
| 93 | + CHECK_THAT( |
| 94 | + smt2irep("("), smt2_parser_error_containingt{"unexpected end of file"}); |
| 95 | + CHECK_THAT(smt2irep(")"), smt2_parser_error_containingt{"unexpected ')'"}); |
| 96 | + CHECK_THAT( |
| 97 | + smt2irep("#"), |
| 98 | + smt2_parser_error_containingt{"unexpected EOF in numeral token"}); |
| 99 | + CHECK_THAT( |
| 100 | + smt2irep("#0"), smt2_parser_error_containingt{"unknown numeral token"}); |
| 101 | + CHECK_THAT( |
| 102 | + smt2irep("\\"), smt2_parser_error_containingt{"unexpected character '\\'"}); |
| 103 | +} |
| 104 | + |
| 105 | +TEST_CASE("smt2irept parse single tokens", "[core][solvers][smt2]") |
| 106 | +{ |
| 107 | + CHECK( |
| 108 | + smt2irep("|example_symbol|") == |
| 109 | + smt2_parser_success({"example_symbol", {}, {}})); |
| 110 | + CHECK( |
| 111 | + smt2irep("\"example string literal\"") == |
| 112 | + smt2_parser_success({"example string literal", {}, {}})); |
| 113 | + CHECK(smt2irep("#b00101010") == smt2_parser_success({"#b00101010", {}, {}})); |
| 114 | + CHECK(smt2irep("#x2A") == smt2_parser_success({"#x2A", {}, {}})); |
| 115 | +} |
| 116 | + |
| 117 | +TEST_CASE("smt2irept comments", "[core][solvers][smt2]") |
| 118 | +{ |
| 119 | + CHECK(smt2irep(";") == smt2_parser_test_resultt{{}, ""}); |
| 120 | + CHECK(smt2irep(";foobar") == smt2_parser_test_resultt{{}, ""}); |
| 121 | + CHECK( |
| 122 | + smt2irep("|example_symbol|;foobar") == |
| 123 | + smt2_parser_success({"example_symbol", {}, {}})); |
| 124 | + CHECK( |
| 125 | + smt2irep(";foobar\n|example_symbol|") == |
| 126 | + smt2_parser_success({"example_symbol", {}, {}})); |
| 127 | +} |
| 128 | + |
| 129 | +TEST_CASE("smt2irept parse with sub trees", "[core][solvers][smt2]") |
| 130 | +{ |
| 131 | + CHECK(smt2irep("()") == smt2_parser_success({})); |
| 132 | + const irept foo_symbol{"foo", {}, {}}; |
| 133 | + CHECK( |
| 134 | + smt2irep("(|foo|)") == smt2_parser_success(irept{"", {}, {foo_symbol}})); |
| 135 | + const irept bar_symbol{"bar", {}, {}}; |
| 136 | + CHECK( |
| 137 | + smt2irep("(|foo| |bar|)") == |
| 138 | + smt2_parser_success(irept{"", {}, {foo_symbol, bar_symbol}})); |
| 139 | + const irept baz_symbol{"baz", {}, {}}; |
| 140 | + CHECK( |
| 141 | + smt2irep("(|foo| |bar| |baz|)") == |
| 142 | + smt2_parser_success(irept{"", {}, {foo_symbol, bar_symbol, baz_symbol}})); |
| 143 | + CHECK(smt2irep("(())") == smt2_parser_success({"", {}, {{}}})); |
| 144 | + CHECK(smt2irep("(() ())") == smt2_parser_success({"", {}, {{}, {}}})); |
| 145 | + CHECK(smt2irep("(() () ())") == smt2_parser_success({"", {}, {{}, {}, {}}})); |
| 146 | + CHECK( |
| 147 | + smt2irep("(|foo| () |bar|)") == |
| 148 | + smt2_parser_success({"", {}, {foo_symbol, {}, bar_symbol}})); |
| 149 | + CHECK( |
| 150 | + smt2irep("(|foo| (|bar|) |baz|)") == |
| 151 | + smt2_parser_success( |
| 152 | + {"", {}, {foo_symbol, {"", {}, {bar_symbol}}, baz_symbol}})); |
| 153 | + CHECK( |
| 154 | + smt2irep("((|foo| |bar| |baz|) (|baz| |bar| |foo|))") == |
| 155 | + smt2_parser_success( |
| 156 | + {"", |
| 157 | + {}, |
| 158 | + {irept{"", {}, {foo_symbol, bar_symbol, baz_symbol}}, |
| 159 | + irept{"", {}, {baz_symbol, bar_symbol, foo_symbol}}}})); |
| 160 | +} |
0 commit comments