|
| 1 | +// Author: Diffblue Ltd. |
| 2 | + |
| 3 | +#include <testing-utils/use_catch.h> |
| 4 | + |
| 5 | +#include <solvers/smt2_incremental/construct_value_expr_from_smt.h> |
| 6 | + |
| 7 | +#include <solvers/smt2_incremental/smt_core_theory.h> |
| 8 | +#include <solvers/smt2_incremental/smt_terms.h> |
| 9 | +#include <solvers/smt2_incremental/smt_to_smt2_string.h> |
| 10 | + |
| 11 | +#include <testing-utils/invariant.h> |
| 12 | + |
| 13 | +#include <util/arith_tools.h> |
| 14 | +#include <util/bitvector_types.h> |
| 15 | +#include <util/mp_arith.h> |
| 16 | +#include <util/std_expr.h> |
| 17 | +#include <util/std_types.h> |
| 18 | + |
| 19 | +#include <string> |
| 20 | + |
| 21 | +static mp_integer power2(unsigned exponent) |
| 22 | +{ |
| 23 | + mp_integer result; |
| 24 | + result.setPower2(exponent); |
| 25 | + return result; |
| 26 | +} |
| 27 | + |
| 28 | +/// Returns the maximum integer value which can be stored in \p bits as an |
| 29 | +/// unsigned integer. |
| 30 | +static mp_integer max_int(const std::size_t bits) |
| 31 | +{ |
| 32 | + return power2(bits) - 1; |
| 33 | +} |
| 34 | + |
| 35 | +TEST_CASE("Value expr construction from smt.", "[core][smt2_incremental]") |
| 36 | +{ |
| 37 | + optionalt<smt_termt> input_term; |
| 38 | + optionalt<exprt> expected_result; |
| 39 | + |
| 40 | + using rowt = std::pair<smt_termt, exprt>; |
| 41 | + |
| 42 | + // clang-format off |
| 43 | +#define UNSIGNED_BIT_VECTOR_TESTS(bits) \ |
| 44 | + rowt{smt_bit_vector_constant_termt{0, (bits)}, \ |
| 45 | + from_integer(0, unsignedbv_typet{(bits)})}, \ |
| 46 | + rowt{smt_bit_vector_constant_termt{42, (bits)}, \ |
| 47 | + from_integer(42, unsignedbv_typet{(bits)})}, \ |
| 48 | + rowt{smt_bit_vector_constant_termt{max_int((bits) - 1), (bits)}, \ |
| 49 | + from_integer(max_int((bits) - 1), unsignedbv_typet{(bits)})}, \ |
| 50 | + rowt{smt_bit_vector_constant_termt{power2((bits) - 1), (bits)}, \ |
| 51 | + from_integer(power2((bits) - 1), unsignedbv_typet{(bits)})}, \ |
| 52 | + rowt{smt_bit_vector_constant_termt{max_int((bits)), (bits)}, \ |
| 53 | + from_integer(max_int((bits)), unsignedbv_typet{(bits)})} |
| 54 | + |
| 55 | +#define SIGNED_BIT_VECTOR_TESTS(bits) \ |
| 56 | + rowt{smt_bit_vector_constant_termt{0, (bits)}, \ |
| 57 | + from_integer(0, signedbv_typet{(bits)})}, \ |
| 58 | + rowt{smt_bit_vector_constant_termt{42, (bits)}, \ |
| 59 | + from_integer(42, signedbv_typet{(bits)})}, \ |
| 60 | + rowt{smt_bit_vector_constant_termt{max_int((bits) - 1), (bits)}, \ |
| 61 | + from_integer(max_int((bits) - 1), signedbv_typet{(bits)})}, \ |
| 62 | + rowt{smt_bit_vector_constant_termt{power2((bits) - 1), (bits)}, \ |
| 63 | + from_integer(-power2((bits) - 1), signedbv_typet{(bits)})}, \ |
| 64 | + rowt{smt_bit_vector_constant_termt{max_int((bits)), (bits)}, \ |
| 65 | + from_integer(-1, signedbv_typet{(bits)})} |
| 66 | + // clang-format on |
| 67 | + |
| 68 | + std::tie(input_term, expected_result) = GENERATE( |
| 69 | + rowt{smt_bool_literal_termt{true}, true_exprt{}}, |
| 70 | + rowt{smt_bool_literal_termt{false}, false_exprt{}}, |
| 71 | + UNSIGNED_BIT_VECTOR_TESTS(8), |
| 72 | + SIGNED_BIT_VECTOR_TESTS(8), |
| 73 | + UNSIGNED_BIT_VECTOR_TESTS(16), |
| 74 | + SIGNED_BIT_VECTOR_TESTS(16), |
| 75 | + UNSIGNED_BIT_VECTOR_TESTS(32), |
| 76 | + SIGNED_BIT_VECTOR_TESTS(32), |
| 77 | + UNSIGNED_BIT_VECTOR_TESTS(64), |
| 78 | + SIGNED_BIT_VECTOR_TESTS(64)); |
| 79 | + SECTION( |
| 80 | + "Construction of \"" + id2string(expected_result->type().id()) + |
| 81 | + "\" from \"" + smt_to_smt2_string(*input_term) + "\"") |
| 82 | + { |
| 83 | + REQUIRE( |
| 84 | + construct_value_expr_from_smt(*input_term, expected_result->type()) == |
| 85 | + *expected_result); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +TEST_CASE( |
| 90 | + "Invariant violations in value expr construction from smt.", |
| 91 | + "[core][smt2_incremental]") |
| 92 | +{ |
| 93 | + optionalt<smt_termt> input_term; |
| 94 | + optionalt<typet> input_type; |
| 95 | + std::string invariant_reason; |
| 96 | + |
| 97 | + using rowt = std::tuple<smt_termt, typet, std::string>; |
| 98 | + std::tie(input_term, input_type, invariant_reason) = GENERATE( |
| 99 | + rowt{smt_bool_literal_termt{true}, |
| 100 | + unsignedbv_typet{16}, |
| 101 | + "Bool terms may only be used to construct bool typed expressions."}, |
| 102 | + rowt{smt_identifier_termt{"foo", smt_bit_vector_sortt{16}}, |
| 103 | + unsignedbv_typet{16}, |
| 104 | + "Unexpected conversion of identifier to value expression."}, |
| 105 | + rowt{ |
| 106 | + smt_bit_vector_constant_termt{0, 8}, |
| 107 | + unsignedbv_typet{16}, |
| 108 | + "Width of smt bit vector term must match the width of bit vector type."}, |
| 109 | + rowt{smt_bit_vector_constant_termt{0, 8}, |
| 110 | + empty_typet{}, |
| 111 | + "construct_value_expr_from_smt for bit vector should not be applied " |
| 112 | + "to unsupported type empty"}, |
| 113 | + rowt{smt_core_theoryt::make_not(smt_bool_literal_termt{true}), |
| 114 | + unsignedbv_typet{16}, |
| 115 | + "Unexpected conversion of function application to value expression."}); |
| 116 | + SECTION(invariant_reason) |
| 117 | + { |
| 118 | + const cbmc_invariants_should_throwt invariants_throw; |
| 119 | + REQUIRE_THROWS_MATCHES( |
| 120 | + construct_value_expr_from_smt(*input_term, *input_type), |
| 121 | + invariant_failedt, |
| 122 | + invariant_failure_containing(invariant_reason)); |
| 123 | + } |
| 124 | +} |
0 commit comments