|
| 1 | +// Author: Diffblue Ltd. |
| 2 | + |
| 3 | +#include <util/arith_tools.h> |
| 4 | +#include <util/bitvector_expr.h> |
| 5 | +#include <util/bitvector_types.h> |
| 6 | +#include <util/namespace.h> |
| 7 | +#include <util/symbol_table.h> |
| 8 | + |
| 9 | +#include <solvers/smt2_incremental/struct_encoding.h> |
| 10 | +#include <testing-utils/use_catch.h> |
| 11 | + |
| 12 | +struct struct_encoding_test_environmentt |
| 13 | +{ |
| 14 | + symbol_tablet symbol_table; |
| 15 | + namespacet ns{symbol_table}; |
| 16 | + struct_encodingt struct_encoding{ns}; |
| 17 | + |
| 18 | + static struct_encoding_test_environmentt make() |
| 19 | + { |
| 20 | + return {}; |
| 21 | + } |
| 22 | + |
| 23 | +private: |
| 24 | + struct_encoding_test_environmentt() = default; |
| 25 | +}; |
| 26 | + |
| 27 | +TEST_CASE( |
| 28 | + "struct encoding of non-stuct type is a no-op", |
| 29 | + "[core][smt2_incremental]") |
| 30 | +{ |
| 31 | + auto test = struct_encoding_test_environmentt::make(); |
| 32 | + typet input = signedbv_typet{8}; |
| 33 | + REQUIRE(test.struct_encoding.encode(input) == input); |
| 34 | +} |
| 35 | + |
| 36 | +TEST_CASE("struct encoding of types", "[core][smt2_incremental]") |
| 37 | +{ |
| 38 | + const struct_union_typet::componentst components{ |
| 39 | + {"foo", unsignedbv_typet{8}}, {"bar", signedbv_typet{16}}}; |
| 40 | + struct_typet struct_type{components}; |
| 41 | + type_symbolt type_symbol{"my_structt", struct_type, ID_C}; |
| 42 | + auto test = struct_encoding_test_environmentt::make(); |
| 43 | + test.symbol_table.insert(type_symbol); |
| 44 | + struct_tag_typet struct_tag{type_symbol.name}; |
| 45 | + SECTION("direct struct_tag_type encoding") |
| 46 | + { |
| 47 | + REQUIRE(test.struct_encoding.encode(struct_tag) == bv_typet{24}); |
| 48 | + } |
| 49 | + SECTION("array of structs encoding") |
| 50 | + { |
| 51 | + const auto index_type = signedbv_typet{32}; |
| 52 | + const auto array_size = from_integer(5, index_type); |
| 53 | + array_typet array_of_struct{struct_tag, array_size}; |
| 54 | + array_typet expected_encoded_array{bv_typet{24}, array_size}; |
| 55 | + REQUIRE( |
| 56 | + test.struct_encoding.encode(array_of_struct) == expected_encoded_array); |
| 57 | + } |
| 58 | + SECTION("array of array of structs encoding") |
| 59 | + { |
| 60 | + const auto index_type = signedbv_typet{32}; |
| 61 | + const auto array_size_inner = from_integer(4, index_type); |
| 62 | + const auto array_size_outer = from_integer(2, index_type); |
| 63 | + array_typet array_of_struct{struct_tag, array_size_inner}; |
| 64 | + array_typet array_of_array_of_struct{array_of_struct, array_size_outer}; |
| 65 | + array_typet expected_encoded_array{ |
| 66 | + array_typet{bv_typet{24}, array_size_inner}, array_size_outer}; |
| 67 | + REQUIRE( |
| 68 | + test.struct_encoding.encode(array_of_array_of_struct) == |
| 69 | + expected_encoded_array); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +TEST_CASE("struct encoding of expressions", "[core][smt2_incremental]") |
| 74 | +{ |
| 75 | + const struct_union_typet::componentst component_types{ |
| 76 | + {"green", signedbv_typet{32}}, {"eggs", unsignedbv_typet{16}}}; |
| 77 | + const struct_typet struct_type{component_types}; |
| 78 | + const type_symbolt type_symbol{"my_structt", struct_type, ID_C}; |
| 79 | + auto test = struct_encoding_test_environmentt::make(); |
| 80 | + test.symbol_table.insert(type_symbol); |
| 81 | + const struct_tag_typet struct_tag{type_symbol.name}; |
| 82 | + const symbolt struct_value_symbol{"my_struct", struct_tag, ID_C}; |
| 83 | + test.symbol_table.insert(struct_value_symbol); |
| 84 | + const auto symbol_expr = struct_value_symbol.symbol_expr(); |
| 85 | + const auto symbol_expr_as_bv = symbol_exprt{"my_struct", bv_typet{48}}; |
| 86 | + SECTION("struct typed symbol expression") |
| 87 | + { |
| 88 | + REQUIRE(test.struct_encoding.encode(symbol_expr) == symbol_expr_as_bv); |
| 89 | + } |
| 90 | + SECTION("struct equality expression") |
| 91 | + { |
| 92 | + const auto struct_equal = equal_exprt{symbol_expr, symbol_expr}; |
| 93 | + const auto bv_equal = equal_exprt{symbol_expr_as_bv, symbol_expr_as_bv}; |
| 94 | + REQUIRE(test.struct_encoding.encode(struct_equal) == bv_equal); |
| 95 | + } |
| 96 | + SECTION("expression for a struct from list of components") |
| 97 | + { |
| 98 | + const symbolt green_ham{"ham", signedbv_typet{32}, ID_C}; |
| 99 | + test.symbol_table.insert(green_ham); |
| 100 | + const auto forty_two = from_integer(42, unsignedbv_typet{16}); |
| 101 | + const exprt::operandst components{green_ham.symbol_expr(), forty_two}; |
| 102 | + const struct_exprt struct_expr{components, struct_tag}; |
| 103 | + const concatenation_exprt expected_result{ |
| 104 | + {green_ham.symbol_expr(), forty_two}, bv_typet{48}}; |
| 105 | + REQUIRE(test.struct_encoding.encode(struct_expr) == expected_result); |
| 106 | + } |
| 107 | +} |
0 commit comments