|
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> |
| 7 | +#include <util/config.h> |
6 | 8 | #include <util/format.h>
|
7 | 9 | #include <util/std_expr.h>
|
8 | 10 |
|
9 | 11 | #include <solvers/smt2_incremental/convert_expr_to_smt.h>
|
10 | 12 | #include <solvers/smt2_incremental/smt_bit_vector_theory.h>
|
11 | 13 | #include <solvers/smt2_incremental/smt_core_theory.h>
|
12 | 14 | #include <solvers/smt2_incremental/smt_terms.h>
|
| 15 | +#include <solvers/smt2_incremental/smt_to_smt2_string.h> |
13 | 16 | #include <testing-utils/use_catch.h>
|
14 | 17 |
|
15 | 18 | TEST_CASE("\"typet\" to smt sort conversion", "[core][smt2_incremental]")
|
@@ -814,3 +817,52 @@ SCENARIO(
|
814 | 817 | }
|
815 | 818 | }
|
816 | 819 | }
|
| 820 | + |
| 821 | +TEST_CASE("expr to smt conversion for type casts", "[core][smt2_incremental]") |
| 822 | +{ |
| 823 | + const symbol_exprt bool_expr{"foo", bool_typet{}}; |
| 824 | + const smt_termt bool_term = smt_identifier_termt{"foo", smt_bool_sortt{}}; |
| 825 | + const symbol_exprt bv_expr{"bar", signedbv_typet(12)}; |
| 826 | + const smt_termt bv_term = |
| 827 | + smt_identifier_termt{"bar", smt_bit_vector_sortt{12}}; |
| 828 | + SECTION("Casts to bool") |
| 829 | + { |
| 830 | + CHECK( |
| 831 | + convert_expr_to_smt(typecast_exprt{bool_expr, bool_typet{}}) == |
| 832 | + bool_term); |
| 833 | + CHECK( |
| 834 | + convert_expr_to_smt(typecast_exprt{bv_expr, bool_typet{}}) == |
| 835 | + smt_core_theoryt::distinct( |
| 836 | + bv_term, smt_bit_vector_constant_termt{0, 12})); |
| 837 | + } |
| 838 | + SECTION("Casts to C bool") |
| 839 | + { |
| 840 | + // The config lines are necessary because when we do casting to C bool the |
| 841 | + // bit width depends on the configuration. |
| 842 | + config.ansi_c.mode = configt::ansi_ct::flavourt::GCC; |
| 843 | + config.ansi_c.set_arch_spec_i386(); |
| 844 | + const std::size_t c_bool_width = config.ansi_c.bool_width; |
| 845 | + const smt_bit_vector_constant_termt c_true{1, c_bool_width}; |
| 846 | + const smt_bit_vector_constant_termt c_false{0, c_bool_width}; |
| 847 | + SECTION("from bool") |
| 848 | + { |
| 849 | + const auto cast_bool = |
| 850 | + convert_expr_to_smt(typecast_exprt{bool_expr, c_bool_type()}); |
| 851 | + const auto expected_bool_conversion = |
| 852 | + smt_core_theoryt::if_then_else(bool_term, c_true, c_false); |
| 853 | + CHECK(cast_bool == expected_bool_conversion); |
| 854 | + } |
| 855 | + SECTION("from bit vector") |
| 856 | + { |
| 857 | + const auto cast_bit_vector = |
| 858 | + convert_expr_to_smt(typecast_exprt{bv_expr, c_bool_type()}); |
| 859 | + const auto expected_bit_vector_conversion = |
| 860 | + smt_core_theoryt::if_then_else( |
| 861 | + smt_core_theoryt::distinct( |
| 862 | + bv_term, smt_bit_vector_constant_termt{0, 12}), |
| 863 | + c_true, |
| 864 | + c_false); |
| 865 | + CHECK(cast_bit_vector == expected_bit_vector_conversion); |
| 866 | + } |
| 867 | + } |
| 868 | +} |
0 commit comments