|
24 | 24 | #include <util/string_constant.h> |
25 | 25 | #include <util/symbol_table.h> |
26 | 26 |
|
| 27 | +TEST_CASE("byte extract and bits", "[core][solvers][lowering][byte_extract]") |
| 28 | +{ |
| 29 | + // this test does require a proper architecture to be set so that byte extract |
| 30 | + // uses adequate endianness |
| 31 | + cmdlinet cmdline; |
| 32 | + config.set(cmdline); |
| 33 | + |
| 34 | + const symbol_tablet symbol_table; |
| 35 | + const namespacet ns(symbol_table); |
| 36 | + |
| 37 | + const unsignedbv_typet u16{16}; |
| 38 | + const exprt sixteen_bits = from_integer(0x1234, u16); |
| 39 | + const array_typet bit_array_type{bv_typet{1}, from_integer(16, size_type())}; |
| 40 | + |
| 41 | + bool little_endian; |
| 42 | + GIVEN("Little endian") |
| 43 | + { |
| 44 | + little_endian = true; |
| 45 | + |
| 46 | + const auto bit_string = expr2bits(sixteen_bits, little_endian, ns); |
| 47 | + REQUIRE(bit_string.has_value()); |
| 48 | + REQUIRE(bit_string->size() == 16); |
| 49 | + |
| 50 | + const auto array_of_bits = |
| 51 | + bits2expr(*bit_string, bit_array_type, little_endian, ns); |
| 52 | + REQUIRE(array_of_bits.has_value()); |
| 53 | + |
| 54 | + const auto bit_string2 = expr2bits(*array_of_bits, little_endian, ns); |
| 55 | + REQUIRE(bit_string2.has_value()); |
| 56 | + REQUIRE(*bit_string == *bit_string2); |
| 57 | + |
| 58 | + const byte_extract_exprt be1{little_endian ? ID_byte_extract_little_endian |
| 59 | + : ID_byte_extract_big_endian, |
| 60 | + sixteen_bits, |
| 61 | + from_integer(0, index_type()), |
| 62 | + bit_array_type}; |
| 63 | + const exprt lower_be1 = lower_byte_extract(be1, ns); |
| 64 | + REQUIRE(lower_be1 == *array_of_bits); |
| 65 | + } |
| 66 | + |
| 67 | + GIVEN("Big endian") |
| 68 | + { |
| 69 | + little_endian = false; |
| 70 | + |
| 71 | + const auto bit_string = expr2bits(sixteen_bits, little_endian, ns); |
| 72 | + REQUIRE(bit_string.has_value()); |
| 73 | + REQUIRE(bit_string->size() == 16); |
| 74 | + |
| 75 | + const auto array_of_bits = |
| 76 | + bits2expr(*bit_string, bit_array_type, little_endian, ns); |
| 77 | + REQUIRE(array_of_bits.has_value()); |
| 78 | + |
| 79 | + const auto bit_string2 = expr2bits(*array_of_bits, little_endian, ns); |
| 80 | + REQUIRE(bit_string2.has_value()); |
| 81 | + REQUIRE(*bit_string == *bit_string2); |
| 82 | + |
| 83 | + const byte_extract_exprt be1{little_endian ? ID_byte_extract_little_endian |
| 84 | + : ID_byte_extract_big_endian, |
| 85 | + sixteen_bits, |
| 86 | + from_integer(0, index_type()), |
| 87 | + bit_array_type}; |
| 88 | + const exprt lower_be1 = lower_byte_extract(be1, ns); |
| 89 | + REQUIRE(lower_be1 == *array_of_bits); |
| 90 | + } |
| 91 | +} |
| 92 | + |
27 | 93 | SCENARIO("byte_extract_lowering", "[core][solvers][lowering][byte_extract]") |
28 | 94 | { |
29 | 95 | // this test does require a proper architecture to be set so that byte extract |
|
0 commit comments