|
3 | 3 | #include <util/arith_tools.h>
|
4 | 4 | #include <util/bitvector_expr.h>
|
5 | 5 | #include <util/bitvector_types.h>
|
| 6 | +#include <util/c_types.h> |
6 | 7 | #include <util/mathematical_types.h>
|
7 | 8 | #include <util/namespace.h>
|
8 | 9 | #include <util/symbol_table.h>
|
@@ -71,6 +72,58 @@ TEST_CASE("struct encoding of types", "[core][smt2_incremental]")
|
71 | 72 | }
|
72 | 73 | }
|
73 | 74 |
|
| 75 | +TEST_CASE("Encoding of union types", "[core][smt2_incremental]") |
| 76 | +{ |
| 77 | + auto test = struct_encoding_test_environmentt::make(); |
| 78 | + SECTION("Two components") |
| 79 | + { |
| 80 | + const struct_union_typet::componentst components{ |
| 81 | + {"foo", unsignedbv_typet{8}}, {"bar", signedbv_typet{16}}}; |
| 82 | + union_typet union_type{components}; |
| 83 | + type_symbolt type_symbol{"my_uniont", union_type, ID_C}; |
| 84 | + test.symbol_table.insert(type_symbol); |
| 85 | + union_tag_typet union_tag{type_symbol.name}; |
| 86 | + SECTION("Direct union_tag_type encoding") |
| 87 | + { |
| 88 | + REQUIRE(test.struct_encoding.encode(union_tag) == bv_typet{16}); |
| 89 | + } |
| 90 | + SECTION("Array of unions encoding") |
| 91 | + { |
| 92 | + const auto index_type = signedbv_typet{32}; |
| 93 | + const auto array_size = from_integer(5, index_type); |
| 94 | + array_typet array_of_struct{union_tag, array_size}; |
| 95 | + array_typet expected_encoded_array{bv_typet{16}, array_size}; |
| 96 | + REQUIRE( |
| 97 | + test.struct_encoding.encode(array_of_struct) == expected_encoded_array); |
| 98 | + } |
| 99 | + SECTION("Array of array of unions encoding") |
| 100 | + { |
| 101 | + const auto index_type = signedbv_typet{32}; |
| 102 | + const auto array_size_inner = from_integer(4, index_type); |
| 103 | + const auto array_size_outer = from_integer(2, index_type); |
| 104 | + array_typet array_of_struct{union_tag, array_size_inner}; |
| 105 | + array_typet array_of_array_of_struct{array_of_struct, array_size_outer}; |
| 106 | + array_typet expected_encoded_array{ |
| 107 | + array_typet{bv_typet{16}, array_size_inner}, array_size_outer}; |
| 108 | + REQUIRE( |
| 109 | + test.struct_encoding.encode(array_of_array_of_struct) == |
| 110 | + expected_encoded_array); |
| 111 | + } |
| 112 | + } |
| 113 | + SECTION("Empty union") |
| 114 | + { |
| 115 | + const struct_union_typet::componentst components{}; |
| 116 | + union_typet union_type{components}; |
| 117 | + type_symbolt type_symbol{"my_empty_uniont", union_type, ID_C}; |
| 118 | + test.symbol_table.insert(type_symbol); |
| 119 | + union_tag_typet union_tag{type_symbol.name}; |
| 120 | + SECTION("Direct union_tag_type encoding") |
| 121 | + { |
| 122 | + REQUIRE(test.struct_encoding.encode(union_tag) == bv_typet{8}); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
74 | 127 | exprt make_member_name_expression(const irep_idt component_name)
|
75 | 128 | {
|
76 | 129 | exprt result{ID_member_name};
|
|
0 commit comments