|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | + Module: Unit tests for array pool |
| 4 | + solvers/refinement/string_constraint_generator.h |
| 5 | +
|
| 6 | + Author: Diffblue Ltd. |
| 7 | +
|
| 8 | +\*******************************************************************/ |
| 9 | + |
| 10 | +#include <testing-utils/catch.hpp> |
| 11 | + |
| 12 | +#include <solvers/refinement/string_constraint_generator.h> |
| 13 | + |
| 14 | +SCENARIO( |
| 15 | + "array_pool", "[core][solvers][refinement][string_constraint_generator]") |
| 16 | +{ |
| 17 | + const std::size_t pointer_width = 16; |
| 18 | + const auto char_type = unsignedbv_typet(8); |
| 19 | + const auto length_type = signedbv_typet(32); |
| 20 | + const auto pointer_type = pointer_typet(char_type, pointer_width); |
| 21 | + |
| 22 | + GIVEN("An array pool") |
| 23 | + { |
| 24 | + symbol_generatort symbol_generator; |
| 25 | + array_poolt pool(symbol_generator); |
| 26 | + |
| 27 | + WHEN("Looking for a pointer symbol") |
| 28 | + { |
| 29 | + const symbol_exprt pointer("my_pointer", pointer_type); |
| 30 | + const symbol_exprt pointer_length("my_pointer_length", length_type); |
| 31 | + const array_string_exprt associated_array = |
| 32 | + pool.find(pointer, pointer_length); |
| 33 | + |
| 34 | + THEN("A second find give the same array") |
| 35 | + { |
| 36 | + const array_string_exprt second_lookup = |
| 37 | + pool.find(pointer, pointer_length); |
| 38 | + |
| 39 | + REQUIRE(second_lookup == associated_array); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + WHEN("Looking for the address of the first element of a constant array") |
| 44 | + { |
| 45 | + const array_typet array_type(char_type, from_integer(3, length_type)); |
| 46 | + const exprt array = [array_type, char_type]{ |
| 47 | + array_exprt a(array_type); |
| 48 | + a.operands().push_back(from_integer('f', char_type)); |
| 49 | + a.operands().push_back(from_integer('o', char_type)); |
| 50 | + a.operands().push_back(from_integer('o', char_type)); |
| 51 | + return a; |
| 52 | + }(); |
| 53 | + const exprt first_element = |
| 54 | + index_exprt(array, from_integer(0, length_type)); |
| 55 | + const exprt pointer = address_of_exprt(first_element, pointer_type); |
| 56 | + const array_string_exprt associated_array = |
| 57 | + pool.find(pointer, array_type.size()); |
| 58 | + |
| 59 | + THEN("The associated array should be the pointed array") |
| 60 | + { |
| 61 | + REQUIRE(associated_array == array); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + WHEN("Looking for a null pointer") |
| 66 | + { |
| 67 | + const null_pointer_exprt null_pointer(pointer_type); |
| 68 | + const symbol_exprt pointer_length("null_pointer_length", length_type); |
| 69 | + const array_string_exprt associated_array = |
| 70 | + pool.find(null_pointer, pointer_length); |
| 71 | + |
| 72 | + THEN("`find` always gives the same array") |
| 73 | + { |
| 74 | + const symbol_exprt pointer_length2("null_pointer_length2", length_type); |
| 75 | + const array_string_exprt second_lookup = |
| 76 | + pool.find(null_pointer, pointer_length2); |
| 77 | + |
| 78 | + REQUIRE(second_lookup == associated_array); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + WHEN("Looking for an if-expression") |
| 83 | + { |
| 84 | + const exprt boolean_symbol = symbol_exprt("boolean", bool_typet()); |
| 85 | + const exprt true_case = symbol_exprt("pointer1", pointer_type); |
| 86 | + const exprt false_case = symbol_exprt("pointer2", pointer_type); |
| 87 | + const exprt if_expr = if_exprt(boolean_symbol, true_case, false_case); |
| 88 | + const symbol_exprt pointer_length("pointer_length", length_type); |
| 89 | + |
| 90 | + const array_string_exprt associated_array = |
| 91 | + pool.find(if_expr, pointer_length); |
| 92 | + |
| 93 | + THEN("Arrays associated to the subexpressions are the subexpressions of " |
| 94 | + "the associated array") |
| 95 | + { |
| 96 | + const symbol_exprt pointer_length1("pointer_length1", length_type); |
| 97 | + const array_string_exprt associated_to_true = |
| 98 | + pool.find(true_case, pointer_length1); |
| 99 | + const symbol_exprt pointer_length2("pointer_length2", length_type); |
| 100 | + const array_string_exprt associated_to_false = |
| 101 | + pool.find(false_case, pointer_length2); |
| 102 | + |
| 103 | + const typet recomposed_type = array_typet( |
| 104 | + char_type, if_exprt(boolean_symbol, pointer_length, pointer_length)); |
| 105 | + const exprt recomposed_array = if_exprt( |
| 106 | + boolean_symbol, |
| 107 | + associated_to_true, |
| 108 | + associated_to_false, |
| 109 | + recomposed_type); |
| 110 | + |
| 111 | + REQUIRE(associated_array == recomposed_array); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + WHEN("Looking for two pointer symbols") |
| 116 | + { |
| 117 | + const exprt true_case = symbol_exprt("pointer1", pointer_type); |
| 118 | + const symbol_exprt pointer_length1("pointer_length1", length_type); |
| 119 | + const exprt false_case = symbol_exprt("pointer2", pointer_type); |
| 120 | + const symbol_exprt pointer_length2("pointer_length2", length_type); |
| 121 | + |
| 122 | + const array_string_exprt associated_to_true = |
| 123 | + pool.find(true_case, pointer_length1); |
| 124 | + const array_string_exprt associated_to_false = |
| 125 | + pool.find(false_case, pointer_length2); |
| 126 | + |
| 127 | + THEN("Looking for an if-expression containing these two symbols") |
| 128 | + { |
| 129 | + const exprt boolean_symbol = symbol_exprt("boolean", bool_typet()); |
| 130 | + const exprt if_expr = if_exprt(boolean_symbol, true_case, false_case); |
| 131 | + const symbol_exprt pointer_length("pointer_length", length_type); |
| 132 | + const array_string_exprt associated_array = |
| 133 | + pool.find(if_expr, pointer_length); |
| 134 | + |
| 135 | + const typet recomposed_type = array_typet( |
| 136 | + char_type, |
| 137 | + if_exprt(boolean_symbol, pointer_length1, pointer_length2)); |
| 138 | + const exprt recomposed_array = if_exprt( |
| 139 | + boolean_symbol, |
| 140 | + associated_to_true, |
| 141 | + associated_to_false, |
| 142 | + recomposed_type); |
| 143 | + |
| 144 | + REQUIRE(associated_array == recomposed_array); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments