|
| 1 | +// Author: Diffblue Ltd. |
| 2 | + |
| 3 | +#include <util/arith_tools.h> |
| 4 | +#include <util/bitvector_types.h> |
| 5 | +#include <util/namespace.h> |
| 6 | +#include <util/symbol_table.h> |
| 7 | + |
| 8 | +#include <solvers/smt2_incremental/struct_encoding.h> |
| 9 | +#include <testing-utils/use_catch.h> |
| 10 | + |
| 11 | +struct struct_encoding_test_environmentt |
| 12 | +{ |
| 13 | + symbol_tablet symbol_table; |
| 14 | + namespacet ns{symbol_table}; |
| 15 | + struct_encodingt struct_encoding{ns}; |
| 16 | + |
| 17 | + static struct_encoding_test_environmentt make() |
| 18 | + { |
| 19 | + return {}; |
| 20 | + } |
| 21 | + |
| 22 | +private: |
| 23 | + struct_encoding_test_environmentt() = default; |
| 24 | +}; |
| 25 | + |
| 26 | +TEST_CASE( |
| 27 | + "struct encoding of non-stuct type is a no-op", |
| 28 | + "[core][smt2_incremental]") |
| 29 | +{ |
| 30 | + auto test = struct_encoding_test_environmentt::make(); |
| 31 | + typet input = signedbv_typet{8}; |
| 32 | + REQUIRE(test.struct_encoding.encode(input) == input); |
| 33 | +} |
| 34 | + |
| 35 | +TEST_CASE("struct encoding of struct_tag_type", "[core][smt2_incremental]") |
| 36 | +{ |
| 37 | + const struct_union_typet::componentst components{ |
| 38 | + {"foo", unsignedbv_typet{8}}, {"bar", signedbv_typet{16}}}; |
| 39 | + struct_typet struct_type{components}; |
| 40 | + type_symbolt type_symbol{"my_structt", struct_type, ID_C}; |
| 41 | + auto test = struct_encoding_test_environmentt::make(); |
| 42 | + test.symbol_table.insert(type_symbol); |
| 43 | + struct_tag_typet struct_tag{type_symbol.name}; |
| 44 | + REQUIRE(test.struct_encoding.encode(struct_tag) == bv_typet{24}); |
| 45 | +} |
| 46 | + |
| 47 | +TEST_CASE("struct encoding of array of structs", "[core][smt2_incremental]") |
| 48 | +{ |
| 49 | + const struct_union_typet::componentst components{ |
| 50 | + {"foo", unsignedbv_typet{8}}, {"bar", signedbv_typet{16}}}; |
| 51 | + struct_typet struct_type{components}; |
| 52 | + type_symbolt type_symbol{"my_structt", struct_type, ID_C}; |
| 53 | + auto test = struct_encoding_test_environmentt::make(); |
| 54 | + test.symbol_table.insert(type_symbol); |
| 55 | + struct_tag_typet struct_tag{type_symbol.name}; |
| 56 | + const auto index_type = signedbv_typet{32}; |
| 57 | + const auto array_size = from_integer(5, index_type); |
| 58 | + array_typet array_of_struct{struct_tag, array_size}; |
| 59 | + array_typet expected_encoded_array{bv_typet{24}, array_size}; |
| 60 | + REQUIRE( |
| 61 | + test.struct_encoding.encode(array_of_struct) == expected_encoded_array); |
| 62 | +} |
| 63 | + |
| 64 | +TEST_CASE( |
| 65 | + "struct encoding of array of array of structs", |
| 66 | + "[core][smt2_incremental]") |
| 67 | +{ |
| 68 | + const struct_union_typet::componentst components{ |
| 69 | + {"foo", unsignedbv_typet{8}}, {"bar", signedbv_typet{16}}}; |
| 70 | + struct_typet struct_type{components}; |
| 71 | + type_symbolt type_symbol{"my_structt", struct_type, ID_C}; |
| 72 | + auto test = struct_encoding_test_environmentt::make(); |
| 73 | + test.symbol_table.insert(type_symbol); |
| 74 | + struct_tag_typet struct_tag{type_symbol.name}; |
| 75 | + const auto index_type = signedbv_typet{32}; |
| 76 | + const auto array_size_inner = from_integer(4, index_type); |
| 77 | + const auto array_size_outer = from_integer(2, index_type); |
| 78 | + array_typet array_of_struct{struct_tag, array_size_inner}; |
| 79 | + array_typet array_of_array_of_struct{array_of_struct, array_size_outer}; |
| 80 | + array_typet expected_encoded_array{ |
| 81 | + array_typet{bv_typet{24}, array_size_inner}, array_size_outer}; |
| 82 | + REQUIRE( |
| 83 | + test.struct_encoding.encode(array_of_array_of_struct) == |
| 84 | + expected_encoded_array); |
| 85 | +} |
0 commit comments